1 | /* |
2 | Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com> |
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 "filteractionjob_p.h" |
21 | |
22 | #include <akonadi/collection.h> |
23 | #include <akonadi/itemfetchjob.h> |
24 | #include <akonadi/itemfetchscope.h> |
25 | |
26 | #include <KDebug> |
27 | |
28 | using namespace Akonadi; |
29 | |
30 | class Akonadi::FilterActionJob::Private |
31 | { |
32 | public: |
33 | Private( FilterActionJob *qq ) |
34 | : q( qq ), functor( 0 ) |
35 | { |
36 | } |
37 | |
38 | ~Private() |
39 | { |
40 | delete functor; |
41 | } |
42 | |
43 | FilterActionJob *q; |
44 | Collection collection; |
45 | Item::List items; |
46 | FilterAction *functor; |
47 | ItemFetchScope fetchScope; |
48 | |
49 | // slots: |
50 | void fetchResult( KJob *job ); |
51 | |
52 | void traverseItems(); |
53 | }; |
54 | |
55 | void FilterActionJob::Private::fetchResult( KJob *job ) |
56 | { |
57 | if ( job->error() ) { |
58 | // KCompositeJob takes care of errors. |
59 | return; |
60 | } |
61 | |
62 | ItemFetchJob *fjob = dynamic_cast<ItemFetchJob*>( job ); |
63 | Q_ASSERT( fjob ); |
64 | Q_ASSERT( items.isEmpty() ); |
65 | items = fjob->items(); |
66 | traverseItems(); |
67 | } |
68 | |
69 | void FilterActionJob::Private::traverseItems() |
70 | { |
71 | Q_ASSERT( functor ); |
72 | kDebug() << "Traversing" << items.count() << "items." ; |
73 | foreach ( const Item &item, items ) { |
74 | if ( functor->itemAccepted( item ) ) { |
75 | functor->itemAction( item, q ); |
76 | kDebug() << "Added subjob for item" << item.id(); |
77 | } |
78 | } |
79 | if ( q->subjobs().isEmpty() ) { |
80 | kDebug() << "No subjobs; I am done" ; |
81 | } else { |
82 | kDebug() << "Have subjobs; Done when last of them is" ; |
83 | } |
84 | q->commit(); |
85 | } |
86 | |
87 | FilterAction::~FilterAction() |
88 | { |
89 | } |
90 | |
91 | FilterActionJob::FilterActionJob( const Item &item, FilterAction *functor, QObject *parent ) |
92 | : TransactionSequence( parent ), d( new Private( this ) ) |
93 | { |
94 | d->functor = functor; |
95 | d->items << item; |
96 | } |
97 | |
98 | FilterActionJob::FilterActionJob( const Item::List &items, FilterAction *functor, QObject *parent ) |
99 | : TransactionSequence( parent ), d( new Private( this ) ) |
100 | { |
101 | d->functor = functor; |
102 | d->items = items; |
103 | } |
104 | |
105 | FilterActionJob::FilterActionJob( const Collection &collection, |
106 | FilterAction *functor, QObject *parent ) |
107 | : TransactionSequence( parent ), d( new Private( this ) ) |
108 | { |
109 | d->functor = functor; |
110 | Q_ASSERT( collection.isValid() ); |
111 | d->collection = collection; |
112 | } |
113 | |
114 | FilterActionJob::~FilterActionJob() |
115 | { |
116 | delete d; |
117 | } |
118 | |
119 | void FilterActionJob::doStart() |
120 | { |
121 | if ( d->collection.isValid() ) { |
122 | kDebug() << "Fetching collection" << d->collection.id(); |
123 | ItemFetchJob *fjob = new ItemFetchJob( d->collection, this ); |
124 | Q_ASSERT( d->functor ); |
125 | d->fetchScope = d->functor->fetchScope(); |
126 | fjob->setFetchScope( d->fetchScope ); |
127 | connect( fjob, SIGNAL(result(KJob*)), this, SLOT(fetchResult(KJob*)) ); |
128 | } else { |
129 | d->traverseItems(); |
130 | } |
131 | } |
132 | |
133 | #include "moc_filteractionjob_p.cpp" |
134 | |