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>
29 #include <QStandardPaths>
34 IdentityEditWidget::IdentityEditWidget(QWidget *parent)
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"));
45 // We need to know whenever the state of input widgets changes...
46 connect(ui.realName, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
47 connect(ui.nicknameList, SIGNAL(itemChanged(QListWidgetItem *)), this, SIGNAL(widgetHasChanged()));
48 connect(ui.awayNick, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
49 connect(ui.awayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
50 connect(ui.autoAwayEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
51 connect(ui.autoAwayTime, SIGNAL(valueChanged(int)), this, SIGNAL(widgetHasChanged()));
52 connect(ui.autoAwayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
53 connect(ui.autoAwayReasonEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
54 connect(ui.detachAwayEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
55 connect(ui.detachAwayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
56 connect(ui.ident, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
57 connect(ui.kickReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
58 connect(ui.partReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
59 connect(ui.quitReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
62 connect(ui.nicknameList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
64 connect(ui.continueUnsecured, SIGNAL(clicked()), this, SIGNAL(requestEditSsl()));
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()));
70 // disabling unused stuff
71 ui.autoAwayEnabled->hide();
73 ui.awayNickLabel->hide();
75 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);
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>";
91 // Function to add a row to the tooltip table
92 auto addRow = [&](const QString& key, const QString& value, bool condition) {
94 formatTooltip << "<tr><td class='bold' align='right'>"
95 << 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>" << tr("You can add date/time to this message "
104 "<br/>%%<span class='italic'><format></span>%%, where "
105 "<span class='italic'><format></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>";
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
128 void IdentityEditWidget::setWidgetStates()
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);
136 ui.renameNick->setDisabled(true);
137 ui.nickUp->setDisabled(true);
138 ui.nickDown->setDisabled(true);
140 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()) 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());
173 showKeyState(id->sslKey());
174 showCertState(id->sslCert());
179 void IdentityEditWidget::saveToIdentity(CertIdentity *id)
181 QRegExp linebreaks = QRegExp("[\\r\\n]");
182 id->setRealName(ui.realName->text());
184 for (int i = 0; i < ui.nicknameList->count(); i++) {
185 nicks << ui.nicknameList->item(i)->text();
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));
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()));
210 void IdentityEditWidget::on_addNick_clicked()
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);
219 emit widgetHasChanged();
224 void IdentityEditWidget::on_deleteNick_clicked()
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));
231 emit widgetHasChanged();
236 void IdentityEditWidget::on_renameNick_clicked()
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());
249 void IdentityEditWidget::on_nickUp_clicked()
251 if (!ui.nicknameList->selectedItems().count()) return;
252 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
254 ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
255 ui.nicknameList->setCurrentRow(row-1);
257 emit widgetHasChanged();
262 void IdentityEditWidget::on_nickDown_clicked()
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);
270 emit widgetHasChanged();
275 void IdentityEditWidget::showAdvanced(bool advanced)
277 int idx = ui.tabWidget->indexOf(ui.advancedTab);
280 return; // already added
281 ui.tabWidget->addTab(ui.advancedTab, tr("Advanced"));
285 return; // already removed
286 ui.tabWidget->removeTab(idx);
291 void IdentityEditWidget::setSslState(SslState state)
295 ui.keyAndCertSettings->setCurrentIndex(0);
298 ui.keyAndCertSettings->setCurrentIndex(1);
301 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);
324 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent *event)
326 if (event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
327 event->setDropAction(Qt::CopyAction);
333 void IdentityEditWidget::sslDropEvent(QDropEvent *event, bool isCert)
336 if (event->mimeData()->hasFormat("text/uri-list"))
337 rawUris = event->mimeData()->data("text/uri-list");
339 rawUris = event->mimeData()->data("text/uri");
341 QTextStream uriStream(rawUris);
342 QString filename = QUrl(uriStream.readLine()).toLocalFile();
345 QSslCertificate cert = certByFilename(filename);
350 QSslKey key = keyByFilename(filename);
355 emit widgetHasChanged();
359 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked()
363 if (ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
364 key = keyByFilename(QFileDialog::getOpenFileName(this, tr("Load a Key"), QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
367 emit widgetHasChanged();
371 QSslKey IdentityEditWidget::keyByFilename(const QString &filename)
375 QFile keyFile(filename);
376 keyFile.open(QIODevice::ReadOnly);
377 QByteArray keyRaw = keyFile.read(2 << 20);
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);
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."));
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."));
398 void IdentityEditWidget::showKeyState(const QSslKey &key)
401 ui.keyTypeLabel->setText(tr("No Key loaded"));
402 ui.clearOrLoadKeyButton->setText(tr("Load"));
405 switch (key.algorithm()) {
407 ui.keyTypeLabel->setText(tr("RSA"));
410 ui.keyTypeLabel->setText(tr("ECDSA"));
413 ui.keyTypeLabel->setText(tr("DSA"));
416 ui.keyTypeLabel->setText(tr("Invalid key or no key loaded"));
418 ui.clearOrLoadKeyButton->setText(tr("Clear"));
420 ui.keyTypeLabel->setProperty("sslKey", key.toPem());
421 ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
425 void IdentityEditWidget::on_clearOrLoadCertButton_clicked()
427 QSslCertificate cert;
429 if (ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
430 cert = certByFilename(QFileDialog::getOpenFileName(this, tr("Load a Certificate"), QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
432 emit widgetHasChanged();
436 QSslCertificate IdentityEditWidget::certByFilename(const QString &filename)
438 QSslCertificate cert;
439 QFile certFile(filename);
440 certFile.open(QIODevice::ReadOnly);
441 QByteArray certRaw = certFile.read(2 << 20);
444 for (int i = 0; i < 2; i++) {
445 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());