akonadi
collectionpathresolver.cpp
00001 /* 00002 Copyright (c) 2007 Volker Krause <vkrause@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 "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 //@cond PRIVATE 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 QStringList splitPath( const QString &path ) 00044 { 00045 if ( path.isEmpty() ) // path is normalized, so non-empty means at least one hit 00046 return QStringList(); 00047 00048 QStringList rv; 00049 int begin = 0; 00050 const int pathSize( path.size() ); 00051 for ( int i = 0; i < pathSize; ++i ) { 00052 if ( path[i] == QLatin1Char('/') ) { 00053 QString pathElement = path.mid( begin, i - begin ); 00054 pathElement = pathElement.replace( QLatin1String( "\\/" ), QLatin1String( "/" ) ); 00055 rv.append( pathElement ); 00056 begin = i + 1; 00057 } 00058 if ( i < path.size() - 2 && path[i] == QLatin1Char('\\') && path[i + 1] == QLatin1Char('/') ) 00059 ++i; 00060 } 00061 QString pathElement = path.mid( begin ); 00062 pathElement = pathElement.replace( QLatin1String( "\\/" ), QLatin1String( "/" ) ); 00063 rv.append( pathElement ); 00064 return rv; 00065 } 00066 00067 Q_DECLARE_PUBLIC( CollectionPathResolver ) 00068 00069 Collection::Id mColId; 00070 QString mPath; 00071 bool mPathToId; 00072 QStringList mPathParts; 00073 Collection mCurrentNode; 00074 }; 00075 00076 void CollectionPathResolverPrivate::jobResult(KJob *job ) 00077 { 00078 if ( job->error() ) 00079 return; 00080 00081 Q_Q( CollectionPathResolver ); 00082 00083 CollectionFetchJob *list = static_cast<CollectionFetchJob*>( job ); 00084 CollectionFetchJob *nextJob = 0; 00085 const Collection::List cols = list->collections(); 00086 if ( cols.isEmpty() ) { 00087 q->setError( CollectionPathResolver::Unknown ); 00088 q->setErrorText( i18n( "No such collection." ) ); 00089 q->emitResult(); 00090 return; 00091 } 00092 00093 if ( mPathToId ) { 00094 const QString currentPart = mPathParts.takeFirst(); 00095 bool found = false; 00096 foreach ( const Collection &c, cols ) { 00097 if ( c.name() == currentPart ) { 00098 mCurrentNode = c; 00099 found = true; 00100 break; 00101 } 00102 } 00103 if ( !found ) { 00104 q->setError( CollectionPathResolver::Unknown ); 00105 q->setErrorText( i18n( "No such collection." ) ); 00106 q->emitResult(); 00107 return; 00108 } 00109 if ( mPathParts.isEmpty() ) { 00110 mColId = mCurrentNode.id(); 00111 q->emitResult(); 00112 return; 00113 } 00114 nextJob = new CollectionFetchJob( mCurrentNode, CollectionFetchJob::FirstLevel, q ); 00115 } else { 00116 Collection col = list->collections().first(); 00117 mCurrentNode = col.parentCollection(); 00118 mPathParts.prepend( col.name() ); 00119 if ( mCurrentNode == Collection::root() ) { 00120 q->emitResult(); 00121 return; 00122 } 00123 nextJob = new CollectionFetchJob( mCurrentNode, CollectionFetchJob::Base, q ); 00124 } 00125 q->connect( nextJob, SIGNAL(result(KJob*)), q, SLOT(jobResult(KJob*)) ); 00126 } 00127 00128 CollectionPathResolver::CollectionPathResolver(const QString & path, QObject * parent) 00129 : Job( new CollectionPathResolverPrivate( this ), parent ) 00130 { 00131 Q_D( CollectionPathResolver ); 00132 00133 d->mPathToId = true; 00134 d->mPath = path; 00135 if ( d->mPath.startsWith( pathDelimiter() ) ) 00136 d->mPath = d->mPath.right( d->mPath.length() - pathDelimiter().length() ); 00137 if ( d->mPath.endsWith( pathDelimiter() ) ) 00138 d->mPath = d->mPath.left( d->mPath.length() - pathDelimiter().length() ); 00139 00140 d->mPathParts = d->splitPath( d->mPath ); 00141 d->mCurrentNode = Collection::root(); 00142 } 00143 00144 CollectionPathResolver::CollectionPathResolver(const Collection & collection, QObject * parent) 00145 : Job( new CollectionPathResolverPrivate( this ), parent ) 00146 { 00147 Q_D( CollectionPathResolver ); 00148 00149 d->mPathToId = false; 00150 d->mColId = collection.id(); 00151 d->mCurrentNode = collection; 00152 } 00153 00154 CollectionPathResolver::~CollectionPathResolver() 00155 { 00156 } 00157 00158 Collection::Id CollectionPathResolver::collection() const 00159 { 00160 Q_D( const CollectionPathResolver ); 00161 00162 return d->mColId; 00163 } 00164 00165 QString CollectionPathResolver::path() const 00166 { 00167 Q_D( const CollectionPathResolver ); 00168 00169 if ( d->mPathToId ) 00170 return d->mPath; 00171 return d->mPathParts.join( pathDelimiter() ); 00172 } 00173 00174 QString CollectionPathResolver::pathDelimiter() 00175 { 00176 return QLatin1String( "/" ); 00177 } 00178 00179 void CollectionPathResolver::doStart() 00180 { 00181 Q_D( CollectionPathResolver ); 00182 00183 CollectionFetchJob *job = 0; 00184 if ( d->mPathToId ) { 00185 if ( d->mPath.isEmpty() ) { 00186 d->mColId = Collection::root().id(); 00187 emitResult(); 00188 return; 00189 } 00190 job = new CollectionFetchJob( d->mCurrentNode, CollectionFetchJob::FirstLevel, this ); 00191 } else { 00192 if ( d->mColId == 0 ) { 00193 d->mColId = Collection::root().id(); 00194 emitResult(); 00195 return; 00196 } 00197 job = new CollectionFetchJob( d->mCurrentNode, CollectionFetchJob::Base, this ); 00198 } 00199 connect( job, SIGNAL(result(KJob*)), SLOT(jobResult(KJob*)) ); 00200 } 00201 00202 //@endcond 00203 00204 #include "collectionpathresolver_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue May 8 2012 00:00:40 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue May 8 2012 00:00:40 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.