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()));
77 void IdentitiesSettingsPage::setWidgetStates() {
78 if(ui.nicknameList->selectedItems().count()) {
79 ui.renameNick->setEnabled(true);
80 ui.nickUp->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) > 0);
81 ui.nickDown->setEnabled(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]) < ui.nicknameList->count()-1);
83 ui.renameNick->setDisabled(true);
84 ui.nickUp->setDisabled(true);
85 ui.nickDown->setDisabled(true);
87 ui.deleteNick->setEnabled(ui.nicknameList->count() > 1);
90 void IdentitiesSettingsPage::coreConnectionStateChanged(bool state) {
91 this->setEnabled(state);
100 void IdentitiesSettingsPage::save() {
102 QList<Identity *> toCreate, toUpdate;
103 // we need to remove our temporarily created identities.
104 // these are going to be re-added after the core has propagated them back...
105 QHash<IdentityId, Identity *>::iterator i = identities.begin();
106 while(i != identities.end()) {
109 i = identities.erase(i);
110 toCreate.append(temp);
111 ui.identityList->removeItem(ui.identityList->findData(temp->id().toInt()));
113 if(**i != *Client::identity((*i)->id())) {
119 SaveIdentitiesDlg dlg(toCreate, toUpdate, deletedIdentities, this);
120 int ret = dlg.exec();
121 if(ret == QDialog::Rejected) {
122 // canceled -> reload everything to be safe
125 foreach(Identity *id, toCreate) {
128 changedIdentities.clear();
129 deletedIdentities.clear();
130 setChangedState(false);
134 void IdentitiesSettingsPage::load() {
136 foreach(Identity *identity, identities.values()) {
137 identity->deleteLater();
140 deletedIdentities.clear();
141 changedIdentities.clear();
142 ui.identityList->clear();
143 foreach(IdentityId id, Client::identityIds()) {
144 clientIdentityCreated(id);
146 setChangedState(false);
149 void IdentitiesSettingsPage::widgetHasChanged() {
150 bool changed = testHasChanged();
151 if(changed != hasChanged()) setChangedState(changed);
154 bool IdentitiesSettingsPage::testHasChanged() {
155 if(deletedIdentities.count()) return true;
157 return true; // new identity
160 changedIdentities.removeAll(currentId);
161 Identity temp(currentId, this);
162 saveToIdentity(&temp);
163 temp.setIdentityName(identities[currentId]->identityName());
164 if(temp != *Client::identity(currentId)) changedIdentities.append(currentId);
166 return changedIdentities.count();
170 bool IdentitiesSettingsPage::aboutToSave() {
171 saveToIdentity(identities[currentId]);
173 foreach(Identity *id, identities.values()) {
174 if(id->identityName().isEmpty()) errors.append(1);
175 if(!id->nicks().count()) errors.append(2);
176 if(id->realName().isEmpty()) errors.append(3);
177 if(id->ident().isEmpty()) errors.append(4);
179 if(!errors.count()) return true;
180 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
181 if(errors.contains(1)) error += tr("<li>All identities need an identity name set</li>");
182 if(errors.contains(2)) error += tr("<li>Every identity needs at least one nickname defined</li>");
183 if(errors.contains(3)) error += tr("<li>You need to specify a real name for every identity</li>");
184 if(errors.contains(4)) error += tr("<li>You need to specify an ident for every identity</li>");
185 error += tr("</ul>");
186 QMessageBox::warning(this, tr("One or more identities are invalid"), error);
190 void IdentitiesSettingsPage::clientIdentityCreated(IdentityId id) {
191 insertIdentity(new Identity(*Client::identity(id), this));
192 connect(Client::identity(id), SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
195 void IdentitiesSettingsPage::clientIdentityUpdated() {
196 const Identity *clientIdentity = qobject_cast<Identity *>(sender());
197 if(!clientIdentity) {
198 qWarning() << "Invalid identity to update!";
201 if(!identities.contains(clientIdentity->id())) {
202 qWarning() << "Unknown identity to update:" << clientIdentity->identityName();
205 Identity *identity = identities[clientIdentity->id()];
206 if(identity->identityName() != clientIdentity->identityName()) renameIdentity(identity->id(), clientIdentity->identityName());
207 identity->update(*clientIdentity);
208 if(identity->id() == currentId) displayIdentity(identity, true);
211 void IdentitiesSettingsPage::clientIdentityRemoved(IdentityId id) {
212 if(identities.contains(id)) {
213 removeIdentity(identities[id]);
214 changedIdentities.removeAll(id);
215 deletedIdentities.removeAll(id);
219 void IdentitiesSettingsPage::insertIdentity(Identity *identity) {
220 IdentityId id = identity->id();
221 identities[id] = identity;
223 // default identity is always the first one!
224 ui.identityList->insertItem(0, identity->identityName(), id.toInt());
226 QString name = identity->identityName();
227 for(int j = 0; j < ui.identityList->count(); j++) {
228 if((j>0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
229 ui.identityList->insertItem(j, name, id.toInt());
235 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
240 void IdentitiesSettingsPage::renameIdentity(IdentityId id, const QString &newName) {
241 Identity *identity = identities[id];
242 ui.identityList->setItemText(ui.identityList->findData(identity->id().toInt()), newName);
243 identity->setIdentityName(newName);
246 void IdentitiesSettingsPage::removeIdentity(Identity *id) {
247 identities.remove(id->id());
248 ui.identityList->removeItem(ui.identityList->findData(id->id().toInt()));
249 changedIdentities.removeAll(id->id());
250 if(currentId == id->id()) currentId = 0;
255 void IdentitiesSettingsPage::on_identityList_currentIndexChanged(int index) {
257 //ui.identityList->setEditable(false);
260 IdentityId id = ui.identityList->itemData(index).toInt();
261 if(identities.contains(id)) displayIdentity(identities[id]);
262 ui.deleteIdentity->setEnabled(id != 1); // default identity cannot be deleted
263 ui.renameIdentity->setEnabled(id != 1); // ...or renamed
267 void IdentitiesSettingsPage::displayIdentity(Identity *id, bool dontsave) {
268 if(currentId != 0 && !dontsave && identities.contains(currentId)) {
269 saveToIdentity(identities[currentId]);
272 currentId = id->id();
273 ui.realName->setText(id->realName());
274 ui.nicknameList->clear();
275 ui.nicknameList->addItems(id->nicks());
276 //for(int i = 0; i < ui.nicknameList->count(); i++) {
277 // ui.nicknameList->item(i)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
279 if(ui.nicknameList->count()) ui.nicknameList->setCurrentRow(0);
280 ui.awayNick->setText(id->awayNick());
281 ui.awayNickEnabled->setChecked(id->awayNickEnabled());
282 ui.awayReason->setText(id->awayReason());
283 ui.awayReasonEnabled->setChecked(id->awayReasonEnabled());
284 ui.autoAwayEnabled->setChecked(id->autoAwayEnabled());
285 ui.autoAwayTime->setValue(id->autoAwayTime());
286 ui.autoAwayReason->setText(id->autoAwayReason());
287 ui.autoAwayReasonEnabled->setChecked(id->autoAwayReasonEnabled());
288 ui.detachAwayEnabled->setChecked(id->detachAwayEnabled());
289 ui.detachAwayReason->setText(id->detachAwayReason());
290 ui.detachAwayReasonEnabled->setChecked(id->detachAwayReasonEnabled());
291 ui.ident->setText(id->ident());
292 ui.kickReason->setText(id->kickReason());
293 ui.partReason->setText(id->partReason());
294 ui.quitReason->setText(id->quitReason());
298 void IdentitiesSettingsPage::saveToIdentity(Identity *id) {
299 id->setRealName(ui.realName->text());
301 for(int i = 0; i < ui.nicknameList->count(); i++) {
302 nicks << ui.nicknameList->item(i)->text();
305 id->setAwayNick(ui.awayNick->text());
306 id->setAwayNickEnabled(ui.awayNickEnabled->isChecked());
307 id->setAwayReason(ui.awayReason->text());
308 id->setAwayReasonEnabled(ui.awayReasonEnabled->isChecked());
309 id->setAutoAwayEnabled(ui.autoAwayEnabled->isChecked());
310 id->setAutoAwayTime(ui.autoAwayTime->value());
311 id->setAutoAwayReason(ui.autoAwayReason->text());
312 id->setAutoAwayReasonEnabled(ui.autoAwayReasonEnabled->isChecked());
313 id->setDetachAwayEnabled(ui.detachAwayEnabled->isChecked());
314 id->setDetachAwayReason(ui.detachAwayReason->text());
315 id->setDetachAwayReasonEnabled(ui.detachAwayReasonEnabled->isChecked());
316 id->setIdent(ui.ident->text());
317 id->setKickReason(ui.kickReason->text());
318 id->setPartReason(ui.partReason->text());
319 id->setQuitReason(ui.quitReason->text());
322 void IdentitiesSettingsPage::on_addIdentity_clicked() {
323 CreateIdentityDlg dlg(ui.identityList->model(), this);
324 if(dlg.exec() == QDialog::Accepted) {
325 // find a free (negative) ID
327 for(id = 1; id <= identities.count(); id++) {
328 if(!identities.keys().contains(-id.toInt())) break;
331 Identity *newId = new Identity(id, this);
332 if(dlg.duplicateId() != 0) {
334 newId->update(*identities[dlg.duplicateId()]);
337 newId->setIdentityName(dlg.identityName());
338 identities[id] = newId;
339 insertIdentity(newId);
340 ui.identityList->setCurrentIndex(ui.identityList->findData(id.toInt()));
345 void IdentitiesSettingsPage::on_deleteIdentity_clicked() {
346 Identity *id = identities[currentId];
347 int ret = QMessageBox::question(this, tr("Delete Identity?"),
348 tr("Do you really want to delete identity \"%1\"?").arg(id->identityName()),
349 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
350 if(ret != QMessageBox::Yes) return;
351 if(id->id() > 0) deletedIdentities.append(id->id());
356 void IdentitiesSettingsPage::on_renameIdentity_clicked() {
357 QString oldName = identities[currentId]->identityName();
359 QString name = QInputDialog::getText(this, tr("Rename Identity"),
360 tr("Please enter a new name for the identity \"%1\"!").arg(oldName),
361 QLineEdit::Normal, oldName, &ok);
362 if(ok && !name.isEmpty()) {
363 renameIdentity(currentId, name);
368 void IdentitiesSettingsPage::on_addNick_clicked() {
369 QStringList existing;
370 for(int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
371 NickEditDlg dlg(QString(), existing, this);
372 if(dlg.exec() == QDialog::Accepted) {
373 ui.nicknameList->addItem(dlg.nick());
374 ui.nicknameList->setCurrentRow(ui.nicknameList->count()-1);
380 void IdentitiesSettingsPage::on_deleteNick_clicked() {
381 // no confirmation, since a nickname is really nothing hard to recreate
382 if(ui.nicknameList->selectedItems().count()) {
383 delete ui.nicknameList->takeItem(ui.nicknameList->row(ui.nicknameList->selectedItems()[0]));
384 ui.nicknameList->setCurrentRow(qMin(ui.nicknameList->currentRow()+1, ui.nicknameList->count()-1));
390 void IdentitiesSettingsPage::on_renameNick_clicked() {
391 if(!ui.nicknameList->selectedItems().count()) return;
392 QString old = ui.nicknameList->selectedItems()[0]->text();
393 QStringList existing;
394 for(int i = 0; i < ui.nicknameList->count(); i++) existing << ui.nicknameList->item(i)->text();
395 NickEditDlg dlg(old, existing, this);
396 if(dlg.exec() == QDialog::Accepted) {
397 ui.nicknameList->selectedItems()[0]->setText(dlg.nick());
402 void IdentitiesSettingsPage::on_nickUp_clicked() {
403 if(!ui.nicknameList->selectedItems().count()) return;
404 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
406 ui.nicknameList->insertItem(row-1, ui.nicknameList->takeItem(row));
407 ui.nicknameList->setCurrentRow(row-1);
413 void IdentitiesSettingsPage::on_nickDown_clicked() {
414 if(!ui.nicknameList->selectedItems().count()) return;
415 int row = ui.nicknameList->row(ui.nicknameList->selectedItems()[0]);
416 if(row < ui.nicknameList->count()-1) {
417 ui.nicknameList->insertItem(row+1, ui.nicknameList->takeItem(row));
418 ui.nicknameList->setCurrentRow(row+1);
424 /*****************************************************************************************/
426 CreateIdentityDlg::CreateIdentityDlg(QAbstractItemModel *model, QWidget *parent) : QDialog(parent) {
429 ui.identityList->setModel(model); // now we use the identity list of the main page... Trolltech <3
430 on_identityName_textChanged(""); // disable ok button :)
433 QString CreateIdentityDlg::identityName() const {
434 return ui.identityName->text();
437 IdentityId CreateIdentityDlg::duplicateId() const {
438 if(!ui.duplicateIdentity->isChecked()) return 0;
439 if(ui.identityList->currentIndex() >= 0) {
440 return ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
445 void CreateIdentityDlg::on_identityName_textChanged(const QString &text) {
446 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(text.count());
450 /*********************************************************************************************/
452 SaveIdentitiesDlg::SaveIdentitiesDlg(const QList<Identity *> &toCreate, const QList<Identity *> &toUpdate, const QList<IdentityId> &toRemove, QWidget *parent)
453 : QDialog(parent) { //, toCreate(tocreate), toUpdate(toupdate), toRemove(toremove) {
455 ui.abort->setIcon(SmallIcon("dialog-cancel"));
457 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
460 ui.progressBar->setMaximum(numevents);
461 ui.progressBar->setValue(0);
463 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientEvent()));
464 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientEvent()));
466 foreach(Identity *id, toCreate) {
467 Client::createIdentity(*id);
469 foreach(Identity *id, toUpdate) {
470 const Identity *cid = Client::identity(id->id());
472 qWarning() << "Invalid client identity!";
476 connect(cid, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
477 Client::updateIdentity(id->id(), id->toVariantMap());
479 foreach(IdentityId id, toRemove) {
480 Client::removeIdentity(id);
483 qWarning() << "Sync dialog called without stuff to change!";
488 void SaveIdentitiesDlg::clientEvent() {
489 ui.progressBar->setValue(++rcvevents);
490 if(rcvevents >= numevents) accept();
493 /*************************************************************************************************/
495 NickEditDlg::NickEditDlg(const QString &old, const QStringList &exist, QWidget *parent)
496 : QDialog(parent), oldNick(old), existing(exist) {
499 // define a regexp for valid nicknames
500 // TODO: add max nicklength according to ISUPPORT
501 QString letter = "A-Za-z";
502 QString special = "\x5b-\x60\x7b-\x7d";
503 QRegExp rx(QString("[%1%2][%1%2\\d-]*").arg(letter, special));
504 ui.nickEdit->setValidator(new QRegExpValidator(rx, ui.nickEdit));
507 setWindowTitle(tr("Add Nickname"));
508 on_nickEdit_textChanged(""); // disable ok button
509 } else ui.nickEdit->setText(old);
512 QString NickEditDlg::nick() const {
513 return ui.nickEdit->text();
517 void NickEditDlg::on_nickEdit_textChanged(const QString &text) {
518 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text));