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