20 #include "collectionpathresolver_p.h"
22 #include "collectionfetchjob.h"
27 #include <QtCore/QStringList>
29 using namespace Akonadi;
33 class Akonadi::CollectionPathResolverPrivate :
public JobPrivate
36 CollectionPathResolverPrivate( CollectionPathResolver *parent )
37 : JobPrivate( parent )
41 void jobResult( KJob* );
43 QStringList splitPath(
const QString &path )
50 const int pathSize( path.size() );
51 for (
int i = 0; i < pathSize; ++i ) {
52 if ( path[i] == QLatin1Char(
'/') ) {
53 QString pathElement = path.mid( begin, i - begin );
54 pathElement = pathElement.replace( QLatin1String(
"\\/" ), QLatin1String(
"/" ) );
55 rv.append( pathElement );
58 if ( i < path.size() - 2 && path[i] == QLatin1Char(
'\\') && path[i + 1] == QLatin1Char(
'/') )
61 QString pathElement = path.mid( begin );
62 pathElement = pathElement.replace( QLatin1String(
"\\/" ), QLatin1String(
"/" ) );
63 rv.append( pathElement );
67 Q_DECLARE_PUBLIC( CollectionPathResolver )
69 Collection::Id mColId;
72 QStringList mPathParts;
73 Collection mCurrentNode;
76 void CollectionPathResolverPrivate::jobResult(KJob *job )
81 Q_Q( CollectionPathResolver );
83 CollectionFetchJob *list =
static_cast<CollectionFetchJob*
>( job );
84 CollectionFetchJob *nextJob = 0;
85 const Collection::List cols = list->collections();
86 if ( cols.isEmpty() ) {
87 q->setError( CollectionPathResolver::Unknown );
88 q->setErrorText( i18n(
"No such collection." ) );
94 const QString currentPart = mPathParts.takeFirst();
96 foreach (
const Collection &c, cols ) {
97 if ( c.name() == currentPart ) {
104 q->setError( CollectionPathResolver::Unknown );
105 q->setErrorText( i18n(
"No such collection." ) );
109 if ( mPathParts.isEmpty() ) {
110 mColId = mCurrentNode.id();
114 nextJob =
new CollectionFetchJob( mCurrentNode, CollectionFetchJob::FirstLevel, q );
116 Collection col = list->collections().first();
117 mCurrentNode = col.parentCollection();
118 mPathParts.prepend( col.name() );
119 if ( mCurrentNode == Collection::root() ) {
123 nextJob =
new CollectionFetchJob( mCurrentNode, CollectionFetchJob::Base, q );
125 q->connect( nextJob, SIGNAL(result(KJob*)), q, SLOT(jobResult(KJob*)) );
128 CollectionPathResolver::CollectionPathResolver(
const QString & path, QObject * parent)
129 : Job( new CollectionPathResolverPrivate( this ), parent )
131 Q_D( CollectionPathResolver );
135 if ( d->mPath.startsWith( pathDelimiter() ) )
136 d->mPath = d->mPath.right( d->mPath.length() - pathDelimiter().length() );
137 if ( d->mPath.endsWith( pathDelimiter() ) )
138 d->mPath = d->mPath.left( d->mPath.length() - pathDelimiter().length() );
140 d->mPathParts = d->splitPath( d->mPath );
141 d->mCurrentNode = Collection::root();
144 CollectionPathResolver::CollectionPathResolver(
const Collection & collection, QObject * parent)
145 : Job( new CollectionPathResolverPrivate( this ), parent )
147 Q_D( CollectionPathResolver );
149 d->mPathToId =
false;
150 d->mColId = collection.id();
151 d->mCurrentNode = collection;
154 CollectionPathResolver::~CollectionPathResolver()
158 Collection::Id CollectionPathResolver::collection()
const
160 Q_D(
const CollectionPathResolver );
165 QString CollectionPathResolver::path()
const
167 Q_D(
const CollectionPathResolver );
171 return d->mPathParts.join( pathDelimiter() );
174 QString CollectionPathResolver::pathDelimiter()
176 return QLatin1String(
"/" );
179 void CollectionPathResolver::doStart()
181 Q_D( CollectionPathResolver );
183 CollectionFetchJob *job = 0;
184 if ( d->mPathToId ) {
185 if ( d->mPath.isEmpty() ) {
186 d->mColId = Collection::root().id();
190 job =
new CollectionFetchJob( d->mCurrentNode, CollectionFetchJob::FirstLevel,
this );
192 if ( d->mColId == 0 ) {
193 d->mColId = Collection::root().id();
197 job =
new CollectionFetchJob( d->mCurrentNode, CollectionFetchJob::Base,
this );
199 connect( job, SIGNAL(result(KJob*)), SLOT(jobResult(KJob*)) );
204 #include "collectionpathresolver_p.moc"