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