1 /***************************************************************************
2 * Copyright (C) 2009 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "coreaccountsettingspage.h"
24 #include "clientsettings.h"
25 #include "coreaccountmodel.h"
26 #include "iconloader.h"
28 CoreAccountSettingsPage::CoreAccountSettingsPage(QWidget *parent)
29 : SettingsPage(tr("Misc"), tr("Core Accounts"), parent),
31 _lastAutoConnectId(0),
36 ui.addAccountButton->setIcon(SmallIcon("list-add"));
37 ui.editAccountButton->setIcon(SmallIcon("document-edit"));
38 ui.deleteAccountButton->setIcon(SmallIcon("edit-delete"));
40 _model = new CoreAccountModel(Client::coreAccountModel(), this);
41 _filteredModel = new FilteredCoreAccountModel(_model, this);
43 ui.accountView->setModel(filteredModel());
44 ui.autoConnectAccount->setModel(filteredModel());
46 connect(filteredModel(), SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), SLOT(rowsAboutToBeRemoved(QModelIndex, int, int)));
47 connect(filteredModel(), SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(rowsInserted(QModelIndex, int, int)));
49 connect(ui.accountView->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(setWidgetStates()));
53 void CoreAccountSettingsPage::setStandAlone(bool standalone) {
54 _standalone = standalone;
57 void CoreAccountSettingsPage::load() {
58 model()->update(Client::coreAccountModel());
61 CoreAccountSettings s;
63 if(Quassel::runMode() != Quassel::Monolithic) {
64 // make sure we don't have selected the internal account as autoconnect account
66 if(s.autoConnectOnStartup() && s.autoConnectToFixedAccount()) {
67 CoreAccount acc = model()->account(s.autoConnectAccount());
69 ui.autoConnectOnStartup->setChecked(false);
72 ui.accountView->setCurrentIndex(filteredModel()->index(0, 0));
73 ui.accountView->selectionModel()->select(filteredModel()->index(0, 0), QItemSelectionModel::Select);
75 QModelIndex idx = filteredModel()->mapFromSource(model()->accountIndex(s.autoConnectAccount()));
76 ui.autoConnectAccount->setCurrentIndex(idx.isValid() ? idx.row() : 0);
80 void CoreAccountSettingsPage::save() {
82 Client::coreAccountModel()->update(model());
83 Client::coreAccountModel()->save();
84 CoreAccountSettings s;
87 QVariant CoreAccountSettingsPage::loadAutoWidgetValue(const QString &widgetName) {
88 if(widgetName == "autoConnectAccount") {
89 CoreAccountSettings s;
90 AccountId id = s.autoConnectAccount();
95 return SettingsPage::loadAutoWidgetValue(widgetName);
98 void CoreAccountSettingsPage::saveAutoWidgetValue(const QString &widgetName, const QVariant &v) {
99 CoreAccountSettings s;
100 if(widgetName == "autoConnectAccount") {
101 AccountId id = filteredModel()->index(ui.autoConnectAccount->currentIndex(), 0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
102 s.setAutoConnectAccount(id);
105 SettingsPage::saveAutoWidgetValue(widgetName, v);
108 // TODO: Qt 4.6 - replace by proper rowsMoved() semantics
109 // NOTE: This is the filtered model
110 void CoreAccountSettingsPage::rowsAboutToBeRemoved(const QModelIndex &index, int start, int end) {
111 _lastAutoConnectId = _lastAccountId = 0;
112 if(index.isValid() || start != end)
115 // the current index is removed, so remember it in case it's reinserted immediately afterwards
116 AccountId id = filteredModel()->index(start, 0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
117 if(start == ui.accountView->currentIndex().row())
119 if(start == ui.autoConnectAccount->currentIndex())
120 _lastAutoConnectId = id;
123 void CoreAccountSettingsPage::rowsInserted(const QModelIndex &index, int start, int end) {
124 if(index.isValid() || start != end)
127 // check if the inserted index was just removed and select it in that case
128 AccountId id = filteredModel()->index(start, 0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
129 if(id == _lastAccountId)
130 ui.accountView->setCurrentIndex(filteredModel()->index(start, 0));
131 if(id == _lastAutoConnectId)
132 ui.autoConnectAccount->setCurrentIndex(start);
133 _lastAccountId = _lastAutoConnectId = 0;
136 AccountId CoreAccountSettingsPage::selectedAccount() const {
137 QModelIndex index = ui.accountView->currentIndex();
140 return index.data(CoreAccountModel::AccountIdRole).value<AccountId>();
143 void CoreAccountSettingsPage::setSelectedAccount(AccountId accId) {
144 QModelIndex index = filteredModel()->mapFromSource(model()->accountIndex(accId));
146 ui.accountView->setCurrentIndex(index);
149 void CoreAccountSettingsPage::on_addAccountButton_clicked() {
150 CoreAccountEditDlg dlg(CoreAccount(), this);
151 if(dlg.exec() == QDialog::Accepted) {
152 AccountId id =model()->createOrUpdateAccount(dlg.account());
153 ui.accountView->setCurrentIndex(filteredModel()->mapFromSource(model()->accountIndex(id)));
158 void CoreAccountSettingsPage::on_editAccountButton_clicked() {
159 QModelIndex idx = ui.accountView->selectionModel()->currentIndex();
166 void CoreAccountSettingsPage::editAccount(const QModelIndex &index) {
170 CoreAccountEditDlg dlg(model()->account(filteredModel()->mapToSource(index)), this);
171 if(dlg.exec() == QDialog::Accepted) {
172 AccountId id = model()->createOrUpdateAccount(dlg.account());
173 ui.accountView->setCurrentIndex(filteredModel()->mapFromSource(model()->accountIndex(id)));
178 void CoreAccountSettingsPage::on_deleteAccountButton_clicked() {
179 if(!ui.accountView->selectionModel()->selectedIndexes().count())
182 AccountId id = ui.accountView->selectionModel()->selectedIndexes().at(0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
184 model()->removeAccount(id);
189 void CoreAccountSettingsPage::on_accountView_doubleClicked(const QModelIndex &index) {
194 emit connectToCore(index.data(CoreAccountModel::AccountIdRole).value<AccountId>());
199 void CoreAccountSettingsPage::setWidgetStates() {
200 AccountId accId = selectedAccount();
201 bool editable = accId.isValid() && accId != model()->internalAccount();
203 ui.editAccountButton->setEnabled(editable);
204 ui.deleteAccountButton->setEnabled(editable);
207 void CoreAccountSettingsPage::widgetHasChanged() {
208 setChangedState(testHasChanged());
212 bool CoreAccountSettingsPage::testHasChanged() {
213 if(!(*model() == *Client::coreAccountModel()))
219 /*****************************************************************************************
221 *****************************************************************************************/
222 CoreAccountEditDlg::CoreAccountEditDlg(const CoreAccount &acct, QWidget *parent)
229 ui.hostName->setText(acct.hostName());
230 ui.port->setValue(acct.port());
231 ui.accountName->setText(acct.accountName());
232 ui.user->setText(acct.user());
233 ui.password->setText(acct.password());
234 ui.rememberPassword->setChecked(acct.storePassword());
235 ui.useProxy->setChecked(acct.useProxy());
236 ui.proxyHostName->setText(acct.proxyHostName());
237 ui.proxyPort->setValue(acct.proxyPort());
238 ui.proxyType->setCurrentIndex(acct.proxyType() == QNetworkProxy::Socks5Proxy ? 0 : 1);
239 ui.proxyUser->setText(acct.proxyUser());
240 ui.proxyPassword->setText(acct.proxyPassword());
242 if(acct.accountId().isValid())
243 setWindowTitle(tr("Edit Core Account"));
245 setWindowTitle(tr("Add Core Account"));
248 CoreAccount CoreAccountEditDlg::account() {
249 _account.setAccountName(ui.accountName->text().trimmed());
250 _account.setHostName(ui.hostName->text().trimmed());
251 _account.setPort(ui.port->value());
252 _account.setUser(ui.user->text().trimmed());
253 _account.setPassword(ui.password->text());
254 _account.setStorePassword(ui.rememberPassword->isChecked());
255 _account.setUseProxy(ui.useProxy->isChecked());
256 _account.setProxyHostName(ui.proxyHostName->text().trimmed());
257 _account.setProxyPort(ui.proxyPort->value());
258 _account.setProxyType(ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy);
259 _account.setProxyUser(ui.proxyUser->text().trimmed());
260 _account.setProxyPassword(ui.proxyPassword->text());
264 void CoreAccountEditDlg::setWidgetStates() {
265 bool ok = !ui.accountName->text().trimmed().isEmpty()
266 && !ui.user->text().trimmed().isEmpty()
267 && !ui.hostName->text().isEmpty();
268 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
271 void CoreAccountEditDlg::on_hostName_textChanged(const QString &text) {
276 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {
281 void CoreAccountEditDlg::on_user_textChanged(const QString &text) {
286 /*****************************************************************************************
287 * FilteredCoreAccountModel
288 *****************************************************************************************/
290 FilteredCoreAccountModel::FilteredCoreAccountModel(CoreAccountModel *model, QObject *parent) : QSortFilterProxyModel(parent) {
291 _internalAccount = model->internalAccount();
292 setSourceModel(model);
295 bool FilteredCoreAccountModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
296 if(Quassel::runMode() == Quassel::Monolithic)
299 if(!_internalAccount.isValid())
302 return _internalAccount != sourceModel()->index(source_row, 0, source_parent).data(CoreAccountModel::AccountIdRole).value<AccountId>();