modernize: Replace most remaining old-style connects by PMF ones
[quassel.git] / src / qtui / settingspages / identityeditwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 <QMessageBox>
27 #include <QMimeData>
28 #include <QUrl>
29 #include <QStandardPaths>
30
31 #include "client.h"
32 #include "icon.h"
33 #include "util.h"
34
35 IdentityEditWidget::IdentityEditWidget(QWidget *parent)
36     : QWidget(parent)
37 {
38     ui.setupUi(this);
39
40     ui.addNick->setIcon(icon::get("list-add"));
41     ui.deleteNick->setIcon(icon::get("edit-delete"));
42     ui.renameNick->setIcon(icon::get("edit-rename"));
43     ui.nickUp->setIcon(icon::get("go-up"));
44     ui.nickDown->setIcon(icon::get("go-down"));
45
46     // We need to know whenever the state of input widgets changes...
47     connect(ui.realName, &QLineEdit::textEdited, this, &IdentityEditWidget::widgetHasChanged);
48     connect(ui.nicknameList, &QListWidget::itemChanged, this, &IdentityEditWidget::widgetHasChanged);
49     connect(ui.awayNick, &QLineEdit::textEdited, this, &IdentityEditWidget::widgetHasChanged);
50     connect(ui.awayReason, &QLineEdit::textEdited, this, &IdentityEditWidget::widgetHasChanged);
51     connect(ui.autoAwayEnabled, &QGroupBox::clicked, this, &IdentityEditWidget::widgetHasChanged);
52     connect(ui.autoAwayTime, selectOverload<int>(&QSpinBox::valueChanged), this, &IdentityEditWidget::widgetHasChanged);
53     connect(ui.autoAwayReason, &QLineEdit::textEdited, this, &IdentityEditWidget::widgetHasChanged);
54     connect(ui.autoAwayReasonEnabled, &QAbstractButton::clicked, this, &IdentityEditWidget::widgetHasChanged);
55     connect(ui.detachAwayEnabled, &QGroupBox::clicked, this, &IdentityEditWidget::widgetHasChanged);
56     connect(ui.detachAwayReason, &QLineEdit::textEdited, this, &IdentityEditWidget::widgetHasChanged);
57     connect(ui.ident, &QLineEdit::textEdited, this, &IdentityEditWidget::widgetHasChanged);
58     connect(ui.kickReason, &QLineEdit::textEdited, this, &IdentityEditWidget::widgetHasChanged);
59     connect(ui.partReason, &QLineEdit::textEdited, this, &IdentityEditWidget::widgetHasChanged);
60     connect(ui.quitReason, &QLineEdit::textEdited, this, &IdentityEditWidget::widgetHasChanged);
61
62     setWidgetStates();
63     connect(ui.nicknameList, &QListWidget::itemSelectionChanged, this, &IdentityEditWidget::setWidgetStates);
64
65     connect(ui.continueUnsecured, &QAbstractButton::clicked, this, &IdentityEditWidget::requestEditSsl);
66
67     // we would need this if we enabled drag and drop in the nicklist...
68     //connect(ui.nicknameList, &QListWidget::rowsInserted, this, &IdentityEditWidget::setWidgetStates);
69     //connect(ui.nicknameList->model(), &NickListModel::rowsInserted, this, IdentityEditWidget::nicklistHasChanged);
70
71     // disabling unused stuff
72     ui.autoAwayEnabled->hide();
73     ui.awayNick->hide();
74     ui.awayNickLabel->hide();
75
76     ui.detachAwayEnabled->setVisible(!Client::internalCore());
77
78 #ifdef HAVE_SSL
79     ui.sslKeyGroupBox->setAcceptDrops(true);
80     ui.sslKeyGroupBox->installEventFilter(this);
81     ui.sslCertGroupBox->setAcceptDrops(true);
82     ui.sslCertGroupBox->installEventFilter(this);
83 #endif
84
85     if (Client::isCoreFeatureEnabled(Quassel::Feature::AwayFormatTimestamp)) {
86         // Core allows formatting %%timestamp%% messages in away strings.  Update tooltips.
87         QString strFormatTooltip;
88         QTextStream formatTooltip( &strFormatTooltip, QIODevice::WriteOnly );
89         formatTooltip << "<qt><style>.bold { font-weight: bold; } "
90                          ".italic { font-style: italic; }</style>";
91
92         // Function to add a row to the tooltip table
93         auto addRow = [&](const QString& key, const QString& value, bool condition) {
94             if (condition) {
95                 formatTooltip << "<tr><td class='bold' align='right'>"
96                         << key << "</td><td>" << value << "</td></tr>";
97             }
98         };
99
100         // Original tooltip goes here
101         formatTooltip << "<p>%1</p>";
102         // New timestamp formatting guide here
103         formatTooltip << "<p>" << tr("You can add date/time to this message "
104                                "using the syntax: "
105                                "<br/>%%<span class='italic'>&lt;format&gt;</span>%%, where "
106                                "<span class='italic'>&lt;format&gt;</span> is:") << "</p>";
107         formatTooltip << "<table cellspacing='5' cellpadding='0'>";
108         addRow("hh", tr("the hour"), true);
109         addRow("mm", tr("the minutes"), true);
110         addRow("ss", tr("seconds"), true);
111         addRow("AP", tr("AM/PM"), true);
112         addRow("dd", tr("day"), true);
113         addRow("MM", tr("month"), true);
114         addRow("t", tr("current timezone"), true);
115         formatTooltip << "</table>";
116         formatTooltip << "<p>" << tr("Example: Away since %%hh:mm%% on %%dd.MM%%.") << "</p>";
117         formatTooltip << "<p>" << tr("%%%% without anything inside represents %%.  Other format "
118                                      "codes are available.") << "</p>";
119         formatTooltip << "</qt>";
120
121         // Split up the message to allow re-using translations:
122         // [Original tool-tip]  [Timestamp format message]
123         ui.awayReason->setToolTip(strFormatTooltip.arg(ui.awayReason->toolTip()));
124         ui.detachAwayEnabled->setToolTip(strFormatTooltip.arg(ui.detachAwayEnabled->toolTip()));
125     } // else: Do nothing, leave the original translated string
126 }
127
128
129 void IdentityEditWidget::setWidgetStates()
130 {
131     if (ui.nicknameList->selectedItems().count()) {
132         ui.renameNick->setEnabled(true);
133         ui.nickUp->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) > 0);
134         ui.nickDown->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) < ui.nicknameList->count()-1);
135     }
136     else {
137         ui.renameNick->setDisabled(true);
138         ui.nickUp->setDisabled(true);
139         ui.nickDown->setDisabled(true);
140     }
141     ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
142 }
143
144
145 void IdentityEditWidget::displayIdentity(CertIdentity *id, CertIdentity *saveId)
146 {
147     if (saveId) {
148         saveToIdentity(saveId);
149     }
150
151     if (!id)
152         return;
153
154     ui.realName->setText(id->realName());
155     ui.nicknameList->clear();
156     ui.nicknameList->addItems(id->nicks());
157     //for(int i = 0; i < ui.nicknameList->count(); i++) {
158     //  ui.nicknameList->item(i)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
159     //}
160     if (ui.nicknameList->count()) ui.nicknameList->setCurrentRow(0);
161     ui.awayNick->setText(id->awayNick());
162     ui.awayReason->setText(id->awayReason());
163     ui.autoAwayEnabled->setChecked(id->autoAwayEnabled());
164     ui.autoAwayTime->setValue(id->autoAwayTime());
165     ui.autoAwayReason->setText(id->autoAwayReason());
166     ui.autoAwayReasonEnabled->setChecked(id->autoAwayReasonEnabled());
167     ui.detachAwayEnabled->setChecked(id->detachAwayEnabled());
168     ui.detachAwayReason->setText(id->detachAwayReason());
169     ui.ident->setText(id->ident());
170     ui.kickReason->setText(id->kickReason());
171     ui.partReason->setText(id->partReason());
172     ui.quitReason->setText(id->quitReason());
173 #ifdef HAVE_SSL
174     showKeyState(id->sslKey());
175     showCertState(id->sslCert());
176 #endif
177 }
178
179
180 void IdentityEditWidget::saveToIdentity(CertIdentity *id)
181 {
182     QRegExp linebreaks = QRegExp("[\\r\\n]");
183     id->setRealName(ui.realName->text());
184     QStringList nicks;
185     for (int i = 0; i < ui.nicknameList->count(); i++) {
186         nicks << ui.nicknameList->item(i)->text();
187     }
188     id->setNicks(nicks);
189     id->setAwayNick(ui.awayNick->text());
190     id->setAwayNickEnabled(true);
191     id->setAwayReason(ui.awayReason->text().remove(linebreaks));
192     id->setAwayReasonEnabled(true);
193     id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
194     id->setAutoAwayTime(ui.autoAwayTime->value());
195     id->setAutoAwayReason(ui.autoAwayReason->text().remove(linebreaks));
196     id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
197     id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
198     id->setDetachAwayReason(ui.detachAwayReason->text().remove(linebreaks));
199     id->setDetachAwayReasonEnabled(true);
200     id->setIdent(ui.ident->text());
201     id->setKickReason(ui.kickReason->text().remove(linebreaks));
202     id->setPartReason(ui.partReason->text().remove(linebreaks));
203     id->setQuitReason(ui.quitReason->text().remove(linebreaks));
204 #ifdef HAVE_SSL
205     id->setSslKey(QSslKey(ui.keyTypeLabel->property("sslKey").toByteArray(), (QSsl::KeyAlgorithm)(ui.keyTypeLabel->property("sslKeyType").toInt())));
206     id->setSslCert(QSslCertificate(ui.certOrgLabel->property("sslCert").toByteArray()));
207 #endif
208 }
209
210
211 void IdentityEditWidget::on_addNick_clicked()
212 {
213     QStringList existing;
214     for (int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
215     NickEditDlg dlg(QString(), existing, this);
216     if (dlg.exec() == QDialog::Accepted) {
217         ui.nicknameList->addItem(dlg.nick());
218         ui.nicknameList->setCurrentRow(ui.nicknameList->count()-1);
219         setWidgetStates();
220         emit widgetHasChanged();
221     }
222 }
223
224
225 void IdentityEditWidget::on_deleteNick_clicked()
226 {
227     // no confirmation, since a nickname is really nothing hard to recreate
228     if (ui.nicknameList->selectedItems().count()) {
229         delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
230         ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow()+1, ui.nicknameList->count()-1));
231         setWidgetStates();
232         emit widgetHasChanged();
233     }
234 }
235
236
237 void IdentityEditWidget::on_renameNick_clicked()
238 {
239     if (!ui.nicknameList->selectedItems().count()) return;
240     QString old = ui.nicknameList->selectedItems()[0]->text();
241     QStringList existing;
242     for (int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
243     NickEditDlg dlg(old, existing, this);
244     if (dlg.exec() == QDialog::Accepted) {
245         ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
246     }
247 }
248
249
250 void IdentityEditWidget::on_nickUp_clicked()
251 {
252     if (!ui.nicknameList->selectedItems().count()) return;
253     int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
254     if (row > 0) {
255         ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
256         ui.nicknameList->setCurrentRow(row-1);
257         setWidgetStates();
258         emit widgetHasChanged();
259     }
260 }
261
262
263 void IdentityEditWidget::on_nickDown_clicked()
264 {
265     if (!ui.nicknameList->selectedItems().count()) return;
266     int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
267     if (row < ui.nicknameList->count()-1) {
268         ui.nicknameList->insertItem(row+1, ui.nicknameList->takeItem(row));
269         ui.nicknameList->setCurrentRow(row+1);
270         setWidgetStates();
271         emit widgetHasChanged();
272     }
273 }
274
275
276 void IdentityEditWidget::showAdvanced(bool advanced)
277 {
278     int idx = ui.tabWidget->indexOf(ui.advancedTab);
279     if (advanced) {
280         if (idx != -1)
281             return;  // already added
282         ui.tabWidget->addTab(ui.advancedTab, tr("Advanced"));
283     }
284     else {
285         if (idx == -1)
286             return;  // already removed
287         ui.tabWidget->removeTab(idx);
288     }
289 }
290
291
292 void IdentityEditWidget::setSslState(SslState state)
293 {
294     switch (state) {
295     case NoSsl:
296         ui.keyAndCertSettings->setCurrentIndex(0);
297         break;
298     case UnsecureSsl:
299         ui.keyAndCertSettings->setCurrentIndex(1);
300         break;
301     case AllowSsl:
302         ui.keyAndCertSettings->setCurrentIndex(2);
303         break;
304     }
305 }
306
307
308 #ifdef HAVE_SSL
309 bool IdentityEditWidget::eventFilter(QObject *watched, QEvent *event)
310 {
311     bool isCert = (watched == ui.sslCertGroupBox);
312     switch (event->type()) {
313     case QEvent::DragEnter:
314         sslDragEnterEvent(static_cast<QDragEnterEvent *>(event));
315         return true;
316     case QEvent::Drop:
317         sslDropEvent(static_cast<QDropEvent *>(event), isCert);
318         return true;
319     default:
320         return false;
321     }
322 }
323
324
325 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent *event)
326 {
327     if (event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
328         event->setDropAction(Qt::CopyAction);
329         event->accept();
330     }
331 }
332
333
334 void IdentityEditWidget::sslDropEvent(QDropEvent *event, bool isCert)
335 {
336     QByteArray rawUris;
337     if (event->mimeData()->hasFormat("text/uri-list"))
338         rawUris = event->mimeData()->data("text/uri-list");
339     else
340         rawUris = event->mimeData()->data("text/uri");
341
342     QTextStream uriStream(rawUris);
343     QString filename = QUrl(uriStream.readLine()).toLocalFile();
344
345     if (isCert) {
346         QSslCertificate cert = certByFilename(filename);
347         if (!cert.isNull())
348             showCertState(cert);
349     }
350     else {
351         QSslKey key = keyByFilename(filename);
352         if (!key.isNull())
353             showKeyState(key);
354     }
355     event->accept();
356     emit widgetHasChanged();
357 }
358
359
360 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked()
361 {
362     QSslKey key;
363
364     if (ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
365         key = keyByFilename(QFileDialog::getOpenFileName(this, tr("Load a Key"), QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
366
367     showKeyState(key);
368     emit widgetHasChanged();
369 }
370
371
372 QSslKey IdentityEditWidget::keyByFilename(const QString &filename)
373 {
374     QSslKey key;
375
376     QFile keyFile(filename);
377     keyFile.open(QIODevice::ReadOnly);
378     QByteArray keyRaw = keyFile.read(2 << 20);
379     keyFile.close();
380
381     for (int i = 0; i < 2; i++) {
382         // On Qt5.5+, support QSsl::KeyAlgorithm::Rsa (1), QSsl::KeyAlgorithm::Dsa (2), and QSsl::KeyAlgorithm::Ec (3)
383         for (int j = 1; j < 4; j++) {
384             key = QSslKey(keyRaw, (QSsl::KeyAlgorithm)j, (QSsl::EncodingFormat)i);
385             if (!key.isNull())
386                 goto returnKey;
387         }
388     }
389     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."));
390 returnKey:
391     if(!key.isNull() && key.algorithm() == QSsl::KeyAlgorithm::Ec && !Client::isCoreFeatureEnabled(Quassel::Feature::EcdsaCertfpKeys)) {
392         QMessageBox::information(this, tr("Core does not support ECDSA keys"), tr("You loaded an ECDSA key, but the core does not support ECDSA keys. Please contact the core administrator."));
393         key.clear();
394     }
395     return key;
396 }
397
398
399 void IdentityEditWidget::showKeyState(const QSslKey &key)
400 {
401     if (key.isNull()) {
402         ui.keyTypeLabel->setText(tr("No Key loaded"));
403         ui.clearOrLoadKeyButton->setText(tr("Load"));
404     }
405     else {
406         switch (key.algorithm()) {
407         case QSsl::Rsa:
408             ui.keyTypeLabel->setText(tr("RSA"));
409             break;
410         case QSsl::Ec:
411             ui.keyTypeLabel->setText(tr("ECDSA"));
412             break;
413         case QSsl::Dsa:
414             ui.keyTypeLabel->setText(tr("DSA"));
415             break;
416         default:
417             ui.keyTypeLabel->setText(tr("Invalid key or no key loaded"));
418         }
419         ui.clearOrLoadKeyButton->setText(tr("Clear"));
420     }
421     ui.keyTypeLabel->setProperty("sslKey", key.toPem());
422     ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
423 }
424
425
426 void IdentityEditWidget::on_clearOrLoadCertButton_clicked()
427 {
428     QSslCertificate cert;
429
430     if (ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
431         cert = certByFilename(QFileDialog::getOpenFileName(this, tr("Load a Certificate"), QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
432     showCertState(cert);
433     emit widgetHasChanged();
434 }
435
436
437 QSslCertificate IdentityEditWidget::certByFilename(const QString &filename)
438 {
439     QSslCertificate cert;
440     QFile certFile(filename);
441     certFile.open(QIODevice::ReadOnly);
442     QByteArray certRaw = certFile.read(2 << 20);
443     certFile.close();
444
445     for (int i = 0; i < 2; i++) {
446         cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
447         if (!cert.isNull())
448             break;
449     }
450     return cert;
451 }
452
453
454 void IdentityEditWidget::showCertState(const QSslCertificate &cert)
455 {
456     if (cert.isNull()) {
457         ui.certOrgLabel->setText(tr("No Certificate loaded"));
458         ui.certCNameLabel->setText(tr("No Certificate loaded"));
459         ui.clearOrLoadCertButton->setText(tr("Load"));
460     }
461     else {
462         ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization).join(", "));
463         ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName).join(", "));
464         ui.clearOrLoadCertButton->setText(tr("Clear"));
465     }
466     ui.certOrgLabel->setProperty("sslCert", cert.toPem());
467 }
468
469
470 #endif //HAVE_SSL