31c87589bde90ef691fa697e8fc7c3169e89d27a
[quassel.git] / src / qtui / settingspages / identityeditwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 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 <QStandardPaths>
29 #include <QUrl>
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'>" << 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>"
103                       << 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:")
107                       << "</p>";
108         formatTooltip << "<table cellspacing='5' cellpadding='0'>";
109         addRow("hh", tr("the hour"), true);
110         addRow("mm", tr("the minutes"), true);
111         addRow("ss", tr("seconds"), true);
112         addRow("AP", tr("AM/PM"), true);
113         addRow("dd", tr("day"), true);
114         addRow("MM", tr("month"), true);
115         addRow("t", tr("current timezone"), true);
116         formatTooltip << "</table>";
117         formatTooltip << "<p>" << tr("Example: Away since %%hh:mm%% on %%dd.MM%%.") << "</p>";
118         formatTooltip << "<p>"
119                       << tr("%%%% without anything inside represents %%.  Other format "
120                             "codes are available.")
121                       << "</p>";
122         formatTooltip << "</qt>";
123
124         // Split up the message to allow re-using translations:
125         // [Original tool-tip]  [Timestamp format message]
126         ui.awayReason->setToolTip(strFormatTooltip.arg(ui.awayReason->toolTip()));
127         ui.detachAwayEnabled->setToolTip(strFormatTooltip.arg(ui.detachAwayEnabled->toolTip()));
128     }  // else: Do nothing, leave the original translated string
129 }
130
131 void IdentityEditWidget::setWidgetStates()
132 {
133     if (ui.nicknameList->selectedItems().count()) {
134         ui.renameNick->setEnabled(true);
135         ui.nickUp->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) > 0);
136         ui.nickDown->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) < ui.nicknameList->count() - 1);
137     }
138     else {
139         ui.renameNick->setDisabled(true);
140         ui.nickUp->setDisabled(true);
141         ui.nickDown->setDisabled(true);
142     }
143     ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
144 }
145
146 void IdentityEditWidget::displayIdentity(CertIdentity* id, CertIdentity* saveId)
147 {
148     if (saveId) {
149         saveToIdentity(saveId);
150     }
151
152     if (!id)
153         return;
154
155     ui.realName->setText(id->realName());
156     ui.nicknameList->clear();
157     ui.nicknameList->addItems(id->nicks());
158     // for(int i = 0; i < ui.nicknameList->count(); i++) {
159     //  ui.nicknameList->item(i)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
160     //}
161     if (ui.nicknameList->count())
162         ui.nicknameList->setCurrentRow(0);
163     ui.awayNick->setText(id->awayNick());
164     ui.awayReason->setText(id->awayReason());
165     ui.autoAwayEnabled->setChecked(id->autoAwayEnabled());
166     ui.autoAwayTime->setValue(id->autoAwayTime());
167     ui.autoAwayReason->setText(id->autoAwayReason());
168     ui.autoAwayReasonEnabled->setChecked(id->autoAwayReasonEnabled());
169     ui.detachAwayEnabled->setChecked(id->detachAwayEnabled());
170     ui.detachAwayReason->setText(id->detachAwayReason());
171     ui.ident->setText(id->ident());
172     ui.kickReason->setText(id->kickReason());
173     ui.partReason->setText(id->partReason());
174     ui.quitReason->setText(id->quitReason());
175 #ifdef HAVE_SSL
176     showKeyState(id->sslKey());
177     showCertState(id->sslCert());
178 #endif
179 }
180
181 void IdentityEditWidget::saveToIdentity(CertIdentity* id)
182 {
183     QRegExp linebreaks = QRegExp("[\\r\\n]");
184     id->setRealName(ui.realName->text());
185     QStringList nicks;
186     for (int i = 0; i < ui.nicknameList->count(); i++) {
187         nicks << ui.nicknameList->item(i)->text();
188     }
189     id->setNicks(nicks);
190     id->setAwayNick(ui.awayNick->text());
191     id->setAwayNickEnabled(true);
192     id->setAwayReason(ui.awayReason->text().remove(linebreaks));
193     id->setAwayReasonEnabled(true);
194     id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
195     id->setAutoAwayTime(ui.autoAwayTime->value());
196     id->setAutoAwayReason(ui.autoAwayReason->text().remove(linebreaks));
197     id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
198     id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
199     id->setDetachAwayReason(ui.detachAwayReason->text().remove(linebreaks));
200     id->setDetachAwayReasonEnabled(true);
201     id->setIdent(ui.ident->text());
202     id->setKickReason(ui.kickReason->text().remove(linebreaks));
203     id->setPartReason(ui.partReason->text().remove(linebreaks));
204     id->setQuitReason(ui.quitReason->text().remove(linebreaks));
205 #ifdef HAVE_SSL
206     id->setSslKey(
207         QSslKey(ui.keyTypeLabel->property("sslKey").toByteArray(), (QSsl::KeyAlgorithm)(ui.keyTypeLabel->property("sslKeyType").toInt())));
208     id->setSslCert(QSslCertificate(ui.certOrgLabel->property("sslCert").toByteArray()));
209 #endif
210 }
211
212 void IdentityEditWidget::on_addNick_clicked()
213 {
214     QStringList existing;
215     for (int i = 0; i < ui.nicknameList->count(); i++)
216         existing << ui.nicknameList->item(i)->text();
217     NickEditDlg dlg(QString(), existing, this);
218     if (dlg.exec() == QDialog::Accepted) {
219         ui.nicknameList->addItem(dlg.nick());
220         ui.nicknameList->setCurrentRow(ui.nicknameList->count() - 1);
221         setWidgetStates();
222         emit widgetHasChanged();
223     }
224 }
225
226 void IdentityEditWidget::on_deleteNick_clicked()
227 {
228     // no confirmation, since a nickname is really nothing hard to recreate
229     if (ui.nicknameList->selectedItems().count()) {
230         delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
231         ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow() + 1, ui.nicknameList->count() - 1));
232         setWidgetStates();
233         emit widgetHasChanged();
234     }
235 }
236
237 void IdentityEditWidget::on_renameNick_clicked()
238 {
239     if (!ui.nicknameList->selectedItems().count())
240         return;
241     QString old = ui.nicknameList->selectedItems()[0]->text();
242     QStringList existing;
243     for (int i = 0; i < ui.nicknameList->count(); i++)
244         existing << ui.nicknameList->item(i)->text();
245     NickEditDlg dlg(old, existing, this);
246     if (dlg.exec() == QDialog::Accepted) {
247         ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
248     }
249 }
250
251 void IdentityEditWidget::on_nickUp_clicked()
252 {
253     if (!ui.nicknameList->selectedItems().count())
254         return;
255     int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
256     if (row > 0) {
257         ui.nicknameList->insertItem(row - 1, ui.nicknameList->takeItem(row));
258         ui.nicknameList->setCurrentRow(row - 1);
259         setWidgetStates();
260         emit widgetHasChanged();
261     }
262 }
263
264 void IdentityEditWidget::on_nickDown_clicked()
265 {
266     if (!ui.nicknameList->selectedItems().count())
267         return;
268     int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
269     if (row < ui.nicknameList->count() - 1) {
270         ui.nicknameList->insertItem(row + 1, ui.nicknameList->takeItem(row));
271         ui.nicknameList->setCurrentRow(row + 1);
272         setWidgetStates();
273         emit widgetHasChanged();
274     }
275 }
276
277 void IdentityEditWidget::showAdvanced(bool advanced)
278 {
279     int idx = ui.tabWidget->indexOf(ui.advancedTab);
280     if (advanced) {
281         if (idx != -1)
282             return;  // already added
283         ui.tabWidget->addTab(ui.advancedTab, tr("Advanced"));
284     }
285     else {
286         if (idx == -1)
287             return;  // already removed
288         ui.tabWidget->removeTab(idx);
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 #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 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent* event)
324 {
325     if (event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
326         event->setDropAction(Qt::CopyAction);
327         event->accept();
328     }
329 }
330
331 void IdentityEditWidget::sslDropEvent(QDropEvent* event, bool isCert)
332 {
333     QByteArray rawUris;
334     if (event->mimeData()->hasFormat("text/uri-list"))
335         rawUris = event->mimeData()->data("text/uri-list");
336     else
337         rawUris = event->mimeData()->data("text/uri");
338
339     QTextStream uriStream(rawUris);
340     QString filename = QUrl(uriStream.readLine()).toLocalFile();
341
342     if (isCert) {
343         QSslCertificate cert = certByFilename(filename);
344         if (!cert.isNull())
345             showCertState(cert);
346     }
347     else {
348         QSslKey key = keyByFilename(filename);
349         if (!key.isNull())
350             showKeyState(key);
351     }
352     event->accept();
353     emit widgetHasChanged();
354 }
355
356 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked()
357 {
358     QSslKey key;
359
360     if (ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
361         key = keyByFilename(
362             QFileDialog::getOpenFileName(this, tr("Load a Key"), QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
363
364     showKeyState(key);
365     emit widgetHasChanged();
366 }
367
368 QSslKey IdentityEditWidget::keyByFilename(const QString& filename)
369 {
370     QSslKey key;
371
372     QFile keyFile(filename);
373     keyFile.open(QIODevice::ReadOnly);
374     QByteArray keyRaw = keyFile.read(2 << 20);
375     keyFile.close();
376
377     for (int i = 0; i < 2; i++) {
378         // On Qt5.5+, support QSsl::KeyAlgorithm::Rsa (1), QSsl::KeyAlgorithm::Dsa (2), and QSsl::KeyAlgorithm::Ec (3)
379         for (int j = 1; j < 4; j++) {
380             key = QSslKey(keyRaw, (QSsl::KeyAlgorithm)j, (QSsl::EncodingFormat)i);
381             if (!key.isNull())
382                 goto returnKey;
383         }
384     }
385     QMessageBox::information(
386         this,
387         tr("Failed to read key"),
388         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::
392             information(this,
393                         tr("Core does not support ECDSA keys"),
394                         tr("You loaded an ECDSA key, but the core does not support ECDSA keys. Please contact the core administrator."));
395         key.clear();
396     }
397     return key;
398 }
399
400 void IdentityEditWidget::showKeyState(const QSslKey& key)
401 {
402     if (key.isNull()) {
403         ui.keyTypeLabel->setText(tr("No Key loaded"));
404         ui.clearOrLoadKeyButton->setText(tr("Load"));
405     }
406     else {
407         switch (key.algorithm()) {
408         case QSsl::Rsa:
409             ui.keyTypeLabel->setText(tr("RSA"));
410             break;
411         case QSsl::Ec:
412             ui.keyTypeLabel->setText(tr("ECDSA"));
413             break;
414         case QSsl::Dsa:
415             ui.keyTypeLabel->setText(tr("DSA"));
416             break;
417         default:
418             ui.keyTypeLabel->setText(tr("Invalid key or no key loaded"));
419         }
420         ui.clearOrLoadKeyButton->setText(tr("Clear"));
421     }
422     ui.keyTypeLabel->setProperty("sslKey", key.toPem());
423     ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
424 }
425
426 void IdentityEditWidget::on_clearOrLoadCertButton_clicked()
427 {
428     QSslCertificate cert;
429
430     if (ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
431         cert = certByFilename(
432             QFileDialog::getOpenFileName(this, tr("Load a Certificate"), QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
433     showCertState(cert);
434     emit widgetHasChanged();
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 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 #endif  // HAVE_SSL