Categories in the settings dialog are now clickable
[quassel.git] / src / qtui / settingspages / identitiessettingspage.cpp
index e51fa7a..bb9b564 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   Copyright (C) 2005-09 by the Quassel Project                          *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
 #include "identitiessettingspage.h"
 
 #include <QInputDialog>
-#include <QFileDialog>
-#include <QDesktopServices>
 #include <QMessageBox>
-#include <QSslKey>
 
 #include "client.h"
 #include "iconloader.h"
 #include "signalproxy.h"
 
 IdentitiesSettingsPage::IdentitiesSettingsPage(QWidget *parent)
-  : SettingsPage(tr("General"), tr("Identities"), parent),
+  : SettingsPage(tr("Misc"), tr("Identities"), parent),
     _editSsl(false)
 {
-
   ui.setupUi(this);
   ui.renameIdentity->setIcon(BarIcon("edit-rename"));
   ui.addIdentity->setIcon(BarIcon("list-add-user"));
   ui.deleteIdentity->setIcon(BarIcon("list-remove-user"));
-  ui.addNick->setIcon(SmallIcon("list-add"));
-  ui.deleteNick->setIcon(SmallIcon("edit-delete"));
-  ui.renameNick->setIcon(SmallIcon("edit-rename"));
-  ui.nickUp->setIcon(SmallIcon("go-up"));
-  ui.nickDown->setIcon(SmallIcon("go-down"));
-
-  setEnabled(Client::isConnected());  // need a core connection!
-  setWidgetStates();
+
+  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;
 
-  // We need to know whenever the state of input widgets changes...
   //connect(ui.identityList, SIGNAL(editTextChanged(const QString &)), this, SLOT(widgetHasChanged()));
-  connect(ui.realName, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
-  connect(ui.nicknameList, SIGNAL(itemChanged(QListWidgetItem *)), this, SLOT(widgetHasChanged()));
-  connect(ui.awayNick, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
-  connect(ui.awayNickEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
-  connect(ui.awayReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
-  connect(ui.awayReasonEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
-  connect(ui.autoAwayEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
-  connect(ui.autoAwayTime, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
-  connect(ui.autoAwayReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
-  connect(ui.autoAwayReasonEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
-  connect(ui.detachAwayEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
-  connect(ui.detachAwayReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
-  connect(ui.detachAwayReasonEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
-  connect(ui.ident, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
-  connect(ui.kickReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
-  connect(ui.partReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
-  connect(ui.quitReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
-
-  connect(ui.nicknameList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
+}
 
+void IdentitiesSettingsPage::coreConnectionStateChanged(bool connected) {
+  setEnabled(connected);
+  if(connected) {
 #ifdef HAVE_SSL
-  ui.keyAndCertSettings->setCurrentIndex(1);
+    if(Client::signalProxy()->isSecure()) {
+      ui.identityEditor->setSslState(IdentityEditWidget::AllowSsl);
+      _editSsl = true;
+    } else {
+      ui.identityEditor->setSslState(IdentityEditWidget::UnsecureSsl);
+      _editSsl = false;
+    }
 #else
-  ui.keyAndCertSettings->setCurrentIndex(0);
+    ui.identityEditor->setSslState(IdentityEditWidget::NoSsl);
 #endif
-
-  // we would need this if we enabled drag and drop in the nicklist...
-  //connect(ui.nicknameList, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(setWidgetStates()));
-  //connect(ui.nicknameList->model(), SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(nicklistHasChanged()));
-}
-
-void IdentitiesSettingsPage::setWidgetStates() {
-  if(ui.nicknameList->selectedItems().count()) {
-    ui.renameNick->setEnabled(true);
-    ui.nickUp->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) > 0);
-    ui.nickDown->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) < ui.nicknameList->count()-1);
-  } else {
-    ui.renameNick->setDisabled(true);
-    ui.nickUp->setDisabled(true);
-    ui.nickDown->setDisabled(true);
-  }
-  ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
-}
-
-void IdentitiesSettingsPage::coreConnectionStateChanged(bool state) {
-  this->setEnabled(state);
-  if(state) {
     load();
   } else {
     // reset
@@ -109,6 +73,19 @@ void IdentitiesSettingsPage::coreConnectionStateChanged(bool state) {
   }
 }
 
+#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;
@@ -171,7 +148,7 @@ bool IdentitiesSettingsPage::testHasChanged() {
     if(currentId != 0) {
       changedIdentities.removeAll(currentId);
       CertIdentity temp(currentId, this);
-      saveToIdentity(&temp);
+      ui.identityEditor->saveToIdentity(&temp);
       temp.setIdentityName(identities[currentId]->identityName());
       if(temp != *Client::identity(currentId) || temp.isDirty())
        changedIdentities.append(currentId);
@@ -181,7 +158,7 @@ bool IdentitiesSettingsPage::testHasChanged() {
 }
 
 bool IdentitiesSettingsPage::aboutToSave() {
-  saveToIdentity(identities[currentId]);
+  ui.identityEditor->saveToIdentity(identities[currentId]);
   QList<int> errors;
   foreach(Identity *id, identities.values()) {
     if(id->identityName().isEmpty()) errors.append(1);
@@ -202,9 +179,13 @@ bool IdentitiesSettingsPage::aboutToSave() {
 
 void IdentitiesSettingsPage::clientIdentityCreated(IdentityId id) {
   CertIdentity *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()));
 }
 
@@ -224,10 +205,10 @@ void IdentitiesSettingsPage::clientIdentityUpdated() {
   if(identity->identityName() != clientIdentity->identityName())
     renameIdentity(identity->id(), clientIdentity->identityName());
 
-  identity->update(*clientIdentity);
+  identity->copyFrom(*clientIdentity);
 
   if(identity->id() == currentId)
-    displayIdentity(identity, true);
+    ui.identityEditor->displayIdentity(identity);
 }
 
 void IdentitiesSettingsPage::clientIdentityRemoved(IdentityId id) {
@@ -275,76 +256,25 @@ void IdentitiesSettingsPage::removeIdentity(Identity *id) {
 }
 
 void IdentitiesSettingsPage::on_identityList_currentIndexChanged(int index) {
+  CertIdentity *previousIdentity = 0;
+  if(currentId != 0 && identities.contains(currentId))
+    previousIdentity = identities[currentId];
+
   if(index < 0) {
     //ui.identityList->setEditable(false);
-    displayIdentity(0);
+    ui.identityEditor->displayIdentity(0, previousIdentity);
+    currentId = 0;
   } else {
     IdentityId id = ui.identityList->itemData(index).toInt();
-    if(identities.contains(id)) displayIdentity(identities[id]);
+    if(identities.contains(id)) {
+      ui.identityEditor->displayIdentity(identities[id], previousIdentity);
+      currentId = id;
+    }
     ui.deleteIdentity->setEnabled(id != 1); // default identity cannot be deleted
     ui.renameIdentity->setEnabled(id != 1); // ...or renamed
   }
 }
 
-void IdentitiesSettingsPage::displayIdentity(CertIdentity *id, bool dontsave) {
-  if(currentId != 0 && !dontsave && identities.contains(currentId)) {
-    saveToIdentity(identities[currentId]);
-  }
-  if(id) {
-    currentId = id->id();
-    ui.realName->setText(id->realName());
-    ui.nicknameList->clear();
-    ui.nicknameList->addItems(id->nicks());
-    //for(int i = 0; i < ui.nicknameList->count(); i++) {
-    //  ui.nicknameList->item(i)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
-    //}
-    if(ui.nicknameList->count()) ui.nicknameList->setCurrentRow(0);
-    ui.awayNick->setText(id->awayNick());
-    ui.awayNickEnabled->setChecked(id->awayNickEnabled());
-    ui.awayReason->setText(id->awayReason());
-    ui.awayReasonEnabled->setChecked(id->awayReasonEnabled());
-    ui.autoAwayEnabled->setChecked(id->autoAwayEnabled());
-    ui.autoAwayTime->setValue(id->autoAwayTime());
-    ui.autoAwayReason->setText(id->autoAwayReason());
-    ui.autoAwayReasonEnabled->setChecked(id->autoAwayReasonEnabled());
-    ui.detachAwayEnabled->setChecked(id->detachAwayEnabled());
-    ui.detachAwayReason->setText(id->detachAwayReason());
-    ui.detachAwayReasonEnabled->setChecked(id->detachAwayReasonEnabled());
-    ui.ident->setText(id->ident());
-    ui.kickReason->setText(id->kickReason());
-    ui.partReason->setText(id->partReason());
-    ui.quitReason->setText(id->quitReason());
-    showKeyState(id->sslKey());
-    showCertState(id->sslCert());
-  }
-}
-
-void IdentitiesSettingsPage::saveToIdentity(CertIdentity *id) {
-  id->setRealName(ui.realName->text());
-  QStringList nicks;
-  for(int i = 0; i < ui.nicknameList->count(); i++) {
-    nicks << ui.nicknameList->item(i)->text();
-  }
-  id->setNicks(nicks);
-  id->setAwayNick(ui.awayNick->text());
-  id->setAwayNickEnabled(ui.awayNickEnabled->isChecked());
-  id->setAwayReason(ui.awayReason->text());
-  id->setAwayReasonEnabled(ui.awayReasonEnabled->isChecked());
-  id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
-  id->setAutoAwayTime(ui.autoAwayTime->value());
-  id->setAutoAwayReason(ui.autoAwayReason->text());
-  id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
-  id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
-  id->setDetachAwayReason(ui.detachAwayReason->text());
-  id->setDetachAwayReasonEnabled(ui.detachAwayReasonEnabled->isChecked());
-  id->setIdent(ui.ident->text());
-  id->setKickReason(ui.kickReason->text());
-  id->setPartReason(ui.partReason->text());
-  id->setQuitReason(ui.quitReason->text());
-  id->setSslKey(QSslKey(ui.keyTypeLabel->property("sslKey").toByteArray(), (QSsl::KeyAlgorithm)(ui.keyTypeLabel->property("sslKeyType").toInt())));
-  id->setSslCert(QSslCertificate(ui.certOrgLabel->property("sslCert").toByteArray()));
-}
-
 void IdentitiesSettingsPage::on_addIdentity_clicked() {
   CreateIdentityDlg dlg(ui.identityList->model(), this);
   if(dlg.exec() == QDialog::Accepted) {
@@ -355,10 +285,12 @@ void IdentitiesSettingsPage::on_addIdentity_clicked() {
     }
     id = -id.toInt();
     CertIdentity *newId = new CertIdentity(id, this);
+#ifdef HAVE_SSL
     newId->enableEditSsl(_editSsl);
+#endif
     if(dlg.duplicateId() != 0) {
       // duplicate
-      newId->update(*identities[dlg.duplicateId()]);
+      newId->copyFrom(*identities[dlg.duplicateId()]);
       newId->setId(id);
     }
     newId->setIdentityName(dlg.identityName());
@@ -392,152 +324,6 @@ void IdentitiesSettingsPage::on_renameIdentity_clicked() {
   }
 }
 
-void IdentitiesSettingsPage::on_addNick_clicked() {
-  QStringList existing;
-  for(int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
-  NickEditDlg dlg(QString(), existing, this);
-  if(dlg.exec() == QDialog::Accepted) {
-    ui.nicknameList->addItem(dlg.nick());
-    ui.nicknameList->setCurrentRow(ui.nicknameList->count()-1);
-    setWidgetStates();
-    widgetHasChanged();
-  }
-}
-
-void IdentitiesSettingsPage::on_deleteNick_clicked() {
-  // no confirmation, since a nickname is really nothing hard to recreate
-  if(ui.nicknameList->selectedItems().count()) {
-    delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
-    ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow()+1, ui.nicknameList->count()-1));
-    setWidgetStates();
-    widgetHasChanged();
-  }
-}
-
-void IdentitiesSettingsPage::on_renameNick_clicked() {
-  if(!ui.nicknameList->selectedItems().count()) return;
-  QString old = ui.nicknameList->selectedItems()[0]->text();
-  QStringList existing;
-  for(int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
-  NickEditDlg dlg(old, existing, this);
-  if(dlg.exec() == QDialog::Accepted) {
-    ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
-  }
-
-}
-
-void IdentitiesSettingsPage::on_nickUp_clicked() {
-  if(!ui.nicknameList->selectedItems().count()) return;
-  int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
-  if(row > 0) {
-    ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
-    ui.nicknameList->setCurrentRow(row-1);
-    setWidgetStates();
-    widgetHasChanged();
-  }
-}
-
-void IdentitiesSettingsPage::on_nickDown_clicked() {
-  if(!ui.nicknameList->selectedItems().count()) return;
-  int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
-  if(row < ui.nicknameList->count()-1) {
-    ui.nicknameList->insertItem(row+1, ui.nicknameList->takeItem(row));
-    ui.nicknameList->setCurrentRow(row+1);
-    setWidgetStates();
-    widgetHasChanged();
-  }
-}
-
-void IdentitiesSettingsPage::on_continueUnsecured_clicked() {
-  _editSsl = true;
-
-  QHash<IdentityId, CertIdentity *>::iterator idIter;
-  for(idIter = identities.begin(); idIter != identities.end(); idIter++) {
-    idIter.value()->enableEditSsl();
-  }
-
-  ui.keyAndCertSettings->setCurrentIndex(2);
-}
-
-void IdentitiesSettingsPage::on_clearOrLoadKeyButton_clicked() {
-  QSslKey key;
-
-  if(ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty()) {
-    QString keyFileName = QFileDialog::getOpenFileName(this, tr("Load a Key"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
-    QFile keyFile(keyFileName);
-    keyFile.open(QIODevice::ReadOnly);
-    QByteArray keyRaw = keyFile.read(2 << 20);
-    keyFile.close();
-
-    for(int i = 0; i < 2; i++) {
-      for(int j = 0; j < 2; j++) {
-       key = QSslKey(keyRaw, (QSsl::KeyAlgorithm)j, (QSsl::EncodingFormat)i);
-       if(!key.isNull())
-         goto showKey;
-      }
-    }
-  }
-
- showKey:
-  showKeyState(key);
-  widgetHasChanged();
-}
-
-void IdentitiesSettingsPage::showKeyState(const QSslKey &key) {
-  if(key.isNull()) {
-    ui.keyTypeLabel->setText(tr("No Key loaded"));
-    ui.clearOrLoadKeyButton->setText(tr("Load"));
-  } else {
-    switch(key.algorithm()) {
-    case QSsl::Rsa:
-      ui.keyTypeLabel->setText(tr("RSA"));
-      break;
-    case QSsl::Dsa:
-      ui.keyTypeLabel->setText(tr("DSA"));
-      break;
-    default:
-      ui.keyTypeLabel->setText(tr("No Key Loaded"));
-    }
-    ui.clearOrLoadKeyButton->setText(tr("Clear"));
-  }
-  ui.keyTypeLabel->setProperty("sslKey", key.toPem());
-  ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
-}
-
-void IdentitiesSettingsPage::on_clearOrLoadCertButton_clicked() {
-  QSslCertificate cert;
-
-  if(ui.certOrgLabel->property("sslCert").toByteArray().isEmpty()) {
-    QString certFileName = QFileDialog::getOpenFileName(this, tr("Load a Certificate"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
-    QFile certFile(certFileName);
-    certFile.open(QIODevice::ReadOnly);
-    QByteArray certRaw = certFile.read(2 << 20);
-    certFile.close();
-
-    for(int i = 0; i < 2; i++) {
-      cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
-      if(cert.isValid())
-       break;
-    }
-  }
-
-  showCertState(cert);
-  widgetHasChanged();
-}
-
-void IdentitiesSettingsPage::showCertState(const QSslCertificate &cert) {
-  if(!cert.isValid()) {
-    ui.certOrgLabel->setText(tr("No Certificate loaded"));
-    ui.certCNameLabel->setText(tr("No Certificate loaded"));
-    ui.clearOrLoadCertButton->setText(tr("Load"));
-  } else {
-    ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization));
-    ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName));
-    ui.clearOrLoadCertButton->setText(tr("Clear"));
-  }
-  ui.certOrgLabel->setProperty("sslCert", cert.toPem());
- }
-
 /*****************************************************************************************/
 
 CreateIdentityDlg::CreateIdentityDlg(QAbstractItemModel *model, QWidget *parent)
@@ -595,7 +381,9 @@ SaveIdentitiesDlg::SaveIdentitiesDlg(const QList<CertIdentity *> &toCreate, cons
       }
       connect(cid, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
       Client::updateIdentity(id->id(), id->toVariantMap());
+#ifdef HAVE_SSL
       id->requestUpdateSslSettings();
+#endif
     }
     foreach(IdentityId id, toRemove) {
       Client::removeIdentity(id);