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) any later version. *
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 <QInputDialog>
22 #include <QMessageBox>
24 #include "identitiessettingspage.h"
27 #include "iconloader.h"
29 IdentitiesSettingsPage::IdentitiesSettingsPage(QWidget *parent)
30 : SettingsPage(tr("General"), tr("Identities"), parent) {
33 ui.renameIdentity->setIcon(BarIcon("edit-rename"));
34 ui.addIdentity->setIcon(BarIcon("list-add-user"));
35 ui.deleteIdentity->setIcon(BarIcon("list-remove-user"));
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 setEnabled(Client::isConnected()); // need a core connection!
44 connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
45 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityCreated(IdentityId)));
46 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
50 // We need to know whenever the state of input widgets changes...
51 //connect(ui.identityList, SIGNAL(editTextChanged(const QString &)), this, SLOT(widgetHasChanged()));
52 connect(ui.realName, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
53 connect(ui.nicknameList, SIGNAL(itemChanged(QListWidgetItem *)), this, SLOT(widgetHasChanged()));
54 connect(ui.awayNick, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
55 connect(ui.awayNickEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
56 connect(ui.awayReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
57 connect(ui.awayReasonEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
58 connect(ui.autoAwayEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
59 connect(ui.autoAwayTime, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
60 connect(ui.autoAwayReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
61 connect(ui.autoAwayReasonEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
62 connect(ui.detachAwayEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
63 connect(ui.detachAwayReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
64 connect(ui.detachAwayReasonEnabled, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
65 connect(ui.ident, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
66 connect(ui.kickReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
67 connect(ui.partReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
68 connect(ui.quitReason, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
70 connect(ui.nicknameList, SIGNAL(itemSelectionChanged()), this, SLOT(setWidgetStates()));
72 // we would need this if we enabled drag and drop in the nicklist...
73 //connect(ui.nicknameList, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(setWidgetStates()));
74 //connect(ui.nicknameList->model(), SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(nicklistHasChanged()));
78 void IdentitiesSettingsPage::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);
90 // FIXME this is until stuff has been implemented
91 ui.detachAwayEnabled->setEnabled(false);
94 void IdentitiesSettingsPage::coreConnectionStateChanged(bool state) {
95 this->setEnabled(state);
104 void IdentitiesSettingsPage::save() {
106 QList<Identity *> toCreate, toUpdate;
107 // we need to remove our temporarily created identities.
108 // these are going to be re-added after the core has propagated them back...
109 QHash<IdentityId, Identity *>::iterator i = identities.begin();
110 while(i != identities.end()) {
113 i = identities.erase(i);
114 toCreate.append(temp);
115 ui.identityList->removeItem(ui.identityList->findData(temp->id().toInt()));
117 if(**i != *Client::identity((*i)->id())) {
123 SaveIdentitiesDlg dlg(toCreate, toUpdate, deletedIdentities, this);
124 int ret = dlg.exec();
125 if(ret == QDialog::Rejected) {
126 // canceled -> reload everything to be safe
129 foreach(Identity *id, toCreate) {
132 changedIdentities.clear();
133 deletedIdentities.clear();
134 setChangedState(false);
138 void IdentitiesSettingsPage::load() {
140 foreach(Identity *identity, identities.values()) {
141 identity->deleteLater();
144 deletedIdentities.clear();
145 changedIdentities.clear();
146 ui.identityList->clear();
147 foreach(IdentityId id, Client::identityIds()) {
148 clientIdentityCreated(id);
150 setChangedState(false);
153 void IdentitiesSettingsPage::widgetHasChanged() {
154 bool changed = testHasChanged();
155 if(changed != hasChanged()) setChangedState(changed);
158 bool IdentitiesSettingsPage::testHasChanged() {
159 if(deletedIdentities.count()) return true;
161 return true; // new identity
164 changedIdentities.removeAll(currentId);
165 Identity temp(currentId, this);
166 saveToIdentity(&temp);
167 temp.setIdentityName(identities[currentId]->identityName());
168 if(temp != *Client::identity(currentId)) changedIdentities.append(currentId);
170 return changedIdentities.count();
174 bool IdentitiesSettingsPage::aboutToSave() {
175 saveToIdentity(identities[currentId]);
177 foreach(Identity *id, identities.values()) {
178 if(id->identityName().isEmpty()) errors.append(1);
179 if(!id->nicks().count()) errors.append(2);
180 if(id->realName().isEmpty()) errors.append(3);
181 if(id->ident().isEmpty()) errors.append(4);
183 if(!errors.count()) return true;
184 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
185 if(errors.contains(1)) error += tr("<li>All identities need an identity name set</li>");
186 if(errors.contains(2)) error += tr("<li>Every identity needs at least one nickname defined</li>");
187 if(errors.contains(3)) error += tr("<li>You need to specify a real name for every identity</li>");
188 if(errors.contains(4)) error += tr("<li>You need to specify an ident for every identity</li>");
189 error += tr("</ul>");
190 QMessageBox::warning(this, tr("One or more identities are invalid"), error);
194 void IdentitiesSettingsPage::clientIdentityCreated(IdentityId id) {
195 insertIdentity(new Identity(*Client::identity(id), this));
196 connect(Client::identity(id), SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
199 void IdentitiesSettingsPage::clientIdentityUpdated() {
200 const Identity *clientIdentity = qobject_cast<Identity *>(sender());
201 if(!clientIdentity) {
202 qWarning() << "Invalid identity to update!";
205 if(!identities.contains(clientIdentity->id())) {
206 qWarning() << "Unknown identity to update:" << clientIdentity->identityName();
209 Identity *identity = identities[clientIdentity->id()];
210 if(identity->identityName() != clientIdentity->identityName()) renameIdentity(identity->id(), clientIdentity->identityName());
211 identity->update(*clientIdentity);
212 if(identity->id() == currentId) displayIdentity(identity, true);
215 void IdentitiesSettingsPage::clientIdentityRemoved(IdentityId id) {
216 if(identities.contains(id)) {
217 removeIdentity(identities[id]);
218 changedIdentities.removeAll(id);
219 deletedIdentities.removeAll(id);
223 void IdentitiesSettingsPage::insertIdentity(Identity *identity) {
224 IdentityId id = identity->id();
225 identities[id] = identity;
227 // default identity is always the first one!
228 ui.identityList->insertItem(0, identity->identityName(), id.toInt());
230 QString name = identity->identityName();
231 for(int j = 0; j < ui.identityList->count(); j++) {
232 if((j>0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
233 ui.identityList->insertItem(j, name, id.toInt());
239 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
244 void IdentitiesSettingsPage::renameIdentity(IdentityId id, const QString &newName) {
245 Identity *identity = identities[id];
246 ui.identityList->setItemText(ui.identityList->findData(identity->id().toInt()), newName);
247 identity->setIdentityName(newName);
250 void IdentitiesSettingsPage::removeIdentity(Identity *id) {
251 identities.remove(id->id());
252 ui.identityList->removeItem(ui.identityList->findData(id->id().toInt()));
253 changedIdentities.removeAll(id->id());
254 if(currentId == id->id()) currentId = 0;
259 void IdentitiesSettingsPage::on_identityList_currentIndexChanged(int index) {
261 //ui.identityList->setEditable(false);
264 IdentityId id = ui.identityList->itemData(index).toInt();
265 if(identities.contains(id)) displayIdentity(identities[id]);
266 ui.deleteIdentity->setEnabled(id != 1); // default identity cannot be deleted
267 ui.renameIdentity->setEnabled(id != 1); // ...or renamed
271 void IdentitiesSettingsPage::displayIdentity(Identity *id, bool dontsave) {
272 if(currentId != 0 && !dontsave && identities.contains(currentId)) {
273 saveToIdentity(identities[currentId]);
276 currentId = id->id();
277 ui.realName->setText(id->realName());
278 ui.nicknameList->clear();
279 ui.nicknameList->addItems(id->nicks());
280 //for(int i = 0; i < ui.nicknameList->count(); i++) {
281 // ui.nicknameList->item(i)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
283 if(ui.nicknameList->count()) ui.nicknameList->setCurrentRow(0);
284 ui.awayNick->setText(id->awayNick());
285 ui.awayNickEnabled->setChecked(id->awayNickEnabled());
286 ui.awayReason->setText(id->awayReason());
287 ui.awayReasonEnabled->setChecked(id->awayReasonEnabled());
288 ui.autoAwayEnabled->setChecked(id->autoAwayEnabled());
289 ui.autoAwayTime->setValue(id->autoAwayTime());
290 ui.autoAwayReason->setText(id->autoAwayReason());
291 ui.autoAwayReasonEnabled->setChecked(id->autoAwayReasonEnabled());
292 ui.detachAwayEnabled->setChecked(id->detachAwayEnabled());
293 ui.detachAwayReason->setText(id->detachAwayReason());
294 ui.detachAwayReasonEnabled->setChecked(id->detachAwayReasonEnabled());
295 ui.ident->setText(id->ident());
296 ui.kickReason->setText(id->kickReason());
297 ui.partReason->setText(id->partReason());
298 ui.quitReason->setText(id->quitReason());
302 void IdentitiesSettingsPage::saveToIdentity(Identity *id) {
303 id->setRealName(ui.realName->text());
305 for(int i = 0; i < ui.nicknameList->count(); i++) {
306 nicks << ui.nicknameList->item(i)->text();
309 id->setAwayNick(ui.awayNick->text());
310 id->setAwayNickEnabled(ui.awayNickEnabled->isChecked());
311 id->setAwayReason(ui.awayReason->text());
312 id->setAwayReasonEnabled(ui.awayReasonEnabled->isChecked());
313 id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
314 id->setAutoAwayTime(ui.autoAwayTime->value());
315 id->setAutoAwayReason(ui.autoAwayReason->text());
316 id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
317 id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
318 id->setDetachAwayReason(ui.detachAwayReason->text());
319 id->setDetachAwayReasonEnabled(ui.detachAwayReasonEnabled->isChecked());
320 id->setIdent(ui.ident->text());
321 id->setKickReason(ui.kickReason->text());
322 id->setPartReason(ui.partReason->text());
323 id->setQuitReason(ui.quitReason->text());
326 void IdentitiesSettingsPage::on_addIdentity_clicked() {
327 CreateIdentityDlg dlg(ui.identityList->model(), this);
328 if(dlg.exec() == QDialog::Accepted) {
329 // find a free (negative) ID
331 for(id = 1; id <= identities.count(); id++) {
332 if(!identities.keys().contains(-id.toInt())) break;
335 Identity *newId = new Identity(id, this);
336 if(dlg.duplicateId() != 0) {
338 newId->update(*identities[dlg.duplicateId()]);
341 newId->setIdentityName(dlg.identityName());
342 identities[id] = newId;
343 insertIdentity(newId);
344 ui.identityList->setCurrentIndex(ui.identityList->findData(id.toInt()));
349 void IdentitiesSettingsPage::on_deleteIdentity_clicked() {
350 Identity *id = identities[currentId];
351 int ret = QMessageBox::question(this, tr("Delete Identity?"),
352 tr("Do you really want to delete identity \"%1\"?").arg(id->identityName()),
353 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
354 if(ret != QMessageBox::Yes) return;
355 if(id->id() > 0) deletedIdentities.append(id->id());
360 void IdentitiesSettingsPage::on_renameIdentity_clicked() {
361 QString oldName = identities[currentId]->identityName();
363 QString name = QInputDialog::getText(this, tr("Rename Identity"),
364 tr("Please enter a new name for the identity \"%1\"!").arg(oldName),
365 QLineEdit::Normal, oldName, &ok);
366 if(ok && !name.isEmpty()) {
367 renameIdentity(currentId, name);
372 void IdentitiesSettingsPage::on_addNick_clicked() {
373 QStringList existing;
374 for(int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
375 NickEditDlg dlg(QString(), existing, this);
376 if(dlg.exec() == QDialog::Accepted) {
377 ui.nicknameList->addItem(dlg.nick());
378 ui.nicknameList->setCurrentRow(ui.nicknameList->count()-1);
384 void IdentitiesSettingsPage::on_deleteNick_clicked() {
385 // no confirmation, since a nickname is really nothing hard to recreate
386 if(ui.nicknameList->selectedItems().count()) {
387 delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
388 ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow()+1, ui.nicknameList->count()-1));
394 void IdentitiesSettingsPage::on_renameNick_clicked() {
395 if(!ui.nicknameList->selectedItems().count()) return;
396 QString old = ui.nicknameList->selectedItems()[0]->text();
397 QStringList existing;
398 for(int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
399 NickEditDlg dlg(old, existing, this);
400 if(dlg.exec() == QDialog::Accepted) {
401 ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
406 void IdentitiesSettingsPage::on_nickUp_clicked() {
407 if(!ui.nicknameList->selectedItems().count()) return;
408 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
410 ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
411 ui.nicknameList->setCurrentRow(row-1);
417 void IdentitiesSettingsPage::on_nickDown_clicked() {
418 if(!ui.nicknameList->selectedItems().count()) return;
419 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
420 if(row < ui.nicknameList->count()-1) {
421 ui.nicknameList->insertItem(row+1, ui.nicknameList->takeItem(row));
422 ui.nicknameList->setCurrentRow(row+1);
428 /*****************************************************************************************/
430 CreateIdentityDlg::CreateIdentityDlg(QAbstractItemModel *model, QWidget *parent) : QDialog(parent) {
433 ui.identityList->setModel(model); // now we use the identity list of the main page... Trolltech <3
434 on_identityName_textChanged(""); // disable ok button :)
437 QString CreateIdentityDlg::identityName() const {
438 return ui.identityName->text();
441 IdentityId CreateIdentityDlg::duplicateId() const {
442 if(!ui.duplicateIdentity->isChecked()) return 0;
443 if(ui.identityList->currentIndex() >= 0) {
444 return ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
449 void CreateIdentityDlg::on_identityName_textChanged(const QString &text) {
450 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(text.count());
454 /*********************************************************************************************/
456 SaveIdentitiesDlg::SaveIdentitiesDlg(const QList<Identity *> &toCreate, const QList<Identity *> &toUpdate, const QList<IdentityId> &toRemove, QWidget *parent)
457 : QDialog(parent) { //, toCreate(tocreate), toUpdate(toupdate), toRemove(toremove) {
459 ui.abort->setIcon(SmallIcon("dialog-cancel"));
461 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
464 ui.progressBar->setMaximum(numevents);
465 ui.progressBar->setValue(0);
467 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientEvent()));
468 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientEvent()));
470 foreach(Identity *id, toCreate) {
471 Client::createIdentity(*id);
473 foreach(Identity *id, toUpdate) {
474 const Identity *cid = Client::identity(id->id());
476 qWarning() << "Invalid client identity!";
480 connect(cid, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
481 Client::updateIdentity(id->id(), id->toVariantMap());
483 foreach(IdentityId id, toRemove) {
484 Client::removeIdentity(id);
487 qWarning() << "Sync dialog called without stuff to change!";
492 void SaveIdentitiesDlg::clientEvent() {
493 ui.progressBar->setValue(++rcvevents);
494 if(rcvevents >= numevents) accept();
497 /*************************************************************************************************/
499 NickEditDlg::NickEditDlg(const QString &old, const QStringList &exist, QWidget *parent)
500 : QDialog(parent), oldNick(old), existing(exist) {
503 // define a regexp for valid nicknames
504 // TODO: add max nicklength according to ISUPPORT
505 QString letter = "A-Za-z";
506 QString special = "\x5b-\x60\x7b-\x7d";
507 QRegExp rx(QString("[%1%2][%1%2\\d-]*").arg(letter, special));
508 ui.nickEdit->setValidator(new QRegExpValidator(rx, ui.nickEdit));
511 setWindowTitle(tr("Add Nickname"));
512 on_nickEdit_textChanged(""); // disable ok button
513 } else ui.nickEdit->setText(old);
516 QString NickEditDlg::nick() const {
517 return ui.nickEdit->text();
521 void NickEditDlg::on_nickEdit_textChanged(const QString &text) {
522 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text));