Introduce settingspage for editing core accounts
[quassel.git] / src / qtui / settingspages / coreaccountsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2009 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) version 3.                                           *
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 "coreaccountsettingspage.h"
22
23 #include "client.h"
24 #include "clientsettings.h"
25 #include "coreaccountmodel.h"
26 #include "iconloader.h"
27
28 CoreAccountSettingsPage::CoreAccountSettingsPage(QWidget *parent)
29 : SettingsPage(tr("Misc"), tr("Core Accounts"), parent),
30 _lastAccountId(0),
31 _lastAutoConnectId(0)
32 {
33   ui.setupUi(this);
34   initAutoWidgets();
35   ui.addAccountButton->setIcon(SmallIcon("list-add"));
36   ui.editAccountButton->setIcon(SmallIcon("document-edit"));
37   ui.deleteAccountButton->setIcon(SmallIcon("edit-delete"));
38
39   _model = new CoreAccountModel(Client::coreAccountModel(), this);
40   ui.accountView->setModel(_model);
41   ui.autoConnectAccount->setModel(_model);
42
43   connect(model(), SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), SLOT(rowsAboutToBeRemoved(QModelIndex, int, int)));
44   connect(model(), SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(rowsInserted(QModelIndex, int, int)));
45
46   connect(ui.accountView->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(setWidgetStates()));
47   setWidgetStates();
48 }
49
50 void CoreAccountSettingsPage::load() {
51   _model->update(Client::coreAccountModel());
52
53   SettingsPage::load();
54   ui.accountView->setCurrentIndex(model()->index(0, 0));
55   ui.accountView->selectionModel()->select(model()->index(0, 0), QItemSelectionModel::Select);
56   setWidgetStates();
57 }
58
59 void CoreAccountSettingsPage::save() {
60   SettingsPage::save();
61   Client::coreAccountModel()->update(_model);
62   Client::coreAccountModel()->save();
63   CoreAccountSettings s;
64 }
65
66 QVariant CoreAccountSettingsPage::loadAutoWidgetValue(const QString &widgetName) {
67   if(widgetName == "autoConnectAccount") {
68     CoreAccountSettings s;
69     AccountId id = s.autoConnectAccount();
70     if(id <= 0)
71       return QVariant();
72     ui.autoConnectAccount->setCurrentIndex(model()->accountIndex(id).row());
73     return id.toInt();
74   }
75   return SettingsPage::loadAutoWidgetValue(widgetName);
76 }
77
78 void CoreAccountSettingsPage::saveAutoWidgetValue(const QString &widgetName, const QVariant &v) {
79   CoreAccountSettings s;
80   if(widgetName == "autoConnectAccount") {
81     AccountId id = _model->index(ui.autoConnectAccount->currentIndex(), 0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
82     s.setAutoConnectAccount(id);
83     return;
84   }
85   SettingsPage::saveAutoWidgetValue(widgetName, v);
86 }
87
88 // TODO: Qt 4.6 - replace by proper rowsMoved() semantics
89 void CoreAccountSettingsPage::rowsAboutToBeRemoved(const QModelIndex &index, int start, int end) {
90   _lastAutoConnectId = _lastAccountId = 0;
91   if(index.isValid() || start != end)
92     return;
93
94   // the current index is removed, so remember it in case it's reinserted immediately afterwards
95   AccountId id = model()->index(start, 0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
96   if(start == ui.accountView->currentIndex().row())
97     _lastAccountId = id;
98   if(start == ui.autoConnectAccount->currentIndex())
99     _lastAutoConnectId = id;
100 }
101
102 void CoreAccountSettingsPage::rowsInserted(const QModelIndex &index, int start, int end) {
103   if(index.isValid() || start != end)
104     return;
105
106   // check if the inserted index was just removed and select it in that case
107   AccountId id = model()->index(start, 0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
108   if(id == _lastAccountId)
109     ui.accountView->setCurrentIndex(model()->index(start, 0));
110   if(id == _lastAutoConnectId)
111     ui.autoConnectAccount->setCurrentIndex(start);
112   _lastAccountId = _lastAutoConnectId = 0;
113 }
114
115 void CoreAccountSettingsPage::on_addAccountButton_clicked() {
116   CoreAccountEditDlg dlg(CoreAccount(), this);
117   if(dlg.exec() == QDialog::Accepted) {
118     AccountId id =_model->createOrUpdateAccount(dlg.account());
119     ui.accountView->setCurrentIndex(model()->accountIndex(id));
120     widgetHasChanged();
121   }
122 }
123
124 void CoreAccountSettingsPage::on_editAccountButton_clicked() {
125   QModelIndex idx = ui.accountView->selectionModel()->currentIndex();
126   if(!idx.isValid())
127     return;
128
129   CoreAccountEditDlg dlg(_model->account(idx), this);
130   if(dlg.exec() == QDialog::Accepted) {
131     AccountId id = _model->createOrUpdateAccount(dlg.account());
132     ui.accountView->setCurrentIndex(model()->accountIndex(id));
133     widgetHasChanged();
134   }
135 }
136
137 void CoreAccountSettingsPage::on_deleteAccountButton_clicked() {
138   if(!ui.accountView->selectionModel()->selectedIndexes().count())
139     return;
140   AccountId id = ui.accountView->selectionModel()->selectedIndexes().at(0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
141   if(id.isValid()) {
142     model()->removeAccount(id);
143     widgetHasChanged();
144   }
145 }
146
147 void CoreAccountSettingsPage::setWidgetStates() {
148   bool selected = ui.accountView->selectionModel()->selectedIndexes().count();
149
150   ui.editAccountButton->setEnabled(selected);
151   ui.deleteAccountButton->setEnabled(selected);
152 }
153
154 void CoreAccountSettingsPage::widgetHasChanged() {
155   setChangedState(testHasChanged());
156   setWidgetStates();
157 }
158
159 bool CoreAccountSettingsPage::testHasChanged() {
160   if(!(*model() == *Client::coreAccountModel()))
161     return true;
162
163   return false;
164 }
165
166 /*****************************************************************************************
167  * CoreAccountEditDlg
168  *****************************************************************************************/
169 CoreAccountEditDlg::CoreAccountEditDlg(const CoreAccount &acct, QWidget *parent)
170   : QDialog(parent)
171 {
172   ui.setupUi(this);
173
174   _account = acct;
175
176   ui.hostName->setText(acct.hostName());
177   ui.port->setValue(acct.port());
178   ui.accountName->setText(acct.accountName());
179   ui.user->setText(acct.user());
180   ui.password->setText(acct.password());
181   ui.rememberPassword->setChecked(acct.storePassword());
182   ui.useProxy->setChecked(acct.useProxy());
183   ui.proxyHostName->setText(acct.proxyHostName());
184   ui.proxyPort->setValue(acct.proxyPort());
185   ui.proxyType->setCurrentIndex(acct.proxyType() == QNetworkProxy::Socks5Proxy ? 0 : 1);
186   ui.proxyUser->setText(acct.proxyUser());
187   ui.proxyPassword->setText(acct.proxyPassword());
188
189   if(acct.accountId().isValid())
190     setWindowTitle(tr("Edit Core Account"));
191   else
192     setWindowTitle(tr("Add Core Account"));
193 }
194
195 CoreAccount CoreAccountEditDlg::account() {
196   _account.setAccountName(ui.accountName->text().trimmed());
197   _account.setHostName(ui.hostName->text().trimmed());
198   _account.setPort(ui.port->value());
199   _account.setUser(ui.user->text().trimmed());
200   _account.setPassword(ui.password->text());
201   _account.setStorePassword(ui.rememberPassword->isChecked());
202   _account.setUseProxy(ui.useProxy->isChecked());
203   _account.setProxyHostName(ui.proxyHostName->text().trimmed());
204   _account.setProxyPort(ui.proxyPort->value());
205   _account.setProxyType(ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy);
206   _account.setProxyUser(ui.proxyUser->text().trimmed());
207   _account.setProxyPassword(ui.proxyPassword->text());
208   return _account;
209 }
210
211 void CoreAccountEditDlg::setWidgetStates() {
212   bool ok = !ui.accountName->text().trimmed().isEmpty()
213             && !ui.user->text().trimmed().isEmpty()
214             && !ui.hostName->text().isEmpty();
215   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
216 }
217
218 void CoreAccountEditDlg::on_hostName_textChanged(const QString &text) {
219   Q_UNUSED(text);
220   setWidgetStates();
221 }
222
223 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {
224   Q_UNUSED(text);
225   setWidgetStates();
226 }
227
228 void CoreAccountEditDlg::on_user_textChanged(const QString &text) {
229   Q_UNUSED(text)
230   setWidgetStates();
231 }