1/*
2 Copyright (c) 2006 - 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 "collectionstatisticsjob.h"
21
22#include "collection.h"
23#include "collectionstatistics.h"
24#include "imapparser_p.h"
25#include "job_p.h"
26
27#include <kdebug.h>
28
29using namespace Akonadi;
30
31class Akonadi::CollectionStatisticsJobPrivate : public JobPrivate
32{
33public:
34 CollectionStatisticsJobPrivate(CollectionStatisticsJob *parent)
35 : JobPrivate(parent)
36 {
37 }
38
39 QString jobDebuggingString() const { /*Q_DECL_OVERRIDE*/
40 return QString::fromLatin1("Collection Id %1").arg(mCollection.id());
41 }
42
43 Collection mCollection;
44 CollectionStatistics mStatistics;
45};
46
47CollectionStatisticsJob::CollectionStatisticsJob(const Collection &collection, QObject *parent)
48 : Job(new CollectionStatisticsJobPrivate(this), parent)
49{
50 Q_D(CollectionStatisticsJob);
51
52 d->mCollection = collection;
53}
54
55CollectionStatisticsJob::~CollectionStatisticsJob()
56{
57}
58
59void CollectionStatisticsJob::doStart()
60{
61 Q_D(CollectionStatisticsJob);
62
63 d->writeData(d->newTag() + " STATUS " + QByteArray::number(d->mCollection.id()) + " (MESSAGES UNSEEN SIZE)\n");
64}
65
66void CollectionStatisticsJob::doHandleResponse(const QByteArray &tag, const QByteArray &data)
67{
68 Q_D(CollectionStatisticsJob);
69
70 if (tag == "*") {
71 QByteArray token;
72 int current = ImapParser::parseString(data, token);
73 if (token == "STATUS") {
74 // folder path
75 current = ImapParser::parseString(data, token, current);
76 // result list
77 QList<QByteArray> list;
78 current = ImapParser::parseParenthesizedList(data, list, current);
79 for (int i = 0; i < list.count() - 1; i += 2) {
80 if (list[i] == "MESSAGES") {
81 d->mStatistics.setCount(list[i + 1].toLongLong());
82 } else if (list[i] == "UNSEEN") {
83 d->mStatistics.setUnreadCount(list[i + 1].toLongLong());
84 } else if (list[i] == "SIZE") {
85 d->mStatistics.setSize(list[i + 1].toLongLong());
86 } else {
87 kDebug() << "Unknown STATUS response: " << list[i];
88 }
89 }
90
91 d->mCollection.setStatistics(d->mStatistics);
92 return;
93 }
94 }
95 kDebug() << "Unhandled response: " << tag << data;
96}
97
98Collection CollectionStatisticsJob::collection() const
99{
100 Q_D(const CollectionStatisticsJob);
101
102 return d->mCollection;
103}
104
105CollectionStatistics Akonadi::CollectionStatisticsJob::statistics() const
106{
107 Q_D(const CollectionStatisticsJob);
108
109 return d->mStatistics;
110}
111