1 | /* |
2 | Copyright (c) 2009 Tobias Koenig <tokoe@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 "recursiveitemfetchjob.h" |
21 | |
22 | #include <akonadi/collectionfetchjob.h> |
23 | #include <akonadi/collectionfetchscope.h> |
24 | #include <akonadi/itemfetchjob.h> |
25 | #include <akonadi/itemfetchscope.h> |
26 | |
27 | #include <QtCore/QStringList> |
28 | #include <QtCore/QVariant> |
29 | |
30 | using namespace Akonadi; |
31 | |
32 | class RecursiveItemFetchJob::Private |
33 | { |
34 | public: |
35 | Private(const Collection &collection, const QStringList &mimeTypes, RecursiveItemFetchJob *parent) |
36 | : mParent(parent) |
37 | , mCollection(collection) |
38 | , mMimeTypes(mimeTypes) |
39 | , mFetchCount(0) |
40 | { |
41 | } |
42 | |
43 | void collectionFetchResult(KJob *job) |
44 | { |
45 | if (job->error()) { |
46 | mParent->emitResult(); |
47 | return; |
48 | } |
49 | |
50 | const CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob *>(job); |
51 | |
52 | Collection::List collections = fetchJob->collections(); |
53 | collections.prepend(mCollection); |
54 | |
55 | foreach (const Collection &collection, collections) { |
56 | ItemFetchJob *itemFetchJob = new ItemFetchJob(collection, mParent); |
57 | itemFetchJob->setFetchScope(mFetchScope); |
58 | mParent->connect(itemFetchJob, SIGNAL(result(KJob*)), |
59 | mParent, SLOT(itemFetchResult(KJob*))); |
60 | |
61 | mFetchCount++; |
62 | } |
63 | } |
64 | |
65 | void itemFetchResult(KJob *job) |
66 | { |
67 | if (!job->error()) { |
68 | const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob *>(job); |
69 | |
70 | if (!mMimeTypes.isEmpty()) { |
71 | foreach (const Item &item, fetchJob->items()) { |
72 | if (mMimeTypes.contains(item.mimeType())) { |
73 | mItems << item; |
74 | } |
75 | } |
76 | } else { |
77 | mItems << fetchJob->items(); |
78 | } |
79 | } |
80 | |
81 | mFetchCount--; |
82 | |
83 | if (mFetchCount == 0) { |
84 | mParent->emitResult(); |
85 | } |
86 | } |
87 | |
88 | RecursiveItemFetchJob *mParent; |
89 | Collection mCollection; |
90 | Item::List mItems; |
91 | ItemFetchScope mFetchScope; |
92 | QStringList mMimeTypes; |
93 | |
94 | int mFetchCount; |
95 | }; |
96 | |
97 | RecursiveItemFetchJob::RecursiveItemFetchJob(const Collection &collection, const QStringList &mimeTypes, QObject *parent) |
98 | : KJob(parent) |
99 | , d(new Private(collection, mimeTypes, this)) |
100 | { |
101 | } |
102 | |
103 | RecursiveItemFetchJob::~RecursiveItemFetchJob() |
104 | { |
105 | delete d; |
106 | } |
107 | |
108 | void RecursiveItemFetchJob::setFetchScope(const ItemFetchScope &fetchScope) |
109 | { |
110 | d->mFetchScope = fetchScope; |
111 | } |
112 | |
113 | ItemFetchScope &RecursiveItemFetchJob::fetchScope() |
114 | { |
115 | return d->mFetchScope; |
116 | } |
117 | |
118 | void RecursiveItemFetchJob::start() |
119 | { |
120 | CollectionFetchJob *job = new CollectionFetchJob(d->mCollection, CollectionFetchJob::Recursive, this); |
121 | |
122 | if (!d->mMimeTypes.isEmpty()) { |
123 | job->fetchScope().setContentMimeTypes(d->mMimeTypes); |
124 | } |
125 | |
126 | connect(job, SIGNAL(result(KJob*)), this, SLOT(collectionFetchResult(KJob*))); |
127 | } |
128 | |
129 | Akonadi::Item::List RecursiveItemFetchJob::items() const |
130 | { |
131 | return d->mItems; |
132 | } |
133 | |
134 | #include "moc_recursiveitemfetchjob.cpp" |
135 | |