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),
35 ui.addAccountButton->setIcon(SmallIcon("list-add"));
36 ui.editAccountButton->setIcon(SmallIcon("document-edit"));
37 ui.deleteAccountButton->setIcon(SmallIcon("edit-delete"));
39 _model = new CoreAccountModel(Client::coreAccountModel(), this);
40 ui.accountView->setModel(_model);
41 ui.autoConnectAccount->setModel(_model);
43 connect(model(), SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), SLOT(rowsAboutToBeRemoved(QModelIndex, int, int)));
44 connect(model(), SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(rowsInserted(QModelIndex, int, int)));
46 connect(ui.accountView->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(setWidgetStates()));
50 void CoreAccountSettingsPage::load() {
51 _model->update(Client::coreAccountModel());
54 ui.accountView->setCurrentIndex(model()->index(0, 0));
55 ui.accountView->selectionModel()->select(model()->index(0, 0), QItemSelectionModel::Select);
59 void CoreAccountSettingsPage::save() {
61 Client::coreAccountModel()->update(_model);
62 Client::coreAccountModel()->save();
63 CoreAccountSettings s;
66 QVariant CoreAccountSettingsPage::loadAutoWidgetValue(const QString &widgetName) {
67 if(widgetName == "autoConnectAccount") {
68 CoreAccountSettings s;
69 AccountId id = s.autoConnectAccount();
72 ui.autoConnectAccount->setCurrentIndex(model()->accountIndex(id).row());
75 return SettingsPage::loadAutoWidgetValue(widgetName);
78 void CoreAccountSettingsPage::saveAutoWidgetValue(const QString &widgetName, const QVariant &v) {
79 CoreAccountSettings s;
80 if(widgetName == "autoConnectAccount") {
81 AccountId id = _model->index(ui.autoConnectAccount->currentIndex(), 0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
82 s.setAutoConnectAccount(id);
85 SettingsPage::saveAutoWidgetValue(widgetName, v);
88 // TODO: Qt 4.6 - replace by proper rowsMoved() semantics
89 void CoreAccountSettingsPage::rowsAboutToBeRemoved(const QModelIndex &index, int start, int end) {
90 _lastAutoConnectId = _lastAccountId = 0;
91 if(index.isValid() || start != end)
94 // the current index is removed, so remember it in case it's reinserted immediately afterwards
95 AccountId id = model()->index(start, 0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
96 if(start == ui.accountView->currentIndex().row())
98 if(start == ui.autoConnectAccount->currentIndex())
99 _lastAutoConnectId = id;
102 void CoreAccountSettingsPage::rowsInserted(const QModelIndex &index, int start, int end) {
103 if(index.isValid() || start != end)
106 // check if the inserted index was just removed and select it in that case
107 AccountId id = model()->index(start, 0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
108 if(id == _lastAccountId)
109 ui.accountView->setCurrentIndex(model()->index(start, 0));
110 if(id == _lastAutoConnectId)
111 ui.autoConnectAccount->setCurrentIndex(start);
112 _lastAccountId = _lastAutoConnectId = 0;
115 void CoreAccountSettingsPage::on_addAccountButton_clicked() {
116 CoreAccountEditDlg dlg(CoreAccount(), this);
117 if(dlg.exec() == QDialog::Accepted) {
118 AccountId id =_model->createOrUpdateAccount(dlg.account());
119 ui.accountView->setCurrentIndex(model()->accountIndex(id));
124 void CoreAccountSettingsPage::on_editAccountButton_clicked() {
125 QModelIndex idx = ui.accountView->selectionModel()->currentIndex();
129 CoreAccountEditDlg dlg(_model->account(idx), this);
130 if(dlg.exec() == QDialog::Accepted) {
131 AccountId id = _model->createOrUpdateAccount(dlg.account());
132 ui.accountView->setCurrentIndex(model()->accountIndex(id));
137 void CoreAccountSettingsPage::on_deleteAccountButton_clicked() {
138 if(!ui.accountView->selectionModel()->selectedIndexes().count())
140 AccountId id = ui.accountView->selectionModel()->selectedIndexes().at(0).data(CoreAccountModel::AccountIdRole).value<AccountId>();
142 model()->removeAccount(id);
147 void CoreAccountSettingsPage::setWidgetStates() {
148 bool selected = ui.accountView->selectionModel()->selectedIndexes().count();
150 ui.editAccountButton->setEnabled(selected);
151 ui.deleteAccountButton->setEnabled(selected);
154 void CoreAccountSettingsPage::widgetHasChanged() {
155 setChangedState(testHasChanged());
159 bool CoreAccountSettingsPage::testHasChanged() {
160 if(!(*model() == *Client::coreAccountModel()))
166 /*****************************************************************************************
168 *****************************************************************************************/
169 CoreAccountEditDlg::CoreAccountEditDlg(const CoreAccount &acct, QWidget *parent)
176 ui.hostName->setText(acct.hostName());
177 ui.port->setValue(acct.port());
178 ui.accountName->setText(acct.accountName());
179 ui.user->setText(acct.user());
180 ui.password->setText(acct.password());
181 ui.rememberPassword->setChecked(acct.storePassword());
182 ui.useProxy->setChecked(acct.useProxy());
183 ui.proxyHostName->setText(acct.proxyHostName());
184 ui.proxyPort->setValue(acct.proxyPort());
185 ui.proxyType->setCurrentIndex(acct.proxyType() == QNetworkProxy::Socks5Proxy ? 0 : 1);
186 ui.proxyUser->setText(acct.proxyUser());
187 ui.proxyPassword->setText(acct.proxyPassword());
189 if(acct.accountId().isValid())
190 setWindowTitle(tr("Edit Core Account"));
192 setWindowTitle(tr("Add Core Account"));
195 CoreAccount CoreAccountEditDlg::account() {
196 _account.setAccountName(ui.accountName->text().trimmed());
197 _account.setHostName(ui.hostName->text().trimmed());
198 _account.setPort(ui.port->value());
199 _account.setUser(ui.user->text().trimmed());
200 _account.setPassword(ui.password->text());
201 _account.setStorePassword(ui.rememberPassword->isChecked());
202 _account.setUseProxy(ui.useProxy->isChecked());
203 _account.setProxyHostName(ui.proxyHostName->text().trimmed());
204 _account.setProxyPort(ui.proxyPort->value());
205 _account.setProxyType(ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy);
206 _account.setProxyUser(ui.proxyUser->text().trimmed());
207 _account.setProxyPassword(ui.proxyPassword->text());
211 void CoreAccountEditDlg::setWidgetStates() {
212 bool ok = !ui.accountName->text().trimmed().isEmpty()
213 && !ui.user->text().trimmed().isEmpty()
214 && !ui.hostName->text().isEmpty();
215 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
218 void CoreAccountEditDlg::on_hostName_textChanged(const QString &text) {
223 void CoreAccountEditDlg::on_accountName_textChanged(const QString &text) {
228 void CoreAccountEditDlg::on_user_textChanged(const QString &text) {