~ubuntu-branches/ubuntu/vivid/psi/vivid

« back to all changes in this revision

Viewing changes to src/privacydlg.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-08-28 18:46:52 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080828184652-iiik12dl91nq7cdi
Tags: 0.12-2
Uploading to unstable (Closes: Bug#494352)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * privacydlg.cpp
3
 
 * Copyright (C) 2006  Remko Troncon
4
 
 *
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.
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 library; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
 *
19
 
 */
20
 
 
21
 
#include <QListView>
22
 
#include <QInputDialog>
23
 
#include <QMessageBox>
24
 
 
25
 
#include "privacydlg.h"
26
 
#include "psiaccount.h"
27
 
#include "privacylist.h"
28
 
#include "privacymanager.h"
29
 
#include "privacylistmodel.h"
30
 
 
31
 
PrivacyDlg::PrivacyDlg(PsiAccount* acc, QWidget* parent) : QDialog(parent), acc_(acc)
32
 
{
33
 
        ui_.setupUi(this);
34
 
        setAttribute(Qt::WA_DeleteOnClose);
35
 
        setWindowTitle(tr("%1: Privacy Lists").arg(acc->name()));
36
 
 
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()));
43
 
 
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()));
54
 
 
55
 
        connect(ui_.pb_newList,SIGNAL(clicked()),SLOT(newList()));
56
 
        connect(ui_.pb_deleteList,SIGNAL(clicked()),SLOT(removeList()));
57
 
        
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()));
64
 
 
65
 
        connect(ui_.pb_close,SIGNAL(clicked()),SLOT(close()));
66
 
        setWidgetsEnabled(false);
67
 
 
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);
73
 
 
74
 
        // FIXME: Temporarily disabling auto-activate
75
 
        ui_.ck_autoActivate->hide();
76
 
        
77
 
        manager->requestListNames();
78
 
}
79
 
 
80
 
void PrivacyDlg::setWidgetsEnabled(bool b)
81
 
{
82
 
        ui_.gb_settings->setEnabled(b);
83
 
        ui_.gb_listSettings->setEnabled(b);
84
 
}
85
 
 
86
 
void PrivacyDlg::setEditRuleEnabled(bool b)
87
 
{
88
 
        ui_.pb_up->setEnabled(b);
89
 
        ui_.pb_down->setEnabled(b);
90
 
        ui_.pb_edit->setEnabled(b);
91
 
        ui_.pb_remove->setEnabled(b);
92
 
}
93
 
 
94
 
void PrivacyDlg::addRule()
95
 
{
96
 
        model_.add();
97
 
}
98
 
 
99
 
void PrivacyDlg::editCurrentRule()
100
 
{
101
 
        // Maybe should use MVC setData here
102
 
        model_.edit(ui_.lv_rules->currentIndex());
103
 
}
104
 
 
105
 
void PrivacyDlg::removeCurrentRule()
106
 
{
107
 
        if (ui_.lv_rules->currentIndex().isValid()) {
108
 
                model_.removeRow(ui_.lv_rules->currentIndex().row(),ui_.lv_rules->currentIndex().parent());
109
 
        }
110
 
}
111
 
 
112
 
void PrivacyDlg::moveCurrentRuleUp()
113
 
{
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));
117
 
        }
118
 
}
119
 
 
120
 
void PrivacyDlg::moveCurrentRuleDown()
121
 
{
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));
125
 
        }
126
 
}
127
 
 
128
 
void PrivacyDlg::applyList()
129
 
{
130
 
        if (!model_.list().isEmpty()) {
131
 
                setWidgetsEnabled(false);
132
 
                acc_->privacyManager()->changeList(model_.list());
133
 
                if (newList_)
134
 
                        acc_->privacyManager()->requestListNames();
135
 
        }
136
 
}
137
 
 
138
 
void PrivacyDlg::close()
139
 
{
140
 
        done(0);
141
 
}
142
 
 
143
 
void PrivacyDlg::updateLists(const QString& defaultList, const QString& activeList, const QStringList& names)
144
 
{
145
 
        // Active list
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);
151
 
        }
152
 
        else {
153
 
                ui_.cb_active->setCurrentItem(0);
154
 
        }
155
 
        previousActive_ = ui_.cb_active->currentItem();
156
 
        
157
 
        // Default list
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);
163
 
        }
164
 
        else {
165
 
                ui_.cb_default->setCurrentItem(0);
166
 
        }
167
 
        previousDefault_ = ui_.cb_default->currentItem();
168
 
        
169
 
        // All lists
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));
176
 
                }
177
 
                else {
178
 
                        QString currentList = (activeList.isEmpty() ? activeList : defaultList);
179
 
                        if (!currentList.isEmpty()) {
180
 
                                ui_.cb_lists->setCurrentItem(names.findIndex(currentList));
181
 
                        }
182
 
                }
183
 
                acc_->privacyManager()->requestList(ui_.cb_lists->currentText());
184
 
        }
185
 
        else {
186
 
                setWidgetsEnabled(true);
187
 
        }
188
 
        
189
 
        ui_.lv_rules->setModel(&model_);
190
 
}
191
 
 
192
 
void PrivacyDlg::listChanged()
193
 
