ca965aac79ddd097c51ffa4d1ae6bfa71e1bc58f
[quassel.git] / src / qtui / settingspages / identitiessettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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) any later version.                                   *
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 "identitiessettingspage.h"
22
23 #include <QInputDialog>
24 #include <QMessageBox>
25
26 #include "client.h"
27 #include "iconloader.h"
28 #include "signalproxy.h"
29
30 IdentitiesSettingsPage::IdentitiesSettingsPage(QWidget *parent)
31   : SettingsPage(tr("General"), tr("Identities"), parent),
32     _editSsl(false)
33 {
34   ui.setupUi(this);
35   ui.renameIdentity->setIcon(BarIcon("edit-rename"));
36   ui.addIdentity->setIcon(BarIcon("list-add-user"));
37   ui.deleteIdentity->setIcon(BarIcon("list-remove-user"));
38
39   coreConnectionStateChanged(Client::isConnected());  // need a core connection!
40   connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
41
42   connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityCreated(IdentityId)));
43   connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
44
45   connect(ui.identityEditor, SIGNAL(widgetHasChanged()), this, SLOT(widgetHasChanged()));
46 #ifdef HAVE_SSL
47   connect(ui.identityEditor, SIGNAL(requestEditSsl()), this, SLOT(continueUnsecured()));
48 #endif
49
50   currentId = 0;
51
52   //connect(ui.identityList, SIGNAL(editTextChanged(const QString &)), this, SLOT(widgetHasChanged()));
53 }
54
55 void IdentitiesSettingsPage::coreConnectionStateChanged(bool connected) {
56   setEnabled(connected);
57   if(connected) {
58 #ifdef HAVE_SSL
59     if(Client::signalProxy()->isSecure()) {
60       ui.identityEditor->setSslState(IdentityEditWidget::AllowSsl);
61       _editSsl = true;
62     } else {
63       ui.identityEditor->setSslState(IdentityEditWidget::UnsecureSsl);
64       _editSsl = false;
65     }
66 #else
67     ui.identityEditor->setSslState(IdentityEditWidget::NoSsl);
68 #endif
69     load();
70   } else {
71     // reset
72     currentId = 0;
73   }
74 }
75
76 #ifdef HAVE_SSL
77 void IdentitiesSettingsPage::continueUnsecured() {
78   _editSsl = true;
79
80   QHash<IdentityId, CertIdentity *>::iterator idIter;
81   for(idIter = identities.begin(); idIter != identities.end(); idIter++) {
82     idIter.value()->enableEditSsl();
83   }
84
85   ui.identityEditor->setSslState(IdentityEditWidget::AllowSsl);
86 }
87 #endif
88
89 void IdentitiesSettingsPage::save() {
90   setEnabled(false);
91   QList<CertIdentity *> toCreate, toUpdate;
92   // we need to remove our temporarily created identities.
93   // these are going to be re-added after the core has propagated them back...
94   QHash<IdentityId, CertIdentity *>::iterator i = identities.begin();
95   while(i != identities.end()) {
96     if((*i)->id() < 0) {
97       CertIdentity *temp = *i;
98       i = identities.erase(i);
99       toCreate.append(temp);
100       ui.identityList->removeItem(ui.identityList->findData(temp->id().toInt()));
101     } else {
102       if(**i != *Client::identity((*i)->id()) || (*i)->isDirty()) {
103         toUpdate.append(*i);
104       }
105       ++i;
106     }
107   }
108   SaveIdentitiesDlg dlg(toCreate, toUpdate, deletedIdentities, this);
109   int ret = dlg.exec();
110   if(ret == QDialog::Rejected) {
111     // canceled -> reload everything to be safe
112     load();
113   }
114   foreach(Identity *id, toCreate) {
115     id->deleteLater();
116   }
117   changedIdentities.clear();
118   deletedIdentities.clear();
119   setChangedState(false);
120   setEnabled(true);
121 }
122
123 void IdentitiesSettingsPage::load() {
124   currentId = 0;
125   foreach(Identity *identity, identities.values()) {
126     identity->deleteLater();
127   }
128   identities.clear();
129   deletedIdentities.clear();
130   changedIdentities.clear();
131   ui.identityList->clear();
132   foreach(IdentityId id, Client::identityIds()) {
133     clientIdentityCreated(id);
134   }
135   setChangedState(false);
136 }
137
138 void IdentitiesSettingsPage::widgetHasChanged() {
139   bool changed = testHasChanged();
140   if(changed != hasChanged()) setChangedState(changed);
141 }
142
143 bool IdentitiesSettingsPage::testHasChanged() {
144   if(deletedIdentities.count()) return true;
145   if(currentId < 0) {
146     return true; // new identity
147   } else {
148     if(currentId != 0) {
149       changedIdentities.removeAll(currentId);
150       CertIdentity temp(currentId, this);
151       ui.identityEditor->saveToIdentity(&temp);
152       temp.setIdentityName(identities[currentId]->identityName());
153       if(temp != *Client::identity(currentId) || temp.isDirty())
154         changedIdentities.append(currentId);
155     }
156     return changedIdentities.count();
157   }
158 }
159
160 bool IdentitiesSettingsPage::aboutToSave() {
161   ui.identityEditor->saveToIdentity(identities[currentId]);
162   QList<int> errors;
163   foreach(Identity *id, identities.values()) {
164     if(id->identityName().isEmpty()) errors.append(1);
165     if(!id->nicks().count()) errors.append(2);
166     if(id->realName().isEmpty()) errors.append(3);
167     if(id->ident().isEmpty()) errors.append(4);
168   }
169   if(!errors.count()) return true;
170   QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
171   if(errors.contains(1)) error += tr("<li>All identities need an identity name set</li>");
172   if(errors.contains(2)) error += tr("<li>Every identity needs at least one nickname defined</li>");
173   if(errors.contains(3)) error += tr("<li>You need to specify a real name for every identity</li>");
174   if(errors.contains(4)) error += tr("<li>You need to specify an ident for every identity</li>");
175   error += tr("</ul>");
176   QMessageBox::warning(this, tr("One or more identities are invalid"), error);
177   return false;
178 }
179
180 void IdentitiesSettingsPage::clientIdentityCreated(IdentityId id) {
181   CertIdentity *identity = new CertIdentity(*Client::identity(id), this);
182 #ifdef HAVE_SSL
183   identity->enableEditSsl(_editSsl);
184 #endif
185   insertIdentity(identity);
186 #ifdef HAVE_SSL
187   connect(identity, SIGNAL(sslSettingsUpdated()), this, SLOT(clientIdentityUpdated()));
188 #endif
189   connect(Client::identity(id), SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
190 }
191
192 void IdentitiesSettingsPage::clientIdentityUpdated() {
193   const Identity *clientIdentity = qobject_cast<Identity *>(sender());
194   if(!clientIdentity) {
195     qWarning() << "Invalid identity to update!";
196     return;
197   }
198   if(!identities.contains(clientIdentity->id())) {
199     qWarning() << "Unknown identity to update:" << clientIdentity->identityName();
200     return;
201   }
202
203   CertIdentity *identity = identities[clientIdentity->id()];
204
205   if(identity->identityName() != clientIdentity->identityName())
206     renameIdentity(identity->id(), clientIdentity->identityName());
207
208   identity->copyFrom(*clientIdentity);
209
210   if(identity->id() == currentId)
211     ui.identityEditor->displayIdentity(identity);
212 }
213
214 void IdentitiesSettingsPage::clientIdentityRemoved(IdentityId id) {
215   if(identities.contains(id)) {
216     removeIdentity(identities[id]);
217     changedIdentities.removeAll(id);
218     deletedIdentities.removeAll(id);
219   }
220 }
221
222 void IdentitiesSettingsPage::insertIdentity(CertIdentity *identity) {
223   IdentityId id = identity->id();
224   identities[id] = identity;
225   if(id == 1) {
226     // default identity is always the first one!
227     ui.identityList->insertItem(0, identity->identityName(), id.toInt());
228   } else {
229     QString name = identity->identityName();
230     for(int j = 0; j < ui.identityList->count(); j++) {
231       if((j>0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
232         ui.identityList->insertItem(j, name, id.toInt());
233         widgetHasChanged();
234         return;
235       }
236     }
237     // append
238     ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
239     widgetHasChanged();
240   }
241 }
242
243 void IdentitiesSettingsPage::renameIdentity(IdentityId id, const QString &newName) {
244   Identity *identity = identities[id];
245   ui.identityList->setItemText(ui.identityList->findData(identity->id().toInt()), newName);
246   identity->setIdentityName(newName);
247 }
248
249 void IdentitiesSettingsPage::removeIdentity(Identity *id) {
250   identities.remove(id->id());
251   ui.identityList->removeItem(ui.identityList->findData(id->id().toInt()));
252   changedIdentities.removeAll(id->id());
253   if(currentId == id->id()) currentId = 0;
254   id->deleteLater();
255   widgetHasChanged();
256 }
257
258 void IdentitiesSettingsPage::on_identityList_currentIndexChanged(int index) {
259   CertIdentity *previousIdentity = 0;
260   if(currentId != 0 && identities.contains(currentId))
261     previousIdentity = identities[currentId];
262
263   if(index < 0) {
264     //ui.identityList->setEditable(false);
265     ui.identityEditor->displayIdentity(0, previousIdentity);
266     currentId = 0;
267   } else {
268     IdentityId id = ui.identityList->itemData(index).toInt();
269     if(identities.contains(id)) {
270       ui.identityEditor->displayIdentity(identities[id], previousIdentity);
271       currentId = id;
272     }
273     ui.deleteIdentity->setEnabled(id != 1); // default identity cannot be deleted
274     ui.renameIdentity->setEnabled(id != 1); // ...or renamed
275   }
276 }
277
278 void IdentitiesSettingsPage::on_addIdentity_clicked() {
279   CreateIdentityDlg dlg(ui.identityList->model(), this);
280   if(dlg.exec() == QDialog::Accepted) {
281     // find a free (negative) ID
282     IdentityId id;
283     for(id = 1; id <= identities.count(); id++) {
284       if(!identities.keys().contains(-id.toInt())) break;
285     }
286     id = -id.toInt();
287     CertIdentity *newId = new CertIdentity(id, this);
288 #ifdef HAVE_SSL
289     newId->enableEditSsl(_editSsl);
290 #endif
291     if(dlg.duplicateId() != 0) {
292       // duplicate
293       newId->copyFrom(*identities[dlg.duplicateId()]);
294       newId->setId(id);
295     }
296     newId->setIdentityName(dlg.identityName());
297     identities[id] = newId;
298     insertIdentity(newId);
299     ui.identityList->setCurrentIndex(ui.identityList->findData(id.toInt()));
300     widgetHasChanged();
301   }
302 }
303
304 void IdentitiesSettingsPage::on_deleteIdentity_clicked() {
305   Identity *id = identities[currentId];
306   int ret = QMessageBox::question(this, tr("Delete Identity?"),
307                                   tr("Do you really want to delete identity \"%1\"?").arg(id->identityName()),
308                                   QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
309   if(ret != QMessageBox::Yes) return;
310   if(id->id() > 0) deletedIdentities.append(id->id());
311   currentId = 0;
312   removeIdentity(id);
313 }
314
315 void IdentitiesSettingsPage::on_renameIdentity_clicked() {
316   QString oldName = identities[currentId]->identityName();
317   bool ok = false;
318   QString name = QInputDialog::getText(this, tr("Rename Identity"),
319                                        tr("Please enter a new name for the identity \"%1\"!").arg(oldName),
320                                        QLineEdit::Normal, oldName, &ok);
321   if(ok && !name.isEmpty()) {
322     renameIdentity(currentId, name);
323     widgetHasChanged();
324   }
325 }
326
327 /*****************************************************************************************/
328
329 CreateIdentityDlg::CreateIdentityDlg(QAbstractItemModel *model, QWidget *parent)
330   : QDialog(parent)
331 {
332   ui.setupUi(this);
333
334   ui.identityList->setModel(model);  // now we use the identity list of the main page... Trolltech <3
335   on_identityName_textChanged("");   // disable ok button :)
336 }
337
338 QString CreateIdentityDlg::identityName() const {
339   return ui.identityName->text();
340 }
341
342 IdentityId CreateIdentityDlg::duplicateId() const {
343   if(!ui.duplicateIdentity->isChecked()) return 0;
344   if(ui.identityList->currentIndex() >= 0) {
345     return ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
346   }
347   return 0;
348 }
349
350 void CreateIdentityDlg::on_identityName_textChanged(const QString &text) {
351   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(text.count());
352
353 }
354
355 /*********************************************************************************************/
356
357 SaveIdentitiesDlg::SaveIdentitiesDlg(const QList<CertIdentity *> &toCreate, const QList<CertIdentity *> &toUpdate, const QList<IdentityId> &toRemove, QWidget *parent)
358   : QDialog(parent)
359 {
360   ui.setupUi(this);
361   ui.abort->setIcon(SmallIcon("dialog-cancel"));
362
363   numevents = toCreate.count() + toUpdate.count() + toRemove.count();
364   rcvevents = 0;
365   if(numevents) {
366     ui.progressBar->setMaximum(numevents);
367     ui.progressBar->setValue(0);
368
369     connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientEvent()));
370     connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientEvent()));
371
372     foreach(CertIdentity *id, toCreate) {
373       Client::createIdentity(*id);
374     }
375     foreach(CertIdentity *id, toUpdate) {
376       const Identity *cid = Client::identity(id->id());
377       if(!cid) {
378         qWarning() << "Invalid client identity!";
379         numevents--;
380         continue;
381       }
382       connect(cid, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
383       Client::updateIdentity(id->id(), id->toVariantMap());
384 #ifdef HAVE_SSL
385       id->requestUpdateSslSettings();
386 #endif
387     }
388     foreach(IdentityId id, toRemove) {
389       Client::removeIdentity(id);
390     }
391   } else {
392     qWarning() << "Sync dialog called without stuff to change!";
393     accept();
394   }
395 }
396
397 void SaveIdentitiesDlg::clientEvent() {
398   ui.progressBar->setValue(++rcvevents);
399   if(rcvevents >= numevents) accept();
400 }
401
402 /*************************************************************************************************/
403
404 NickEditDlg::NickEditDlg(const QString &old, const QStringList &exist, QWidget *parent)
405   : QDialog(parent), oldNick(old), existing(exist) {
406   ui.setupUi(this);
407
408   // define a regexp for valid nicknames
409   // TODO: add max nicklength according to ISUPPORT
410   QString letter = "A-Za-z";
411   QString special = "\x5b-\x60\x7b-\x7d";
412   QRegExp rx(QString("[%1%2][%1%2\\d-]*").arg(letter, special));
413   ui.nickEdit->setValidator(new QRegExpValidator(rx, ui.nickEdit));
414   if(old.isEmpty()) {
415     // new nick
416     setWindowTitle(tr("Add Nickname"));
417     on_nickEdit_textChanged(""); // disable ok button
418   } else ui.nickEdit->setText(old);
419 }
420
421 QString NickEditDlg::nick() const {
422   return ui.nickEdit->text();
423
424 }
425
426 void NickEditDlg::on_nickEdit_textChanged(const QString &text) {
427   ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text));
428 }
429
430
431