• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • akonadi
collectionpathresolver.cpp
1 /*
2  Copyright (c) 2007 Volker Krause <vkrause@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "collectionpathresolver_p.h"
21 
22 #include "collectionfetchjob.h"
23 #include "job_p.h"
24 
25 #include <klocalizedstring.h>
26 
27 #include <QtCore/QStringList>
28 
29 using namespace Akonadi;
30 
31 //@cond PRIVATE
32 
33 class Akonadi::CollectionPathResolverPrivate : public JobPrivate
34 {
35  public:
36  CollectionPathResolverPrivate( CollectionPathResolver *parent )
37  : JobPrivate( parent )
38  {
39  }
40 
41  void jobResult( KJob* );
42 
43  QStringList splitPath( const QString &path )
44  {
45  if ( path.isEmpty() ) { // path is normalized, so non-empty means at least one hit
46  return QStringList();
47  }
48 
49  QStringList rv;
50  int begin = 0;
51  const int pathSize( path.size() );
52  for ( int i = 0; i < pathSize; ++i ) {
53  if ( path[i] == QLatin1Char( '/' ) ) {
54  QString pathElement = path.mid( begin, i - begin );
55  pathElement = pathElement.replace( QLatin1String( "\\/" ), QLatin1String( "/" ) );
56  rv.append( pathElement );
57  begin = i + 1;
58  }
59  if ( i < path.size() - 2 && path[i] == QLatin1Char( '\\' ) && path[i + 1] == QLatin1Char( '/' ) ) {
60  ++i;
61  }
62  }
63  QString pathElement = path.mid( begin );
64  pathElement = pathElement.replace( QLatin1String( "\\/" ), QLatin1String( "/" ) );
65  rv.append( pathElement );
66  return rv;
67  }
68 
69  Q_DECLARE_PUBLIC( CollectionPathResolver )
70 
71  Collection::Id mColId;
72  QString mPath;
73  bool mPathToId;
74  QStringList mPathParts;
75  Collection mCurrentNode;
76 };
77 
78 void CollectionPathResolverPrivate::jobResult(KJob *job )
79 {
80  if ( job->error() ) {
81  return;
82  }
83 
84  Q_Q( CollectionPathResolver );
85 
86  CollectionFetchJob *list = static_cast<CollectionFetchJob*>( job );
87  CollectionFetchJob *nextJob = 0;
88  const Collection::List cols = list->collections();
89  if ( cols.isEmpty() ) {
90  q->setError( CollectionPathResolver::Unknown );
91  q->setErrorText( i18n( "No such collection." ) );
92  q->emitResult();
93  return;
94  }
95 
96  if ( mPathToId ) {
97  const QString currentPart = mPathParts.takeFirst();
98  bool found = false;
99  foreach ( const Collection &c, cols ) {
100  if ( c.name() == currentPart ) {
101  mCurrentNode = c;
102  found = true;
103  break;
104  }
105  }
106  if ( !found ) {
107  q->setError( CollectionPathResolver::Unknown );
108  q->setErrorText( i18n( "No such collection." ) );
109  q->emitResult();
110  return;
111  }
112  if ( mPathParts.isEmpty() ) {
113  mColId = mCurrentNode.id();
114  q->emitResult();
115  return;
116  }
117  nextJob = new CollectionFetchJob( mCurrentNode, CollectionFetchJob::FirstLevel, q );
118  } else {
119  Collection col = list->collections().first();
120  mCurrentNode = col.parentCollection();
121  mPathParts.prepend( col.name() );
122  if ( mCurrentNode == Collection::root() ) {
123  q->emitResult();
124  return;
125  }
126  nextJob = new CollectionFetchJob( mCurrentNode, CollectionFetchJob::Base, q );
127  }
128  q->connect( nextJob, SIGNAL(result(KJob*)), q, SLOT(jobResult(KJob*)) );
129 }
130 
131 CollectionPathResolver::CollectionPathResolver(const QString & path, QObject * parent)
132  : Job( new CollectionPathResolverPrivate( this ), parent )
133 {
134  Q_D( CollectionPathResolver );
135 
136  d->mPathToId = true;
137  d->mPath = path;
138  if ( d->mPath.startsWith( pathDelimiter() ) ) {
139  d->mPath = d->mPath.right( d->mPath.length() - pathDelimiter().length() );
140  }
141  if ( d->mPath.endsWith( pathDelimiter() ) ) {
142  d->mPath = d->mPath.left( d->mPath.length() - pathDelimiter().length() );
143  }
144 
145  d->mPathParts = d->splitPath( d->mPath );
146  d->mCurrentNode = Collection::root();
147 }
148 
149 CollectionPathResolver::CollectionPathResolver(const Collection & collection, QObject * parent)
150  : Job( new CollectionPathResolverPrivate( this ), parent )
151 {
152  Q_D( CollectionPathResolver );
153 
154  d->mPathToId = false;
155  d->mColId = collection.id();
156  d->mCurrentNode = collection;
157 }
158 
159 CollectionPathResolver::~CollectionPathResolver()
160 {
161 }
162 
163 Collection::Id CollectionPathResolver::collection() const
164 {
165  Q_D( const CollectionPathResolver );
166 
167  return d->mColId;
168 }
169 
170 QString CollectionPathResolver::path() const
171 {
172  Q_D( const CollectionPathResolver );
173 
174  if ( d->mPathToId ) {
175  return d->mPath;
176  }
177  return d->mPathParts.join( pathDelimiter() );
178 }
179 
180 QString CollectionPathResolver::pathDelimiter()
181 {
182  return QLatin1String( "/" );
183 }
184 
185 void CollectionPathResolver::doStart()
186 {
187  Q_D( CollectionPathResolver );
188 
189  CollectionFetchJob *job = 0;
190  if ( d->mPathToId ) {
191  if ( d->mPath.isEmpty() ) {
192  d->mColId = Collection::root().id();
193  emitResult();
194  return;
195  }
196  job = new CollectionFetchJob( d->mCurrentNode, CollectionFetchJob::FirstLevel, this );
197  } else {
198  if ( d->mColId == 0 ) {
199  d->mColId = Collection::root().id();
200  emitResult();
201  return;
202  }
203  job = new CollectionFetchJob( d->mCurrentNode, CollectionFetchJob::Base, this );
204  }
205  connect( job, SIGNAL(result(KJob*)), SLOT(jobResult(KJob*)) );
206 }
207 
208 //@endcond
209 
210 #include "moc_collectionpathresolver_p.cpp"
Akonadi::CollectionPathResolver::CollectionPathResolver
CollectionPathResolver(const QString &path, QObject *parent=0)
Creates a new collection path resolver to convert a path into a id.
Akonadi::CollectionFetchJob::collections
Collection::List collections() const
Returns the list of fetched collection.
Definition: collectionfetchjob.cpp:175
Akonadi::CollectionPathResolver::collection
Collection::Id collection() const
Returns the collection id.
Akonadi::Collection::name
QString name() const
Returns the i18n'ed name of the collection.
Definition: collection.cpp:81
Akonadi::Job::Unknown
Unknown error.
Definition: job.h:109
Akonadi::CollectionFetchJob::FirstLevel
Only list direct sub-collections of the base collection.
Definition: collectionfetchjob.h:63
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::CollectionFetchJob
Job that fetches collections from the Akonadi storage.
Definition: collectionfetchjob.h:53
Akonadi::Entity::Id
qint64 Id
Describes the unique id type.
Definition: entity.h:65
Akonadi::Job
Base class for all actions in the Akonadi storage.
Definition: job.h:86
Akonadi::CollectionPathResolver
Definition: collectionpathresolver_p.h:42
Akonadi::CollectionPathResolver::~CollectionPathResolver
~CollectionPathResolver()
Destroys the collection path resolver.
Akonadi::CollectionFetchJob::Base
Only fetch the base collection.
Definition: collectionfetchjob.h:62
Akonadi::Entity::parentCollection
Collection parentCollection() const
Returns the parent collection of this object.
Definition: entity.cpp:186
Akonadi::CollectionPathResolver::path
QString path() const
Returns the collection path.
Akonadi::Collection::root
static Collection root()
Returns the root collection.
Definition: collection.cpp:192
Akonadi::Entity::id
Id id() const
Returns the unique identifier of the entity.
Definition: entity.cpp:72
Akonadi::JobPrivate
Definition: job_p.h:31
Akonadi::CollectionPathResolver::pathDelimiter
static QString pathDelimiter()
Returns the path delimiter for collections.
Akonadi::Collection::List
QList< Collection > List
Describes a list of collections.
Definition: collection.h:81
Akonadi::CollectionPathResolver::doStart
void doStart()
This method must be reimplemented in the concrete jobs.
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:26 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal