00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "recursiveitemfetchjob.h"
00021
00022 #include <akonadi/collectionfetchjob.h>
00023 #include <akonadi/collectionfetchscope.h>
00024 #include <akonadi/itemfetchjob.h>
00025 #include <akonadi/itemfetchscope.h>
00026
00027 #include <QtCore/QStringList>
00028 #include <QtCore/QVariant>
00029
00030 using namespace Akonadi;
00031
00032 class RecursiveItemFetchJob::Private
00033 {
00034 public:
00035 Private( const Collection &collection, const QStringList &mimeTypes, RecursiveItemFetchJob *parent )
00036 : mParent( parent ), mCollection( collection ), mMimeTypes( mimeTypes ), mFetchCount( 0 )
00037 {
00038 }
00039
00040 void collectionFetchResult( KJob *job )
00041 {
00042 if ( job->error() ) {
00043 mParent->emitResult();
00044 return;
00045 }
00046
00047 const CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob*>( job );
00048
00049 Collection::List collections = fetchJob->collections();
00050 collections.prepend( mCollection );
00051
00052 foreach ( const Collection &collection, collections ) {
00053 ItemFetchJob *itemFetchJob = new ItemFetchJob( collection, mParent );
00054 itemFetchJob->setFetchScope( mFetchScope );
00055 mParent->connect( itemFetchJob, SIGNAL( result( KJob* ) ),
00056 mParent, SLOT( itemFetchResult( KJob* ) ) );
00057
00058 mFetchCount++;
00059 }
00060 }
00061
00062 void itemFetchResult( KJob *job )
00063 {
00064 if ( !job->error() ) {
00065 const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
00066
00067 if ( !mMimeTypes.isEmpty() ) {
00068 foreach ( const Item &item, fetchJob->items() ) {
00069 if ( mMimeTypes.contains( item.mimeType() ) )
00070 mItems << item;
00071 }
00072 } else {
00073 mItems << fetchJob->items();
00074 }
00075 }
00076
00077 mFetchCount--;
00078
00079 if ( mFetchCount == 0 )
00080 mParent->emitResult();
00081 }
00082
00083 RecursiveItemFetchJob *mParent;
00084 Collection mCollection;
00085 Item::List mItems;
00086 ItemFetchScope mFetchScope;
00087 QStringList mMimeTypes;
00088
00089 int mFetchCount;
00090 };
00091
00092 RecursiveItemFetchJob::RecursiveItemFetchJob( const Collection &collection,
00093 const QStringList &mimeTypes,
00094 QObject * parent )
00095 : KJob( parent ), d( new Private( collection, mimeTypes, this ) )
00096 {
00097 }
00098
00099 RecursiveItemFetchJob::~RecursiveItemFetchJob()
00100 {
00101 delete d;
00102 }
00103
00104 void RecursiveItemFetchJob::setFetchScope( const ItemFetchScope &fetchScope )
00105 {
00106 d->mFetchScope = fetchScope;
00107 }
00108
00109 ItemFetchScope &RecursiveItemFetchJob::fetchScope()
00110 {
00111 return d->mFetchScope;
00112 }
00113
00114 void RecursiveItemFetchJob::start()
00115 {
00116 CollectionFetchJob *job = new CollectionFetchJob( d->mCollection, CollectionFetchJob::Recursive, this );
00117
00118 if ( !d->mMimeTypes.isEmpty() )
00119 job->fetchScope().setContentMimeTypes( d->mMimeTypes );
00120
00121 connect( job, SIGNAL( result( KJob* ) ), this, SLOT( collectionFetchResult( KJob* ) ) );
00122 }
00123
00124 Akonadi::Item::List RecursiveItemFetchJob::items() const
00125 {
00126 return d->mItems;
00127 }
00128
00129 #include "recursiveitemfetchjob.moc"