1 /***************************************************************************
2 * Copyright (C) 2005-09 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 "identitiessettingspage.h"
23 #include <QInputDialog>
24 #include <QMessageBox>
27 #include "iconloader.h"
28 #include "signalproxy.h"
30 IdentitiesSettingsPage::IdentitiesSettingsPage(QWidget *parent)
31 : SettingsPage(tr("Misc"), tr("Identities"), parent),
35 ui.renameIdentity->setIcon(BarIcon("edit-rename"));
36 ui.addIdentity->setIcon(BarIcon("list-add-user"));
37 ui.deleteIdentity->setIcon(BarIcon("list-remove-user"));
39 coreConnectionStateChanged(Client::isConnected()); // need a core connection!
40 connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
42 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityCreated(IdentityId)));
43 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
45 connect(ui.identityEditor, SIGNAL(widgetHasChanged()), this, SLOT(widgetHasChanged()));
47 connect(ui.identityEditor, SIGNAL(requestEditSsl()), this, SLOT(continueUnsecured()));
52 //connect(ui.identityList, SIGNAL(editTextChanged(const QString &)), this, SLOT(widgetHasChanged()));
55 void IdentitiesSettingsPage::coreConnectionStateChanged(bool connected) {
56 setEnabled(connected);
59 if(Client::signalProxy()->isSecure()) {
60 ui.identityEditor->setSslState(IdentityEditWidget::AllowSsl);
63 ui.identityEditor->setSslState(IdentityEditWidget::UnsecureSsl);
67 ui.identityEditor->setSslState(IdentityEditWidget::NoSsl);
77 void IdentitiesSettingsPage::continueUnsecured() {
80 QHash<IdentityId, CertIdentity *>::iterator idIter;
81 for(idIter = identities.begin(); idIter != identities.end(); idIter++) {
82 idIter.value()->enableEditSsl();
85 ui.identityEditor->setSslState(IdentityEditWidget::AllowSsl);
89 void IdentitiesSettingsPage::save() {
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()) {
97 CertIdentity *temp = *i;
98 i = identities.erase(i);
99 toCreate.append(temp);
100 ui.identityList->removeItem(ui.identityList->findData(temp->id().toInt()));
102 if(**i != *Client::identity((*i)->id()) || (*i)->isDirty()) {
108 SaveIdentitiesDlg dlg(toCreate, toUpdate, deletedIdentities, this);
109 int ret = dlg.exec();
110 if(ret == QDialog::Rejected) {
111 // canceled -> reload everything to be safe
114 foreach(Identity *id, toCreate) {
117 changedIdentities.clear();
118 deletedIdentities.clear();
119 setChangedState(false);
123 void IdentitiesSettingsPage::load() {
125 foreach(Identity *identity, identities.values()) {
126 identity->deleteLater();
129 deletedIdentities.clear();
130 changedIdentities.clear();
131 ui.identityList->clear();
132 foreach(IdentityId id, Client::identityIds()) {
133 clientIdentityCreated(id);
135 setChangedState(false);
138 void IdentitiesSettingsPage::widgetHasChanged() {
139 bool changed = testHasChanged();
140 if(changed != hasChanged()) setChangedState(changed);
143 bool IdentitiesSettingsPage::testHasChanged() {
144 if(deletedIdentities.count()) return true;
146 return true; // new identity
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);
156 return changedIdentities.count();
160 bool IdentitiesSettingsPage::aboutToSave() {
161 ui.identityEditor->saveToIdentity(identities[currentId]);
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);
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);
180 void IdentitiesSettingsPage::clientIdentityCreated(IdentityId id) {
181 CertIdentity *identity = new CertIdentity(*Client::identity(id), this);
183 identity->enableEditSsl(_editSsl);
185 insertIdentity(identity);
187 connect(identity, SIGNAL(sslSettingsUpdated()), this, SLOT(clientIdentityUpdated()));
189 connect(Client::identity(id), SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
192 void IdentitiesSettingsPage::clientIdentityUpdated() {
193 const Identity *clientIdentity = qobject_cast<Identity *>(sender());
194 if(!clientIdentity) {
195 qWarning() << "Invalid identity to update!";
198 if(!identities.contains(clientIdentity->id())) {
199 qWarning() << "Unknown identity to update:" << clientIdentity->identityName();
203 CertIdentity *identity = identities[clientIdentity->id()];
205 if(identity->identityName() != clientIdentity->identityName())
206 renameIdentity(identity->id(), clientIdentity->identityName());
208 identity->copyFrom(*clientIdentity);
210 if(identity->id() == currentId)
211 ui.identityEditor->displayIdentity(identity);
214 void IdentitiesSettingsPage::clientIdentityRemoved(IdentityId id) {
215 if(identities.contains(id)) {
216 removeIdentity(identities[id]);
217 changedIdentities.removeAll(id);
218 deletedIdentities.removeAll(id);
222 void IdentitiesSettingsPage::insertIdentity(CertIdentity *identity) {
223 IdentityId id = identity->id();
224 identities[id] = identity;
226 // default identity is always the first one!
227 ui.identityList->insertItem(0, identity->identityName(), id.toInt());
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());
238 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
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);
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;
258 void IdentitiesSettingsPage::on_identityList_currentIndexChanged(int index) {
259 CertIdentity *previousIdentity = 0;
260 if(currentId != 0 && identities.contains(currentId))
261 previousIdentity = identities[currentId];
264 //ui.identityList->setEditable(false);
265 ui.identityEditor->displayIdentity(0, previousIdentity);
268 IdentityId id = ui.identityList->itemData(index).toInt();
269 if(identities.contains(id)) {
270 ui.identityEditor->displayIdentity(identities[id], previousIdentity);
273 ui.deleteIdentity->setEnabled(id != 1); // default identity cannot be deleted
274 ui.renameIdentity->setEnabled(id != 1); // ...or renamed
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
283 for(id = 1; id <= identities.count(); id++) {
284 if(!identities.keys().contains(-id.toInt())) break;
287 CertIdentity *newId = new CertIdentity(id, this);
289 newId->enableEditSsl(_editSsl);
291 if(dlg.duplicateId() != 0) {
293 newId->copyFrom(*identities[dlg.duplicateId()]);
296 newId->setIdentityName(dlg.identityName());
297 identities[id] = newId;
298 insertIdentity(newId);
299 ui.identityList->setCurrentIndex(ui.identityList->findData(id.toInt()));
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());
315 void IdentitiesSettingsPage::on_renameIdentity_clicked() {
316 QString oldName = identities[currentId]->identityName();
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);
327 /*****************************************************************************************/
329 CreateIdentityDlg::CreateIdentityDlg(QAbstractItemModel *model, QWidget *parent)
334 ui.identityList->setModel(model); // now we use the identity list of the main page... Trolltech <3
335 on_identityName_textChanged(""); // disable ok button :)
338 QString CreateIdentityDlg::identityName() const {
339 return ui.identityName->text();
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();
350 void CreateIdentityDlg::on_identityName_textChanged(const QString &text) {
351 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(text.count());
355 /*********************************************************************************************/
357 SaveIdentitiesDlg::SaveIdentitiesDlg(const QList<CertIdentity *> &toCreate, const QList<CertIdentity *> &toUpdate, const QList<IdentityId> &toRemove, QWidget *parent)
361 ui.abort->setIcon(SmallIcon("dialog-cancel"));
363 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
366 ui.progressBar->setMaximum(numevents);
367 ui.progressBar->setValue(0);
369 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientEvent()));
370 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientEvent()));
372 foreach(CertIdentity *id, toCreate) {
373 Client::createIdentity(*id);
375 foreach(CertIdentity *id, toUpdate) {
376 const Identity *cid = Client::identity(id->id());
378 qWarning() << "Invalid client identity!";
382 connect(cid, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
383 Client::updateIdentity(id->id(), id->toVariantMap());
385 id->requestUpdateSslSettings();
388 foreach(IdentityId id, toRemove) {
389 Client::removeIdentity(id);
392 qWarning() << "Sync dialog called without stuff to change!";
397 void SaveIdentitiesDlg::clientEvent() {
398 ui.progressBar->setValue(++rcvevents);
399 if(rcvevents >= numevents) accept();
402 /*************************************************************************************************/
404 NickEditDlg::NickEditDlg(const QString &old, const QStringList &exist, QWidget *parent)
405 : QDialog(parent), oldNick(old), existing(exist) {
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));
416 setWindowTitle(tr("Add Nickname"));
417 on_nickEdit_textChanged(""); // disable ok button
418 } else ui.nickEdit->setText(old);
421 QString NickEditDlg::nick() const {
422 return ui.nickEdit->text();
426 void NickEditDlg::on_nickEdit_textChanged(const QString &text) {
427 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text));