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