1 /***************************************************************************
2 * Copyright (C) 2005-2022 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "identityeditwidget.h"
23 #include <QDragEnterEvent>
25 #include <QFileDialog>
26 #include <QMessageBox>
28 #include <QStandardPaths>
35 IdentityEditWidget::IdentityEditWidget(QWidget* parent)
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"));
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);
63 connect(ui.nicknameList, &QListWidget::itemSelectionChanged, this, &IdentityEditWidget::setWidgetStates);
65 connect(ui.continueUnsecured, &QAbstractButton::clicked, this, &IdentityEditWidget::requestEditSsl);
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);
71 // disabling unused stuff
72 ui.autoAwayEnabled->hide();
74 ui.awayNickLabel->hide();
76 ui.detachAwayEnabled->setVisible(!Client::internalCore());
78 ui.sslKeyGroupBox->setAcceptDrops(true);
79 ui.sslKeyGroupBox->installEventFilter(this);
80 ui.sslCertGroupBox->setAcceptDrops(true);
81 ui.sslCertGroupBox->installEventFilter(this);
83 if (Client::isCoreFeatureEnabled(Quassel::Feature::AwayFormatTimestamp)) {
84 // Core allows formatting %%timestamp%% messages in away strings. Update tooltips.
85 QString strFormatTooltip;
86 QTextStream formatTooltip(&strFormatTooltip, QIODevice::WriteOnly);
87 formatTooltip << "<qt><style>.bold { font-weight: bold; } "
88 ".italic { font-style: italic; }</style>";
90 // Function to add a row to the tooltip table
91 auto addRow = [&](const QString& key, const QString& value, bool condition) {
93 formatTooltip << "<tr><td class='bold' align='right'>" << key << "</td><td>" << value << "</td></tr>";
97 // Original tooltip goes here
98 formatTooltip << "<p>%1</p>";
99 // New timestamp formatting guide here
100 formatTooltip << "<p>"
101 << tr("You can add date/time to this message "
103 "<br/>%%<span class='italic'><format></span>%%, where "
104 "<span class='italic'><format></span> is:")
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>"
117 << tr("%%%% without anything inside represents %%. Other format "
118 "codes are available.")
120 formatTooltip << "</qt>";
122 // Split up the message to allow re-using translations:
123 // [Original tool-tip] [Timestamp format message]
124 ui.awayReason->setToolTip(strFormatTooltip.arg(ui.awayReason->toolTip()));
125 ui.detachAwayEnabled->setToolTip(strFormatTooltip.arg(ui.detachAwayEnabled->toolTip()));
126 } // else: Do nothing, leave the original translated string
129 void IdentityEditWidget::setWidgetStates()
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);
137 ui.renameNick->setDisabled(true);
138 ui.nickUp->setDisabled(true);
139 ui.nickDown->setDisabled(true);
141 ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
144 void IdentityEditWidget::displayIdentity(CertIdentity* id, CertIdentity* saveId)
147 saveToIdentity(saveId);
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);
159 if (ui.nicknameList->count())
160 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 showKeyState(id->sslKey());
174 showCertState(id->sslCert());
177 void IdentityEditWidget::saveToIdentity(CertIdentity* id)
179 QRegExp linebreaks = QRegExp("[\\r\\n]");
180 id->setRealName(ui.realName->text());
182 for (int i = 0; i < ui.nicknameList->count(); i++) {
183 nicks << ui.nicknameList->item(i)->text();
186 id->setAwayNick(ui.awayNick->text());
187 id->setAwayNickEnabled(true);
188 id->setAwayReason(ui.awayReason->text().remove(linebreaks));
189 id->setAwayReasonEnabled(true);
190 id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
191 id->setAutoAwayTime(ui.autoAwayTime->value());
192 id->setAutoAwayReason(ui.autoAwayReason->text().remove(linebreaks));
193 id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
194 id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
195 id->setDetachAwayReason(ui.detachAwayReason->text().remove(linebreaks));
196 id->setDetachAwayReasonEnabled(true);
197 id->setIdent(ui.ident->text());
198 id->setKickReason(ui.kickReason->text().remove(linebreaks));
199 id->setPartReason(ui.partReason->text().remove(linebreaks));
200 id->setQuitReason(ui.quitReason->text().remove(linebreaks));
202 QSslKey(ui.keyTypeLabel->property("sslKey").toByteArray(), (QSsl::KeyAlgorithm)(ui.keyTypeLabel->property("sslKeyType").toInt())));
203 id->setSslCert(QSslCertificate(ui.certOrgLabel->property("sslCert").toByteArray()));
206 void IdentityEditWidget::on_addNick_clicked()
208 QStringList existing;
209 for (int i = 0; i < ui.nicknameList->count(); i++)
210 existing << ui.nicknameList->item(i)->text();
211 NickEditDlg dlg(QString(), existing, this);
212 if (dlg.exec() == QDialog::Accepted) {
213 ui.nicknameList->addItem(dlg.nick());
214 ui.nicknameList->setCurrentRow(ui.nicknameList->count() - 1);
216 emit widgetHasChanged();
220 void IdentityEditWidget::on_deleteNick_clicked()
222 // no confirmation, since a nickname is really nothing hard to recreate
223 if (ui.nicknameList->selectedItems().count()) {
224 delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
225 ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow() + 1, ui.nicknameList->count() - 1));
227 emit widgetHasChanged();
231 void IdentityEditWidget::on_renameNick_clicked()
233 if (!ui.nicknameList->selectedItems().count())
235 QString old = ui.nicknameList->selectedItems()[0]->text();
236 QStringList existing;
237 for (int i = 0; i < ui.nicknameList->count(); i++)
238 existing << ui.nicknameList->item(i)->text();
239 NickEditDlg dlg(old, existing, this);
240 if (dlg.exec() == QDialog::Accepted) {
241 ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
245 void IdentityEditWidget::on_nickUp_clicked()
247 if (!ui.nicknameList->selectedItems().count())
249 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
251 ui.nicknameList->insertItem(row - 1, ui.nicknameList->takeItem(row));
252 ui.nicknameList->setCurrentRow(row - 1);
254 emit widgetHasChanged();
258 void IdentityEditWidget::on_nickDown_clicked()
260 if (!ui.nicknameList->selectedItems().count())
262 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
263 if (row < ui.nicknameList->count() - 1) {
264 ui.nicknameList->insertItem(row + 1, ui.nicknameList->takeItem(row));
265 ui.nicknameList->setCurrentRow(row + 1);
267 emit widgetHasChanged();
271 void IdentityEditWidget::showAdvanced(bool advanced)
273 int idx = ui.tabWidget->indexOf(ui.advancedTab);
276 return; // already added
277 ui.tabWidget->addTab(ui.advancedTab, tr("Advanced"));
281 return; // already removed
282 ui.tabWidget->removeTab(idx);
286 void IdentityEditWidget::setSslState(SslState state)
290 ui.keyAndCertSettings->setCurrentIndex(0);
293 ui.keyAndCertSettings->setCurrentIndex(1);
296 ui.keyAndCertSettings->setCurrentIndex(2);
301 bool IdentityEditWidget::eventFilter(QObject* watched, QEvent* event)
303 bool isCert = (watched == ui.sslCertGroupBox);
304 switch (event->type()) {
305 case QEvent::DragEnter:
306 sslDragEnterEvent(static_cast<QDragEnterEvent*>(event));
309 sslDropEvent(static_cast<QDropEvent*>(event), isCert);
316 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent* event)
318 if (event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
319 event->setDropAction(Qt::CopyAction);
324 void IdentityEditWidget::sslDropEvent(QDropEvent* event, bool isCert)
327 if (event->mimeData()->hasFormat("text/uri-list"))
328 rawUris = event->mimeData()->data("text/uri-list");
330 rawUris = event->mimeData()->data("text/uri");
332 QTextStream uriStream(rawUris);
333 QString filename = QUrl(uriStream.readLine()).toLocalFile();
336 QSslCertificate cert = certByFilename(filename);
341 QSslKey key = keyByFilename(filename);
346 emit widgetHasChanged();
349 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked()
353 if (ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
355 QFileDialog::getOpenFileName(this, tr("Load a Key"), QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
358 emit widgetHasChanged();
361 QSslKey IdentityEditWidget::keyByFilename(const QString& filename)
365 QFile keyFile(filename);
366 keyFile.open(QIODevice::ReadOnly);
367 QByteArray keyRaw = keyFile.read(2 << 20);
370 for (int i = 0; i < 2; i++) {
371 // On Qt5.5+, support QSsl::KeyAlgorithm::Rsa (1), QSsl::KeyAlgorithm::Dsa (2), and QSsl::KeyAlgorithm::Ec (3)
372 for (int j = 1; j < 4; j++) {
373 key = QSslKey(keyRaw, (QSsl::KeyAlgorithm)j, (QSsl::EncodingFormat)i);
378 QMessageBox::information(
380 tr("Failed to read key"),
381 tr("Failed to read the key file. It is either incompatible or invalid. Note that the key file must not have a passphrase."));
383 if (!key.isNull() && key.algorithm() == QSsl::KeyAlgorithm::Ec && !Client::isCoreFeatureEnabled(Quassel::Feature::EcdsaCertfpKeys)) {
386 tr("Core does not support ECDSA keys"),
387 tr("You loaded an ECDSA key, but the core does not support ECDSA keys. Please contact the core administrator."));
393 void IdentityEditWidget::showKeyState(const QSslKey& key)
396 ui.keyTypeLabel->setText(tr("No Key loaded"));
397 ui.clearOrLoadKeyButton->setText(tr("Load"));
400 switch (key.algorithm()) {
402 ui.keyTypeLabel->setText(tr("RSA"));
405 ui.keyTypeLabel->setText(tr("ECDSA"));
408 ui.keyTypeLabel->setText(tr("DSA"));
411 ui.keyTypeLabel->setText(tr("Invalid key or no key loaded"));
413 ui.clearOrLoadKeyButton->setText(tr("Clear"));
415 ui.keyTypeLabel->setProperty("sslKey", key.toPem());
416 ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
419 void IdentityEditWidget::on_clearOrLoadCertButton_clicked()
421 QSslCertificate cert;
423 if (ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
424 cert = certByFilename(
425 QFileDialog::getOpenFileName(this, tr("Load a Certificate"), QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
427 emit widgetHasChanged();
430 QSslCertificate IdentityEditWidget::certByFilename(const QString& filename)
432 QSslCertificate cert;
433 QFile certFile(filename);
434 certFile.open(QIODevice::ReadOnly);
435 QByteArray certRaw = certFile.read(2 << 20);
438 for (int i = 0; i < 2; i++) {
439 cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
446 void IdentityEditWidget::showCertState(const QSslCertificate& cert)
449 ui.certOrgLabel->setText(tr("No Certificate loaded"));
450 ui.certCNameLabel->setText(tr("No Certificate loaded"));
451 ui.clearOrLoadCertButton->setText(tr("Load"));
454 ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization).join(", "));
455 ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName).join(", "));
456 ui.clearOrLoadCertButton->setText(tr("Clear"));
458 ui.certOrgLabel->setProperty("sslCert", cert.toPem());