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