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

akonadi

  • 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 <klocale.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  QStringList rv;
49  int begin = 0;
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 );
56  begin = i + 1;
57  }
58  if ( i < path.size() - 2 && path[i] == QLatin1Char('\\') && path[i + 1] == QLatin1Char('/') )
59  ++i;
60  }
61  QString pathElement = path.mid( begin );
62  pathElement = pathElement.replace( QLatin1String( "\\/" ), QLatin1String( "/" ) );
63  rv.append( pathElement );
64  return rv;
65  }
66 
67  Q_DECLARE_PUBLIC( CollectionPathResolver )
68 
69  Collection::Id mColId;
70  QString mPath;
71  bool mPathToId;
72  QStringList mPathParts;
73  Collection mCurrentNode;
74 };
75 
76 void CollectionPathResolverPrivate::jobResult(KJob *job )
77 {
78  if ( job->error() )
79  return;
80 
81  Q_Q( CollectionPathResolver );
82 
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." ) );
89  q->emitResult();
90  return;
91  }
92 
93  if ( mPathToId ) {
94  const QString currentPart = mPathParts.takeFirst();
95  bool found = false;
96  foreach ( const Collection &c, cols ) {
97  if ( c.name() == currentPart ) {
98  mCurrentNode = c;
99  found = true;
100  break;
101  }
102  }
103  if ( !found ) {
104  q->setError( CollectionPathResolver::Unknown );
105  q->setErrorText( i18n( "No such collection." ) );
106  q->emitResult();
107  return;
108  }
109  if ( mPathParts.isEmpty() ) {
110  mColId = mCurrentNode.id();
111  q->emitResult();
112  return;
113  }
114  nextJob = new CollectionFetchJob( mCurrentNode, CollectionFetchJob::FirstLevel, q );
115  } else {
116  Collection col = list->collections().first();
117  mCurrentNode = col.parentCollection();
118  mPathParts.prepend( col.name() );
119  if ( mCurrentNode == Collection::root() ) {
120  q->emitResult();
121  return;
122  }
123  nextJob = new CollectionFetchJob( mCurrentNode, CollectionFetchJob::Base, q );
124  }
125  q->connect( nextJob, SIGNAL(result(KJob*)), q, SLOT(jobResult(KJob*)) );
126 }
127 
128 CollectionPathResolver::CollectionPathResolver(const QString & path, QObject * parent)
129  : Job( new CollectionPathResolverPrivate( this ), parent )
130 {
131  Q_D( CollectionPathResolver );
132 
133  d->mPathToId = true;
134  d->mPath = path;
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() );
139 
140  d->mPathParts = d->splitPath( d->mPath );
141  d->mCurrentNode = Collection::root();
142 }
143 
144 CollectionPathResolver::CollectionPathResolver(const Collection & collection, QObject * parent)
145  : Job( new CollectionPathResolverPrivate( this ), parent )
146 {
147  Q_D( CollectionPathResolver );
148 
149  d->mPathToId = false;
150  d->mColId = collection.id();
151  d->mCurrentNode = collection;
152 }
153 
154 CollectionPathResolver::~CollectionPathResolver()
155 {
156 }
157 
158 Collection::Id CollectionPathResolver::collection() const
159 {
160  Q_D( const CollectionPathResolver );
161 
162  return d->mColId;
163 }
164 
165 QString CollectionPathResolver::path() const
166 {
167  Q_D( const CollectionPathResolver );
168 
169  if ( d->mPathToId )
170  return d->mPath;
171  return d->mPathParts.join( pathDelimiter() );
172 }
173 
174 QString CollectionPathResolver::pathDelimiter()
175 {
176  return QLatin1String( "/" );
177 }
178 
179 void CollectionPathResolver::doStart()
180 {
181  Q_D( CollectionPathResolver );
182 
183  CollectionFetchJob *job = 0;
184  if ( d->mPathToId ) {
185  if ( d->mPath.isEmpty() ) {
186  d->mColId = Collection::root().id();
187  emitResult();
188  return;
189  }
190  job = new CollectionFetchJob( d->mCurrentNode, CollectionFetchJob::FirstLevel, this );
191  } else {
192  if ( d->mColId == 0 ) {
193  d->mColId = Collection::root().id();
194  emitResult();
195  return;
196  }
197  job = new CollectionFetchJob( d->mCurrentNode, CollectionFetchJob::Base, this );
198  }
199  connect( job, SIGNAL(result(KJob*)), SLOT(jobResult(KJob*)) );
200 }
201 
202 //@endcond
203 
204 #include "collectionpathresolver_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jan 5 2013 19:46:02 by doxygen 1.8.1.2 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-4.9.5 API Reference

Skip menu "kdepimlibs-4.9.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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