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