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();
133 foreach(IdentityId id, Client::identityIds()) {
134 clientIdentityCreated(id);
136 setChangedState(false);
139 void IdentitiesSettingsPage::widgetHasChanged() {
140 bool changed = testHasChanged();
141 if(changed != hasChanged()) setChangedState(changed);
144 void IdentitiesSettingsPage::setWidgetStates() {
145 bool enabled = (ui.identityList->count() > 0);
146 ui.identityEditor->setEnabled(enabled);
147 ui.renameIdentity->setEnabled(enabled);
148 ui.deleteIdentity->setEnabled(ui.identityList->count() > 1);
151 bool IdentitiesSettingsPage::testHasChanged() {
152 if(deletedIdentities.count()) return true;
154 return true; // new identity
157 changedIdentities.removeAll(currentId);
158 CertIdentity temp(currentId, this);
159 ui.identityEditor->saveToIdentity(&temp);
160 temp.setIdentityName(identities[currentId]->identityName());
161 if(temp != *Client::identity(currentId) || temp.isDirty())
162 changedIdentities.append(currentId);
164 return changedIdentities.count();
168 bool IdentitiesSettingsPage::aboutToSave() {
169 ui.identityEditor->saveToIdentity(identities[currentId]);
171 foreach(Identity *id, identities.values()) {
172 if(id->identityName().isEmpty()) errors.append(1);
173 if(!id->nicks().count()) errors.append(2);
174 if(id->realName().isEmpty()) errors.append(3);
175 if(id->ident().isEmpty()) errors.append(4);
177 if(!errors.count()) return true;
178 QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
179 if(errors.contains(1)) error += tr("<li>All identities need an identity name set</li>");
180 if(errors.contains(2)) error += tr("<li>Every identity needs at least one nickname defined</li>");
181 if(errors.contains(3)) error += tr("<li>You need to specify a real name for every identity</li>");
182 if(errors.contains(4)) error += tr("<li>You need to specify an ident for every identity</li>");
183 error += tr("</ul>");
184 QMessageBox::warning(this, tr("One or more identities are invalid"), error);
188 void IdentitiesSettingsPage::clientIdentityCreated(IdentityId id) {
189 CertIdentity *identity = new CertIdentity(*Client::identity(id), this);
191 identity->enableEditSsl(_editSsl);
193 insertIdentity(identity);
195 connect(identity, SIGNAL(sslSettingsUpdated()), this, SLOT(clientIdentityUpdated()));
197 connect(Client::identity(id), SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
200 void IdentitiesSettingsPage::clientIdentityUpdated() {
201 const Identity *clientIdentity = qobject_cast<Identity *>(sender());
202 if(!clientIdentity) {
203 qWarning() << "Invalid identity to update!";
206 if(!identities.contains(clientIdentity->id())) {
207 qWarning() << "Unknown identity to update:" << clientIdentity->identityName();
211 CertIdentity *identity = identities[clientIdentity->id()];
213 if(identity->identityName() != clientIdentity->identityName())
214 renameIdentity(identity->id(), clientIdentity->identityName());
216 identity->copyFrom(*clientIdentity);
218 if(identity->id() == currentId)
219 ui.identityEditor->displayIdentity(identity);
222 void IdentitiesSettingsPage::clientIdentityRemoved(IdentityId id) {
223 if(identities.contains(id)) {
224 removeIdentity(identities[id]);
225 changedIdentities.removeAll(id);
226 deletedIdentities.removeAll(id);
230 void IdentitiesSettingsPage::insertIdentity(CertIdentity *identity) {
231 IdentityId id = identity->id();
232 identities[id] = identity;
234 QString name = identity->identityName();
235 for(int j = 0; j < ui.identityList->count(); j++) {
236 if((j>0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
237 ui.identityList->insertItem(j, name, id.toInt());
243 ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
248 void IdentitiesSettingsPage::renameIdentity(IdentityId id, const QString &newName) {
249 Identity *identity = identities[id];
250 ui.identityList->setItemText(ui.identityList->findData(identity->id().toInt()), newName);
251 identity->setIdentityName(newName);
254 void IdentitiesSettingsPage::removeIdentity(Identity *id) {
255 identities.remove(id->id());
256 ui.identityList->removeItem(ui.identityList->findData(id->id().toInt()));
257 changedIdentities.removeAll(id->id());
258 if(currentId == id->id()) currentId = 0;
264 void IdentitiesSettingsPage::on_identityList_currentIndexChanged(int index) {
265 CertIdentity *previousIdentity = 0;
266 if(currentId != 0 && identities.contains(currentId))
267 previousIdentity = identities[currentId];
270 //ui.identityList->setEditable(false);
271 ui.identityEditor->displayIdentity(0, previousIdentity);
274 IdentityId id = ui.identityList->itemData(index).toInt();
275 if(identities.contains(id)) {
276 ui.identityEditor->displayIdentity(identities[id], previousIdentity);
282 void IdentitiesSettingsPage::on_addIdentity_clicked() {
283 CreateIdentityDlg dlg(ui.identityList->model(), this);
284 if(dlg.exec() == QDialog::Accepted) {
285 // find a free (negative) ID
287 for(id = 1; id <= identities.count(); id++) {
288 if(!identities.keys().contains(-id.toInt())) break;
291 CertIdentity *newId = new CertIdentity(id, this);
293 newId->enableEditSsl(_editSsl);
295 if(dlg.duplicateId() != 0) {
297 newId->copyFrom(*identities[dlg.duplicateId()]);
300 newId->setIdentityName(dlg.identityName());
301 identities[id] = newId;
302 insertIdentity(newId);
303 ui.identityList->setCurrentIndex(ui.identityList->findData(id.toInt()));
308 void IdentitiesSettingsPage::on_deleteIdentity_clicked() {
309 Identity *id = identities[currentId];
310 int ret = QMessageBox::question(this, tr("Delete Identity?"),
311 tr("Do you really want to delete identity \"%1\"?").arg(id->identityName()),
312 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
313 if(ret != QMessageBox::Yes) return;
314 if(id->id() > 0) deletedIdentities.append(id->id());
319 void IdentitiesSettingsPage::on_renameIdentity_clicked() {
320 QString oldName = identities[currentId]->identityName();
322 QString name = QInputDialog::getText(this, tr("Rename Identity"),
323 tr("Please enter a new name for the identity \"%1\"!").arg(oldName),
324 QLineEdit::Normal, oldName, &ok);
325 if(ok && !name.isEmpty()) {
326 renameIdentity(currentId, name);
331 /*****************************************************************************************/
333 CreateIdentityDlg::CreateIdentityDlg(QAbstractItemModel *model, QWidget *parent)
338 ui.identityList->setModel(model); // now we use the identity list of the main page... Trolltech <3
339 on_identityName_textChanged(""); // disable ok button :)
342 QString CreateIdentityDlg::identityName() const {
343 return ui.identityName->text();
346 IdentityId CreateIdentityDlg::duplicateId() const {
347 if(!ui.duplicateIdentity->isChecked()) return 0;
348 if(ui.identityList->currentIndex() >= 0) {
349 return ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
354 void CreateIdentityDlg::on_identityName_textChanged(const QString &text) {
355 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(text.count());
359 /*********************************************************************************************/
361 SaveIdentitiesDlg::SaveIdentitiesDlg(const QList<CertIdentity *> &toCreate, const QList<CertIdentity *> &toUpdate, const QList<IdentityId> &toRemove, QWidget *parent)
365 ui.abort->setIcon(SmallIcon("dialog-cancel"));
367 numevents = toCreate.count() + toUpdate.count() + toRemove.count();
370 ui.progressBar->setMaximum(numevents);
371 ui.progressBar->setValue(0);
373 connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientEvent()));
374 connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientEvent()));
376 foreach(CertIdentity *id, toCreate) {
377 Client::createIdentity(*id);
379 foreach(CertIdentity *id, toUpdate) {
380 const Identity *cid = Client::identity(id->id());
382 qWarning() << "Invalid client identity!";
386 connect(cid, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
387 Client::updateIdentity(id->id(), id->toVariantMap());
389 id->requestUpdateSslSettings();
392 foreach(IdentityId id, toRemove) {
393 Client::removeIdentity(id);
396 qWarning() << "Sync dialog called without stuff to change!";
401 void SaveIdentitiesDlg::clientEvent() {
402 ui.progressBar->setValue(++rcvevents);
403 if(rcvevents >= numevents) accept();
406 /*************************************************************************************************/
408 NickEditDlg::NickEditDlg(const QString &old, const QStringList &exist, QWidget *parent)
409 : QDialog(parent), oldNick(old), existing(exist) {
412 // define a regexp for valid nicknames
413 // TODO: add max nicklength according to ISUPPORT
414 QString letter = "A-Za-z";
415 QString special = "\x5b-\x60\x7b-\x7d";
416 QRegExp rx(QString("[%1%2][%1%2\\d-]*").arg(letter, special));
417 ui.nickEdit->setValidator(new QRegExpValidator(rx, ui.nickEdit));
420 setWindowTitle(tr("Add Nickname"));
421 on_nickEdit_textChanged(""); // disable ok button
422 } else ui.nickEdit->setText(old);
425 QString NickEditDlg::nick() const {
426 return ui.nickEdit->text();
430 void NickEditDlg::on_nickEdit_textChanged(const QString &text) {
431 ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text));