20 #include "itemfetchjob.h"
22 #include "attributefactory.h"
23 #include "collection.h"
24 #include "collectionselectjob_p.h"
25 #include "imapparser_p.h"
26 #include "itemfetchscope.h"
28 #include "protocol_p.h"
29 #include "protocolhelper_p.h"
34 #include <QtCore/QStringList>
35 #include <QtCore/QTimer>
37 using namespace Akonadi;
39 class Akonadi::ItemFetchJobPrivate :
public JobPrivate
42 ItemFetchJobPrivate( ItemFetchJob *parent )
43 : JobPrivate( parent ),
46 mCollection = Collection::root();
49 ~ItemFetchJobPrivate()
57 mEmitTimer =
new QTimer( q );
58 mEmitTimer->setSingleShot(
true );
59 mEmitTimer->setInterval( 100 );
60 q->connect( mEmitTimer, SIGNAL(timeout()), q, SLOT(timeout()) );
61 q->connect( q, SIGNAL(result(KJob*)), q, SLOT(timeout()) );
69 if ( !mPendingItems.isEmpty() ) {
71 emit q->itemsReceived( mPendingItems );
72 mPendingItems.clear();
77 void selectDone( KJob * job );
79 Q_DECLARE_PUBLIC( ItemFetchJob )
81 Collection mCollection;
82 Item::List mRequestedItems;
83 Item::List mResultItems;
84 ItemFetchScope mFetchScope;
85 Item::List mPendingItems;
87 ProtocolHelperValuePool *mValuePool;
90 void ItemFetchJobPrivate::startFetchJob()
93 QByteArray command = newTag();
94 if ( mRequestedItems.isEmpty() ) {
95 command +=
" " AKONADI_CMD_ITEMFETCH
" 1:*";
98 command += ProtocolHelper::entitySetToByteArray( mRequestedItems, AKONADI_CMD_ITEMFETCH );
99 }
catch (
const Exception &e ) {
100 q->setError( Job::Unknown );
101 q->setErrorText( QString::fromUtf8( e.what() ) );
107 command += ProtocolHelper::itemFetchScopeToByteArray( mFetchScope );
109 writeData( command );
112 void ItemFetchJobPrivate::selectDone( KJob * job )
119 ItemFetchJob::ItemFetchJob(
const Collection &collection, QObject * parent )
120 : Job( new ItemFetchJobPrivate( this ), parent )
125 d->mCollection = collection;
126 d->mValuePool =
new ProtocolHelperValuePool;
129 ItemFetchJob::ItemFetchJob(
const Item & item, QObject * parent)
130 : Job( new ItemFetchJobPrivate( this ), parent )
135 d->mRequestedItems.append( item );
138 ItemFetchJob::ItemFetchJob(
const Akonadi::Item::List& items, QObject* parent)
139 : Job( new ItemFetchJobPrivate( this ), parent )
144 d->mRequestedItems = items;
147 ItemFetchJob::ItemFetchJob(
const QList<Akonadi::Item::Id>& items, QObject* parent)
148 : Job( new ItemFetchJobPrivate( this ), parent )
153 foreach(Item::Id
id, items)
154 d->mRequestedItems.append(Item(
id));
158 ItemFetchJob::~ItemFetchJob()
162 void ItemFetchJob::doStart()
166 if ( d->mRequestedItems.isEmpty() ) {
167 if ( d->mCollection == Collection::root() ) {
168 setErrorText( i18n(
"Cannot list root collection." ) );
173 connect( job, SIGNAL(result(KJob*)), SLOT(selectDone(KJob*)) );
179 void ItemFetchJob::doHandleResponse(
const QByteArray & tag,
const QByteArray & data )
184 int begin = data.indexOf(
"FETCH" );
188 QList<QByteArray> fetchResponse;
189 ImapParser::parseParenthesizedList( data, fetchResponse, begin + 6 );
192 ProtocolHelper::parseItemFetchResult( fetchResponse, item, d->mValuePool );
196 d->mResultItems.append( item );
197 d->mPendingItems.append( item );
198 if ( !d->mEmitTimer->isActive() )
199 d->mEmitTimer->start();
203 kDebug() <<
"Unhandled response: " << tag << data;
210 return d->mResultItems;
217 d->mFetchScope = fetchScope;
224 d->mFetchScope = fetchScope;
231 return d->mFetchScope;
234 void ItemFetchJob::setCollection(
const Akonadi::Collection& collection)
238 d->mCollection = collection;
242 #include "itemfetchjob.moc"