1 /***************************************************************************
2 * Copyright (C) 2005-2016 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>
29 #include <QMessageBox>
31 #if QT_VERSION < 0x050000
32 # include <QDesktopServices>
34 # include <QStandardPaths>
39 IdentityEditWidget::IdentityEditWidget(QWidget *parent)
44 ui.addNick->setIcon(QIcon::fromTheme("list-add"));
45 ui.deleteNick->setIcon(QIcon::fromTheme("edit-delete"));
46 ui.renameNick->setIcon(QIcon::fromTheme("edit-rename"));
47 ui.nickUp->setIcon(QIcon::fromTheme("go-up"));
48 ui.nickDown->setIcon(QIcon::fromTheme("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);
91 void IdentityEditWidget::setWidgetStates()
93 if (ui.nicknameList->selectedItems().count()) {
94 ui.renameNick->setEnabled(true);
95 ui.nickUp->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) > 0);
96 ui.nickDown->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) < ui.nicknameList->count()-1);
99 ui.renameNick->setDisabled(true);
100 ui.nickUp->setDisabled(true);
101 ui.nickDown->setDisabled(true);
103 ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
107 void IdentityEditWidget::displayIdentity(CertIdentity *id, CertIdentity *saveId)
110 saveToIdentity(saveId);
116 ui.realName->setText(id->realName());
117 ui.nicknameList->clear();
118 ui.nicknameList->addItems(id->nicks());
119 //for(int i = 0; i < ui.nicknameList->count(); i++) {
120 // ui.nicknameList->item(i)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
122 if (ui.nicknameList->count()) ui.nicknameList->setCurrentRow(0);
123 ui.awayNick->setText(id->awayNick());
124 ui.awayReason->setText(id->awayReason());
125 ui.autoAwayEnabled->setChecked(id->autoAwayEnabled());
126 ui.autoAwayTime->setValue(id->autoAwayTime());
127 ui.autoAwayReason->setText(id->autoAwayReason());
128 ui.autoAwayReasonEnabled->setChecked(id->autoAwayReasonEnabled());
129 ui.detachAwayEnabled->setChecked(id->detachAwayEnabled());
130 ui.detachAwayReason->setText(id->detachAwayReason());
131 ui.ident->setText(id->ident());
132 ui.kickReason->setText(id->kickReason());
133 ui.partReason->setText(id->partReason());
134 ui.quitReason->setText(id->quitReason());
136 showKeyState(id->sslKey());
137 showCertState(id->sslCert());
142 void IdentityEditWidget::saveToIdentity(CertIdentity *id)
144 QRegExp linebreaks = QRegExp("[\\r\\n]");
145 id->setRealName(ui.realName->text());
147 for (int i = 0; i < ui.nicknameList->count(); i++) {
148 nicks << ui.nicknameList->item(i)->text();
151 id->setAwayNick(ui.awayNick->text());
152 id->setAwayNickEnabled(true);
153 id->setAwayReason(ui.awayReason->text().remove(linebreaks));
154 id->setAwayReasonEnabled(true);
155 id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
156 id->setAutoAwayTime(ui.autoAwayTime->value());
157 id->setAutoAwayReason(ui.autoAwayReason->text().remove(linebreaks));
158 id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
159 id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
160 id->setDetachAwayReason(ui.detachAwayReason->text().remove(linebreaks));
161 id->setDetachAwayReasonEnabled(true);
162 id->setIdent(ui.ident->text());
163 id->setKickReason(ui.kickReason->text().remove(linebreaks));
164 id->setPartReason(ui.partReason->text().remove(linebreaks));
165 id->setQuitReason(ui.quitReason->text().remove(linebreaks));
167 id->setSslKey(QSslKey(ui.keyTypeLabel->property("sslKey").toByteArray(), (QSsl::KeyAlgorithm)(ui.keyTypeLabel->property("sslKeyType").toInt())));
168 id->setSslCert(QSslCertificate(ui.certOrgLabel->property("sslCert").toByteArray()));
173 void IdentityEditWidget::on_addNick_clicked()
175 QStringList existing;
176 for (int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
177 NickEditDlg dlg(QString(), existing, this);
178 if (dlg.exec() == QDialog::Accepted) {
179 ui.nicknameList->addItem(dlg.nick());
180 ui.nicknameList->setCurrentRow(ui.nicknameList->count()-1);
182 emit widgetHasChanged();
187 void IdentityEditWidget::on_deleteNick_clicked()
189 // no confirmation, since a nickname is really nothing hard to recreate
190 if (ui.nicknameList->selectedItems().count()) {
191 delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
192 ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow()+1, ui.nicknameList->count()-1));
194 emit widgetHasChanged();
199 void IdentityEditWidget::on_renameNick_clicked()
201 if (!ui.nicknameList->selectedItems().count()) return;
202 QString old = ui.nicknameList->selectedItems()[0]->text();
203 QStringList existing;
204 for (int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
205 NickEditDlg dlg(old, existing, this);
206 if (dlg.exec() == QDialog::Accepted) {
207 ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
212 void IdentityEditWidget::on_nickUp_clicked()
214 if (!ui.nicknameList->selectedItems().count()) return;
215 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
217 ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
218 ui.nicknameList->setCurrentRow(row-1);
220 emit widgetHasChanged();
225 void IdentityEditWidget::on_nickDown_clicked()
227 if (!ui.nicknameList->selectedItems().count()) return;
228 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
229 if (row < ui.nicknameList->count()-1) {
230 ui.nicknameList->insertItem(row+1, ui.nicknameList->takeItem(row));
231 ui.nicknameList->setCurrentRow(row+1);
233 emit widgetHasChanged();
238 void IdentityEditWidget::showAdvanced(bool advanced)
240 int idx = ui.tabWidget->indexOf(ui.advancedTab);
243 return; // already added
244 ui.tabWidget->addTab(ui.advancedTab, tr("Advanced"));
248 return; // already removed
249 ui.tabWidget->removeTab(idx);
254 void IdentityEditWidget::setSslState(SslState state)
258 ui.keyAndCertSettings->setCurrentIndex(0);
261 ui.keyAndCertSettings->setCurrentIndex(1);
264 ui.keyAndCertSettings->setCurrentIndex(2);
271 bool IdentityEditWidget::eventFilter(QObject *watched, QEvent *event)
273 bool isCert = (watched == ui.sslCertGroupBox);
274 switch (event->type()) {
275 case QEvent::DragEnter:
276 sslDragEnterEvent(static_cast<QDragEnterEvent *>(event));
279 sslDropEvent(static_cast<QDropEvent *>(event), isCert);
287 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent *event)
289 if (event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
290 event->setDropAction(Qt::CopyAction);
296 void IdentityEditWidget::sslDropEvent(QDropEvent *event, bool isCert)
299 if (event->mimeData()->hasFormat("text/uri-list"))
300 rawUris = event->mimeData()->data("text/uri-list");
302 rawUris = event->mimeData()->data("text/uri");
304 QTextStream uriStream(rawUris);
305 QString filename = QUrl(uriStream.readLine()).toLocalFile();
308 QSslCertificate cert = certByFilename(filename);
313 QSslKey key = keyByFilename(filename);
318 emit widgetHasChanged();
322 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked()
326 if (ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
327 key = keyByFilename(QFileDialog::getOpenFileName(this, tr("Load a Key"),
328 #if QT_VERSION < 0x050000
329 QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
331 QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
335 emit widgetHasChanged();
339 QSslKey IdentityEditWidget::keyByFilename(const QString &filename)
343 QFile keyFile(filename);
344 keyFile.open(QIODevice::ReadOnly);
345 QByteArray keyRaw = keyFile.read(2 << 20);
348 for (int i = 0; i < 2; i++) {
349 for (int j = 0; j < 2; j++) {
350 key = QSslKey(keyRaw, (QSsl::KeyAlgorithm)j, (QSsl::EncodingFormat)i);
355 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."));
361 void IdentityEditWidget::showKeyState(const QSslKey &key)
364 ui.keyTypeLabel->setText(tr("No Key loaded"));
365 ui.clearOrLoadKeyButton->setText(tr("Load"));
368 switch (key.algorithm()) {
370 ui.keyTypeLabel->setText(tr("RSA"));
373 ui.keyTypeLabel->setText(tr("DSA"));
376 ui.keyTypeLabel->setText(tr("No Key loaded"));
378 ui.clearOrLoadKeyButton->setText(tr("Clear"));
380 ui.keyTypeLabel->setProperty("sslKey", key.toPem());
381 ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
385 void IdentityEditWidget::on_clearOrLoadCertButton_clicked()
387 QSslCertificate cert;
389 if (ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
390 cert = certByFilename(QFileDialog::getOpenFileName(this, tr("Load a Certificate"),
391 #if QT_VERSION < 0x050000
392 QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
394 QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
397 emit widgetHasChanged();
401 QSslCertificate IdentityEditWidget::certByFilename(const QString &filename)
403 QSslCertificate cert;
404 QFile certFile(filename);
405 certFile.open(QIODevice::ReadOnly);
406 QByteArray certRaw = certFile.read(2 << 20);
409 for (int i = 0; i < 2; i++) {
410 cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
418 void IdentityEditWidget::showCertState(const QSslCertificate &cert)
421 ui.certOrgLabel->setText(tr("No Certificate loaded"));
422 ui.certCNameLabel->setText(tr("No Certificate loaded"));
423 ui.clearOrLoadCertButton->setText(tr("Load"));
426 #if QT_VERSION < 0x050000
427 ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization));
428 ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName));
430 ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization).join(", "));
431 ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName).join(", "));
433 ui.clearOrLoadCertButton->setText(tr("Clear"));
435 ui.certOrgLabel->setProperty("sslCert", cert.toPem());