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