1 /***************************************************************************
2 * Copyright (C) 2005-2015 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) version 3. *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "coreaccountsettingspage.h"
26 #include "clientsettings.h"
27 #include "coreaccountmodel.h"
29 CoreAccountSettingsPage::CoreAccountSettingsPage(QWidget *parent)
30 : SettingsPage(tr("Remote Cores"), QString(), parent),
32 _lastAutoConnectId(0),
37 ui.addAccountButton->setIcon(QIcon::fromTheme("list-add"));
38 ui.editAccountButton->setIcon(QIcon::fromTheme("document-edit"));
39 ui.deleteAccountButton->setIcon(QIcon::fromTheme("edit-delete"));
41 _model = new CoreAccountModel(Client::coreAccountModel(), this);
42 _filteredModel = new FilteredCoreAccountModel(_model, this);
44 ui.accountView->setModel(filteredModel());
45 ui.autoConnectAccount->setModel(filteredModel());
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)));
50 connect(ui.accountView->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(setWidgetStates()));
51 connect(ui.autoConnectAccount, SIGNAL(currentIndexChanged(int)), SLOT(widgetHasChanged()));
56 void CoreAccountSettingsPage::setStandAlone(bool standalone)
58 _standalone = standalone;
62 void CoreAccountSettingsPage::load()
64 model()->update(Client::coreAccountModel());
67 CoreAccountSettings s;
69 if (Quassel::runMode() != Quassel::Monolithic) {
70 // make sure we don't have selected the internal account as autoconnect account
72 if (s.autoConnectOnStartup() && s.autoConnectToFixedAccount()) {
73 CoreAccount acc = model()->account(s.autoConnectAccount());
75 ui.autoConnectOnStartup->setChecked(false);
78 ui.accountView->setCurrentIndex(filteredModel()->index(0, 0));
79 ui.accountView->selectionModel()->select(filteredModel()->index(0, 0), QItemSelectionModel::Select);
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());
88 void CoreAccountSettingsPage::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());
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)
104 _lastAutoConnectId = _lastAccountId = 0;
105 if (index.isValid() || start != end)
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())
112 if (start == ui.autoConnectAccount->currentIndex())
113 _lastAutoConnectId = id;
117 void CoreAccountSettingsPage::rowsInserted(const QModelIndex &index, int start, int end)
119 if (index.isValid() || start != end)
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;
132 AccountId CoreAccountSettingsPage::selectedAccount() const
134 QModelIndex index = ui.accountView->currentIndex();
135 if (!index.isValid())
137 return index.data(CoreAccountModel::AccountIdRole).value<AccountId>();
141 void CoreAccountSettingsPage::setSelectedAccount(AccountId accId)
143 QModelIndex index = filteredModel()->mapFromSource(model()->accountIndex(accId));
145 ui.accountView->setCurrentIndex(index);
149 void CoreAccountSettingsPage::on_addAccountButton_clicked()
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)));
160 void CoreAccountSettingsPage::on_editAccountButton_clicked()
162 QModelIndex idx = ui.accountView->selectionModel()->currentIndex();
170 void CoreAccountSettingsPage::editAccount(const QModelIndex &index)
172 if (!index.isValid())
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)));
184 void CoreAccountSettingsPage::on_deleteAccountButton_clicked()
186 if (!ui.accountView->selectionModel()->selectedIndexes().count())
189 AccountId id = ui.accountView->selectionModel()->selectedIndexes().at(0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
191 model()->removeAccount(id);
197 void CoreAccountSettingsPage::on_accountView_doubleClicked(const QModelIndex &index)
199 if (!index.isValid())
203 emit connectToCore(index.data(CoreAccountModel::AccountIdRole).value<AccountId>());
209 void CoreAccountSettingsPage::setWidgetStates()
211 AccountId accId = selectedAccount();
212 bool editable = accId.isValid() && accId != model()->internalAccount();
214 ui.editAccountButton->setEnabled(editable);
215 ui.deleteAccountButton->setEnabled(editable);
219 void CoreAccountSettingsPage::widgetHasChanged()
221 setChangedState(testHasChanged());
226 bool CoreAccountSettingsPage::testHasChanged()
228 if (ui.autoConnectAccount->currentIndex() != ui.autoConnectAccount->property("storedValue").toInt())
230 if (!(*model() == *Client::coreAccountModel()))
237 /*****************************************************************************************
239 *****************************************************************************************/
240 CoreAccountEditDlg::CoreAccountEditDlg(const CoreAccount &acct, QWidget *parent)
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());
260 if (acct.accountId().isValid())
261 setWindowTitle(tr("Edit Core Account"));
263 setWindowTitle(tr("Add Core Account"));
267 CoreAccount CoreAccountEditDlg::account()
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());
285 void CoreAccountEditDlg::setWidgetStates()
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);
294 void CoreAccountEditDlg::on_hostName_textChanged(const QString &text)
301 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text)
308 void CoreAccountEditDlg::on_user_textChanged(const QString &text)
315 /*****************************************************************************************
316 * FilteredCoreAccountModel
317 *****************************************************************************************/
319 FilteredCoreAccountModel::FilteredCoreAccountModel(CoreAccountModel *model, QObject *parent) : QSortFilterProxyModel(parent)
321 _internalAccount = model->internalAccount();
322 setSourceModel(model);
326 bool FilteredCoreAccountModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
328 if (Quassel::runMode() == Quassel::Monolithic)
331 if (!_internalAccount.isValid())
334 return _internalAccount != sourceModel()->index(source_row, 0, source_parent).data(CoreAccountModel::AccountIdRole).value<AccountId>();