{
194
 
        if (model_.list().isEmpty()) {
195
 
                ui_.cb_lists->removeItem(previousList_);
196
 
                rememberSettings();
197
 
        }
198
 
        setWidgetsEnabled(false);
199
 
        acc_->privacyManager()->requestList(ui_.cb_lists->currentText());
200
 
}
201
 
 
202
 
void PrivacyDlg::refreshList(const PrivacyList& list)
203
 
{
204
 
        if (list.name() == ui_.cb_lists->currentText()) {
205
 
                rememberSettings();
206
 
                model_.setList(list);
207
 
                setWidgetsEnabled(true);
208
 
        }
209
 
}
210
 
 
211
 
void PrivacyDlg::active_selected(int i)
212
 
{
213
 
        if (i != previousActive_) {
214
 
                setWidgetsEnabled(false);
215
 
                acc_->privacyManager()->changeActiveList((i == 0 ? "" : ui_.cb_active->text(i)));
216
 
        }
217
 
}
218
 
 
219
 
void PrivacyDlg::default_selected(int i)
220
 
{
221
 
        if (i != previousDefault_) {
222
 
                setWidgetsEnabled(false);
223
 
                acc_->privacyManager()->changeDefaultList((i == 0 ? "" : ui_.cb_active->text(i)));
224
 
        }
225
 
}
226
 
 
227
 
void PrivacyDlg::list_selected(int i)
228
 
{
229
 
        if (i != previousList_) {
230
 
                listChanged();
231
 
        }
232
 
}
233
 
 
234
 
void PrivacyDlg::list_changed(int i)
235
 
{
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);
241
 
        newList_ = false;
242
 
}
243
 
 
244
 
void PrivacyDlg::list_failed()
245
 
{
246
 
        revertSettings();
247
 
        setWidgetsEnabled(true);
248
 
}
249
 
 
250
 
void PrivacyDlg::changeList_succeeded()
251
 
{
252
 
        // If we just deleted a list, select the first list
253
 
        if (model_.list().isEmpty()) {
254
 
                ui_.cb_lists->setCurrentIndex(0);
255
 
                listChanged();
256
 
        }
257
 
        else {
258
 
                setWidgetsEnabled(true);
259
 
        }
260
 
}
261
 
 
262
 
void PrivacyDlg::changeList_failed()
263
 
{
264
 
        QMessageBox::critical(0, QObject::tr("Error"), QObject::tr("There was an error changing the list.")); 
265
 
        setWidgetsEnabled(true);
266
 
}
267
 
 
268
 
void PrivacyDlg::change_succeeded()
269
 
{
270
 
        rememberSettings();
271
 
        setWidgetsEnabled(true);
272
 
}
273
 
 
274
 
void PrivacyDlg::change_failed()
275
 
{
276
 
        revertSettings();
277
 
        QMessageBox::critical(0, QObject::tr("Error"), QObject::tr("There was an error processing your request.")); 
278
 
        setWidgetsEnabled(true);
279
 
}
280
 
 
281
 
void PrivacyDlg::rememberSettings()
282
 
{
283
 
        previousDefault_ = ui_.cb_default->currentItem();
284
 
        previousActive_ = ui_.cb_active->currentItem();
285
 
        previousList_ = ui_.cb_lists->currentItem();
286
 
}
287
 
 
288
 
void PrivacyDlg::revertSettings()
289
 
{
290
 
        ui_.cb_default->setCurrentItem(previousDefault_);
291
 
        ui_.cb_active->setCurrentItem(previousActive_);
292
 
        ui_.cb_lists->setCurrentItem(previousList_);
293
 
}
294
 
 
295
 
 
296
 
void PrivacyDlg::newList()
297
 
{
298
 
        bool done = false;
299
 
        bool ok = false;
300
 
        QString name;
301
 
        while (!done) {
302
 
                name = QInputDialog::getText(this, tr("New List"), tr("Enter the name of the new list:"), QLineEdit::Normal, "", &ok);
303
 
                if (!ok) {
304
 
                        done = true;
305
 
                }
306
 
                else if (ui_.cb_lists->findText(name) != -1) {
307
 
                        QMessageBox::critical(this, tr("Error"), tr("A list with this name already exists."));
308
 
                }
309
 
                else if (!name.isEmpty()) {
310
 
                        done = true;
311
 
                }
312
 
        }
313
 
        
314
 
        if (ok) {
315
 
                if (ui_.cb_lists->currentIndex() != -1 && model_.list().isEmpty()) {
316
 
                        ui_.cb_lists->removeItem(ui_.cb_lists->currentIndex());
317
 
                }
318
 
                ui_.cb_lists->addItem(name);
319
 
                ui_.cb_lists->setCurrentItem(ui_.cb_lists->findText(name));
320
 
                model_.setList(PrivacyList(name));
321
 
                newList_ = true;
322
 
                rememberSettings();
323
 
        }
324
 
}
325
 
 
326
 
void PrivacyDlg::removeList()
327
 
{
328
 
        model_.list().clear();
329
 
        acc_->privacyManager()->changeList(model_.list());
330
 
        acc_->privacyManager()->requestListNames();
331
 
}