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>
28 #include <QMessageBox>
30 #if QT_VERSION < 0x050000
31 # include <QDesktopServices>
33 # include <QStandardPaths>
39 IdentityEditWidget::IdentityEditWidget(QWidget *parent)
44 ui.addNick->setIcon(icon::get("list-add"));
45 ui.deleteNick->setIcon(icon::get("edit-delete"));
46 ui.renameNick->setIcon(icon::get("edit-rename"));
47 ui.nickUp->setIcon(icon::get("go-up"));
48 ui.nickDown->setIcon(icon::get("go-down"));
50 // We need to know whenever the state of input widgets changes...
51 connect(ui.realName, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
52 connect(ui.nicknameList, SIGNAL(itemChanged(QListWidgetItem *)), this, SIGNAL(widgetHasChanged()));
53 connect(ui.awayNick, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
54 connect(ui.awayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
55 connect(ui.autoAwayEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
56 connect(ui.autoAwayTime, SIGNAL(valueChanged(int)), this, SIGNAL(widgetHasChanged()));
57 connect(ui.autoAwayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
58 connect(ui.autoAwayReasonEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
59 connect(ui.detachAwayEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
60 connect(ui.detachAwayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
61 connect(ui.ident, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
62 connect(ui.kickReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
63 connect(ui.partReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
64 connect(ui.quitReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
67 connect(ui.nicknameList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
69 connect(ui.continueUnsecured, SIGNAL(clicked()), this, SIGNAL(requestEditSsl()));
71 // we would need this if we enabled drag and drop in the nicklist...
72 //connect(ui.nicknameList, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(setWidgetStates()));
73 //connect(ui.nicknameList->model(), SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(nicklistHasChanged()));
75 // disabling unused stuff
76 ui.autoAwayEnabled->hide();
78 ui.awayNickLabel->hide();
80 ui.detachAwayEnabled->setVisible(!Client::internalCore());
83 ui.sslKeyGroupBox->setAcceptDrops(true);
84 ui.sslKeyGroupBox->installEventFilter(this);
85 ui.sslCertGroupBox->setAcceptDrops(true);
86 ui.sslCertGroupBox->installEventFilter(this);
89 if (Client::isCoreFeatureEnabled(Quassel::Feature::AwayFormatTimestamp)) {
90 // Core allows formatting %%timestamp%% messages in away strings. Update tooltips.
91 QString strFormatTooltip;
92 QTextStream formatTooltip( &strFormatTooltip, QIODevice::WriteOnly );
93 formatTooltip << "<qt><style>.bold { font-weight: bold; } "
94 ".italic { font-style: italic; }</style>";
96 // Function to add a row to the tooltip table
97 auto addRow = [&](const QString& key, const QString& value, bool condition) {
99 formatTooltip << "<tr><td class='bold' align='right'>"
100 << key << "</td><td>" << value << "</td></tr>";
104 // Original tooltip goes here
105 formatTooltip << "<p>%1</p>";
106 // New timestamp formatting guide here
107 formatTooltip << "<p>" << tr("You can add date/time to this message "
109 "<br/>%%<span class='italic'><format></span>%%, where "
110 "<span class='italic'><format></span> is:") << "</p>";
111 formatTooltip << "<table cellspacing='5' cellpadding='0'>";
112 addRow("hh", tr("the hour"), true);
113 addRow("mm", tr("the minutes"), true);
114 addRow("ss", tr("seconds"), true);
115 addRow("AP", tr("AM/PM"), true);
116 addRow("dd", tr("day"), true);
117 addRow("MM", tr("month"), true);
118 #if QT_VERSION > 0x050000
119 // Alas, this was only added in Qt 5. We don't know what version the core has, just hope
120 // for the best (Qt 4 will soon be dropped).
121 addRow("t", tr("current timezone"), true);
123 formatTooltip << "</table>";
124 formatTooltip << "<p>" << tr("Example: Away since %%hh:mm%% on %%dd.MM%%.") << "</p>";
125 formatTooltip << "<p>" << tr("%%%% without anything inside represents %%. Other format "
126 "codes are available.") << "</p>";
127 formatTooltip << "</qt>";
129 // Split up the message to allow re-using translations:
130 // [Original tool-tip] [Timestamp format message]
131 ui.awayReason->setToolTip(strFormatTooltip.arg(ui.awayReason->toolTip()));
132 ui.detachAwayEnabled->setToolTip(strFormatTooltip.arg(ui.detachAwayEnabled->toolTip()));
133 } // else: Do nothing, leave the original translated string
137 void IdentityEditWidget::setWidgetStates()
139 if (ui.nicknameList->selectedItems().count()) {
140 ui.renameNick->setEnabled(true);
141 ui.nickUp->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) > 0);
142 ui.nickDown->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) < ui.nicknameList->count()-1);
145 ui.renameNick->setDisabled(true);
146 ui.nickUp->setDisabled(true);
147 ui.nickDown->setDisabled(true);
149 ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
153 void IdentityEditWidget::displayIdentity(CertIdentity *id, CertIdentity *saveId)
156 saveToIdentity(saveId);
162 ui.realName->setText(id->realName());
163 ui.nicknameList->clear();
164 ui.nicknameList->addItems(id->nicks());
165 //for(int i = 0; i < ui.nicknameList->count(); i++) {
166 // ui.nicknameList->item(i)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
168 if (ui.nicknameList->count()) ui.nicknameList->setCurrentRow(0);
169 ui.awayNick->setText(id->awayNick());
170 ui.awayReason->setText(id->awayReason());
171 ui.autoAwayEnabled->setChecked(id->autoAwayEnabled());
172 ui.autoAwayTime->setValue(id->autoAwayTime());
173 ui.autoAwayReason->setText(id->autoAwayReason());
174 ui.autoAwayReasonEnabled->setChecked(id->autoAwayReasonEnabled());
175 ui.detachAwayEnabled->setChecked(id->detachAwayEnabled());
176 ui.detachAwayReason->setText(id->detachAwayReason());
177 ui.ident->setText(id->ident());
178 ui.kickReason->setText(id->kickReason());
179 ui.partReason->setText(id->partReason());
180 ui.quitReason->setText(id->quitReason());
182 showKeyState(id->sslKey());
183 showCertState(id->sslCert());
188 void IdentityEditWidget::saveToIdentity(CertIdentity *id)
190 QRegExp linebreaks = QRegExp("[\\r\\n]");
191 id->setRealName(ui.realName->text());
193 for (int i = 0; i < ui.nicknameList->count(); i++) {
194 nicks << ui.nicknameList->item(i)->text();
197 id->setAwayNick(ui.awayNick->text());
198 id->setAwayNickEnabled(true);
199 id->setAwayReason(ui.awayReason->text().remove(linebreaks));
200 id->setAwayReasonEnabled(true);
201 id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
202 id->setAutoAwayTime(ui.autoAwayTime->value());
203 id->setAutoAwayReason(ui.autoAwayReason->text().remove(linebreaks));
204 id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
205 id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
206 id->setDetachAwayReason(ui.detachAwayReason->text().remove(linebreaks));
207 id->setDetachAwayReasonEnabled(true);
208 id->setIdent(ui.ident->text());
209 id->setKickReason(ui.kickReason->text().remove(linebreaks));
210 id->setPartReason(ui.partReason->text().remove(linebreaks));
211 id->setQuitReason(ui.quitReason->text().remove(linebreaks));
213 id->setSslKey(QSslKey(ui.keyTypeLabel->property("sslKey").toByteArray(), (QSsl::KeyAlgorithm)(ui.keyTypeLabel->property("sslKeyType").toInt())));
214 id->setSslCert(QSslCertificate(ui.certOrgLabel->property("sslCert").toByteArray()));
219 void IdentityEditWidget::on_addNick_clicked()
221 QStringList existing;
222 for (int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
223 NickEditDlg dlg(QString(), existing, this);
224 if (dlg.exec() == QDialog::Accepted) {
225 ui.nicknameList->addItem(dlg.nick());
226 ui.nicknameList->setCurrentRow(ui.nicknameList->count()-1);
228 emit widgetHasChanged();
233 void IdentityEditWidget::on_deleteNick_clicked()
235 // no confirmation, since a nickname is really nothing hard to recreate
236 if (ui.nicknameList->selectedItems().count()) {
237 delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
238 ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow()+1, ui.nicknameList->count()-1));
240 emit widgetHasChanged();
245 void IdentityEditWidget::on_renameNick_clicked()
247 if (!ui.nicknameList->selectedItems().count()) return;
248 QString old = ui.nicknameList->selectedItems()[0]->text();
249 QStringList existing;
250 for (int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
251 NickEditDlg dlg(old, existing, this);
252 if (dlg.exec() == QDialog::Accepted) {
253 ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
258 void IdentityEditWidget::on_nickUp_clicked()
260 if (!ui.nicknameList->selectedItems().count()) return;
261 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
263 ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
264 ui.nicknameList->setCurrentRow(row-1);
266 emit widgetHasChanged();
271 void IdentityEditWidget::on_nickDown_clicked()
273 if (!ui.nicknameList->selectedItems().count()) return;
274 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
275 if (row < ui.nicknameList->count()-1) {
276 ui.nicknameList->insertItem(row+1, ui.nicknameList->takeItem(row));
277 ui.nicknameList->setCurrentRow(row+1);
279 emit widgetHasChanged();
284 void IdentityEditWidget::showAdvanced(bool advanced)
286 int idx = ui.tabWidget->indexOf(ui.advancedTab);
289 return; // already added
290 ui.tabWidget->addTab(ui.advancedTab, tr("Advanced"));
294 return; // already removed
295 ui.tabWidget->removeTab(idx);
300 void IdentityEditWidget::setSslState(SslState state)
304 ui.keyAndCertSettings->setCurrentIndex(0);
307 ui.keyAndCertSettings->setCurrentIndex(1);
310 ui.keyAndCertSettings->setCurrentIndex(2);
317 bool IdentityEditWidget::eventFilter(QObject *watched, QEvent *event)
319 bool isCert = (watched == ui.sslCertGroupBox);
320 switch (event->type()) {
321 case QEvent::DragEnter:
322 sslDragEnterEvent(static_cast<QDragEnterEvent *>(event));
325 sslDropEvent(static_cast<QDropEvent *>(event), isCert);
333 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent *event)
335 if (event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
336 event->setDropAction(Qt::CopyAction);
342 void IdentityEditWidget::sslDropEvent(QDropEvent *event, bool isCert)
345 if (event->mimeData()->hasFormat("text/uri-list"))
346 rawUris = event->mimeData()->data("text/uri-list");
348 rawUris = event->mimeData()->data("text/uri");
350 QTextStream uriStream(rawUris);
351 QString filename = QUrl(uriStream.readLine()).toLocalFile();
354 QSslCertificate cert = certByFilename(filename);
359 QSslKey key = keyByFilename(filename);
364 emit widgetHasChanged();
368 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked()
372 if (ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
373 key = keyByFilename(QFileDialog::getOpenFileName(this, tr("Load a Key"),
374 #if QT_VERSION < 0x050000
375 QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
377 QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
381 emit widgetHasChanged();
385 QSslKey IdentityEditWidget::keyByFilename(const QString &filename)
389 QFile keyFile(filename);
390 keyFile.open(QIODevice::ReadOnly);
391 QByteArray keyRaw = keyFile.read(2 << 20);
394 for (int i = 0; i < 2; i++) {
395 #if QT_VERSION >= 0x050500
396 // On Qt5.5+, support QSsl::KeyAlgorithm::Rsa (1), QSsl::KeyAlgorithm::Dsa (2), and QSsl::KeyAlgorithm::Ec (3)
397 for (int j = 1; j < 4; j++) {
398 #elif QT_VERSION >= 0x050000
399 // On Qt5.0-Qt5.4, support QSsl::KeyAlgorithm::Rsa (1) and QSsl::KeyAlgorithm::Dsa (2) (Ec wasn't added until 5.5)
400 for (int j = 1; j < 3; j++) {
402 // On Qt4, support QSsl::KeyAlgorithm::Rsa (0) and QSsl::KeyAlgorithm::Dsa (1) (Qt4 uses different indices for the values)
403 for (int j = 0; j < 2; j++) {
405 key = QSslKey(keyRaw, (QSsl::KeyAlgorithm)j, (QSsl::EncodingFormat)i);
410 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."));
412 #if QT_VERSION >= 0x050500
413 if(!key.isNull() && key.algorithm() == QSsl::KeyAlgorithm::Ec && !Client::isCoreFeatureEnabled(Quassel::Feature::EcdsaCertfpKeys)) {
414 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."));
422 void IdentityEditWidget::showKeyState(const QSslKey &key)
425 ui.keyTypeLabel->setText(tr("No Key loaded"));
426 ui.clearOrLoadKeyButton->setText(tr("Load"));
429 switch (key.algorithm()) {
431 ui.keyTypeLabel->setText(tr("RSA"));
433 #if QT_VERSION >= 0x050500
435 ui.keyTypeLabel->setText(tr("ECDSA"));
439 ui.keyTypeLabel->setText(tr("DSA"));
442 ui.keyTypeLabel->setText(tr("Invalid key or no key loaded"));
444 ui.clearOrLoadKeyButton->setText(tr("Clear"));
446 ui.keyTypeLabel->setProperty("sslKey", key.toPem());
447 ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
451 void IdentityEditWidget::on_clearOrLoadCertButton_clicked()
453 QSslCertificate cert;
455 if (ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
456 cert = certByFilename(QFileDialog::getOpenFileName(this, tr("Load a Certificate"),
457 #if QT_VERSION < 0x050000
458 QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
460 QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
463 emit widgetHasChanged();
467 QSslCertificate IdentityEditWidget::certByFilename(const QString &filename)
469 QSslCertificate cert;
470 QFile certFile(filename);
471 certFile.open(QIODevice::ReadOnly);
472 QByteArray certRaw = certFile.read(2 << 20);
475 for (int i = 0; i < 2; i++) {
476 cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
484 void IdentityEditWidget::showCertState(const QSslCertificate &cert)
487 ui.certOrgLabel->setText(tr("No Certificate loaded"));
488 ui.certCNameLabel->setText(tr("No Certificate loaded"));
489 ui.clearOrLoadCertButton->setText(tr("Load"));
492 #if QT_VERSION < 0x050000
493 ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization));
494 ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName));
496 ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization).join(", "));
497 ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName).join(", "));
499 ui.clearOrLoadCertButton->setText(tr("Clear"));
501 ui.certOrgLabel->setProperty("sslCert", cert.toPem());