1 /***************************************************************************
2 * Copyright (C) 2005-2018 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());
79 ui.sslKeyGroupBox->setAcceptDrops(true);
80 ui.sslKeyGroupBox->installEventFilter(this);
81 ui.sslCertGroupBox->setAcceptDrops(true);
82 ui.sslCertGroupBox->installEventFilter(this);
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>";
92 // Function to add a row to the tooltip table
93 auto addRow = [&](const QString& key, const QString& value, bool condition) {
95 formatTooltip << "<tr><td class='bold' align='right'>" << key << "</td><td>" << value << "</td></tr>";
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 "
105 "<br/>%%<span class='italic'><format></span>%%, where "
106 "<span class='italic'><format></span> is:")
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.")
122 formatTooltip << "</qt>";
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
131 void IdentityEditWidget::setWidgetStates()
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);
139 ui.renameNick->setDisabled(true);
140 ui.nickUp->setDisabled(true);
141 ui.nickDown->setDisabled(true);
143 ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
146 void IdentityEditWidget::displayIdentity(CertIdentity* id, CertIdentity* saveId)
149 saveToIdentity(saveId);
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);
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());
176 showKeyState(id->sslKey());
177 showCertState(id->sslCert());
181 void IdentityEditWidget::saveToIdentity(CertIdentity* id)
183 QRegExp linebreaks = QRegExp("[\\r\\n]");
184 id->setRealName(ui.realName->text());
186 for (int i = 0; i < ui.nicknameList->count(); i++) {
187 nicks << ui.nicknameList->item(i)->text();
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));
207 QSslKey(ui.keyTypeLabel->property("sslKey").toByteArray(), (QSsl::KeyAlgorithm)(ui.keyTypeLabel->property("sslKeyType").toInt())));
208 id->setSslCert(QSslCertificate(ui.certOrgLabel->property("sslCert").toByteArray()));
212 void IdentityEditWidget::on_addNick_clicked()
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);
222 emit widgetHasChanged();
226 void IdentityEditWidget::on_deleteNick_clicked()
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));
233 emit widgetHasChanged();
237 void IdentityEditWidget::on_renameNick_clicked()
239 if (!ui.nicknameList->selectedItems().count())
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());
251 void IdentityEditWidget::on_nickUp_clicked()
253 if (!ui.nicknameList->selectedItems().count())
255 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
257 ui.nicknameList->insertItem(row - 1, ui.nicknameList->takeItem(row));
258 ui.nicknameList->setCurrentRow(row - 1);
260 emit widgetHasChanged();
264 void IdentityEditWidget::on_nickDown_clicked()
266 if (!ui.nicknameList->selectedItems().count())
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);
273 emit widgetHasChanged();
277 void IdentityEditWidget::showAdvanced(bool advanced)
279 int idx = ui.tabWidget->indexOf(ui.advancedTab);
282 return; // already added
283 ui.tabWidget->addTab(ui.advancedTab, tr("Advanced"));
287 return; // already removed
288 ui.tabWidget->removeTab(idx);
292 void IdentityEditWidget::setSslState(SslState state)
296 ui.keyAndCertSettings->setCurrentIndex(0);
299 ui.keyAndCertSettings->setCurrentIndex(1);
302 ui.keyAndCertSettings->setCurrentIndex(2);
308 bool IdentityEditWidget::eventFilter(QObject* watched, QEvent* event)
310 bool isCert = (watched == ui.sslCertGroupBox);
311 switch (event->type()) {
312 case QEvent::DragEnter:
313 sslDragEnterEvent(static_cast<QDragEnterEvent*>(event));
316 sslDropEvent(static_cast<QDropEvent*>(event), isCert);
323 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent* event)
325 if (event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
326 event->setDropAction(Qt::CopyAction);
331 void IdentityEditWidget::sslDropEvent(QDropEvent* event, bool isCert)
334 if (event->mimeData()->hasFormat("text/uri-list"))
335 rawUris = event->mimeData()->data("text/uri-list");
337 rawUris = event->mimeData()->data("text/uri");
339 QTextStream uriStream(rawUris);
340 QString filename = QUrl(uriStream.readLine()).toLocalFile();
343 QSslCertificate cert = certByFilename(filename);
348 QSslKey key = keyByFilename(filename);
353 emit widgetHasChanged();
356 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked()
360 if (ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
362 QFileDialog::getOpenFileName(this, tr("Load a Key"), QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
365 emit widgetHasChanged();
368 QSslKey IdentityEditWidget::keyByFilename(const QString& filename)
372 QFile keyFile(filename);
373 keyFile.open(QIODevice::ReadOnly);
374 QByteArray keyRaw = keyFile.read(2 << 20);
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);
385 QMessageBox::information(
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."));
390 if (!key.isNull() && key.algorithm() == QSsl::KeyAlgorithm::Ec && !Client::isCoreFeatureEnabled(Quassel::Feature::EcdsaCertfpKeys)) {
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."));
400 void IdentityEditWidget::showKeyState(const QSslKey& key)
403 ui.keyTypeLabel->setText(tr("No Key loaded"));
404 ui.clearOrLoadKeyButton->setText(tr("Load"));
407 switch (key.algorithm()) {
409 ui.keyTypeLabel->setText(tr("RSA"));
412 ui.keyTypeLabel->setText(tr("ECDSA"));
415 ui.keyTypeLabel->setText(tr("DSA"));
418 ui.keyTypeLabel->setText(tr("Invalid key or no key loaded"));
420 ui.clearOrLoadKeyButton->setText(tr("Clear"));
422 ui.keyTypeLabel->setProperty("sslKey", key.toPem());
423 ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
426 void IdentityEditWidget::on_clearOrLoadCertButton_clicked()
428 QSslCertificate cert;
430 if (ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
431 cert = certByFilename(
432 QFileDialog::getOpenFileName(this, tr("Load a Certificate"), QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
434 emit widgetHasChanged();
437 QSslCertificate IdentityEditWidget::certByFilename(const QString& filename)
439 QSslCertificate cert;
440 QFile certFile(filename);
441 certFile.open(QIODevice::ReadOnly);
442 QByteArray certRaw = certFile.read(2 << 20);
445 for (int i = 0; i < 2; i++) {
446 cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
453 void IdentityEditWidget::showCertState(const QSslCertificate& cert)
456 ui.certOrgLabel->setText(tr("No Certificate loaded"));
457 ui.certCNameLabel->setText(tr("No Certificate loaded"));
458 ui.clearOrLoadCertButton->setText(tr("Load"));
461 ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization).join(", "));
462 ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName).join(", "));
463 ui.clearOrLoadCertButton->setText(tr("Clear"));
465 ui.certOrgLabel->setProperty("sslCert", cert.toPem());