1 /***************************************************************************
2 * Copyright (C) 2005-08 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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);
83 void IdentityEditWidget::setWidgetStates() {
84 if(ui.nicknameList->selectedItems().count()) {
85 ui.renameNick->setEnabled(true);
86 ui.nickUp->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) > 0);
87 ui.nickDown->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) < ui.nicknameList->count()-1);
89 ui.renameNick->setDisabled(true);
90 ui.nickUp->setDisabled(true);
91 ui.nickDown->setDisabled(true);
93 ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
96 void IdentityEditWidget::displayIdentity(CertIdentity *id, CertIdentity *saveId) {
98 saveToIdentity(saveId);
104 ui.realName->setText(id->realName());
105 ui.nicknameList->clear();
106 ui.nicknameList->addItems(id->nicks());
107 //for(int i = 0; i < ui.nicknameList->count(); i++) {
108 // ui.nicknameList->item(i)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
110 if(ui.nicknameList->count()) ui.nicknameList->setCurrentRow(0);
111 ui.awayNick->setText(id->awayNick());
112 ui.awayReason->setText(id->awayReason());
113 ui.autoAwayEnabled->setChecked(id->autoAwayEnabled());
114 ui.autoAwayTime->setValue(id->autoAwayTime());
115 ui.autoAwayReason->setText(id->autoAwayReason());
116 ui.autoAwayReasonEnabled->setChecked(id->autoAwayReasonEnabled());
117 ui.detachAwayEnabled->setChecked(id->detachAwayEnabled());
118 ui.detachAwayReason->setText(id->detachAwayReason());
119 ui.ident->setText(id->ident());
120 ui.kickReason->setText(id->kickReason());
121 ui.partReason->setText(id->partReason());
122 ui.quitReason->setText(id->quitReason());
124 showKeyState(id->sslKey());
125 showCertState(id->sslCert());
130 void IdentityEditWidget::saveToIdentity(CertIdentity *id) {
131 id->setRealName(ui.realName->text());
133 for(int i = 0; i < ui.nicknameList->count(); i++) {
134 nicks << ui.nicknameList->item(i)->text();
137 id->setAwayNick(ui.awayNick->text());
138 id->setAwayNickEnabled(true);
139 id->setAwayReason(ui.awayReason->text());
140 id->setAwayReasonEnabled(true);
141 id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
142 id->setAutoAwayTime(ui.autoAwayTime->value());
143 id->setAutoAwayReason(ui.autoAwayReason->text());
144 id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
145 id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
146 id->setDetachAwayReason(ui.detachAwayReason->text());
147 id->setDetachAwayReasonEnabled(true);
148 id->setIdent(ui.ident->text());
149 id->setKickReason(ui.kickReason->text());
150 id->setPartReason(ui.partReason->text());
151 id->setQuitReason(ui.quitReason->text());
153 id->setSslKey(QSslKey(ui.keyTypeLabel->property("sslKey").toByteArray(), (QSsl::KeyAlgorithm)(ui.keyTypeLabel->property("sslKeyType").toInt())));
154 id->setSslCert(QSslCertificate(ui.certOrgLabel->property("sslCert").toByteArray()));
158 void IdentityEditWidget::on_addNick_clicked() {
159 QStringList existing;
160 for(int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
161 NickEditDlg dlg(QString(), existing, this);
162 if(dlg.exec() == QDialog::Accepted) {
163 ui.nicknameList->addItem(dlg.nick());
164 ui.nicknameList->setCurrentRow(ui.nicknameList->count()-1);
166 emit widgetHasChanged();
170 void IdentityEditWidget::on_deleteNick_clicked() {
171 // no confirmation, since a nickname is really nothing hard to recreate
172 if(ui.nicknameList->selectedItems().count()) {
173 delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
174 ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow()+1, ui.nicknameList->count()-1));
176 emit widgetHasChanged();
180 void IdentityEditWidget::on_renameNick_clicked() {
181 if(!ui.nicknameList->selectedItems().count()) return;
182 QString old = ui.nicknameList->selectedItems()[0]->text();
183 QStringList existing;
184 for(int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
185 NickEditDlg dlg(old, existing, this);
186 if(dlg.exec() == QDialog::Accepted) {
187 ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
192 void IdentityEditWidget::on_nickUp_clicked() {
193 if(!ui.nicknameList->selectedItems().count()) return;
194 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
196 ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
197 ui.nicknameList->setCurrentRow(row-1);
199 emit widgetHasChanged();
203 void IdentityEditWidget::on_nickDown_clicked() {
204 if(!ui.nicknameList->selectedItems().count()) return;
205 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
206 if(row < ui.nicknameList->count()-1) {
207 ui.nicknameList->insertItem(row+1, ui.nicknameList->takeItem(row));
208 ui.nicknameList->setCurrentRow(row+1);
210 emit widgetHasChanged();
214 void IdentityEditWidget::showAdvanced(bool advanced) {
215 int idx = ui.tabWidget->indexOf(ui.advancedTab);
218 return; // already added
219 ui.tabWidget->addTab(ui.advancedTab, tr("Advanced"));
222 return; // already removed
223 ui.tabWidget->removeTab(idx);
227 void IdentityEditWidget::setSslState(SslState state) {
230 ui.keyAndCertSettings->setCurrentIndex(0);
233 ui.keyAndCertSettings->setCurrentIndex(1);
236 ui.keyAndCertSettings->setCurrentIndex(2);
242 bool IdentityEditWidget::eventFilter(QObject *watched, QEvent *event) {
243 bool isCert = (watched == ui.sslCertGroupBox);
244 switch(event->type()) {
245 case QEvent::DragEnter:
246 sslDragEnterEvent(static_cast<QDragEnterEvent *>(event));
249 sslDropEvent(static_cast<QDropEvent *>(event), isCert);
256 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent *event) {
257 if(event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
258 event->setDropAction(Qt::CopyAction);
263 void IdentityEditWidget::sslDropEvent(QDropEvent *event, bool isCert) {
265 if(event->mimeData()->hasFormat("text/uri-list"))
266 rawUris = event->mimeData()->data("text/uri-list");
268 rawUris = event->mimeData()->data("text/uri");
270 QTextStream uriStream(rawUris);
271 QString filename = QUrl(uriStream.readLine()).toLocalFile();
274 QSslCertificate cert = certByFilename(filename);
278 QSslKey key = keyByFilename(filename);
283 emit widgetHasChanged();
286 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked() {
289 if(ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
290 key = keyByFilename(QFileDialog::getOpenFileName(this, tr("Load a Key"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
293 emit widgetHasChanged();
296 QSslKey IdentityEditWidget::keyByFilename(const QString &filename) {
299 QFile keyFile(filename);
300 keyFile.open(QIODevice::ReadOnly);
301 QByteArray keyRaw = keyFile.read(2 << 20);
304 for(int i = 0; i < 2; i++) {
305 for(int j = 0; j < 2; j++) {
306 key = QSslKey(keyRaw, (QSsl::KeyAlgorithm)j, (QSsl::EncodingFormat)i);
315 void IdentityEditWidget::showKeyState(const QSslKey &key) {
317 ui.keyTypeLabel->setText(tr("No Key loaded"));
318 ui.clearOrLoadKeyButton->setText(tr("Load"));
320 switch(key.algorithm()) {
322 ui.keyTypeLabel->setText(tr("RSA"));
325 ui.keyTypeLabel->setText(tr("DSA"));
328 ui.keyTypeLabel->setText(tr("No Key loaded"));
330 ui.clearOrLoadKeyButton->setText(tr("Clear"));
332 ui.keyTypeLabel->setProperty("sslKey", key.toPem());
333 ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
336 void IdentityEditWidget::on_clearOrLoadCertButton_clicked() {
337 QSslCertificate cert;
339 if(ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
340 cert = certByFilename(QFileDialog::getOpenFileName(this, tr("Load a Certificate"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
343 emit widgetHasChanged();
346 QSslCertificate IdentityEditWidget::certByFilename(const QString &filename) {
347 QSslCertificate cert;
348 QFile certFile(filename);
349 certFile.open(QIODevice::ReadOnly);
350 QByteArray certRaw = certFile.read(2 << 20);
353 for(int i = 0; i < 2; i++) {
354 cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
361 void IdentityEditWidget::showCertState(const QSslCertificate &cert) {
362 if(!cert.isValid()) {
363 ui.certOrgLabel->setText(tr("No Certificate loaded"));
364 ui.certCNameLabel->setText(tr("No Certificate loaded"));
365 ui.clearOrLoadCertButton->setText(tr("Load"));
367 ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization));
368 ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName));
369 ui.clearOrLoadCertButton->setText(tr("Clear"));
371 ui.certOrgLabel->setProperty("sslCert", cert.toPem());