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>
29 #include "iconloader.h"
31 IdentityEditWidget::IdentityEditWidget(QWidget *parent)
36 ui.addNick->setIcon(SmallIcon("list-add"));
37 ui.deleteNick->setIcon(SmallIcon("edit-delete"));
38 ui.renameNick->setIcon(SmallIcon("edit-rename"));
39 ui.nickUp->setIcon(SmallIcon("go-up"));
40 ui.nickDown->setIcon(SmallIcon("go-down"));
42 // We need to know whenever the state of input widgets changes...
43 connect(ui.realName, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
44 connect(ui.nicknameList, SIGNAL(itemChanged(QListWidgetItem *)), this, SIGNAL(widgetHasChanged()));
45 connect(ui.awayNick, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
46 connect(ui.awayNickEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
47 connect(ui.awayReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
48 connect(ui.awayReasonEnabled, SIGNAL(clicked(bool)), 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.detachAwayReasonEnabled, SIGNAL(clicked(bool)), this, SIGNAL(widgetHasChanged()));
56 connect(ui.ident, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
57 connect(ui.kickReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
58 connect(ui.partReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
59 connect(ui.quitReason, SIGNAL(textEdited(const QString &)), this, SIGNAL(widgetHasChanged()));
62 connect(ui.nicknameList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
64 connect(ui.continueUnsecured, SIGNAL(clicked()), this, SIGNAL(requestEditSsl()));
66 // we would need this if we enabled drag and drop in the nicklist...
67 //connect(ui.nicknameList, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(setWidgetStates()));
68 //connect(ui.nicknameList->model(), SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(nicklistHasChanged()));
71 ui.sslKeyGroupBox->setAcceptDrops(true);
72 ui.sslKeyGroupBox->installEventFilter(this);
73 ui.sslCertGroupBox->setAcceptDrops(true);
74 ui.sslCertGroupBox->installEventFilter(this);
78 void IdentityEditWidget::setWidgetStates() {
79 if(ui.nicknameList->selectedItems().count()) {
80 ui.renameNick->setEnabled(true);
81 ui.nickUp->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) > 0);
82 ui.nickDown->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) < ui.nicknameList->count()-1);
84 ui.renameNick->setDisabled(true);
85 ui.nickUp->setDisabled(true);
86 ui.nickDown->setDisabled(true);
88 ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
91 void IdentityEditWidget::displayIdentity(CertIdentity *id, CertIdentity *saveId) {
93 saveToIdentity(saveId);
99 ui.realName->setText(id->realName());
100 ui.nicknameList->clear();
101 ui.nicknameList->addItems(id->nicks());
102 //for(int i = 0; i < ui.nicknameList->count(); i++) {
103 // ui.nicknameList->item(i)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
105 if(ui.nicknameList->count()) ui.nicknameList->setCurrentRow(0);
106 ui.awayNick->setText(id->awayNick());
107 ui.awayNickEnabled->setChecked(id->awayNickEnabled());
108 ui.awayReason->setText(id->awayReason());
109 ui.awayReasonEnabled->setChecked(id->awayReasonEnabled());
110 ui.autoAwayEnabled->setChecked(id->autoAwayEnabled());
111 ui.autoAwayTime->setValue(id->autoAwayTime());
112 ui.autoAwayReason->setText(id->autoAwayReason());
113 ui.autoAwayReasonEnabled->setChecked(id->autoAwayReasonEnabled());
114 ui.detachAwayEnabled->setChecked(id->detachAwayEnabled());
115 ui.detachAwayReason->setText(id->detachAwayReason());
116 ui.detachAwayReasonEnabled->setChecked(id->detachAwayReasonEnabled());
117 ui.ident->setText(id->ident());
118 ui.kickReason->setText(id->kickReason());
119 ui.partReason->setText(id->partReason());
120 ui.quitReason->setText(id->quitReason());
122 showKeyState(id->sslKey());
123 showCertState(id->sslCert());
128 void IdentityEditWidget::saveToIdentity(CertIdentity *id) {
129 id->setRealName(ui.realName->text());
131 for(int i = 0; i < ui.nicknameList->count(); i++) {
132 nicks << ui.nicknameList->item(i)->text();
135 id->setAwayNick(ui.awayNick->text());
136 id->setAwayNickEnabled(ui.awayNickEnabled->isChecked());
137 id->setAwayReason(ui.awayReason->text());
138 id->setAwayReasonEnabled(ui.awayReasonEnabled->isChecked());
139 id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
140 id->setAutoAwayTime(ui.autoAwayTime->value());
141 id->setAutoAwayReason(ui.autoAwayReason->text());
142 id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
143 id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
144 id->setDetachAwayReason(ui.detachAwayReason->text());
145 id->setDetachAwayReasonEnabled(ui.detachAwayReasonEnabled->isChecked());
146 id->setIdent(ui.ident->text());
147 id->setKickReason(ui.kickReason->text());
148 id->setPartReason(ui.partReason->text());
149 id->setQuitReason(ui.quitReason->text());
151 id->setSslKey(QSslKey(ui.keyTypeLabel->property("sslKey").toByteArray(), (QSsl::KeyAlgorithm)(ui.keyTypeLabel->property("sslKeyType").toInt())));
152 id->setSslCert(QSslCertificate(ui.certOrgLabel->property("sslCert").toByteArray()));
156 void IdentityEditWidget::on_addNick_clicked() {
157 QStringList existing;
158 for(int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
159 NickEditDlg dlg(QString(), existing, this);
160 if(dlg.exec() == QDialog::Accepted) {
161 ui.nicknameList->addItem(dlg.nick());
162 ui.nicknameList->setCurrentRow(ui.nicknameList->count()-1);
164 emit widgetHasChanged();
168 void IdentityEditWidget::on_deleteNick_clicked() {
169 // no confirmation, since a nickname is really nothing hard to recreate
170 if(ui.nicknameList->selectedItems().count()) {
171 delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
172 ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow()+1, ui.nicknameList->count()-1));
174 emit widgetHasChanged();
178 void IdentityEditWidget::on_renameNick_clicked() {
179 if(!ui.nicknameList->selectedItems().count()) return;
180 QString old = ui.nicknameList->selectedItems()[0]->text();
181 QStringList existing;
182 for(int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
183 NickEditDlg dlg(old, existing, this);
184 if(dlg.exec() == QDialog::Accepted) {
185 ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
190 void IdentityEditWidget::on_nickUp_clicked() {
191 if(!ui.nicknameList->selectedItems().count()) return;
192 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
194 ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
195 ui.nicknameList->setCurrentRow(row-1);
197 emit widgetHasChanged();
201 void IdentityEditWidget::on_nickDown_clicked() {
202 if(!ui.nicknameList->selectedItems().count()) return;
203 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
204 if(row < ui.nicknameList->count()-1) {
205 ui.nicknameList->insertItem(row+1, ui.nicknameList->takeItem(row));
206 ui.nicknameList->setCurrentRow(row+1);
208 emit widgetHasChanged();
212 void IdentityEditWidget::showAdvanced(bool advanced) {
213 int idx = ui.tabWidget->indexOf(ui.advancedTab);
216 return; // already added
217 ui.tabWidget->addTab(ui.advancedTab, tr("Advanced"));
220 return; // already removed
221 ui.tabWidget->removeTab(idx);
226 void IdentityEditWidget::setSslState(SslState state) {
229 ui.keyAndCertSettings->setCurrentIndex(0);
232 ui.keyAndCertSettings->setCurrentIndex(1);
235 ui.keyAndCertSettings->setCurrentIndex(2);
240 bool IdentityEditWidget::eventFilter(QObject *watched, QEvent *event) {
241 bool isCert = (watched == ui.sslCertGroupBox);
242 switch(event->type()) {
243 case QEvent::DragEnter:
244 sslDragEnterEvent(static_cast<QDragEnterEvent *>(event));
247 sslDropEvent(static_cast<QDropEvent *>(event), isCert);
254 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent *event) {
255 if(event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
256 event->setDropAction(Qt::CopyAction);
261 void IdentityEditWidget::sslDropEvent(QDropEvent *event, bool isCert) {
263 if(event->mimeData()->hasFormat("text/uri-list"))
264 rawUris = event->mimeData()->data("text/uri-list");
266 rawUris = event->mimeData()->data("text/uri");
268 QTextStream uriStream(rawUris);
269 QString filename = QUrl(uriStream.readLine()).toLocalFile();
272 QSslCertificate cert = certByFilename(filename);
276 QSslKey key = keyByFilename(filename);
281 emit widgetHasChanged();
284 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked() {
287 if(ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
288 key = keyByFilename(QFileDialog::getOpenFileName(this, tr("Load a Key"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
291 emit widgetHasChanged();
294 QSslKey IdentityEditWidget::keyByFilename(const QString &filename) {
297 QFile keyFile(filename);
298 keyFile.open(QIODevice::ReadOnly);
299 QByteArray keyRaw = keyFile.read(2 << 20);
302 for(int i = 0; i < 2; i++) {
303 for(int j = 0; j < 2; j++) {
304 key = QSslKey(keyRaw, (QSsl::KeyAlgorithm)j, (QSsl::EncodingFormat)i);
313 void IdentityEditWidget::showKeyState(const QSslKey &key) {
315 ui.keyTypeLabel->setText(tr("No Key loaded"));
316 ui.clearOrLoadKeyButton->setText(tr("Load"));
318 switch(key.algorithm()) {
320 ui.keyTypeLabel->setText(tr("RSA"));
323 ui.keyTypeLabel->setText(tr("DSA"));
326 ui.keyTypeLabel->setText(tr("No Key loaded"));
328 ui.clearOrLoadKeyButton->setText(tr("Clear"));
330 ui.keyTypeLabel->setProperty("sslKey", key.toPem());
331 ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
334 void IdentityEditWidget::on_clearOrLoadCertButton_clicked() {
335 QSslCertificate cert;
337 if(ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
338 cert = certByFilename(QFileDialog::getOpenFileName(this, tr("Load a Certificate"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
341 emit widgetHasChanged();
344 QSslCertificate IdentityEditWidget::certByFilename(const QString &filename) {
345 QSslCertificate cert;
346 QFile certFile(filename);
347 certFile.open(QIODevice::ReadOnly);
348 QByteArray certRaw = certFile.read(2 << 20);
351 for(int i = 0; i < 2; i++) {
352 cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
359 void IdentityEditWidget::showCertState(const QSslCertificate &cert) {
360 if(!cert.isValid()) {
361 ui.certOrgLabel->setText(tr("No Certificate loaded"));
362 ui.certCNameLabel->setText(tr("No Certificate loaded"));
363 ui.clearOrLoadCertButton->setText(tr("Load"));
365 ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization));
366 ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName));
367 ui.clearOrLoadCertButton->setText(tr("Clear"));
369 ui.certOrgLabel->setProperty("sslCert", cert.toPem());