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