Remove the obsolete Icon and IconLoader classes
[quassel.git] / src / qtui / settingspages / coreaccountsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2014 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "coreaccountsettingspage.h"
22
23 #include <QIcon>
24
25 #include "client.h"
26 #include "clientsettings.h"
27 #include "coreaccountmodel.h"
28
29 CoreAccountSettingsPage::CoreAccountSettingsPage(QWidget *parent)
30     : SettingsPage(tr("Remote Cores"), QString(), parent),
31     _lastAccountId(0),
32     _lastAutoConnectId(0),
33     _standalone(false)
34 {
35     ui.setupUi(this);
36     initAutoWidgets();
37     ui.addAccountButton->setIcon(QIcon::fromTheme("list-add"));
38     ui.editAccountButton->setIcon(QIcon::fromTheme("document-edit"));
39     ui.deleteAccountButton->setIcon(QIcon::fromTheme("edit-delete"));
40
41     _model = new CoreAccountModel(Client::coreAccountModel(), this);
42     _filteredModel = new FilteredCoreAccountModel(_model, this);
43
44     ui.accountView->setModel(filteredModel());
45     ui.autoConnectAccount->setModel(filteredModel());
46
47     connect(filteredModel(), SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), SLOT(rowsAboutToBeRemoved(QModelIndex, int, int)));
48     connect(filteredModel(), SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(rowsInserted(QModelIndex, int, int)));
49
50     connect(ui.accountView->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(setWidgetStates()));
51     connect(ui.autoConnectAccount, SIGNAL(currentIndexChanged(int)), SLOT(widgetHasChanged()));
52     setWidgetStates();
53 }
54
55
56 void CoreAccountSettingsPage::setStandAlone(bool standalone)
57 {
58     _standalone = standalone;
59 }
60
61
62 void CoreAccountSettingsPage::load()
63 {
64     model()->update(Client::coreAccountModel());
65     SettingsPage::load();
66
67     CoreAccountSettings s;
68
69     if (Quassel::runMode() != Quassel::Monolithic) {
70         // make sure we don't have selected the internal account as autoconnect account
71
72         if (s.autoConnectOnStartup() && s.autoConnectToFixedAccount()) {
73             CoreAccount acc = model()->account(s.autoConnectAccount());
74             if (acc.isInternal())
75                 ui.autoConnectOnStartup->setChecked(false);
76         }
77     }
78     ui.accountView->setCurrentIndex(filteredModel()->index(0, 0));
79     ui.accountView->selectionModel()->select(filteredModel()->index(0, 0), QItemSelectionModel::Select);
80
81     QModelIndex idx = filteredModel()->mapFromSource(model()->accountIndex(s.autoConnectAccount()));
82     ui.autoConnectAccount->setCurrentIndex(idx.isValid() ? idx.row() : 0);
83     ui.autoConnectAccount->setProperty("storedValue", ui.autoConnectAccount->currentIndex());
84     setWidgetStates();
85 }
86
87
88 void CoreAccountSettingsPage::save()
89 {
90     SettingsPage::save();
91     Client::coreAccountModel()->update(model());
92     Client::coreAccountModel()->save();
93     CoreAccountSettings s;
94     AccountId id = filteredModel()->index(ui.autoConnectAccount->currentIndex(), 0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
95     s.setAutoConnectAccount(id);
96     ui.autoConnectAccount->setProperty("storedValue", ui.autoConnectAccount->currentIndex());
97 }
98
99
100 // TODO: Qt 4.6 - replace by proper rowsMoved() semantics
101 // NOTE: This is the filtered model
102 void CoreAccountSettingsPage::rowsAboutToBeRemoved(const QModelIndex &index, int start, int end)
103 {
104     _lastAutoConnectId = _lastAccountId = 0;
105     if (index.isValid() || start != end)
106         return;
107
108     // the current index is removed, so remember it in case it's reinserted immediately afterwards
109     AccountId id = filteredModel()->index(start, 0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
110     if (start == ui.accountView->currentIndex().row())
111         _lastAccountId = id;
112     if (start == ui.autoConnectAccount->currentIndex())
113         _lastAutoConnectId = id;
114 }
115
116
117 void CoreAccountSettingsPage::rowsInserted(const QModelIndex &index, int start, int end)
118 {
119     if (index.isValid() || start != end)
120         return;
121
122     // check if the inserted index was just removed and select it in that case
123     AccountId id = filteredModel()->index(start, 0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
124     if (id == _lastAccountId)
125         ui.accountView->setCurrentIndex(filteredModel()->index(start, 0));
126     if (id == _lastAutoConnectId)
127         ui.autoConnectAccount->setCurrentIndex(start);
128     _lastAccountId = _lastAutoConnectId = 0;
129 }
130
131
132 AccountId CoreAccountSettingsPage::selectedAccount() const
133 {
134     QModelIndex index = ui.accountView->currentIndex();
135     if (!index.isValid())
136         return 0;
137     return index.data(CoreAccountModel::AccountIdRole).value<AccountId>();
138 }
139
140
141 void CoreAccountSettingsPage::setSelectedAccount(AccountId accId)
142 {
143     QModelIndex index = filteredModel()->mapFromSource(model()->accountIndex(accId));
144     if (index.isValid())
145         ui.accountView->setCurrentIndex(index);
146 }
147
148
149 void CoreAccountSettingsPage::on_addAccountButton_clicked()
150 {
151     CoreAccountEditDlg dlg(CoreAccount(), this);
152     if (dlg.exec() == QDialog::Accepted) {
153         AccountId id = model()->createOrUpdateAccount(dlg.account());
154         ui.accountView->setCurrentIndex(filteredModel()->mapFromSource(model()->accountIndex(id)));
155         widgetHasChanged();
156     }
157 }
158
159
160 void CoreAccountSettingsPage::on_editAccountButton_clicked()
161 {
162     QModelIndex idx = ui.accountView->selectionModel()->currentIndex();
163     if (!idx.isValid())
164         return;
165
166     editAccount(idx);
167 }
168
169
170 void CoreAccountSettingsPage::editAccount(const QModelIndex &index)
171 {
172     if (!index.isValid())
173         return;
174
175     CoreAccountEditDlg dlg(model()->account(filteredModel()->mapToSource(index)), this);
176     if (dlg.exec() == QDialog::Accepted) {
177         AccountId id = model()->createOrUpdateAccount(dlg.account());
178         ui.accountView->setCurrentIndex(filteredModel()->mapFromSource(model()->accountIndex(id)));
179         widgetHasChanged();
180     }
181 }
182
183
184 void CoreAccountSettingsPage::on_deleteAccountButton_clicked()
185 {
186     if (!ui.accountView->selectionModel()->selectedIndexes().count())
187         return;
188
189     AccountId id = ui.accountView->selectionModel()->selectedIndexes().at(0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
190     if (id.isValid()) {
191         model()->removeAccount(id);
192         widgetHasChanged();
193     }
194 }
195
196
197 void CoreAccountSettingsPage::on_accountView_doubleClicked(const QModelIndex &index)
198 {
199     if (!index.isValid())
200         return;
201
202     if (isStandAlone())
203         emit connectToCore(index.data(CoreAccountModel::AccountIdRole).value<AccountId>());
204     else
205         editAccount(index);
206 }
207
208
209 void CoreAccountSettingsPage::setWidgetStates()
210 {
211     AccountId accId = selectedAccount();
212     bool editable = accId.isValid() && accId != model()->internalAccount();
213
214     ui.editAccountButton->setEnabled(editable);
215     ui.deleteAccountButton->setEnabled(editable);
216 }
217
218
219 void CoreAccountSettingsPage::widgetHasChanged()
220 {
221     setChangedState(testHasChanged());
222     setWidgetStates();
223 }
224
225
226 bool CoreAccountSettingsPage::testHasChanged()
227 {
228     if (ui.autoConnectAccount->currentIndex() != ui.autoConnectAccount->property("storedValue").toInt())
229         return true;
230     if (!(*model() == *Client::coreAccountModel()))
231         return true;
232
233     return false;
234 }
235
236
237 /*****************************************************************************************
238  * CoreAccountEditDlg
239  *****************************************************************************************/
240 CoreAccountEditDlg::CoreAccountEditDlg(const CoreAccount &acct, QWidget *parent)
241     : QDialog(parent)
242 {
243     ui.setupUi(this);
244
245     _account = acct;
246
247     ui.hostName->setText(acct.hostName());
248     ui.port->setValue(acct.port());
249     ui.accountName->setText(acct.accountName());
250     ui.user->setText(acct.user());
251     ui.password->setText(acct.password());
252     ui.rememberPassword->setChecked(acct.storePassword());
253     ui.useProxy->setChecked(acct.useProxy());
254     ui.proxyHostName->setText(acct.proxyHostName());
255     ui.proxyPort->setValue(acct.proxyPort());
256     ui.proxyType->setCurrentIndex(acct.proxyType() == QNetworkProxy::Socks5Proxy ? 0 : 1);
257     ui.proxyUser->setText(acct.proxyUser());
258     ui.proxyPassword->setText(acct.proxyPassword());
259
260     if (acct.accountId().isValid())
261         setWindowTitle(tr("Edit Core Account"));
262     else
263         setWindowTitle(tr("Add Core Account"));
264 }
265
266
267 CoreAccount CoreAccountEditDlg::account()
268 {
269     _account.setAccountName(ui.accountName->text().trimmed());
270     _account.setHostName(ui.hostName->text().trimmed());
271     _account.setPort(ui.port->value());
272     _account.setUser(ui.user->text().trimmed());
273     _account.setPassword(ui.password->text());
274     _account.setStorePassword(ui.rememberPassword->isChecked());
275     _account.setUseProxy(ui.useProxy->isChecked());
276     _account.setProxyHostName(ui.proxyHostName->text().trimmed());
277     _account.setProxyPort(ui.proxyPort->value());
278     _account.setProxyType(ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy);
279     _account.setProxyUser(ui.proxyUser->text().trimmed());
280     _account.setProxyPassword(ui.proxyPassword->text());
281     return _account;
282 }
283
284
285 void CoreAccountEditDlg::setWidgetStates()
286 {
287     bool ok = !ui.accountName->text().trimmed().isEmpty()
288               && !ui.user->text().trimmed().isEmpty()
289               && !ui.hostName->text().isEmpty();
290     ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
291 }
292
293
294 void CoreAccountEditDlg::on_hostName_textChanged(const QString &text)
295 {
296     Q_UNUSED(text);
297     setWidgetStates();
298 }
299
300
301 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text)
302 {
303     Q_UNUSED(text);
304     setWidgetStates();
305 }
306
307
308 void CoreAccountEditDlg::on_user_textChanged(const QString &text)
309 {
310     Q_UNUSED(text)
311     setWidgetStates();
312 }
313
314
315 /*****************************************************************************************
316  * FilteredCoreAccountModel
317  *****************************************************************************************/
318
319 FilteredCoreAccountModel::FilteredCoreAccountModel(CoreAccountModel *model, QObject *parent) : QSortFilterProxyModel(parent)
320 {
321     _internalAccount = model->internalAccount();
322     setSourceModel(model);
323 }
324
325
326 bool FilteredCoreAccountModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
327 {
328     if (Quassel::runMode() == Quassel::Monolithic)
329         return true;
330
331     if (!_internalAccount.isValid())
332         return true;
333
334     return _internalAccount != sourceModel()->index(source_row, 0, source_parent).data(CoreAccountModel::AccountIdRole).value<AccountId>();
335 }