1/*
2 Copyright (c) 2007 Volker Krause <vkrause@kde.org>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "subscriptionjob_p.h"
21
22#include "job_p.h"
23
24using namespace Akonadi;
25
26class Akonadi::SubscriptionJobPrivate : public JobPrivate
27{
28public:
29 SubscriptionJobPrivate(SubscriptionJob *parent)
30 : JobPrivate(parent)
31 {
32 }
33
34 void sendCommand(const QByteArray &cmd, const Collection::List &list)
35 {
36 mTag = newTag();
37 QByteArray line = mTag + ' ' + cmd;
38 foreach (const Collection &col, list) {
39 line += ' ' + QByteArray::number(col.id());
40 }
41 line += '\n';
42 writeData(line);
43 newTag(); // prevent automatic response handling
44 }
45
46 void sendNextCommand()
47 {
48 Q_Q(SubscriptionJob);
49
50 QByteArray cmd;
51 if (!mSub.isEmpty()) {
52 sendCommand("SUBSCRIBE", mSub);
53 mSub.clear();
54 } else if (!mUnsub.isEmpty()) {
55 sendCommand("UNSUBSCRIBE", mUnsub);
56 mUnsub.clear();
57 } else {
58 q->emitResult();
59 }
60 }
61
62 Q_DECLARE_PUBLIC(SubscriptionJob)
63
64 QByteArray mTag;
65 Collection::List mSub, mUnsub;
66};
67
68SubscriptionJob::SubscriptionJob(QObject *parent)
69 : Job(new SubscriptionJobPrivate(this), parent)
70{
71}
72
73SubscriptionJob::~SubscriptionJob()
74{
75}
76
77void SubscriptionJob::subscribe(const Collection::List &list)
78{
79 Q_D(SubscriptionJob);
80
81 d->mSub = list;
82}
83
84void SubscriptionJob::unsubscribe(const Collection::List &list)
85{
86 Q_D(SubscriptionJob);
87
88 d->mUnsub = list;
89}
90
91void SubscriptionJob::doStart()
92{
93 Q_D(SubscriptionJob);
94
95 d->sendNextCommand();
96}
97
98void SubscriptionJob::doHandleResponse(const QByteArray &_tag, const QByteArray &data)
99{
100 Q_D(SubscriptionJob);
101
102 if (_tag == d->mTag) {
103 if (data.startsWith("OK")) { //krazy:exclude=strings
104 d->sendNextCommand();
105 } else {
106 setError(Unknown);
107 setErrorText(QString::fromUtf8(data));
108 emitResult();
109 }
110 return;
111 }
112}
113
114#include "moc_subscriptionjob_p.cpp"
115