fixes #542 - show identities and network settings on first start
[quassel.git] / src / qtui / settingspages / identityeditwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "identityeditwidget.h"
22
23 #include <QDesktopServices>
24 #include <QDragEnterEvent>
25 #include <QDropEvent>
26 #include <QFileDialog>
27 #include <QUrl>
28
29 #include "iconloader.h"
30
31 IdentityEditWidget::IdentityEditWidget(QWidget *parent)
32   : QWidget(parent)
33 {
34   ui.setupUi(this);
35
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"));
41
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()));
60
61   setWidgetStates();
62   connect(ui.nicknameList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
63
64   connect(ui.continueUnsecured, SIGNAL(clicked()), this, SIGNAL(requestEditSsl()));
65
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()));
69
70 #ifdef HAVE_SSL
71   ui.sslKeyGroupBox->setAcceptDrops(true);
72   ui.sslKeyGroupBox->installEventFilter(this);
73   ui.sslCertGroupBox->setAcceptDrops(true);
74   ui.sslCertGroupBox->installEventFilter(this);
75 #endif
76 }
77
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);
83   } else {
84     ui.renameNick->setDisabled(true);
85     ui.nickUp->setDisabled(true);
86     ui.nickDown->setDisabled(true);
87   }
88   ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
89 }
90
91 void IdentityEditWidget::displayIdentity(CertIdentity *id, CertIdentity *saveId) {
92   if(saveId) {
93     saveToIdentity(saveId);
94   }
95
96   if(!id)
97     return;
98   
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);
104   //}
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());
121 #ifdef HAVE_SSL
122   showKeyState(id->sslKey());
123   showCertState(id->sslCert());
124 #endif
125   
126 }
127
128 void IdentityEditWidget::saveToIdentity(CertIdentity *id) {
129   id->setRealName(ui.realName->text());
130   QStringList nicks;
131   for(int i = 0; i < ui.nicknameList->count(); i++) {
132     nicks << ui.nicknameList->item(i)->text();
133   }
134   id->setNicks(nicks);
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());
150 #ifdef HAVE_SSL
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()));
153 #endif
154 }
155
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);
163     setWidgetStates();
164     emit widgetHasChanged();
165   }
166 }
167
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));
173     setWidgetStates();
174     emit widgetHasChanged();
175   }
176 }
177
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());
186   }
187
188 }
189
190 void IdentityEditWidget::on_nickUp_clicked() {
191   if(!ui.nicknameList->selectedItems().count()) return;
192   int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
193   if(row > 0) {
194     ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
195     ui.nicknameList->setCurrentRow(row-1);
196     setWidgetStates();
197     emit widgetHasChanged();
198   }
199 }
200
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);
207     setWidgetStates();
208     emit widgetHasChanged();
209   }
210 }
211
212 void IdentityEditWidget::showAdvanced(bool advanced) {
213   int idx = ui.tabWidget->indexOf(ui.advancedTab);
214   if(advanced) {
215     if(idx != -1)
216       return; // already added
217     ui.tabWidget->addTab(ui.advancedTab, tr("Advanced"));
218   } else {
219     if(idx == -1)
220       return; // already removed
221     ui.tabWidget->removeTab(idx);
222   }
223 }
224
225 #ifdef HAVE_SSL
226 void IdentityEditWidget::setSslState(SslState state) {
227   switch(state) {
228   case NoSsl:
229     ui.keyAndCertSettings->setCurrentIndex(0);
230     break;
231   case UnsecureSsl:
232     ui.keyAndCertSettings->setCurrentIndex(1);
233     break;
234   case AllowSsl:
235     ui.keyAndCertSettings->setCurrentIndex(2);
236     break;
237   }
238 }
239
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));
245     return true;
246   case QEvent::Drop:
247     sslDropEvent(static_cast<QDropEvent *>(event), isCert);
248     return true;
249   default:
250     return false;
251   }
252 }
253
254 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent *event) {
255   if(event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
256     event->setDropAction(Qt::CopyAction);
257     event->accept();
258   }
259 }
260
261 void IdentityEditWidget::sslDropEvent(QDropEvent *event, bool isCert) {
262   QByteArray rawUris;
263   if(event->mimeData()->hasFormat("text/uri-list"))
264     rawUris = event->mimeData()->data("text/uri-list");
265   else
266     rawUris = event->mimeData()->data("text/uri");
267
268   QTextStream uriStream(rawUris);
269   QString filename = QUrl(uriStream.readLine()).toLocalFile();
270
271   if(isCert) {
272     QSslCertificate cert = certByFilename(filename);
273     if(cert.isValid())
274       showCertState(cert);
275   } else {
276     QSslKey key = keyByFilename(filename);
277     if(!key.isNull())
278       showKeyState(key);
279   }
280   event->accept();
281   emit widgetHasChanged();
282 }
283
284 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked() {
285   QSslKey key;
286
287   if(ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
288     key = keyByFilename(QFileDialog::getOpenFileName(this, tr("Load a Key"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
289
290   showKeyState(key);
291   emit widgetHasChanged();
292 }
293
294 QSslKey IdentityEditWidget::keyByFilename(const QString &filename) {
295   QSslKey key;
296
297   QFile keyFile(filename);
298   keyFile.open(QIODevice::ReadOnly);
299   QByteArray keyRaw = keyFile.read(2 << 20);
300   keyFile.close();
301
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);
305       if(!key.isNull())
306         goto returnKey;
307     }
308   }
309  returnKey:
310   return key;
311 }
312
313 void IdentityEditWidget::showKeyState(const QSslKey &key) {
314   if(key.isNull()) {
315     ui.keyTypeLabel->setText(tr("No Key loaded"));
316     ui.clearOrLoadKeyButton->setText(tr("Load"));
317   } else {
318     switch(key.algorithm()) {
319     case QSsl::Rsa:
320       ui.keyTypeLabel->setText(tr("RSA"));
321       break;
322     case QSsl::Dsa:
323       ui.keyTypeLabel->setText(tr("DSA"));
324       break;
325     default:
326       ui.keyTypeLabel->setText(tr("No Key loaded"));
327     }
328     ui.clearOrLoadKeyButton->setText(tr("Clear"));
329   }
330   ui.keyTypeLabel->setProperty("sslKey", key.toPem());
331   ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
332 }
333
334 void IdentityEditWidget::on_clearOrLoadCertButton_clicked() {
335   QSslCertificate cert;
336
337   if(ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
338     cert = certByFilename(QFileDialog::getOpenFileName(this, tr("Load a Certificate"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
339
340   showCertState(cert);
341   emit widgetHasChanged();
342 }
343
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);
349   certFile.close();
350
351   for(int i = 0; i < 2; i++) {
352     cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
353     if(cert.isValid())
354       break;
355   }
356   return cert;
357 }
358
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"));
364   } else {
365     ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization));
366     ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName));
367     ui.clearOrLoadCertButton->setText(tr("Clear"));
368   }
369   ui.certOrgLabel->setProperty("sslCert", cert.toPem());
370  }
371 #endif //HAVE_SSL