+ : SettingsPage(tr("IRC"), tr("Identities"), parent)
+{
+ ui.setupUi(this);
+ ui.renameIdentity->setIcon(icon::get("edit-rename"));
+ ui.addIdentity->setIcon(icon::get("list-add-user"));
+ ui.deleteIdentity->setIcon(icon::get("list-remove-user"));
+
+ coreConnectionStateChanged(Client::isConnected()); // need a core connection!
+ connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
+
+ connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityCreated(IdentityId)));
+ connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
+
+ connect(ui.identityEditor, SIGNAL(widgetHasChanged()), this, SLOT(widgetHasChanged()));
+#ifdef HAVE_SSL
+ connect(ui.identityEditor, SIGNAL(requestEditSsl()), this, SLOT(continueUnsecured()));
+#endif
+
+ currentId = 0;
+
+ //connect(ui.identityList, SIGNAL(editTextChanged(const QString &)), this, SLOT(widgetHasChanged()));
+}
+
+
+void IdentitiesSettingsPage::coreConnectionStateChanged(bool connected)
+{
+ setEnabled(connected);
+ if (connected) {
+#ifdef HAVE_SSL
+ if (Client::signalProxy()->isSecure()) {
+ ui.identityEditor->setSslState(IdentityEditWidget::AllowSsl);
+ _editSsl = true;
+ }
+ else {
+ ui.identityEditor->setSslState(IdentityEditWidget::UnsecureSsl);
+ _editSsl = false;
+ }
+#else
+ ui.identityEditor->setSslState(IdentityEditWidget::NoSsl);
+#endif
+ load();
+ }
+ else {
+ // reset
+ currentId = 0;
+ }
+}
+
+
+#ifdef HAVE_SSL
+void IdentitiesSettingsPage::continueUnsecured()
+{
+ _editSsl = true;
+
+ QHash<IdentityId, CertIdentity *>::iterator idIter;
+ for (idIter = identities.begin(); idIter != identities.end(); ++idIter) {
+ idIter.value()->enableEditSsl();
+ }
+
+ ui.identityEditor->setSslState(IdentityEditWidget::AllowSsl);
+}
+
+
+#endif
+
+void IdentitiesSettingsPage::save()
+{
+ setEnabled(false);
+ QList<CertIdentity *> toCreate, toUpdate;
+ // we need to remove our temporarily created identities.
+ // these are going to be re-added after the core has propagated them back...
+ QHash<IdentityId, CertIdentity *>::iterator i = identities.begin();
+ while (i != identities.end()) {
+ if ((*i)->id() < 0) {
+ CertIdentity *temp = *i;
+ i = identities.erase(i);
+ toCreate.append(temp);
+ ui.identityList->removeItem(ui.identityList->findData(temp->id().toInt()));
+ }
+ else {
+ if (**i != *Client::identity((*i)->id()) || (*i)->isDirty()) {
+ toUpdate.append(*i);
+ }
+ ++i;
+ }
+ }
+ SaveIdentitiesDlg dlg(toCreate, toUpdate, deletedIdentities, this);
+ int ret = dlg.exec();
+ if (ret == QDialog::Rejected) {
+ // canceled -> reload everything to be safe
+ load();
+ }
+ foreach(Identity *id, toCreate) {
+ id->deleteLater();
+ }
+ changedIdentities.clear();
+ deletedIdentities.clear();
+ setChangedState(false);
+ setEnabled(true);
+}
+
+
+void IdentitiesSettingsPage::load()
+{
+ currentId = 0;
+ foreach(Identity *identity, identities.values()) {
+ identity->deleteLater();
+ }
+ identities.clear();
+ deletedIdentities.clear();
+ changedIdentities.clear();
+ ui.identityList->clear();
+ setWidgetStates();
+ foreach(IdentityId id, Client::identityIds()) {
+ clientIdentityCreated(id);
+ }
+ setChangedState(false);
+}
+
+
+void IdentitiesSettingsPage::widgetHasChanged()
+{
+ bool changed = testHasChanged();
+ if (changed != hasChanged()) setChangedState(changed);
+}
+
+
+void IdentitiesSettingsPage::setWidgetStates()
+{
+ bool enabled = (ui.identityList->count() > 0);
+ ui.identityEditor->setEnabled(enabled);
+ ui.renameIdentity->setEnabled(enabled);
+ ui.deleteIdentity->setEnabled(ui.identityList->count() > 1);
+}
+
+
+bool IdentitiesSettingsPage::testHasChanged()
+{
+ if (deletedIdentities.count()) return true;
+ if (currentId < 0) {
+ return true; // new identity
+ }
+ else {
+ if (currentId != 0) {
+ changedIdentities.removeAll(currentId);
+ CertIdentity temp(currentId, this);
+#ifdef HAVE_SSL
+ // we need to set the cert and key manually, as they aren't synced
+ CertIdentity *old = identities[currentId];
+ temp.setSslKey(old->sslKey());
+ temp.setSslCert(old->sslCert());
+#endif
+ ui.identityEditor->saveToIdentity(&temp);
+ temp.setIdentityName(identities[currentId]->identityName());
+ if (temp != *Client::identity(currentId) || temp.isDirty())
+ changedIdentities.append(currentId);
+ }
+ return changedIdentities.count();
+ }
+}
+
+
+bool IdentitiesSettingsPage::aboutToSave()
+{
+ ui.identityEditor->saveToIdentity(identities[currentId]);
+ QList<int> errors;
+ foreach(Identity *id, identities.values()) {
+ if (id->identityName().isEmpty()) errors.append(1);
+ if (!id->nicks().count()) errors.append(2);
+ if (id->realName().isEmpty()) errors.append(3);
+ if (id->ident().isEmpty()) errors.append(4);
+ }
+ if (!errors.count()) return true;
+ QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
+ if (errors.contains(1)) error += tr("<li>All identities need an identity name set</li>");
+ if (errors.contains(2)) error += tr("<li>Every identity needs at least one nickname defined</li>");
+ if (errors.contains(3)) error += tr("<li>You need to specify a real name for every identity</li>");
+ if (errors.contains(4)) error += tr("<li>You need to specify an ident for every identity</li>");
+ error += tr("</ul>");
+ QMessageBox::warning(this, tr("One or more identities are invalid"), error);
+ return false;
+}
+
+
+void IdentitiesSettingsPage::clientIdentityCreated(IdentityId id)
+{
+ auto *identity = new CertIdentity(*Client::identity(id), this);
+#ifdef HAVE_SSL
+ identity->enableEditSsl(_editSsl);
+#endif
+ insertIdentity(identity);
+#ifdef HAVE_SSL
+ connect(identity, SIGNAL(sslSettingsUpdated()), this, SLOT(clientIdentityUpdated()));
+#endif
+ connect(Client::identity(id), SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
+}
+
+
+void IdentitiesSettingsPage::clientIdentityUpdated()
+{
+ const Identity *clientIdentity = qobject_cast<Identity *>(sender());
+ if (!clientIdentity) {
+ qWarning() << "Invalid identity to update!";
+ return;
+ }
+ if (!identities.contains(clientIdentity->id())) {
+ qWarning() << "Unknown identity to update:" << clientIdentity->identityName();
+ return;
+ }
+
+ CertIdentity *identity = identities[clientIdentity->id()];
+
+ if (identity->identityName() != clientIdentity->identityName())
+ renameIdentity(identity->id(), clientIdentity->identityName());
+
+ identity->copyFrom(*clientIdentity);
+
+ if (identity->id() == currentId)
+ ui.identityEditor->displayIdentity(identity);
+}