~ubuntu-branches/ubuntu/wily/psi/wily

« back to all changes in this revision

Viewing changes to src/privacy/psiprivacymanager.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
 * Copyright (C) 2006  Remko Troncon
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License
 
6
 * as published by the Free Software Foundation; either version 2
 
7
 * of the License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this library; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 *
 
18
 */
 
19
 
 
20
#include <QObject>
 
21
#include <QDebug>
 
22
 
 
23
#include "xmpp_xmlcommon.h"
 
24
#include "xmpp_task.h"
 
25
#include "xmpp_jid.h"
 
26
#include "psiprivacymanager.h"
 
27
#include "privacymanager.h"
 
28
#include "privacylist.h"
 
29
 
 
30
#define PRIVACY_NS "jabber:iq:privacy"
 
31
 
 
32
using namespace XMPP;
 
33
 
 
34
// -----------------------------------------------------------------------------
 
35
// 
 
36
class PrivacyListListener : public Task
 
37
{
 
38
        Q_OBJECT
 
39
 
 
40
public:
 
41
        PrivacyListListener(Task* parent) : Task(parent) {
 
42
        }
 
43
        
 
44
        bool take(const QDomElement &e) {
 
45
                if(e.tagName() != "iq" || e.attribute("type") != "set")
 
46
                        return false;
 
47
                
 
48
                QString ns = queryNS(e);
 
49
                if(ns == "jabber:iq:privacy") {
 
50
                        // TODO: Do something with update
 
51
                        
 
52
                        // Confirm receipt
 
53
                        QDomElement iq = createIQ(doc(), "result", e.attribute("from"), e.attribute("id"));
 
54
                        send(iq);
 
55
                        return true;
 
56
                }
 
57
 
 
58
                return false;
 
59
        }
 
60
};
 
61
 
 
62
// -----------------------------------------------------------------------------
 
63
 
 
64
class GetPrivacyListsTask : public Task
 
65
{
 
66
        Q_OBJECT
 
67
 
 
68
private:
 
69
        QDomElement iq_;
 
70
        QStringList lists_;
 
71
        QString default_, active_;
 
72
 
 
73
public:
 
74
        GetPrivacyListsTask(Task* parent) : Task(parent) { 
 
75
                iq_ = createIQ(doc(), "get", "", id());
 
76
                QDomElement query = doc()->createElement("query");
 
77
                query.setAttribute("xmlns",PRIVACY_NS);
 
78
                iq_.appendChild(query); 
 
79
        }
 
80
 
 
81
        void onGo() {
 
82
                send(iq_);
 
83
        }
 
84
        
 
85
        bool take(const QDomElement &x) {
 
86
                if(!iqVerify(x, "", id()))
 
87
                        return false;
 
88
 
 
89
                //qDebug("privacy.cpp: Got reply for privacy lists.");
 
90
                if (x.attribute("type") == "result") {
 
91
                        QDomElement tag, q = queryTag(x);
 
92
                        
 
93
                        for (QDomNode n = q.firstChild(); !n.isNull(); n = n.nextSibling()) {
 
94
                                QDomElement e = n.toElement();
 
95
                                if (e.tagName() == "active") 
 
96
                                        active_ = e.attribute("name");
 
97
                                else if (e.tagName() == "default")
 
98
                                        default_ = e.attribute("name");
 
99
                                else if (e.tagName() == "list") 
 
100
                                        lists_.append(e.attribute("name"));
 
101
                                else
 
102
                                        qWarning("privacy.cpp: Unknown tag in privacy lists.");
 
103
                                
 
104
                        }
 
105
                        setSuccess();
 
106
                }
 
107
                else {
 
108
                        setError(x);
 
109
                }
 
110
                return true;
 
111
        }
 
112
 
 
113
        const QStringList& lists() { 
 
114
                return lists_; 
 
115
        }
 
116
 
 
117
        const QString& defaultList() { 
 
118
                return default_;
 
119
        }
 
120
 
 
121
        const QString& activeList() {
 
122
                return active_;
 
123
        }
 
124
};
 
125
 
 
126
 
 
127
class SetPrivacyListsTask : public Task
 
