akonadi
collectionpathresolver.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "collectionpathresolver_p.h"
00021
00022 #include "collectionfetchjob.h"
00023 #include "job_p.h"
00024
00025 #include <klocale.h>
00026
00027 #include <QtCore/QStringList>
00028
00029 using namespace Akonadi;
00030
00031 class Akonadi::CollectionPathResolverPrivate : public JobPrivate
00032 {
00033 public:
00034 CollectionPathResolverPrivate( CollectionPathResolver *parent )
00035 : JobPrivate( parent )
00036 {
00037 }
00038
00039 void jobResult( KJob* );
00040
00041 Q_DECLARE_PUBLIC( CollectionPathResolver )
00042
00043 Collection::Id mColId;
00044 QString mPath;
00045 bool mPathToId;
00046 QStringList mPathParts;
00047 Collection mCurrentNode;
00048 };
00049
00050 void CollectionPathResolverPrivate::jobResult(KJob *job )
00051 {
00052 if ( job->error() )
00053 return;
00054
00055 Q_Q( CollectionPathResolver );
00056
00057 CollectionFetchJob *list = static_cast<CollectionFetchJob*>( job );
00058 CollectionFetchJob *nextJob = 0;
00059 const Collection::List cols = list->collections();
00060 if ( cols.isEmpty() ) {
00061 q->setError( CollectionPathResolver::Unknown );
00062 q->setErrorText( i18n( "No such collection.") );
00063 q->emitResult();
00064 return;
00065 }
00066
00067 if ( mPathToId ) {
00068 const QString currentPart = mPathParts.takeFirst();
00069 bool found = false;
00070 foreach ( const Collection &c, cols ) {
00071 if ( c.name() == currentPart ) {
00072 mCurrentNode = c;
00073 found = true;
00074 break;
00075 }
00076 }
00077 if ( !found ) {
00078 q->setError( CollectionPathResolver::Unknown );
00079 q->setErrorText( i18n( "No such collection.") );
00080 q->emitResult();
00081 return;
00082 }
00083 if ( mPathParts.isEmpty() ) {
00084 mColId = mCurrentNode.id();
00085 q->emitResult();
00086 return;
00087 }
00088 nextJob = new CollectionFetchJob( mCurrentNode, CollectionFetchJob::FirstLevel, q );
00089 } else {
00090 Collection col = list->collections().first();
00091 mCurrentNode = Collection( col.parent() );
00092 mPathParts.prepend( col.name() );
00093 if ( mCurrentNode == Collection::root() ) {
00094 q->emitResult();
00095 return;
00096 }
00097 nextJob = new CollectionFetchJob( mCurrentNode, CollectionFetchJob::Base, q );
00098 }
00099 q->connect( nextJob, SIGNAL(result(KJob*)), q, SLOT(jobResult(KJob*)) );
00100 }
00101
00102 CollectionPathResolver::CollectionPathResolver(const QString & path, QObject * parent)
00103 : Job( new CollectionPathResolverPrivate( this ), parent )
00104 {
00105 Q_D( CollectionPathResolver );
00106
00107 d->mPathToId = true;
00108 d->mPath = path;
00109 if ( d->mPath.startsWith( pathDelimiter() ) )
00110 d->mPath = d->mPath.right( d->mPath.length() - pathDelimiter().length() );
00111 if ( d->mPath.endsWith( pathDelimiter() ) )
00112 d->mPath = d->mPath.left( d->mPath.length() - pathDelimiter().length() );
00113
00114 d->mPathParts = d->mPath.split( pathDelimiter() );
00115 d->mCurrentNode = Collection::root();
00116 }
00117
00118 CollectionPathResolver::CollectionPathResolver(const Collection & collection, QObject * parent)
00119 : Job( new CollectionPathResolverPrivate( this ), parent )
00120 {
00121 Q_D( CollectionPathResolver );
00122
00123 d->mPathToId = false;
00124 d->mColId = collection.id();
00125 d->mCurrentNode = collection;
00126 }
00127
00128 CollectionPathResolver::~CollectionPathResolver()
00129 {
00130 }
00131
00132 Collection::Id CollectionPathResolver::collection() const
00133 {
00134 Q_D( const CollectionPathResolver );
00135
00136 return d->mColId;
00137 }
00138
00139 QString CollectionPathResolver::path() const
00140 {
00141 Q_D( const CollectionPathResolver );
00142
00143 if ( d->mPathToId )
00144 return d->mPath;
00145 return d->mPathParts.join( pathDelimiter() );
00146 }
00147
00148 QString CollectionPathResolver::pathDelimiter()
00149 {
00150 return QLatin1String( "/" );
00151 }
00152
00153 void CollectionPathResolver::doStart()
00154 {
00155 Q_D( CollectionPathResolver );
00156
00157 CollectionFetchJob *job = 0;
00158 if ( d->mPathToId ) {
00159 if ( d->mPath.isEmpty() ) {
00160 d->mColId = Collection::root().id();
00161 emitResult();
00162 return;
00163 }
00164 job = new CollectionFetchJob( d->mCurrentNode, CollectionFetchJob::FirstLevel, this );
00165 } else {
00166 if ( d->mColId == 0 ) {
00167 d->mColId = Collection::root().id();
00168 emitResult();
00169 return;
00170 }
00171 job = new CollectionFetchJob( d->mCurrentNode, CollectionFetchJob::Base, this );
00172 }
00173 connect( job, SIGNAL(result(KJob*)), SLOT(jobResult(KJob*)) );
00174 }
00175
00176 #include "collectionpathresolver_p.moc"