added SignalProxy::isSecure() to determine if all connections are secure (ssl encrypt...
[quassel.git] / src / qtui / settingspages / identitiessettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "identitiessettingspage.h"
22
23 #include <QInputDialog>
24 #include <QFileDialog>
25 #include <QDesktopServices>
26 #include <QMessageBox>
27 #include <QSslKey>
28
29 #include "client.h"
30 #include "iconloader.h"
31 #include "signalproxy.h"
32
33 IdentitiesSettingsPage::IdentitiesSettingsPage(QWidget *parent)
34   : SettingsPage(tr("General"), tr("Identities"), parent),
35     _editSsl(false)
36 {
37
38   ui.setupUi(this);
39   ui.renameIdentity->setIcon(BarIcon("edit-rename"));
40   ui.addIdentity->setIcon(BarIcon("list-add-user"));
41   ui.deleteIdentity->setIcon(BarIcon("list-remove-user"));
42   ui.addNick->setIcon(SmallIcon("list-add"));
43   ui.deleteNick->setIcon(SmallIcon("edit-delete"));
44   ui.renameNick->setIcon(SmallIcon("edit-rename"));
45   ui.nickUp->setIcon(SmallIcon("go-up"));
46   ui.nickDown->setIcon(SmallIcon("go-down"));
47
48   setEnabled(Client::isConnected());  // need a core connection!
49   setWidgetStates();
50   connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
51   connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityCreated(IdentityId)));
52   connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
53
54   currentId = 0;
55
56   // We need to know whenever the state of input widgets changes...
57   //connect(ui.identityList, SIGNAL(editTextChanged(const QString &)), this, SLOT(widgetHasChanged()));
58   connect(ui.realName, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
59   connect(ui.nicknameList, SIGNAL(itemChanged(QListWidgetItem *)), this, SLOT(widgetHasChanged()));
60   connect(ui.awayNick, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
61   connect(ui.awayNickEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
62   connect(ui.awayReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
63   connect(ui.awayReasonEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
64   connect(ui.autoAwayEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
65   connect(ui.autoAwayTime, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
66   connect(ui.autoAwayReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
67   connect(ui.autoAwayReasonEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
68   connect(ui.detachAwayEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
69   connect(ui.detachAwayReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
70   connect(ui.detachAwayReasonEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
71   connect(ui.ident, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
72   connect(ui.kickReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
73   connect(ui.partReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
74   connect(ui.quitReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
75
76   connect(ui.nicknameList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
77
78 #ifdef HAVE_SSL
79   if(Client::signalProxy()->isSecure())
80     ui.keyAndCertSettings->setCurrentIndex(2);
81   else
82     ui.keyAndCertSettings->setCurrentIndex(1);
83 #else
84   ui.keyAndCertSettings->setCurrentIndex(0);
85 #endif
86
87   // we would need this if we enabled drag and drop in the nicklist...
88   //connect(ui.nicknameList, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(setWidgetStates()));
89   //connect(ui.nicknameList->model(), SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(nicklistHasChanged()));
90 }
91
92 void IdentitiesSettingsPage::setWidgetStates() {
93   if(ui.nicknameList->selectedItems().count()) {
94     ui.renameNick->setEnabled(true);
95     ui.nickUp->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) > 0);
96     ui.nickDown->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) < ui.nicknameList->count()-1);
97   } else {
98     ui.renameNick->setDisabled(true);
99     ui.nickUp->setDisabled(true);
100     ui.nickDown->setDisabled(true);
101   }
102   ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
103 }
104
105 void IdentitiesSettingsPage::coreConnectionStateChanged(bool state) {
106   this->setEnabled(state);
107   if(state) {
108     load();
109   } else {
110     // reset
111     currentId = 0;
112   }
113 }
114
115 void IdentitiesSettingsPage::save() {
116   setEnabled(false);
117   QList<CertIdentity *> toCreate, toUpdate;
118   // we need to remove our temporarily created identities.
119   // these are going to be re-added after the core has propagated them back...
120   QHash<IdentityId, CertIdentity *>::iterator i = identities.begin();
121   while(i != identities.end()) {
122     if((*i)->id() < 0) {
123       CertIdentity *temp = *i;
124       i = identities.erase(i);
125       toCreate.append(temp);
126       ui.identityList->removeItem(ui.identityList->findData(temp->id().toInt()));
127     } else {
128       if(**i != *Client::identity((*i)->id()) || (*i)->isDirty()) {
129         toUpdate.append(*i);
130       }
131       ++i;
132     }
133   }
134   SaveIdentitiesDlg dlg(toCreate, toUpdate, deletedIdentities, this);
135   int ret = dlg.exec();
136   if(ret == QDialog::Rejected) {
137     // canceled -> reload everything to be safe
138     load();
139   }
140   foreach(Identity *id, toCreate) {
141     id->deleteLater();
142   }
143   changedIdentities.clear();
144   deletedIdentities.clear();
145   setChangedState(false);
146   setEnabled(true);
147 }
148
149 void IdentitiesSettingsPage::load() {
150   currentId = 0;
151   foreach(Identity *identity, identities.values()) {
152     identity->deleteLater();
153   }
154   identities.clear();
155   deletedIdentities.clear();
156   changedIdentities.clear();
157   ui.identityList->clear();
158   foreach(IdentityId id, Client::identityIds()) {
159     clientIdentityCreated(id);
160   }
161   setChangedState(false);
162 }
163
164 void IdentitiesSettingsPage::widgetHasChanged() {
165   bool changed = testHasChanged();
166   if(changed != hasChanged()) setChangedState(changed);
167 }
168
169 bool IdentitiesSettingsPage::testHasChanged() {
170   if(deletedIdentities.count()) return true;
171   if(currentId < 0) {
172     return true; // new identity
173   } else {
174     if(currentId != 0) {
175       changedIdentities.removeAll(currentId);
176       CertIdentity temp(currentId, this);
177       saveToIdentity(&temp);
178       temp.setIdentityName(identities[currentId]->identityName());
179       if(temp != *Client::identity(currentId) || temp.isDirty())
180         changedIdentities.append(currentId);
181     }
182     return changedIdentities.count();
183   }
184 }
185
186 bool IdentitiesSettingsPage::aboutToSave() {
187   saveToIdentity(identities[currentId]);
188   QList<int> errors;
189   foreach(Identity *id, identities.values()) {
190     if(id->identityName().isEmpty()) errors.append(1);
191     if(!id->nicks().count()) errors.append(2);
192     if(id->realName().isEmpty()) errors.append(3);
193     if(id->ident().isEmpty()) errors.append(4);
194   }
195   if(!errors.count()) return true;
196   QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
197   if(errors.contains(1)) error += tr("<li>All identities need an identity name set</li>");
198   if(errors.contains(2)) error += tr("<li>Every identity needs at least one nickname defined</li>");
199   if(errors.contains(3)) error += tr("<li>You need to specify a real name for every identity</li>");
200   if(errors.contains(4)) error += tr("<li>You need to specify an ident for every identity</li>");
201   error += tr("</ul>");
202   QMessageBox::warning(this, tr("One or more identities are invalid"), error);
203   return false;
204 }
205
206 void IdentitiesSettingsPage::clientIdentityCreated(IdentityId id) {
207   CertIdentity *identity = new CertIdentity(*Client::identity(id), this);
208   identity->enableEditSsl(_editSsl);
209   insertIdentity(identity);
210   connect(identity, SIGNAL(sslSettingsUpdated()), this, SLOT(clientIdentityUpdated()));
211   connect(Client::identity(id), SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
212 }
213
214 void IdentitiesSettingsPage::clientIdentityUpdated() {
215   const Identity *clientIdentity = qobject_cast<Identity *>(sender());
216   if(!clientIdentity) {
217     qWarning() << "Invalid identity to update!";
218     return;
219   }
220   if(!identities.contains(clientIdentity->id())) {
221     qWarning() << "Unknown identity to update:" << clientIdentity->identityName();
222     return;
223   }
224
225   CertIdentity *identity = identities[clientIdentity->id()];
226
227   if(identity->identityName() != clientIdentity->identityName())
228     renameIdentity(identity->id(), clientIdentity->identityName());
229
230   identity->update(*clientIdentity);
231
232   if(identity->id() == currentId)
233     displayIdentity(identity, true);
234 }
235
236 void IdentitiesSettingsPage::clientIdentityRemoved(IdentityId id) {
237   if(identities.contains(id)) {
238     removeIdentity(identities[id]);
239     changedIdentities.removeAll(id);
240     deletedIdentities.removeAll(id);
241   }
242 }
243
244 void IdentitiesSettingsPage::insertIdentity(CertIdentity *identity) {
245   IdentityId id = identity->id();
246   identities[id] = identity;
247   if(id == 1) {
248     // default identity is always the first one!
249     ui.identityList->insertItem(0, identity->identityName(), id.toInt());
250   } else {
251     QString name = identity->identityName();
252     for(int j = 0; j < ui.identityList->count(); j++) {
253       if((j>0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
254         ui.identityList->insertItem(j, name, id.toInt());
255         widgetHasChanged();
256         return;
257       }
258     }
259     // append
260     ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
261     widgetHasChanged();
262   }
263 }
264
265 void IdentitiesSettingsPage::renameIdentity(IdentityId id, const QString &newName) {
266   Identity *identity = identities[id];
267   ui.identityList->setItemText(ui.identityList->findData(identity->id().toInt()), newName);
268   identity->setIdentityName(newName);
269 }
270
271 void IdentitiesSettingsPage::removeIdentity(Identity *id) {
272   identities.remove(id->id());
273   ui.identityList->removeItem(ui.identityList->findData(id->id().toInt()));
274   changedIdentities.removeAll(id->id());
275   if(currentId == id->id()) currentId = 0;
276   id->deleteLater();
277   widgetHasChanged();
278 }
279
280 void IdentitiesSettingsPage::on_identityList_currentIndexChanged(int index) {
281   if(index < 0) {
282     //ui.identityList->setEditable(false);
283     displayIdentity(0);
284   } else {
285     IdentityId id = ui.identityList->itemData(index).toInt();
286     if(identities.contains(id)) displayIdentity(identities[id]);
287     ui.deleteIdentity->setEnabled(id != 1); // default identity cannot be deleted
288     ui.renameIdentity->setEnabled(id != 1); // ...or renamed
289   }
290 }
291
292 void IdentitiesSettingsPage::displayIdentity(CertIdentity *id, bool dontsave) {
293   if(currentId != 0 && !dontsave && identities.contains(currentId)) {
294     saveToIdentity(identities[currentId]);
295   }
296   if(id) {
297     currentId = id->id();
298     ui.realName->setText(id->realName());
299     ui.nicknameList->clear();
300     ui.nicknameList->addItems(id->nicks());
301     //for(int i = 0; i < ui.nicknameList->count(); i++) {
302     //  ui.nicknameList->item(i)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
303     //}
304     if(ui.nicknameList->count()) ui.nicknameList->setCurrentRow(0);
305     ui.awayNick->setText(id->awayNick());
306     ui.awayNickEnabled->setChecked(id->awayNickEnabled());
307     ui.awayReason->setText(id->awayReason());
308     ui.awayReasonEnabled->setChecked(id->awayReasonEnabled());
309     ui.autoAwayEnabled->setChecked(id->autoAwayEnabled());
310     ui.autoAwayTime->setValue(id->autoAwayTime());
311     ui.autoAwayReason->setText(id->autoAwayReason());
312     ui.autoAwayReasonEnabled->setChecked(id->autoAwayReasonEnabled());
313     ui.detachAwayEnabled->setChecked(id->detachAwayEnabled());
314     ui.detachAwayReason->setText(id->detachAwayReason());
315     ui.detachAwayReasonEnabled->setChecked(id->detachAwayReasonEnabled());
316     ui.ident->setText(id->ident());
317     ui.kickReason->setText(id->kickReason());
318     ui.partReason->setText(id->partReason());
319     ui.quitReason->setText(id->quitReason());
320     showKeyState(id->sslKey());
321     showCertState(id->sslCert());
322   }
323 }
324
325 void IdentitiesSettingsPage::saveToIdentity(CertIdentity *id) {
326   id->setRealName(ui.realName->text());
327   QStringList nicks;
328   for(int i = 0; i < ui.nicknameList->count(); i++) {
329     nicks << ui.nicknameList->item(i)->text();
330   }
331   id->setNicks(nicks);
332   id->setAwayNick(ui.awayNick->text());
333   id->setAwayNickEnabled(ui.awayNickEnabled->isChecked());
334   id->setAwayReason(ui.awayReason->text());
335   id->setAwayReasonEnabled(ui.awayReasonEnabled->isChecked());
336   id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
337   id->setAutoAwayTime(ui.autoAwayTime->value());
338   id->setAutoAwayReason(ui.autoAwayReason->text());
339   id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
340   id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
341   id->setDetachAwayReason(ui.detachAwayReason->text());
342   id->setDetachAwayReasonEnabled(ui.detachAwayReasonEnabled->isChecked());
343   id->setIdent(ui.ident->text());
344   id->setKickReason(ui.kickReason->text());
345   id->setPartReason(ui.partReason->text());
346   id->setQuitReason(ui.quitReason->text());
347   id->setSslKey(QSslKey(ui.keyTypeLabel->property("sslKey").toByteArray(), (QSsl::KeyAlgorithm)(ui.keyTypeLabel->property("sslKeyType").toInt())));
348   id->setSslCert(QSslCertificate(ui.certOrgLabel->property("sslCert").toByteArray()));
349 }
350
351 void IdentitiesSettingsPage::on_addIdentity_clicked() {
352   CreateIdentityDlg dlg(ui.identityList->model(), this);
353   if(dlg.exec() == QDialog::Accepted) {
354     // find a free (negative) ID
355     IdentityId id;
356     for(id = 1; id <= identities.count(); id++) {
357       if(!identities.keys().contains(-id.toInt())) break;
358     }
359     id = -id.toInt();
360     CertIdentity *newId = new CertIdentity(id, this);
361     newId->enableEditSsl(_editSsl);
362     if(dlg.duplicateId() != 0) {
363       // duplicate
364       newId->update(*identities[dlg.duplicateId()]);
365       newId->setId(id);
366     }
367     newId->setIdentityName(dlg.identityName());
368     identities[id] = newId;
369     insertIdentity(newId);
370     ui.identityList->setCurrentIndex(ui.identityList->findData(id.toInt()));
371     widgetHasChanged();
372   }
373 }
374
375 void IdentitiesSettingsPage::on_deleteIdentity_clicked() {
376   Identity *id = identities[currentId];
377   int ret = QMessageBox::question(this, tr("Delete Identity?"),
378                                   tr("Do you really want to delete identity \"%1\"?").arg(id->identityName()),
379                                   QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
380   if(ret != QMessageBox::Yes) return;
381   if(id->id() > 0) deletedIdentities.append(id->id());
382   currentId = 0;
383   removeIdentity(id);
384 }
385
386 void IdentitiesSettingsPage::on_renameIdentity_clicked() {
387   QString oldName = identities[currentId]->identityName();
388   bool ok = false;
389   QString name = QInputDialog::getText(this, tr("Rename Identity"),
390                                        tr("Please enter a new name for the identity \"%1\"!").arg(oldName),
391                                        QLineEdit::Normal, oldName, &ok);
392   if(ok && !name.isEmpty()) {
393     renameIdentity(currentId, name);
394     widgetHasChanged();
395   }
396 }
397
398 void IdentitiesSettingsPage::on_addNick_clicked() {
399   QStringList existing;
400   for(int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
401   NickEditDlg dlg(QString(), existing, this);
402   if(dlg.exec() == QDialog::Accepted) {
403     ui.nicknameList->addItem(dlg.nick());
404     ui.nicknameList->setCurrentRow(ui.nicknameList->count()-1);
405     setWidgetStates();
406     widgetHasChanged();
407   }
408 }
409
410 void IdentitiesSettingsPage::on_deleteNick_clicked() {
411   // no confirmation, since a nickname is really nothing hard to recreate
412   if(ui.nicknameList->selectedItems().count()) {
413     delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
414     ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow()+1, ui.nicknameList->count()-1));
415     setWidgetStates();
416     widgetHasChanged();
417   }
418 }
419
420 void IdentitiesSettingsPage::on_renameNick_clicked() {
421   if(!ui.nicknameList->selectedItems().count()) return;
422   QString old = ui.nicknameList->selectedItems()[0]->text();
423   QStringList existing;
424   for(int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
425   NickEditDlg dlg(old, existing, this);
426   if(dlg.exec() == QDialog::Accepted) {
427     ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
428   }
429
430 }
431
432 void IdentitiesSettingsPage::on_nickUp_clicked() {
433   if(!ui.nicknameList->selectedItems().count()) return;
434   int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
435   if(row > 0) {
436     ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
437     ui.nicknameList->setCurrentRow(row-1);
438     setWidgetStates();
439     widgetHasChanged();
440   }
441 }
442
443 void IdentitiesSettingsPage::on_nickDown_clicked() {
444   if(!ui.nicknameList->selectedItems().count()) return;
445   int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
446   if(row < ui.nicknameList->count()-1) {
447     ui.nicknameList->insertItem(row+1, ui.nicknameList->takeItem(row));
448     ui.nicknameList->setCurrentRow(row+1);
449     setWidgetStates();
450     widgetHasChanged();
451   }
452 }
453
454 void IdentitiesSettingsPage::on_continueUnsecured_clicked() {
455   _editSsl = true;
456
457   QHash<IdentityId, CertIdentity *>::iterator idIter;
458   for(idIter = identities.begin(); idIter != identities.end(); idIter++) {
459     idIter.value()->enableEditSsl();
460   }
461
462   ui.keyAndCertSettings->setCurrentIndex(2);
463 }
464
465 void IdentitiesSettingsPage::on_clearOrLoadKeyButton_clicked() {
466   QSslKey key;
467
468   if(ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty()) {
469     QString keyFileName = QFileDialog::getOpenFileName(this, tr("Load a Key"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
470     QFile keyFile(keyFileName);
471     keyFile.open(QIODevice::ReadOnly);
472     QByteArray keyRaw = keyFile.read(2 << 20);
473     keyFile.close();
474
475     for(int i = 0; i < 2; i++) {
476       for(int j = 0; j < 2; j++) {
477         key = QSslKey(keyRaw, (QSsl::KeyAlgorithm)j, (QSsl::EncodingFormat)i);
478         if(!key.isNull())
479           goto showKey;
480       }
481     }
482   }
483
484  showKey:
485   showKeyState(key);
486   widgetHasChanged();
487 }
488
489 void IdentitiesSettingsPage::showKeyState(const QSslKey &key) {
490   if(key.isNull()) {
491     ui.keyTypeLabel->setText(tr("No Key loaded"));
492     ui.clearOrLoadKeyButton->setText(tr("Load"));
493   } else {
494     switch(key.algorithm()) {
495     case QSsl::Rsa:
496       ui.keyTypeLabel->setText(tr("RSA"));
497       break;
498     case QSsl::Dsa:
499       ui.keyTypeLabel->setText(tr("DSA"));
500       break;
501     default:
502       ui.keyTypeLabel->setText(tr("No Key Loaded"));
503     }
504     ui.clearOrLoadKeyButton->setText(tr("Clear"));
505   }
506   ui.keyTypeLabel->setProperty("sslKey", key.toPem());
507   ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
508 }
509
510 void IdentitiesSettingsPage::on_clearOrLoadCertButton_clicked() {
511   QSslCertificate cert;
512
513   if(ui.certOrgLabel->property("sslCert").toByteArray().isEmpty()) {
514     QString certFileName = QFileDialog::getOpenFileName(this, tr("Load a Certificate"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
515     QFile certFile(certFileName);
516     certFile.open(QIODevice::ReadOnly);
517     QByteArray certRaw = certFile.read(2 << 20);
518     certFile.close();
519
520     for(int i = 0; i < 2; i++) {
521       cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
522       if(cert.isValid())
523         break;
524     }
525   }
526
527   showCertState(cert);
528   widgetHasChanged();
529 }
530
531 void IdentitiesSettingsPage::showCertState(const QSslCertificate &cert) {
532   if(!cert.isValid()) {
533     ui.certOrgLabel->setText(tr("No Certificate loaded"));
534     ui.certCNameLabel->setText(tr("No Certificate loaded"));
535     ui.clearOrLoadCertButton->setText(tr("Load"));
536   } else {
537     ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization));
538     ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName));
539     ui.clearOrLoadCertButton->setText(tr("Clear"));
540   }
541   ui.certOrgLabel->setProperty("sslCert", cert.toPem());
542  }
543
544 /*****************************************************************************************/
545
546 CreateIdentityDlg::CreateIdentityDlg(QAbstractItemModel *model, QWidget *parent)
547   : QDialog(parent)
548 {
549   ui.setupUi(this);
550
551   ui.identityList->setModel(model);  // now we use the identity list of the main page... Trolltech <3
552   on_identityName_textChanged("");   // disable ok button :)
553 }
554
555 QString CreateIdentityDlg::identityName() const {
556   return ui.identityName->text();
557 }
558
559 IdentityId CreateIdentityDlg::duplicateId() const {
560   if(!ui.duplicateIdentity->isChecked()) return 0;
561   if(ui.identityList->currentIndex() >= 0) {
562     return ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
563   }
564   return 0;
565 }
566
567 void CreateIdentityDlg::on_identityName_textChanged(const QString &text) {
568   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(text.count());
569
570 }
571
572 /*********************************************************************************************/
573
574 SaveIdentitiesDlg::SaveIdentitiesDlg(const QList<CertIdentity *> &toCreate, const QList<CertIdentity *> &toUpdate, const QList<IdentityId> &toRemove, QWidget *parent)
575   : QDialog(parent)
576 {
577   ui.setupUi(this);
578   ui.abort->setIcon(SmallIcon("dialog-cancel"));
579
580   numevents = toCreate.count() + toUpdate.count() + toRemove.count();
581   rcvevents = 0;
582   if(numevents) {
583     ui.progressBar->setMaximum(numevents);
584     ui.progressBar->setValue(0);
585
586     connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientEvent()));
587     connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientEvent()));
588
589     foreach(CertIdentity *id, toCreate) {
590       Client::createIdentity(*id);
591     }
592     foreach(CertIdentity *id, toUpdate) {
593       const Identity *cid = Client::identity(id->id());
594       if(!cid) {
595         qWarning() << "Invalid client identity!";
596         numevents--;
597         continue;
598       }
599       connect(cid, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
600       Client::updateIdentity(id->id(), id->toVariantMap());
601       id->requestUpdateSslSettings();
602     }
603     foreach(IdentityId id, toRemove) {
604       Client::removeIdentity(id);
605     }
606   } else {
607     qWarning() << "Sync dialog called without stuff to change!";
608     accept();
609   }
610 }
611
612 void SaveIdentitiesDlg::clientEvent() {
613   ui.progressBar->setValue(++rcvevents);
614   if(rcvevents >= numevents) accept();
615 }
616
617 /*************************************************************************************************/
618
619 NickEditDlg::NickEditDlg(const QString &old, const QStringList &exist, QWidget *parent)
620   : QDialog(parent), oldNick(old), existing(exist) {
621   ui.setupUi(this);
622
623   // define a regexp for valid nicknames
624   // TODO: add max nicklength according to ISUPPORT
625   QString letter = "A-Za-z";
626   QString special = "\x5b-\x60\x7b-\x7d";
627   QRegExp rx(QString("[%1%2][%1%2\\d-]*").arg(letter, special));
628   ui.nickEdit->setValidator(new QRegExpValidator(rx, ui.nickEdit));
629   if(old.isEmpty()) {
630     // new nick
631     setWindowTitle(tr("Add Nickname"));
632     on_nickEdit_textChanged(""); // disable ok button
633   } else ui.nickEdit->setText(old);
634 }
635
636 QString NickEditDlg::nick() const {
637   return ui.nickEdit->text();
638
639 }
640
641 void NickEditDlg::on_nickEdit_textChanged(const QString &text) {
642   ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text));
643 }
644
645
646