128
{
 
129
        Q_OBJECT
 
130
 
 
131
private:
 
132
        bool changeDefault_, changeActive_, changeList_;
 
133
        PrivacyList list_;
 
134
        QString value_;
 
135
 
 
136
public:
 
137
        SetPrivacyListsTask(Task* parent) : Task(parent), changeDefault_(false), changeActive_(false), changeList_(false), list_("") { 
 
138
        }
 
139
 
 
140
        void onGo() {
 
141
                QDomElement iq_ = createIQ(doc(), "set", "", id());
 
142
                QDomElement query = doc()->createElement("query");
 
143
                query.setAttribute("xmlns",PRIVACY_NS);
 
144
                iq_.appendChild(query); 
 
145
 
 
146
                QDomElement e;
 
147
                if (changeDefault_) {
 
148
                        //qDebug("privacy.cpp: Changing default privacy list.");
 
149
                        e = doc()->createElement("default");
 
150
                        if (!value_.isEmpty())
 
151
                                e.setAttribute("name",value_);
 
152
                }
 
153
                else if (changeActive_) {
 
154
                        //qDebug("privacy.cpp: Changing active privacy list.");
 
155
                        e = doc()->createElement("active");
 
156
                        if (!value_.isEmpty()) 
 
157
                                e.setAttribute("name",value_);
 
158
                }
 
159
                else if (changeList_) {
 
160
                        //qDebug("privacy.cpp: Changing privacy list.");
 
161
                        e = list_.toXml(*doc());
 
162
                }
 
163
                else {
 
164
                        qWarning("privacy.cpp: Empty active/default list change request.");
 
165
                        return;
 
166
                }
 
167
                
 
168
                query.appendChild(e);
 
169
                send(iq_);
 
170
        }
 
171
 
 
172
        void setActive(const QString& active) {
 
173
                value_ = active;
 
174
                changeDefault_ = false;
 
175
                changeActive_ = true;
 
176
                changeList_ = false;
 
177
        }
 
178
        
 
179
        void setDefault(const QString& d) {
 
180
                value_ = d;
 
181
                changeDefault_ = true;
 
182
                changeActive_ = false;
 
183
                changeList_ = true;
 
184
        }
 
185
        
 
186
        void setList(const PrivacyList& list) {
 
187
                //qDebug() << "setList: " << list.toString();
 
188
                list_ = list;
 
189
                changeDefault_ = false;
 
190
                changeActive_ = false;
 
191
                changeList_ = true;
 
192
        }
 
193
 
 
194
        bool take(const QDomElement &x) {
 
195
                if(!iqVerify(x, "", id()))
 
196
                        return false;
 
197
 
 
198
                if (x.attribute("type") == "result") {
 
199
                        //qDebug("privacy.cpp: Got succesful reply for list change.");
 
200
                        setSuccess();
 
201
                }
 
202
                else {
 
203
                        qWarning("privacy.cpp: Got error reply for list change.");
 
204
                        setError(x);
 
205
                }
 
206
                return true;
 
207
        }
 
208
};
 
209
 
 
210
class GetPrivacyListTask : public Task
 
211
{
 
212
        Q_OBJECT
 
213
 
 
214
private:
 
215
        QDomElement iq_;
 
216
        QString name_;
 
217
        PrivacyList list_;
 
218
 
 
219
public:
 
220
        GetPrivacyListTask(Task* parent, const QString& name) : Task(parent), name_(name), list_(PrivacyList("")) { 
 
221
                iq_ = createIQ(doc(), "get", "", id());
 
222
                QDomElement query = doc()->createElement("query");
 
223
                query.setAttribute("xmlns",PRIVACY_NS);
 
224
                iq_.appendChild(query); 
 
225
                QDomElement list = doc()->createElement("list");
 
226
                list.setAttribute("name",name);
 
227
                query.appendChild(list);
 
228
        }
 
229
 
 
230
        void onGo() {
 
231
                //qDebug() << "privacy.cpp: Requesting privacy list %1." << name_;
 
232
                send(iq_);
 
233
        }
 
234
        
 
235
        bool take(const QDomElement &x) {
 
236
                if(!iqVerify(x, "", id()))
 
237
                        return false;
 
238
 
 
239
                //qDebug() << QString("privacy.cpp: Got privacy list %1 reply.").arg(name_);
 
240
                if (x.attribute("type") == "result") {
 
241
                        QDomElement q = queryTag(x);
 
242
                        bool found;
 
243
                        QDomElement listTag = findSubTag(q,"list",&found);
 
244
                        if (found) {
 
245
                                list_ = PrivacyList(listTag);
 
246
                        }
 
247
                        else {
 
248
                                qWarning("privacy.cpp: No valid list found.");
 
249
                        }
 
250
                        setSuccess();
 
251
                }
 
252
                else {
 
253
                        setError(x);
 
254
                }
 
255
                return true;
 
256
        }
 
257
 
 
258
        const PrivacyList& list() { 
 
259
                return list_; 
 
260
        }
 
261
};
 
262
 
 
263
// -----------------------------------------------------------------------------
 
264
 
 
265
PsiPrivacyManager::PsiPrivacyManager(XMPP::Task* rootTask) : rootTask_(rootTask), getDefault_waiting_(false), block_waiting_(false)
 
