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