rename translation files to <2 letter ISO code>.po
[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 "client.h"
30 #include "iconloader.h"
31
32 IdentityEditWidget::IdentityEditWidget(QWidget *parent)
33   : QWidget(parent)
34 {
35   ui.setupUi(this);
36
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"));
42
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()));
58
59   setWidgetStates();
60   connect(ui.nicknameList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
61
62   connect(ui.continueUnsecured, SIGNAL(clicked()), this, SIGNAL(requestEditSsl()));
63
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()));
67
68   // disabling unused stuff
69   ui.autoAwayEnabled->hide();
70   ui.awayNick->hide();
71   ui.awayNickLabel->hide();
72
73   ui.detachAwayEnabled->setVisible(!Client::internalCore());
74
75 #ifdef HAVE_SSL
76   ui.sslKeyGroupBox->setAcceptDrops(true);
77   ui.sslKeyGroupBox->installEventFilter(this);
78   ui.sslCertGroupBox->setAcceptDrops(true);
79   ui.sslCertGroupBox->installEventFilter(this);
80 #endif
81 }
82
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);
88   } else {
89     ui.renameNick->setDisabled(true);
90     ui.nickUp->setDisabled(true);
91     ui.nickDown->setDisabled(true);
92   }
93   ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
94 }
95
96 void IdentityEditWidget::displayIdentity(CertIdentity *id, CertIdentity *saveId) {
97   if(saveId) {
98     saveToIdentity(saveId);
99   }
100
101   if(!id)
102     return;
103   
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);
109   //}
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());
123 #ifdef HAVE_SSL
124   showKeyState(id->sslKey());
125   showCertState(id->sslCert());
126 #endif
127   
128 }
129
130 void IdentityEditWidget::saveToIdentity(CertIdentity *id) {
131   id->setRealName(ui.realName->text());
132   QStringList nicks;
133   for(int i = 0; i < ui.nicknameList->count(); i++) {
134     nicks << ui.nicknameList->item(i)->text();
135   }
136   id->setNicks(nicks);
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());
152 #ifdef HAVE_SSL
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()));
155 #endif
156 }
157
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);
165     setWidgetStates();
166     emit widgetHasChanged();
167   }
168 }
169
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));
175     setWidgetStates();
176     emit widgetHasChanged();
177   }
178 }
179
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());
188   }
189
190 }
191
192 void IdentityEditWidget::on_nickUp_clicked() {
193   if(!ui.nicknameList->selectedItems().count()) return;
194   int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
195   if(row > 0) {
196     ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
197     ui.nicknameList->setCurrentRow(row-1);
198     setWidgetStates();
199     emit widgetHasChanged();
200   }
201 }
202
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);
209     setWidgetStates();
210     emit widgetHasChanged();
211   }
212 }
213
214 void IdentityEditWidget::showAdvanced(bool advanced) {
215   int idx = ui.tabWidget->indexOf(ui.advancedTab);
216   if(advanced) {
217     if(idx != -1)
218       return; // already added
219     ui.tabWidget->addTab(ui.advancedTab, tr("Advanced"));
220   } else {
221     if(idx == -1)
222       return; // already removed
223     ui.tabWidget->removeTab(idx);
224   }
225 }
226
227 void IdentityEditWidget::setSslState(SslState state) {
228   switch(state) {
229   case NoSsl:
230     ui.keyAndCertSettings->setCurrentIndex(0);
231     break;
232   case UnsecureSsl:
233     ui.keyAndCertSettings->setCurrentIndex(1);
234     break;
235   case AllowSsl:
236     ui.keyAndCertSettings->setCurrentIndex(2);
237     break;
238   }
239 }
240
241 #ifdef HAVE_SSL
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));
247     return true;
248   case QEvent::Drop:
249     sslDropEvent(static_cast<QDropEvent *>(event), isCert);
250     return true;
251   default:
252     return false;
253   }
254 }
255
256 void IdentityEditWidget::sslDragEnterEvent(QDragEnterEvent *event) {
257   if(event->mimeData()->hasFormat("text/uri-list") || event->mimeData()->hasFormat("text/uri")) {
258     event->setDropAction(Qt::CopyAction);
259     event->accept();
260   }
261 }
262
263 void IdentityEditWidget::sslDropEvent(QDropEvent *event, bool isCert) {
264   QByteArray rawUris;
265   if(event->mimeData()->hasFormat("text/uri-list"))
266     rawUris = event->mimeData()->data("text/uri-list");
267   else
268     rawUris = event->mimeData()->data("text/uri");
269
270   QTextStream uriStream(rawUris);
271   QString filename = QUrl(uriStream.readLine()).toLocalFile();
272
273   if(isCert) {
274     QSslCertificate cert = certByFilename(filename);
275     if(cert.isValid())
276       showCertState(cert);
277   } else {
278     QSslKey key = keyByFilename(filename);
279     if(!key.isNull())
280       showKeyState(key);
281   }
282   event->accept();
283   emit widgetHasChanged();
284 }
285
286 void IdentityEditWidget::on_clearOrLoadKeyButton_clicked() {
287   QSslKey key;
288
289   if(ui.keyTypeLabel->property("sslKey").toByteArray().isEmpty())
290     key = keyByFilename(QFileDialog::getOpenFileName(this, tr("Load a Key"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
291
292   showKeyState(key);
293   emit widgetHasChanged();
294 }
295
296 QSslKey IdentityEditWidget::keyByFilename(const QString &filename) {
297   QSslKey key;
298
299   QFile keyFile(filename);
300   keyFile.open(QIODevice::ReadOnly);
301   QByteArray keyRaw = keyFile.read(2 << 20);
302   keyFile.close();
303
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);
307       if(!key.isNull())
308         goto returnKey;
309     }
310   }
311  returnKey:
312   return key;
313 }
314
315 void IdentityEditWidget::showKeyState(const QSslKey &key) {
316   if(key.isNull()) {
317     ui.keyTypeLabel->setText(tr("No Key loaded"));
318     ui.clearOrLoadKeyButton->setText(tr("Load"));
319   } else {
320     switch(key.algorithm()) {
321     case QSsl::Rsa:
322       ui.keyTypeLabel->setText(tr("RSA"));
323       break;
324     case QSsl::Dsa:
325       ui.keyTypeLabel->setText(tr("DSA"));
326       break;
327     default:
328       ui.keyTypeLabel->setText(tr("No Key loaded"));
329     }
330     ui.clearOrLoadKeyButton->setText(tr("Clear"));
331   }
332   ui.keyTypeLabel->setProperty("sslKey", key.toPem());
333   ui.keyTypeLabel->setProperty("sslKeyType", (int)key.algorithm());
334 }
335
336 void IdentityEditWidget::on_clearOrLoadCertButton_clicked() {
337   QSslCertificate cert;
338
339   if(ui.certOrgLabel->property("sslCert").toByteArray().isEmpty())
340     cert = certByFilename(QFileDialog::getOpenFileName(this, tr("Load a Certificate"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
341
342   showCertState(cert);
343   emit widgetHasChanged();
344 }
345
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);
351   certFile.close();
352
353   for(int i = 0; i < 2; i++) {
354     cert = QSslCertificate(certRaw, (QSsl::EncodingFormat)i);
355     if(cert.isValid())
356       break;
357   }
358   return cert;
359 }
360
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"));
366   } else {
367     ui.certOrgLabel->setText(cert.subjectInfo(QSslCertificate::Organization));
368     ui.certCNameLabel->setText(cert.subjectInfo(QSslCertificate::CommonName));
369     ui.clearOrLoadCertButton->setText(tr("Clear"));
370   }
371   ui.certOrgLabel->setProperty("sslCert", cert.toPem());
372  }
373 #endif //HAVE_SSL