266
{
 
267
   listener_ = new PrivacyListListener(rootTask_);      
 
268
}
 
269
 
 
270
PsiPrivacyManager::~PsiPrivacyManager()
 
271
{
 
272
        delete listener_;
 
273
}
 
274
 
 
275
void PsiPrivacyManager::requestListNames()
 
276
{
 
277
        GetPrivacyListsTask* t = new GetPrivacyListsTask(rootTask_);
 
278
        connect(t,SIGNAL(finished()),SLOT(receiveLists()));
 
279
        t->go(true);
 
280
}
 
281
 
 
282
void PsiPrivacyManager::requestList(const QString& name)
 
283
{
 
284
        GetPrivacyListTask* t = new GetPrivacyListTask(rootTask_, name);
 
285
        connect(t,SIGNAL(finished()),SLOT(receiveList()));
 
286
        t->go(true);
 
287
}
 
288
 
 
289
void PsiPrivacyManager::block(const QString& target)
 
290
{
 
291
        block_targets_.push_back(target);
 
292
        if (!block_waiting_) {
 
293
                block_waiting_ = true;
 
294
                connect(this,SIGNAL(defaultListAvailable(const PrivacyList&)),SLOT(block_getDefaultList_success(const PrivacyList&)));
 
295
                connect(this,SIGNAL(defaultListError()),SLOT(block_getDefaultList_error()));
 
296
                getDefaultList();
 
297
        }
 
298
}
 
299
 
 
300
void PsiPrivacyManager::block_getDefaultList_success(const PrivacyList& l_)
 
301
{
 
302
        PrivacyList l = l_;
 
303
        disconnect(this,SIGNAL(defaultListAvailable(const PrivacyList&)),this,SLOT(block_getDefault_success(const PrivacyList&)));
 
304
        disconnect(this,SIGNAL(defaultListError()),this,SLOT(block_getDefault_error()));
 
305
        block_waiting_ = false;
 
306
        while (!block_targets_.isEmpty()) 
 
307
                l.insertItem(0,PrivacyListItem::blockItem(block_targets_.takeFirst()));
 
308
        changeList(l);
 
309
}
 
310
 
 
311
void PsiPrivacyManager::block_getDefaultList_error()
 
312
{
 
313
        disconnect(this,SIGNAL(defaultListAvailable(const PrivacyList&)),this,SLOT(block_getDefault_success(const PrivacyList&)));
 
314
        disconnect(this,SIGNAL(defaultListError()),this,SLOT(block_getDefault_error()));
 
315
        block_waiting_ = false;
 
316
        block_targets_.clear();
 
317
}
 
318
 
 
319
void PsiPrivacyManager::getDefaultList()
 
320
{
 
321
        connect(this,SIGNAL(listsReceived(const QString&, const QString&, const QStringList&)),SLOT(getDefault_listsReceived(const QString&, const QString&, const QStringList&)));
 
322
        connect(this,SIGNAL(listsError()),SLOT(getDefault_listsError()));
 
323
        requestListNames();
 
324
}
 
325
 
 
326
void PsiPrivacyManager::getDefault_listsReceived(const QString& defaultList, const QString&, const QStringList&)
 
327
{
 
328
        disconnect(this,SIGNAL(listsReceived(const QString&, const QString&, const QStringList&)),this,SLOT(getDefault_listsReceived(const QString&, const QString&, const QStringList&)));
 
329
        disconnect(this,SIGNAL(listsError()),this,SLOT(getDefault_listsError()));
 
330
        
 
331
        getDefault_default_ = defaultList;
 
332
        if (!defaultList.isEmpty()) {
 
333
                getDefault_waiting_ = true;
 
334
                connect(this,SIGNAL(listReceived(const PrivacyList&)),SLOT(getDefault_listReceived(const PrivacyList&)));
 
335
                connect(this,SIGNAL(listError()),SLOT(getDefault_listError()));
 
336
                requestList(defaultList);
 
337
        }
 
338
        else {
 
339
                emit defaultListAvailable(PrivacyList(""));
 
340
        }
 
341
}
 
342
 
 
343
void PsiPrivacyManager::getDefault_listsError()
 
344
{
 
345
        disconnect(this,SIGNAL(listsReceived(const QString&, const QString&, const QStringList&)),this,SLOT(getDefault_listsReceived(const QString&, const QString&, const QStringList&)));
 
346
        disconnect(this,SIGNAL(listsError()),this,SLOT(getDefault_listsError()));
 
347
        emit defaultListError();
 
348
}
 
349
 
 
350
void PsiPrivacyManager::getDefault_listReceived(const PrivacyList& l)
 
