1 /***************************************************************************
2 * Copyright (C) 2005-2014 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>
27 #include <QMessageBox>
29 #if QT_VERSION < 0x050000
30 # include <QDesktopServices>
32 # include <QStandardPaths>
36 #include "iconloader.h"
38 IdentityEditWidget::IdentityEditWidget(QWidget *parent)
43 ui.addNick->setIcon(SmallIcon("list-add"));
44 ui.deleteNick->setIcon(SmallIcon("edit-delete"));
45 ui.renameNick->setIcon(SmallIcon("edit-rename"));
46 ui.nickUp->setIcon(SmallIcon("go-up"));
47 ui.nickDown->setIcon(SmallIcon("go-down"));
49 // We need to know whenever the state of input widgets changes...
50 connect(ui.realName, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
51 connect(ui.nicknameList, SIGNAL(itemChanged(QListWidgetItem *)), this, SIGNAL(widgetHasChanged()));
52 connect(ui.awayNick, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
53 connect(ui.awayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
54 connect(ui.autoAwayEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
55 connect(ui.autoAwayTime, SIGNAL(valueChanged(int)), this, SIGNAL(widgetHasChanged()));
56 connect(ui.autoAwayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
57 connect(ui.autoAwayReasonEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
58 connect(ui.detachAwayEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
59 connect(ui.detachAwayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
60 connect(ui.ident, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
61 connect(ui.kickReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
62 connect(ui.partReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
63 connect(ui.quitReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
66 connect(ui.nicknameList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
68 connect(ui.continueUnsecured, SIGNAL(clicked()), this, SIGNAL(requestEditSsl()));
70 // we would need this if we enabled drag and drop in the nicklist...
71 //connect(ui.nicknameList, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(setWidgetStates()));
72 //connect(ui.nicknameList->model(), SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(nicklistHasChanged()));
74 // disabling unused stuff
75 ui.autoAwayEnabled->hide();
77 ui.awayNickLabel->hide();
79 ui.detachAwayEnabled->setVisible(!Client::internalCore());
82 ui.sslKeyGroupBox->setAcceptDrops(true);
83 ui.sslKeyGroupBox->installEventFilter(this);
84 ui.sslCertGroupBox->setAcceptDrops(true);
85 ui.sslCertGroupBox->installEventFilter(this);
90 void IdentityEditWidget::setWidgetStates()
92 if (ui.nicknameList->selectedItems().count()) {
93 ui.renameNick->setEnabled(true);
94 ui.nickUp->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) > 0);
95 ui.nickDown->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) < ui.nicknameList->count()-1);
98 ui.renameNick->setDisabled(true);
99 ui.nickUp->setDisabled(true);
100 ui.nickDown->setDisabled(true);
102 ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
106 void IdentityEditWidget::displayIdentity(CertIdentity *id, CertIdentity *saveId)
109 saveToIdentity(saveId);
115 ui.realName->setText(id->realName());
116 ui.nicknameList->clear();
117 ui.nicknameList->addItems(id->nicks());
118 //for(int i = 0; i < ui.nicknameList->count(); i++) {
119 // ui.nicknameList->item(i)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
121 if (ui.nicknameList->count()) ui.nicknameList->setCurrentRow(0);
122 ui.awayNick->setText(id->awayNick());
123 ui.awayReason->setText(id->awayReason());
124 ui.autoAwayEnabled->setChecked(id->autoAwayEnabled());
125 ui.autoAwayTime->setValue(id->autoAwayTime());
126 ui.autoAwayReason->setText(id->autoAwayReason());
127 ui.autoAwayReasonEnabled->setChecked(id->autoAwayReasonEnabled());
128 ui.detachAwayEnabled->setChecked(id->detachAwayEnabled());
129 ui.detachAwayReason->setText(id->detachAwayReason());
130 ui.ident->setText(id->ident());
131 ui.kickReason->setText(id->kickReason());
132 ui.partReason->setText(id->partReason());
133 ui.quitReason->setText(id->quitReason());
135 showKeyState(id->sslKey());
136 showCertState(id->sslCert());
141 void IdentityEditWidget::saveToIdentity(CertIdentity *id)
143 QRegExp linebreaks = QRegExp("[\\r\\n]");
144 id->setRealName(ui.realName->text());
146 for (int i = 0; i < ui.nicknameList->count(); i++) {
147 nicks << ui.nicknameList->item(i)->text();
150 id->setAwayNick(ui.awayNick->text());
151 id->setAwayNickEnabled(true);
152 id->setAwayReason(ui.awayReason->text().remove(linebreaks));
153 id->setAwayReasonEnabled(true);
154 id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
155 id->setAutoAwayTime(ui.autoAwayTime->value());
156 id->setAutoAwayReason(ui.autoAwayReason->text().remove(linebreaks));
157 id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
158 id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
159 id->setDetachAwayReason(ui.detachAwayReason->text().remove(linebreaks));
160 id->setDetachAwayReasonEnabled(true);
161 id->setIdent(ui.ident->text());
162 id->setKickReason(ui.kickReason->text().remove(linebreaks));
163 id->setPartReason(ui.partReason->text().remove(linebreaks));
164 id->setQuitReason(ui.quitReason->text().remove(linebreaks));
166 id->setSslKey(QSslKey(ui.keyTypeLabel->property("sslKey").toByteArray(), (QSsl::KeyAlgorithm)(ui.keyTypeLabel->property("sslKeyType").toInt())));
167 id->setSslCert(QSslCertificate(ui.certOrgLabel->property("sslCert").toByteArray()));
172 void IdentityEditWidget::on_addNick_clicked()
174 QStringList existing;
175 for (int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
176 NickEditDlg dlg(QString(), existing, this);
177 if (dlg.exec() == QDialog::Accepted) {
178 ui.nicknameList->addItem(dlg.nick());
179 ui.nicknameList->setCurrentRow(ui.nicknameList->count()-1);
181 emit widgetHasChanged();
186 void IdentityEditWidget::on_deleteNick_clicked()
188 // no confirmation, since a nickname is really nothing hard to recreate
189 if (ui.nicknameList->selectedItems().count()) {
190 delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
191 ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow()+1, ui.nicknameList->count()-1));
193 emit widgetHasChanged();
198 void IdentityEditWidget::on_renameNick_clicked()
200 if (!ui.nicknameList->selectedItems().count()) return;
201 QString old = ui.nicknameList->selectedItems()[0]->text();
202 QStringList existing;
203 for (int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
204 NickEditDlg dlg(old, existing, this);
205 if (dlg.exec() == QDialog::Accepted) {
206 ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
211 void IdentityEditWidget::on_nickUp_clicked()
213 if (!ui.nicknameList->selectedItems().count()) return;
214 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
216 ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
217 ui.nicknameList->setCurrentRow(row-1);
219 emit widgetHasChanged();
224 void IdentityEditWidget::on_nickDown_clicked()
226 if (!ui.nicknameList->selectedItems().count()) return;
227 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
228 if (row < ui.nicknameList->count()-1) {
229 ui.nicknameList->insertItem(row+1, ui.nicknameList->takeItem(row));
230 ui.nicknameList->setCurrentRow(row+1);
232 emit widgetHasChanged();
237 void IdentityEditWidget::showAdvanced(bool advanced)
239 int idx = ui.tabWidget->indexOf(ui.advancedTab);
242 return; // already added
243 ui.tabWidget->addTab(ui.advancedTab, tr("Advanced"));
247 return; // already removed
248 ui.tabWidget->removeTab(idx);
253 void IdentityEditWidget::setSslState(SslState state)
257 ui.keyAndCertSettings->setCurrentIndex(0);
260 ui.keyAndCertSettings->setCurrentIndex(1);
263 ui.keyAndCertSettings->setCurrentIndex(2);
270 bool IdentityEditWidget::eventFilter(QObject *watched, QEvent *event)
272 bool isCert = (watched == ui.sslCertGroupBox);
273 switch (event->type()) {
274 case QEvent::DragEnter:
275 sslDragEnterEvent(static_cast<QDragEnterEvent *>(event));
278 sslDropEvent(static_cast<QDropEvent *>(event), isCert);
286 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent *event)
288 if (event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
289 event->setDropAction(Qt::CopyAction);
295 void IdentityEditWidget::sslDropEvent(QDropEvent *event, bool isCert)
298 if (event->mimeData()->hasFormat("text/uri-list"))
299 rawUris = event->mimeData()->data("text/uri-list");
301 rawUris = event->mimeData()->data("text/uri");
303 QTextStream uriStream(rawUris);
304 QString filename = QUrl(uriStream.readLine()).toLocalFile();
307 QSslCertificate cert = certByFilename(filename);
312 QSslKey key = keyByFilename(filename);
317 emit widgetHasChanged();
321 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked()
325 if (ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
326 key = keyByFilename(QFileDialog::getOpenFileName(this, tr("Load a Key"),
327 #if QT_VERSION < 0x050000
328 QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
330 QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
334 emit widgetHasChanged();
338 QSslKey IdentityEditWidget::keyByFilename(const QString &filename)
342 QFile keyFile(filename);
343 keyFile.open(QIODevice::ReadOnly);
344 QByteArray keyRaw = keyFile.read(2 << 20);
347 for (int i = 0; i < 2; i++) {
348 for (int j = 0; j < 2; j++) {
349 key = QSslKey(keyRaw, (QSsl::KeyAlgorithm)j, (QSsl::EncodingFormat)i);
354 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."));
360 void IdentityEditWidget::showKeyState(const QSslKey &key)
363 ui.keyTypeLabel->setText(tr("No Key loaded"));
364 ui.clearOrLoadKeyButton->setText(tr("Load"));
367 switch (key.algorithm()) {
369 ui.keyTypeLabel->setText(tr("RSA"));
372 ui.keyTypeLabel->setText(tr("DSA"));
375 ui.keyTypeLabel->setText(tr("No Key loaded"));
377 ui.clearOrLoadKeyButton->setText(tr("Clear"));
379 ui.keyTypeLabel->setProperty("sslKey", key.toPem());
380 ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
384 void IdentityEditWidget::on_clearOrLoadCertButton_clicked()
386 QSslCertificate cert;
388 if (ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
389 cert = certByFilename(QFileDialog::getOpenFileName(this, tr("Load a Certificate"),
390 #if QT_VERSION < 0x050000
391 QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
393 QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
396 emit widgetHasChanged();
400 QSslCertificate IdentityEditWidget::certByFilename(const QString &filename)
402 QSslCertificate cert;
403 QFile certFile(filename);
404 certFile.open(QIODevice::ReadOnly);
405 QByteArray certRaw = certFile.read(2 << 20);
408 for (int i = 0; i < 2; i++) {
409 cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
417 void IdentityEditWidget::showCertState(const QSslCertificate &cert)
420 ui.certOrgLabel->setText(tr("No Certificate loaded"));
421 ui.certCNameLabel->setText(tr("No Certificate loaded"));
422 ui.clearOrLoadCertButton->setText(tr("Load"));
425 #if QT_VERSION < 0x050000
426 ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization));
427 ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName));
429 ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization).join(", "));
430 ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName).join(", "));
432 ui.clearOrLoadCertButton->setText(tr("Clear"));
434 ui.certOrgLabel->setProperty("sslCert", cert.toPem());