3
* Copyright (C) 2006 Remko Troncon
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
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 library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
#include <QInputDialog>
23
#include <QMessageBox>
25
#include "privacydlg.h"
26
#include "psiaccount.h"
27
#include "privacylist.h"
28
#include "privacymanager.h"
29
#include "privacylistmodel.h"
31
PrivacyDlg::PrivacyDlg(PsiAccount* acc, QWidget* parent) : QDialog(parent), acc_(acc)
34
setAttribute(Qt::WA_DeleteOnClose);
35
setWindowTitle(tr("%1: Privacy Lists").arg(acc->name()));
37
PrivacyManager* manager = acc->privacyManager();
38
connect(manager,SIGNAL(listsReceived(const QString&, const QString&, const QStringList&)),SLOT(updateLists(const QString&, const QString&, const QStringList&)));
39
connect(manager,SIGNAL(listReceived(const PrivacyList&)),SLOT(refreshList(const PrivacyList&)));
40
connect(manager,SIGNAL(listError()),SLOT(list_failed()));
41
//connect(manager,SIGNAL(listNamesError()),SLOT(listNamesError()));
42
//connect(manager,SIGNAL(listReceiveError()),SLOT(listReceiveError()));
44
connect(ui_.cb_active,SIGNAL(activated(int)),SLOT(active_selected(int)));
45
connect(ui_.cb_default,SIGNAL(activated(int)),SLOT(default_selected(int)));
46
connect(ui_.cb_lists,SIGNAL(activated(int)),SLOT(list_selected(int)));
47
connect(ui_.cb_lists,SIGNAL(currentIndexChanged(int)),SLOT(list_changed(int)));
48
connect(manager,SIGNAL(changeActiveList_success()),SLOT(change_succeeded()));
49
connect(manager,SIGNAL(changeActiveList_error()),SLOT(change_failed()));
50
connect(manager,SIGNAL(changeDefaultList_success()),SLOT(change_succeeded()));
51
connect(manager,SIGNAL(changeDefaultList_error()),SLOT(change_failed()));
52
connect(manager,SIGNAL(changeList_success()),SLOT(changeList_succeeded()));
53
connect(manager,SIGNAL(changeList_error()),SLOT(changeList_failed()));
55
connect(ui_.pb_newList,SIGNAL(clicked()),SLOT(newList()));
56
connect(ui_.pb_deleteList,SIGNAL(clicked()),SLOT(removeList()));
58
connect(ui_.pb_add,SIGNAL(clicked()),SLOT(addRule()));
59
connect(ui_.pb_edit,SIGNAL(clicked()),SLOT(editCurrentRule()));
60
connect(ui_.pb_remove,SIGNAL(clicked()),SLOT(removeCurrentRule()));
61
connect(ui_.pb_up,SIGNAL(clicked()),SLOT(moveCurrentRuleUp()));
62
connect(ui_.pb_down,SIGNAL(clicked()),SLOT(moveCurrentRuleDown()));
63
connect(ui_.pb_apply,SIGNAL(clicked()),SLOT(applyList()));
65
connect(ui_.pb_close,SIGNAL(clicked()),SLOT(close()));
66
setWidgetsEnabled(false);
68
// Disable all buttons
69
ui_.pb_deleteList->setEnabled(false);
70
setEditRuleEnabled(false);
71
ui_.pb_add->setEnabled(false);
72
ui_.pb_apply->setEnabled(false);
74
// FIXME: Temporarily disabling auto-activate
75
ui_.ck_autoActivate->hide();
77
manager->requestListNames();
80
void PrivacyDlg::setWidgetsEnabled(bool b)
82
ui_.gb_settings->setEnabled(b);
83
ui_.gb_listSettings->setEnabled(b);
86
void PrivacyDlg::setEditRuleEnabled(bool b)
88
ui_.pb_up->setEnabled(b);
89
ui_.pb_down->setEnabled(b);
90
ui_.pb_edit->setEnabled(b);
91
ui_.pb_remove->setEnabled(b);
94
void PrivacyDlg::addRule()
99
void PrivacyDlg::editCurrentRule()
101
// Maybe should use MVC setData here
102
model_.edit(ui_.lv_rules->currentIndex());
105
void PrivacyDlg::removeCurrentRule()
107
if (ui_.lv_rules->currentIndex().isValid()) {
108
model_.removeRow(ui_.lv_rules->currentIndex().row(),ui_.lv_rules->currentIndex().parent());
112
void PrivacyDlg::moveCurrentRuleUp()
114
int row = ui_.lv_rules->currentIndex().row();
115
if (model_.moveUp(ui_.lv_rules->currentIndex())) {
116
ui_.lv_rules->setCurrentIndex(model_.index(row-1,0));
120
void PrivacyDlg::moveCurrentRuleDown()
122
int row = ui_.lv_rules->currentIndex().row();
123
if (model_.moveDown(ui_.lv_rules->currentIndex())) {
124
ui_.lv_rules->setCurrentIndex(model_.index(row+1,0));
128
void PrivacyDlg::applyList()
130
if (!model_.list().isEmpty()) {
131
setWidgetsEnabled(false);
132
acc_->privacyManager()->changeList(model_.list());
134
acc_->privacyManager()->requestListNames();
138
void PrivacyDlg::close()
143
void PrivacyDlg::updateLists(const QString& defaultList, const QString& activeList, const QStringList& names)
146
ui_.cb_active->clear();
147
ui_.cb_active->insertItem(tr("<None>"));
148
ui_.cb_active->insertStringList(names);
149
if (!activeList.isEmpty()) {
150
ui_.cb_active->setCurrentItem(names.findIndex(activeList)+1);
153
ui_.cb_active->setCurrentItem(0);
155
previousActive_ = ui_.cb_active->currentItem();
158
ui_.cb_default->clear();
159
ui_.cb_default->insertItem(tr("<None>"));
160
ui_.cb_default->insertStringList(names);
161
if (!defaultList.isEmpty()) {
162
ui_.cb_default->setCurrentItem(names.findIndex(defaultList)+1);
165
ui_.cb_default->setCurrentItem(0);
167
previousDefault_ = ui_.cb_default->currentItem();
170
QString previousList = ui_.cb_lists->currentText();
171
ui_.cb_lists->clear();
172
ui_.cb_lists->insertStringList(names);
173
if (ui_.cb_lists->count() > 0) {
174
if (!previousList.isEmpty() && ui_.cb_lists->findText(previousList) != -1) {
175
ui_.cb_lists->setCurrentItem(ui_.cb_lists->findText(previousList));
178
QString currentList = (activeList.isEmpty() ? activeList : defaultList);
179
if (!currentList.isEmpty()) {
180
ui_.cb_lists->setCurrentItem(names.findIndex(currentList));
183
acc_->privacyManager()->requestList(ui_.cb_lists->currentText());
186
setWidgetsEnabled(true);
189
ui_.lv_rules->setModel(&model_);
192
void PrivacyDlg::listChanged()
194
if (model_.list().isEmpty()) {
195
ui_.cb_lists->removeItem(previousList_);
198
setWidgetsEnabled(false);
199
acc_->privacyManager()->requestList(ui_.cb_lists->currentText());
202
void PrivacyDlg::refreshList(const PrivacyList& list)
204
if (list.name() == ui_.cb_lists->currentText()) {
206
model_.setList(list);
207
setWidgetsEnabled(true);
211
void PrivacyDlg::active_selected(int i)
213
if (i != previousActive_) {
214
setWidgetsEnabled(false);
215
acc_->privacyManager()->changeActiveList((i == 0 ? "" : ui_.cb_active->text(i)));
219
void PrivacyDlg::default_selected(int i)
221
if (i != previousDefault_) {
222
setWidgetsEnabled(false);
223
acc_->privacyManager()->changeDefaultList((i == 0 ? "" : ui_.cb_active->text(i)));
227
void PrivacyDlg::list_selected(int i)
229
if (i != previousList_) {
234
void PrivacyDlg::list_changed(int i)
236
ui_.pb_deleteList->setEnabled(i != -1);
237
ui_.pb_add->setEnabled(i != -1);
238
setEditRuleEnabled(i != -1);
239
ui_.pb_apply->setEnabled(i != -1);
240
//setEditRuleEnabled(false);
244
void PrivacyDlg::list_failed()
247
setWidgetsEnabled(true);
250
void PrivacyDlg::changeList_succeeded()
252
// If we just deleted a list, select the first list
253
if (model_.list().isEmpty()) {
254
ui_.cb_lists->setCurrentIndex(0);
258
setWidgetsEnabled(true);
262
void PrivacyDlg::changeList_failed()
264
QMessageBox::critical(0, QObject::tr("Error"), QObject::tr("There was an error changing the list."));
265
setWidgetsEnabled(true);
268
void PrivacyDlg::change_succeeded()
271
setWidgetsEnabled(true);
274
void PrivacyDlg::change_failed()
277
QMessageBox::critical(0, QObject::tr("Error"), QObject::tr("There was an error processing your request."));
278
setWidgetsEnabled(true);
281
void PrivacyDlg::rememberSettings()
283
previousDefault_ = ui_.cb_default->currentItem();
284
previousActive_ = ui_.cb_active->currentItem();
285
previousList_ = ui_.cb_lists->currentItem();
288
void PrivacyDlg::revertSettings()
290
ui_.cb_default->setCurrentItem(previousDefault_);
291
ui_.cb_active->setCurrentItem(previousActive_);
292
ui_.cb_lists->setCurrentItem(previousList_);
296
void PrivacyDlg::newList()
302
name = QInputDialog::getText(this, tr("New List"), tr("Enter the name of the new list:"), QLineEdit::Normal, "", &ok);
306
else if (ui_.cb_lists->findText(name) != -1) {
307
QMessageBox::critical(this, tr("Error"), tr("A list with this name already exists."));
309
else if (!name.isEmpty()) {
315
if (ui_.cb_lists->currentIndex() != -1 && model_.list().isEmpty()) {
316
ui_.cb_lists->removeItem(ui_.cb_lists->currentIndex());
318
ui_.cb_lists->addItem(name);
319
ui_.cb_lists->setCurrentItem(ui_.cb_lists->findText(name));
320
model_.setList(PrivacyList(name));
326
void PrivacyDlg::removeList()
328
model_.list().clear();
329
acc_->privacyManager()->changeList(model_.list());
330
acc_->privacyManager()->requestListNames();