1 /***************************************************************************
2 * Copyright (C) 2005-2013 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 <QDesktopServices>
24 #include <QDragEnterEvent>
26 #include <QFileDialog>
30 #include "iconloader.h"
32 IdentityEditWidget::IdentityEditWidget(QWidget *parent)
37 ui.addNick->setIcon(SmallIcon("list-add"));
38 ui.deleteNick->setIcon(SmallIcon("edit-delete"));
39 ui.renameNick->setIcon(SmallIcon("edit-rename"));
40 ui.nickUp->setIcon(SmallIcon("go-up"));
41 ui.nickDown->setIcon(SmallIcon("go-down"));
43 // We need to know whenever the state of input widgets changes...
44 connect(ui.realName, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
45 connect(ui.nicknameList, SIGNAL(itemChanged(QListWidgetItem *)), this, SIGNAL(widgetHasChanged()));
46 connect(ui.awayNick, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
47 connect(ui.awayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
48 connect(ui.autoAwayEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
49 connect(ui.autoAwayTime, SIGNAL(valueChanged(int)), this, SIGNAL(widgetHasChanged()));
50 connect(ui.autoAwayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
51 connect(ui.autoAwayReasonEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
52 connect(ui.detachAwayEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
53 connect(ui.detachAwayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
54 connect(ui.ident, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
55 connect(ui.kickReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
56 connect(ui.partReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
57 connect(ui.quitReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
60 connect(ui.nicknameList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
62 connect(ui.continueUnsecured, SIGNAL(clicked()), this, SIGNAL(requestEditSsl()));
64 // we would need this if we enabled drag and drop in the nicklist...
65 //connect(ui.nicknameList, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(setWidgetStates()));
66 //connect(ui.nicknameList->model(), SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(nicklistHasChanged()));
68 // disabling unused stuff
69 ui.autoAwayEnabled->hide();
71 ui.awayNickLabel->hide();
73 ui.detachAwayEnabled->setVisible(!Client::internalCore());
76 ui.sslKeyGroupBox->setAcceptDrops(true);
77 ui.sslKeyGroupBox->installEventFilter(this);
78 ui.sslCertGroupBox->setAcceptDrops(true);
79 ui.sslCertGroupBox->installEventFilter(this);
84 void IdentityEditWidget::setWidgetStates()
86 if (ui.nicknameList->selectedItems().count()) {
87 ui.renameNick->setEnabled(true);
88 ui.nickUp->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) > 0);
89 ui.nickDown->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) < ui.nicknameList->count()-1);
92 ui.renameNick->setDisabled(true);
93 ui.nickUp->setDisabled(true);
94 ui.nickDown->setDisabled(true);
96 ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
100 void IdentityEditWidget::displayIdentity(CertIdentity *id, CertIdentity *saveId)
103 saveToIdentity(saveId);
109 ui.realName->setText(id->realName());
110 ui.nicknameList->clear();
111 ui.nicknameList->addItems(id->nicks());
112 //for(int i = 0; i < ui.nicknameList->count(); i++) {
113 // ui.nicknameList->item(i)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
115 if (ui.nicknameList->count()) ui.nicknameList->setCurrentRow(0);
116 ui.awayNick->setText(id->awayNick());
117 ui.awayReason->setText(id->awayReason());
118 ui.autoAwayEnabled->setChecked(id->autoAwayEnabled());
119 ui.autoAwayTime->setValue(id->autoAwayTime());
120 ui.autoAwayReason->setText(id->autoAwayReason());
121 ui.autoAwayReasonEnabled->setChecked(id->autoAwayReasonEnabled());
122 ui.detachAwayEnabled->setChecked(id->detachAwayEnabled());
123 ui.detachAwayReason->setText(id->detachAwayReason());
124 ui.ident->setText(id->ident());
125 ui.kickReason->setText(id->kickReason());
126 ui.partReason->setText(id->partReason());
127 ui.quitReason->setText(id->quitReason());
129 showKeyState(id->sslKey());
130 showCertState(id->sslCert());
135 void IdentityEditWidget::saveToIdentity(CertIdentity *id)
137 QRegExp linebreaks = QRegExp("[\\r\\n]");
138 id->setRealName(ui.realName->text());
140 for (int i = 0; i < ui.nicknameList->count(); i++) {
141 nicks << ui.nicknameList->item(i)->text();
144 id->setAwayNick(ui.awayNick->text());
145 id->setAwayNickEnabled(true);
146 id->setAwayReason(ui.awayReason->text().remove(linebreaks));
147 id->setAwayReasonEnabled(true);
148 id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
149 id->setAutoAwayTime(ui.autoAwayTime->value());
150 id->setAutoAwayReason(ui.autoAwayReason->text().remove(linebreaks));
151 id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
152 id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
153 id->setDetachAwayReason(ui.detachAwayReason->text().remove(linebreaks));
154 id->setDetachAwayReasonEnabled(true);
155 id->setIdent(ui.ident->text());
156 id->setKickReason(ui.kickReason->text().remove(linebreaks));
157 id->setPartReason(ui.partReason->text().remove(linebreaks));
158 id->setQuitReason(ui.quitReason->text().remove(linebreaks));
160 id->setSslKey(QSslKey(ui.keyTypeLabel->property("sslKey").toByteArray(), (QSsl::KeyAlgorithm)(ui.keyTypeLabel->property("sslKeyType").toInt())));
161 id->setSslCert(QSslCertificate(ui.certOrgLabel->property("sslCert").toByteArray()));
166 void IdentityEditWidget::on_addNick_clicked()
168 QStringList existing;
169 for (int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
170 NickEditDlg dlg(QString(), existing, this);
171 if (dlg.exec() == QDialog::Accepted) {
172 ui.nicknameList->addItem(dlg.nick());
173 ui.nicknameList->setCurrentRow(ui.nicknameList->count()-1);
175 emit widgetHasChanged();
180 void IdentityEditWidget::on_deleteNick_clicked()
182 // no confirmation, since a nickname is really nothing hard to recreate
183 if (ui.nicknameList->selectedItems().count()) {
184 delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
185 ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow()+1, ui.nicknameList->count()-1));
187 emit widgetHasChanged();
192 void IdentityEditWidget::on_renameNick_clicked()
194 if (!ui.nicknameList->selectedItems().count()) return;
195 QString old = ui.nicknameList->selectedItems()[0]->text();
196 QStringList existing;
197 for (int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
198 NickEditDlg dlg(old, existing, this);
199 if (dlg.exec() == QDialog::Accepted) {
200 ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
205 void IdentityEditWidget::on_nickUp_clicked()
207 if (!ui.nicknameList->selectedItems().count()) return;
208 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
210 ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
211 ui.nicknameList->setCurrentRow(row-1);
213 emit widgetHasChanged();
218 void IdentityEditWidget::on_nickDown_clicked()
220 if (!ui.nicknameList->selectedItems().count()) return;
221 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
222 if (row < ui.nicknameList->count()-1) {
223 ui.nicknameList->insertItem(row+1, ui.nicknameList->takeItem(row));
224 ui.nicknameList->setCurrentRow(row+1);
226 emit widgetHasChanged();
231 void IdentityEditWidget::showAdvanced(bool advanced)
233 int idx = ui.tabWidget->indexOf(ui.advancedTab);
236 return; // already added
237 ui.tabWidget->addTab(ui.advancedTab, tr("Advanced"));
241 return; // already removed
242 ui.tabWidget->removeTab(idx);
247 void IdentityEditWidget::setSslState(SslState state)
251 ui.keyAndCertSettings->setCurrentIndex(0);
254 ui.keyAndCertSettings->setCurrentIndex(1);
257 ui.keyAndCertSettings->setCurrentIndex(2);
264 bool IdentityEditWidget::eventFilter(QObject *watched, QEvent *event)
266 bool isCert = (watched == ui.sslCertGroupBox);
267 switch (event->type()) {
268 case QEvent::DragEnter:
269 sslDragEnterEvent(static_cast<QDragEnterEvent *>(event));
272 sslDropEvent(static_cast<QDropEvent *>(event), isCert);
280 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent *event)
282 if (event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
283 event->setDropAction(Qt::CopyAction);
289 void IdentityEditWidget::sslDropEvent(QDropEvent *event, bool isCert)
292 if (event->mimeData()->hasFormat("text/uri-list"))
293 rawUris = event->mimeData()->data("text/uri-list");
295 rawUris = event->mimeData()->data("text/uri");
297 QTextStream uriStream(rawUris);
298 QString filename = QUrl(uriStream.readLine()).toLocalFile();
301 QSslCertificate cert = certByFilename(filename);
306 QSslKey key = keyByFilename(filename);
311 emit widgetHasChanged();
315 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked()
319 if (ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
320 key = keyByFilename(QFileDialog::getOpenFileName(this, tr("Load a Key"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
323 emit widgetHasChanged();
327 QSslKey IdentityEditWidget::keyByFilename(const QString &filename)
331 QFile keyFile(filename);
332 keyFile.open(QIODevice::ReadOnly);
333 QByteArray keyRaw = keyFile.read(2 << 20);
336 for (int i = 0; i < 2; i++) {
337 for (int j = 0; j < 2; j++) {
338 key = QSslKey(keyRaw, (QSsl::KeyAlgorithm)j, (QSsl::EncodingFormat)i);
348 void IdentityEditWidget::showKeyState(const QSslKey &key)
351 ui.keyTypeLabel->setText(tr("No Key loaded"));
352 ui.clearOrLoadKeyButton->setText(tr("Load"));
355 switch (key.algorithm()) {
357 ui.keyTypeLabel->setText(tr("RSA"));
360 ui.keyTypeLabel->setText(tr("DSA"));
363 ui.keyTypeLabel->setText(tr("No Key loaded"));
365 ui.clearOrLoadKeyButton->setText(tr("Clear"));
367 ui.keyTypeLabel->setProperty("sslKey", key.toPem());
368 ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
372 void IdentityEditWidget::on_clearOrLoadCertButton_clicked()
374 QSslCertificate cert;
376 if (ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
377 cert = certByFilename(QFileDialog::getOpenFileName(this, tr("Load a Certificate"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
380 emit widgetHasChanged();
384 QSslCertificate IdentityEditWidget::certByFilename(const QString &filename)
386 QSslCertificate cert;
387 QFile certFile(filename);
388 certFile.open(QIODevice::ReadOnly);
389 QByteArray certRaw = certFile.read(2 << 20);
392 for (int i = 0; i < 2; i++) {
393 cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
401 void IdentityEditWidget::showCertState(const QSslCertificate &cert)
403 if (!cert.isValid()) {
404 ui.certOrgLabel->setText(tr("No Certificate loaded"));
405 ui.certCNameLabel->setText(tr("No Certificate loaded"));
406 ui.clearOrLoadCertButton->setText(tr("Load"));
409 ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization));
410 ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName));
411 ui.clearOrLoadCertButton->setText(tr("Clear"));
413 ui.certOrgLabel->setProperty("sslCert", cert.toPem());