351
{
 
352
        if (l.name() == getDefault_default_ && getDefault_waiting_) {
 
353
                disconnect(this,SIGNAL(listReceived(const PrivacyList&)),this,SLOT(getDefault_listReceived(const PrivacyList&)));
 
354
                disconnect(this,SIGNAL(listError()),this,SLOT(getDefault_listError()));
 
355
                getDefault_waiting_ = false;
 
356
                emit defaultListAvailable(l);
 
357
        }
 
358
}
 
359
 
 
360
void PsiPrivacyManager::getDefault_listError()
 
361
{
 
362
        emit defaultListError();
 
363
}
 
364
 
 
365
void PsiPrivacyManager::changeDefaultList(const QString& name)
 
366
{
 
367
        SetPrivacyListsTask* t = new SetPrivacyListsTask(rootTask_);
 
368
        t->setDefault(name);
 
369
        connect(t,SIGNAL(finished()),SLOT(changeDefaultList_finished()));
 
370
        t->go(true);
 
371
}
 
372
 
 
373
void PsiPrivacyManager::changeDefaultList_finished()
 
374
{
 
375
        SetPrivacyListsTask *t = (SetPrivacyListsTask*)sender();
 
376
        if (!t) {
 
377
                qWarning("privacy.cpp:changeDefault_finished(): Unexpected sender.");
 
378
                return;
 
379
        }
 
380
 
 
381
        if (t->success()) {
 
382
                emit changeDefaultList_success();
 
383
        }
 
384
        else {
 
385
                emit changeDefaultList_error();
 
386
        }
 
387
}
 
388
 
 
389
void PsiPrivacyManager::changeActiveList(const QString& name)
 
390
{
 
391
        SetPrivacyListsTask* t = new SetPrivacyListsTask(rootTask_);
 
392
        t->setActive(name);
 
393
        connect(t,SIGNAL(finished()),SLOT(changeActiveList_finished()));
 
394
        t->go(true);
 
395
}
 
396
 
 
397
void PsiPrivacyManager::changeActiveList_finished()
 
398
{
 
399
        SetPrivacyListsTask *t = (SetPrivacyListsTask*)sender();
 
400
        if (!t) {
 
401
                qWarning("privacy.cpp:changeActive_finished(): Unexpected sender.");
 
402
                return;
 
403
        }
 
404
 
 
405
        if (t->success()) {
 
406
                emit changeActiveList_success();
 
407
        }
 
408
        else {
 
409
                emit changeActiveList_error();
 
410
        }
 
411
}
 
412
 
 
413
void PsiPrivacyManager::changeList(const PrivacyList& list)
 
414
{
 
415
        SetPrivacyListsTask* t = new SetPrivacyListsTask(rootTask_);
 
416
        t->setList(list);
 
417
        connect(t,SIGNAL(finished()),SLOT(changeList_finished()));
 
418
        t->go(true);
 
419
}
 
420
 
 
421
void PsiPrivacyManager::changeList_finished()
 
422
{
 
423
        SetPrivacyListsTask *t = (SetPrivacyListsTask*)sender();
 
424
        if (!t) {
 
425
                qWarning("privacy.cpp:changeList_finished(): Unexpected sender.");
 
426
                return;
 
427
        }
 
428
 
 
429
        if (t->success()) {
 
430
                emit changeList_success();
 
431
        }
 
432
        else {
 
433
                emit changeList_error();
 
434
        }
 
435
}
 
436
 
 
437
void PsiPrivacyManager::receiveLists() 
 
438
{
 
439
        GetPrivacyListsTask *t = (GetPrivacyListsTask*)sender();
 
440
        if (!t) {
 
441
                qWarning("privacy.cpp:receiveLists(): Unexpected sender.");
 
442
                return;
 
443
        }
 
444
        
 
445
        if (t->success()) {
 
446
                emit listsReceived(t->defaultList(),t->activeList(),t->lists());
 
447
        }
 
448
        else {
 
449
                qDebug("privacy.cpp: Error in lists receiving.");
 
450
                emit listsError();
 
451
        }
 
452
}
 
453
 
 
454
void PsiPrivacyManager::receiveList() 
 
455
{
 
456
        GetPrivacyListTask *t = (GetPrivacyListTask*)sender();
 
457
        if (!t) {
 
458
                qWarning("privacy.cpp:receiveList(): Unexpected sender.");
 
459
                return;
 
460
        }
 
461
        
 
462
        if (t->success()) {
 
463
                emit listReceived(t->list());
 
464
        }
 
465
        else {
 
466
                qDebug("privacy.cpp: Error in list receiving.");
 
467
                emit listError();
 
468
        }
 
469
}
 
470
 
 
471
#include "psiprivacymanager.moc"