akonadi
recursiveitemfetchjob.cpp
00001 /* 00002 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 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"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:25:19 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:25:19 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.