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 <QDesktopServices>
24 #include <QDragEnterEvent>
26 #include <QFileDialog>
28 #include <QMessageBox>
31 #include "iconloader.h"
33 IdentityEditWidget::IdentityEditWidget(QWidget *parent)
38 ui.addNick->setIcon(SmallIcon("list-add"));
39 ui.deleteNick->setIcon(SmallIcon("edit-delete"));
40 ui.renameNick->setIcon(SmallIcon("edit-rename"));
41 ui.nickUp->setIcon(SmallIcon("go-up"));
42 ui.nickDown->setIcon(SmallIcon("go-down"));
44 // We need to know whenever the state of input widgets changes...
45 connect(ui.realName, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
46 connect(ui.nicknameList, SIGNAL(itemChanged(QListWidgetItem *)), this, SIGNAL(widgetHasChanged()));
47 connect(ui.awayNick, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
48 connect(ui.awayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
49 connect(ui.autoAwayEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
50 connect(ui.autoAwayTime, SIGNAL(valueChanged(int)), this, SIGNAL(widgetHasChanged()));
51 connect(ui.autoAwayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
52 connect(ui.autoAwayReasonEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
53 connect(ui.detachAwayEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
54 connect(ui.detachAwayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
55 connect(ui.ident, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
56 connect(ui.kickReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
57 connect(ui.partReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
58 connect(ui.quitReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
61 connect(ui.nicknameList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
63 connect(ui.continueUnsecured, SIGNAL(clicked()), this, SIGNAL(requestEditSsl()));
65 // we would need this if we enabled drag and drop in the nicklist...
66 //connect(ui.nicknameList, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(setWidgetStates()));
67 //connect(ui.nicknameList->model(), SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(nicklistHasChanged()));
69 // disabling unused stuff
70 ui.autoAwayEnabled->hide();
72 ui.awayNickLabel->hide();
74 ui.detachAwayEnabled->setVisible(!Client::internalCore());
77 ui.sslKeyGroupBox->setAcceptDrops(true);
78 ui.sslKeyGroupBox->installEventFilter(this);
79 ui.sslCertGroupBox->setAcceptDrops(true);
80 ui.sslCertGroupBox->installEventFilter(this);
85 void IdentityEditWidget::setWidgetStates()
87 if (ui.nicknameList->selectedItems().count()) {
88 ui.renameNick->setEnabled(true);
89 ui.nickUp->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) > 0);
90 ui.nickDown->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) < ui.nicknameList->count()-1);
93 ui.renameNick->setDisabled(true);
94 ui.nickUp->setDisabled(true);
95 ui.nickDown->setDisabled(true);
97 ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
101 void IdentityEditWidget::displayIdentity(CertIdentity *id, CertIdentity *saveId)
104 saveToIdentity(saveId);
110 ui.realName->setText(id->realName());
111 ui.nicknameList->clear();
112 ui.nicknameList->addItems(id->nicks());
113 //for(int i = 0; i < ui.nicknameList->count(); i++) {
114 // ui.nicknameList->item(i)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
116 if (ui.nicknameList->count()) ui.nicknameList->setCurrentRow(0);
117 ui.awayNick->setText(id->awayNick());
118 ui.awayReason->setText(id->awayReason());
119 ui.autoAwayEnabled->setChecked(id->autoAwayEnabled());
120 ui.autoAwayTime->setValue(id->autoAwayTime());
121 ui.autoAwayReason->setText(id->autoAwayReason());
122 ui.autoAwayReasonEnabled->setChecked(id->autoAwayReasonEnabled());
123 ui.detachAwayEnabled->setChecked(id->detachAwayEnabled());
124 ui.detachAwayReason->setText(id->detachAwayReason());
125 ui.ident->setText(id->ident());
126 ui.kickReason->setText(id->kickReason());
127 ui.partReason->setText(id->partReason());
128 ui.quitReason->setText(id->quitReason());
130 showKeyState(id->sslKey());
131 showCertState(id->sslCert());
136 void IdentityEditWidget::saveToIdentity(CertIdentity *id)
138 QRegExp linebreaks = QRegExp("[\\r\\n]");
139 id->setRealName(ui.realName->text());
141 for (int i = 0; i < ui.nicknameList->count(); i++) {
142 nicks << ui.nicknameList->item(i)->text();
145 id->setAwayNick(ui.awayNick->text());
146 id->setAwayNickEnabled(true);
147 id->setAwayReason(ui.awayReason->text().remove(linebreaks));
148 id->setAwayReasonEnabled(true);
149 id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
150 id->setAutoAwayTime(ui.autoAwayTime->value());
151 id->setAutoAwayReason(ui.autoAwayReason->text().remove(linebreaks));
152 id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
153 id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
154 id->setDetachAwayReason(ui.detachAwayReason->text().remove(linebreaks));
155 id->setDetachAwayReasonEnabled(true);
156 id->setIdent(ui.ident->text());
157 id->setKickReason(ui.kickReason->text().remove(linebreaks));
158 id->setPartReason(ui.partReason->text().remove(linebreaks));
159 id->setQuitReason(ui.quitReason->text().remove(linebreaks));
161 id->setSslKey(QSslKey(ui.keyTypeLabel->property("sslKey").toByteArray(), (QSsl::KeyAlgorithm)(ui.keyTypeLabel->property("sslKeyType").toInt())));
162 id->setSslCert(QSslCertificate(ui.certOrgLabel->property("sslCert").toByteArray()));
167 void IdentityEditWidget::on_addNick_clicked()
169 QStringList existing;
170 for (int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
171 NickEditDlg dlg(QString(), existing, this);
172 if (dlg.exec() == QDialog::Accepted) {
173 ui.nicknameList->addItem(dlg.nick());
174 ui.nicknameList->setCurrentRow(ui.nicknameList->count()-1);
176 emit widgetHasChanged();
181 void IdentityEditWidget::on_deleteNick_clicked()
183 // no confirmation, since a nickname is really nothing hard to recreate
184 if (ui.nicknameList->selectedItems().count()) {
185 delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
186 ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow()+1, ui.nicknameList->count()-1));
188 emit widgetHasChanged();
193 void IdentityEditWidget::on_renameNick_clicked()
195 if (!ui.nicknameList->selectedItems().count()) return;
196 QString old = ui.nicknameList->selectedItems()[0]->text();
197 QStringList existing;
198 for (int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
199 NickEditDlg dlg(old, existing, this);
200 if (dlg.exec() == QDialog::Accepted) {
201 ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
206 void IdentityEditWidget::on_nickUp_clicked()
208 if (!ui.nicknameList->selectedItems().count()) return;
209 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
211 ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
212 ui.nicknameList->setCurrentRow(row-1);
214 emit widgetHasChanged();
219 void IdentityEditWidget::on_nickDown_clicked()
221 if (!ui.nicknameList->selectedItems().count()) return;
222 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
223 if (row < ui.nicknameList->count()-1) {
224 ui.nicknameList->insertItem(row+1, ui.nicknameList->takeItem(row));
225 ui.nicknameList->setCurrentRow(row+1);
227 emit widgetHasChanged();
232 void IdentityEditWidget::showAdvanced(bool advanced)
234 int idx = ui.tabWidget->indexOf(ui.advancedTab);
237 return; // already added
238 ui.tabWidget->addTab(ui.advancedTab, tr("Advanced"));
242 return; // already removed
243 ui.tabWidget->removeTab(idx);
248 void IdentityEditWidget::setSslState(SslState state)
252 ui.keyAndCertSettings->setCurrentIndex(0);
255 ui.keyAndCertSettings->setCurrentIndex(1);
258 ui.keyAndCertSettings->setCurrentIndex(2);
265 bool IdentityEditWidget::eventFilter(QObject *watched, QEvent *event)
267 bool isCert = (watched == ui.sslCertGroupBox);
268 switch (event->type()) {
269 case QEvent::DragEnter:
270 sslDragEnterEvent(static_cast<QDragEnterEvent *>(event));
273 sslDropEvent(static_cast<QDropEvent *>(event), isCert);
281 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent *event)
283 if (event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
284 event->setDropAction(Qt::CopyAction);
290 void IdentityEditWidget::sslDropEvent(QDropEvent *event, bool isCert)
293 if (event->mimeData()->hasFormat("text/uri-list"))
294 rawUris = event->mimeData()->data("text/uri-list");
296 rawUris = event->mimeData()->data("text/uri");
298 QTextStream uriStream(rawUris);
299 QString filename = QUrl(uriStream.readLine()).toLocalFile();
302 QSslCertificate cert = certByFilename(filename);
307 QSslKey key = keyByFilename(filename);
312 emit widgetHasChanged();
316 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked()
320 if (ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
321 key = keyByFilename(QFileDialog::getOpenFileName(this, tr("Load a Key"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
324 emit widgetHasChanged();
328 QSslKey IdentityEditWidget::keyByFilename(const QString &filename)
332 QFile keyFile(filename);
333 keyFile.open(QIODevice::ReadOnly);
334 QByteArray keyRaw = keyFile.read(2 << 20);
337 for (int i = 0; i < 2; i++) {
338 for (int j = 0; j < 2; j++) {
339 key = QSslKey(keyRaw, (QSsl::KeyAlgorithm)j, (QSsl::EncodingFormat)i);
344 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."));
350 void IdentityEditWidget::showKeyState(const QSslKey &key)
353 ui.keyTypeLabel->setText(tr("No Key loaded"));
354 ui.clearOrLoadKeyButton->setText(tr("Load"));
357 switch (key.algorithm()) {
359 ui.keyTypeLabel->setText(tr("RSA"));
362 ui.keyTypeLabel->setText(tr("DSA"));
365 ui.keyTypeLabel->setText(tr("No Key loaded"));
367 ui.clearOrLoadKeyButton->setText(tr("Clear"));
369 ui.keyTypeLabel->setProperty("sslKey", key.toPem());
370 ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
374 void IdentityEditWidget::on_clearOrLoadCertButton_clicked()
376 QSslCertificate cert;
378 if (ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
379 cert = certByFilename(QFileDialog::getOpenFileName(this, tr("Load a Certificate"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
382 emit widgetHasChanged();
386 QSslCertificate IdentityEditWidget::certByFilename(const QString &filename)
388 QSslCertificate cert;
389 QFile certFile(filename);
390 certFile.open(QIODevice::ReadOnly);
391 QByteArray certRaw = certFile.read(2 << 20);
394 for (int i = 0; i < 2; i++) {
395 cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
403 void IdentityEditWidget::showCertState(const QSslCertificate &cert)
405 if (!cert.isValid()) {
406 ui.certOrgLabel->setText(tr("No Certificate loaded"));
407 ui.certCNameLabel->setText(tr("No Certificate loaded"));
408 ui.clearOrLoadCertButton->setText(tr("Load"));
411 ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization));
412 ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName));
413 ui.clearOrLoadCertButton->setText(tr("Clear"));
415 ui.certOrgLabel->setProperty("sslCert", cert.toPem());