internal refactoring: ripping identity editor and identitory settingspage apart
[quassel.git] / src / qtui / settingspages / identityeditwidget.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) version 3.                                           *
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 "identityeditwidget.h"
22
23 #include <QDesktopServices>
24 #include <QDragEnterEvent>
25 #include <QDropEvent>
26 #include <QFileDialog>
27 #include <QUrl>
28
29 #include "iconloader.h"
30
31 IdentityEditWidget::IdentityEditWidget(QWidget *parent)
32   : QWidget(parent)
33 {
34   ui.setupUi(this);
35
36   ui.addNick->setIcon(SmallIcon("list-add"));
37   ui.deleteNick->setIcon(SmallIcon("edit-delete"));
38   ui.renameNick->setIcon(SmallIcon("edit-rename"));
39   ui.nickUp->setIcon(SmallIcon("go-up"));
40   ui.nickDown->setIcon(SmallIcon("go-down"));
41
42   // We need to know whenever the state of input widgets changes...
43   connect(ui.realName, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
44   connect(ui.nicknameList, SIGNAL(itemChanged(QListWidgetItem *)), this, SIGNAL(widgetHasChanged()));
45   connect(ui.awayNick, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
46   connect(ui.awayNickEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
47   connect(ui.awayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
48   connect(ui.awayReasonEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
49   connect(ui.autoAwayEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
50   connect(ui.autoAwayTime, SIGNAL(valueChanged(int)), this, SIGNAL(widgetHasChanged()));
51   connect(ui.autoAwayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
52   connect(ui.autoAwayReasonEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
53   connect(ui.detachAwayEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
54   connect(ui.detachAwayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
55   connect(ui.detachAwayReasonEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
56   connect(ui.ident, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
57   connect(ui.kickReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
58   connect(ui.partReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
59   connect(ui.quitReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
60
61   setWidgetStates();
62   connect(ui.nicknameList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
63
64   connect(ui.continueUnsecured, SIGNAL(clicked()), this, SIGNAL(requestEditSsl()));
65
66   // we would need this if we enabled drag and drop in the nicklist...
67   //connect(ui.nicknameList, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(setWidgetStates()));
68   //connect(ui.nicknameList->model(), SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(nicklistHasChanged()));
69
70 #ifdef HAVE_SSL
71   ui.sslKeyGroupBox->setAcceptDrops(true);
72   ui.sslKeyGroupBox->installEventFilter(this);
73   ui.sslCertGroupBox->setAcceptDrops(true);
74   ui.sslCertGroupBox->installEventFilter(this);
75 #endif
76 }
77
78 void IdentityEditWidget::setWidgetStates() {
79   if(ui.nicknameList->selectedItems().count()) {
80     ui.renameNick->setEnabled(true);
81     ui.nickUp->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) > 0);
82     ui.nickDown->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) < ui.nicknameList->count()-1);
83   } else {
84     ui.renameNick->setDisabled(true);
85     ui.nickUp->setDisabled(true);
86     ui.nickDown->setDisabled(true);
87   }
88   ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
89 }
90
91 void IdentityEditWidget::displayIdentity(CertIdentity *id, CertIdentity *saveId) {
92   if(saveId) {
93     saveToIdentity(saveId);
94   }
95
96   if(!id)
97     return;
98   
99   ui.realName->setText(id->realName());
100   ui.nicknameList->clear();
101   ui.nicknameList->addItems(id->nicks());
102   //for(int i = 0; i < ui.nicknameList->count(); i++) {
103   //  ui.nicknameList->item(i)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
104   //}
105   if(ui.nicknameList->count()) ui.nicknameList->setCurrentRow(0);
106   ui.awayNick->setText(id->awayNick());
107   ui.awayNickEnabled->setChecked(id->awayNickEnabled());
108   ui.awayReason->setText(id->awayReason());
109   ui.awayReasonEnabled->setChecked(id->awayReasonEnabled());
110   ui.autoAwayEnabled->setChecked(id->autoAwayEnabled());
111   ui.autoAwayTime->setValue(id->autoAwayTime());
112   ui.autoAwayReason->setText(id->autoAwayReason());
113   ui.autoAwayReasonEnabled->setChecked(id->autoAwayReasonEnabled());
114   ui.detachAwayEnabled->setChecked(id->detachAwayEnabled());
115   ui.detachAwayReason->setText(id->detachAwayReason());
116   ui.detachAwayReasonEnabled->setChecked(id->detachAwayReasonEnabled());
117   ui.ident->setText(id->ident());
118   ui.kickReason->setText(id->kickReason());
119   ui.partReason->setText(id->partReason());
120   ui.quitReason->setText(id->quitReason());
121 #ifdef HAVE_SSL
122   showKeyState(id->sslKey());
123   showCertState(id->sslCert());
124 #endif
125   
126 }
127
128 void IdentityEditWidget::saveToIdentity(CertIdentity *id) {
129   id->setRealName(ui.realName->text());
130   QStringList nicks;
131   for(int i = 0; i < ui.nicknameList->count(); i++) {
132     nicks << ui.nicknameList->item(i)->text();
133   }
134   id->setNicks(nicks);
135   id->setAwayNick(ui.awayNick->text());
136   id->setAwayNickEnabled(ui.awayNickEnabled->isChecked());
137   id->setAwayReason(ui.awayReason->text());
138   id->setAwayReasonEnabled(ui.awayReasonEnabled->isChecked());
139   id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
140   id->setAutoAwayTime(ui.autoAwayTime->value());
141   id->setAutoAwayReason(ui.autoAwayReason->text());
142   id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
143   id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
144   id->setDetachAwayReason(ui.detachAwayReason->text());
145   id->setDetachAwayReasonEnabled(ui.detachAwayReasonEnabled->isChecked());
146   id->setIdent(ui.ident->text());
147   id->setKickReason(ui.kickReason->text());
148   id->setPartReason(ui.partReason->text());
149   id->setQuitReason(ui.quitReason->text());
150 #ifdef HAVE_SSL
151   id->setSslKey(QSslKey(ui.keyTypeLabel->property("sslKey").toByteArray(), (QSsl::KeyAlgorithm)(ui.keyTypeLabel->property("sslKeyType").toInt())));
152   id->setSslCert(QSslCertificate(ui.certOrgLabel->property("sslCert").toByteArray()));
153 #endif
154 }
155
156 void IdentityEditWidget::on_addNick_clicked() {
157   QStringList existing;
158   for(int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
159   NickEditDlg dlg(QString(), existing, this);
160   if(dlg.exec() == QDialog::Accepted) {
161     ui.nicknameList->addItem(dlg.nick());
162     ui.nicknameList->setCurrentRow(ui.nicknameList->count()-1);
163     setWidgetStates();
164     emit widgetHasChanged();
165   }
166 }
167
168 void IdentityEditWidget::on_deleteNick_clicked() {
169   // no confirmation, since a nickname is really nothing hard to recreate
170   if(ui.nicknameList->selectedItems().count()) {
171     delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
172     ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow()+1, ui.nicknameList->count()-1));
173     setWidgetStates();
174     emit widgetHasChanged();
175   }
176 }
177
178 void IdentityEditWidget::on_renameNick_clicked() {
179   if(!ui.nicknameList->selectedItems().count()) return;
180   QString old = ui.nicknameList->selectedItems()[0]->text();
181   QStringList existing;
182   for(int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
183   NickEditDlg dlg(old, existing, this);
184   if(dlg.exec() == QDialog::Accepted) {
185     ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
186   }
187
188 }
189
190 void IdentityEditWidget::on_nickUp_clicked() {
191   if(!ui.nicknameList->selectedItems().count()) return;
192   int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
193   if(row > 0) {
194     ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
195     ui.nicknameList->setCurrentRow(row-1);
196     setWidgetStates();
197     emit widgetHasChanged();
198   }
199 }
200
201 void IdentityEditWidget::on_nickDown_clicked() {
202   if(!ui.nicknameList->selectedItems().count()) return;
203   int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
204   if(row < ui.nicknameList->count()-1) {
205     ui.nicknameList->insertItem(row+1, ui.nicknameList->takeItem(row));
206     ui.nicknameList->setCurrentRow(row+1);
207     setWidgetStates();
208     emit widgetHasChanged();
209   }
210 }
211
212 #ifdef HAVE_SSL
213 void IdentityEditWidget::setSslState(SslState state) {
214   switch(state) {
215   case NoSsl:
216     ui.keyAndCertSettings->setCurrentIndex(0);
217     break;
218   case UnsecureSsl:
219     ui.keyAndCertSettings->setCurrentIndex(1);
220     break;
221   case AllowSsl:
222     ui.keyAndCertSettings->setCurrentIndex(2);
223     break;
224   }
225 }
226
227 bool IdentityEditWidget::eventFilter(QObject *watched, QEvent *event) {
228   bool isCert = (watched == ui.sslCertGroupBox);
229   switch(event->type()) {
230   case QEvent::DragEnter:
231     sslDragEnterEvent(static_cast<QDragEnterEvent *>(event));
232     return true;
233   case QEvent::Drop:
234     sslDropEvent(static_cast<QDropEvent *>(event), isCert);
235     return true;
236   default:
237     return false;
238   }
239 }
240
241 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent *event) {
242   if(event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
243     event->setDropAction(Qt::CopyAction);
244     event->accept();
245   }
246 }
247
248 void IdentityEditWidget::sslDropEvent(QDropEvent *event, bool isCert) {
249   QByteArray rawUris;
250   if(event->mimeData()->hasFormat("text/uri-list"))
251     rawUris = event->mimeData()->data("text/uri-list");
252   else
253     rawUris = event->mimeData()->data("text/uri");
254
255   QTextStream uriStream(rawUris);
256   QString filename = QUrl(uriStream.readLine()).toLocalFile();
257
258   if(isCert) {
259     QSslCertificate cert = certByFilename(filename);
260     if(cert.isValid())
261       showCertState(cert);
262   } else {
263     QSslKey key = keyByFilename(filename);
264     if(!key.isNull())
265       showKeyState(key);
266   }
267   event->accept();
268   emit widgetHasChanged();
269 }
270
271 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked() {
272   QSslKey key;
273
274   if(ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
275     key = keyByFilename(QFileDialog::getOpenFileName(this, tr("Load a Key"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
276
277   showKeyState(key);
278   emit widgetHasChanged();
279 }
280
281 QSslKey IdentityEditWidget::keyByFilename(const QString &filename) {
282   QSslKey key;
283
284   QFile keyFile(filename);
285   keyFile.open(QIODevice::ReadOnly);
286   QByteArray keyRaw = keyFile.read(2 << 20);
287   keyFile.close();
288
289   for(int i = 0; i < 2; i++) {
290     for(int j = 0; j < 2; j++) {
291       key = QSslKey(keyRaw, (QSsl::KeyAlgorithm)j, (QSsl::EncodingFormat)i);
292       if(!key.isNull())
293         goto returnKey;
294     }
295   }
296  returnKey:
297   return key;
298 }
299
300 void IdentityEditWidget::showKeyState(const QSslKey &key) {
301   if(key.isNull()) {
302     ui.keyTypeLabel->setText(tr("No Key loaded"));
303     ui.clearOrLoadKeyButton->setText(tr("Load"));
304   } else {
305     switch(key.algorithm()) {
306     case QSsl::Rsa:
307       ui.keyTypeLabel->setText(tr("RSA"));
308       break;
309     case QSsl::Dsa:
310       ui.keyTypeLabel->setText(tr("DSA"));
311       break;
312     default:
313       ui.keyTypeLabel->setText(tr("No Key loaded"));
314     }
315     ui.clearOrLoadKeyButton->setText(tr("Clear"));
316   }
317   ui.keyTypeLabel->setProperty("sslKey", key.toPem());
318   ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
319 }
320
321 void IdentityEditWidget::on_clearOrLoadCertButton_clicked() {
322   QSslCertificate cert;
323
324   if(ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
325     cert = certByFilename(QFileDialog::getOpenFileName(this, tr("Load a Certificate"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
326
327   showCertState(cert);
328   emit widgetHasChanged();
329 }
330
331 QSslCertificate IdentityEditWidget::certByFilename(const QString &filename) {
332   QSslCertificate cert;
333   QFile certFile(filename);
334   certFile.open(QIODevice::ReadOnly);
335   QByteArray certRaw = certFile.read(2 << 20);
336   certFile.close();
337
338   for(int i = 0; i < 2; i++) {
339     cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
340     if(cert.isValid())
341       break;
342   }
343   return cert;
344 }
345
346 void IdentityEditWidget::showCertState(const QSslCertificate &cert) {
347   if(!cert.isValid()) {
348     ui.certOrgLabel->setText(tr("No Certificate loaded"));
349     ui.certCNameLabel->setText(tr("No Certificate loaded"));
350     ui.clearOrLoadCertButton->setText(tr("Load"));
351   } else {
352     ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization));
353     ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName));
354     ui.clearOrLoadCertButton->setText(tr("Clear"));
355   }
356   ui.certOrgLabel->setProperty("sslCert", cert.toPem());
357  }
358 #endif //HAVE_SSL