• 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
collectionfetchjob.cpp
1 /*
2  Copyright (c) 2006 - 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 "collectionfetchjob.h"
21 
22 #include "imapparser_p.h"
23 #include "job_p.h"
24 #include "protocol_p.h"
25 #include "protocolhelper_p.h"
26 #include "entity_p.h"
27 #include "collectionfetchscope.h"
28 #include "collectionutils_p.h"
29 
30 #include <kdebug.h>
31 #include <KLocalizedString>
32 
33 #include <QtCore/QHash>
34 #include <QtCore/QStringList>
35 #include <QtCore/QTimer>
36 
37 using namespace Akonadi;
38 
39 class Akonadi::CollectionFetchJobPrivate : public JobPrivate
40 {
41  public:
42  CollectionFetchJobPrivate( CollectionFetchJob *parent )
43  : JobPrivate( parent ), mEmitTimer( 0 ), mBasePrefetch( false )
44  {
45 
46  }
47 
48  void init()
49  {
50  mEmitTimer = new QTimer( q_ptr );
51  mEmitTimer->setSingleShot( true );
52  mEmitTimer->setInterval( 100 );
53  q_ptr->connect( mEmitTimer, SIGNAL(timeout()), q_ptr, SLOT(timeout()) );
54  q_ptr->connect( q_ptr, SIGNAL(result(KJob*)), q_ptr, SLOT(timeout()) );
55  }
56 
57  Q_DECLARE_PUBLIC( CollectionFetchJob )
58 
59  CollectionFetchJob::Type mType;
60  Collection mBase;
61  Collection::List mBaseList;
62  Collection::List mCollections;
63  CollectionFetchScope mScope;
64  Collection::List mPendingCollections;
65  QTimer *mEmitTimer;
66  bool mBasePrefetch;
67  Collection::List mPrefetchList;
68 
69  void timeout()
70  {
71  Q_Q( CollectionFetchJob );
72 
73  mEmitTimer->stop(); // in case we are called by result()
74  if ( !mPendingCollections.isEmpty() ) {
75  if ( !q->error() ) {
76  emit q->collectionsReceived( mPendingCollections );
77  }
78  mPendingCollections.clear();
79  }
80  }
81 
82  void subJobCollectionReceived( const Akonadi::Collection::List &collections )
83  {
84  mPendingCollections += collections;
85  if ( !mEmitTimer->isActive() ) {
86  mEmitTimer->start();
87  }
88  }
89 
90  void flushIterativeResult()
91  {
92  Q_Q( CollectionFetchJob );
93 
94  if ( mPendingCollections.isEmpty() ) {
95  return;
96  }
97 
98  emit q->collectionsReceived( mPendingCollections );
99  mPendingCollections.clear();
100  }
101 
102  QString jobDebuggingString() const
103  {
104  if ( mBase.isValid() ) {
105  return QString::fromLatin1( "Collection Id %1" ).arg( mBase.id() );
106  } else if ( CollectionUtils::hasValidHierarchicalRID( mBase ) ) {
107  return QString::fromUtf8( '(' + ProtocolHelper::hierarchicalRidToByteArray( mBase ) + ')' );
108  } else {
109  return QString::fromLatin1( "Collection RemoteId %1" ).arg( mBase.remoteId() );
110  }
111  }
112 };
113 
114 CollectionFetchJob::CollectionFetchJob( const Collection &collection, Type type, QObject *parent )
115  : Job( new CollectionFetchJobPrivate( this ), parent )
116 {
117  Q_D( CollectionFetchJob );
118  d->init();
119 
120  d->mBase = collection;
121  d->mType = type;
122 }
123 
124 CollectionFetchJob::CollectionFetchJob( const Collection::List & cols, QObject * parent )
125  : Job( new CollectionFetchJobPrivate( this ), parent )
126 {
127  Q_D( CollectionFetchJob );
128  d->init();
129 
130  Q_ASSERT( !cols.isEmpty() );
131  if ( cols.size() == 1 ) {
132  d->mBase = cols.first();
133  } else {
134  d->mBaseList = cols;
135  }
136  d->mType = CollectionFetchJob::Base;
137 }
138 
139 CollectionFetchJob::CollectionFetchJob( const Collection::List & cols, Type type, QObject * parent )
140  : Job( new CollectionFetchJobPrivate( this ), parent )
141 {
142  Q_D( CollectionFetchJob );
143  d->init();
144 
145  Q_ASSERT( !cols.isEmpty() );
146  if ( cols.size() == 1 ) {
147  d->mBase = cols.first();
148  } else {
149  d->mBaseList = cols;
150  }
151  d->mType = type;
152 }
153 
154 CollectionFetchJob::CollectionFetchJob( const QList<Collection::Id> & cols, Type type, QObject * parent )
155  : Job( new CollectionFetchJobPrivate( this ), parent )
156 {
157  Q_D( CollectionFetchJob );
158  d->init();
159 
160  Q_ASSERT( !cols.isEmpty() );
161  if ( cols.size() == 1 ) {
162  d->mBase = Collection( cols.first() );
163  } else {
164  foreach ( Collection::Id id, cols ) {
165  d->mBaseList.append( Collection( id ) );
166  }
167  }
168  d->mType = type;
169 }
170 
171 CollectionFetchJob::~CollectionFetchJob()
172 {
173 }
174 
175 Akonadi::Collection::List CollectionFetchJob::collections() const
176 {
177  Q_D( const CollectionFetchJob );
178 
179  return d->mCollections;
180 }
181 
182 void CollectionFetchJob::doStart()
183 {
184  Q_D( CollectionFetchJob );
185 
186  if ( !d->mBaseList.isEmpty() ) {
187  if ( d->mType == Recursive ) {
188  // Because doStart starts several subjobs and @p cols could contain descendants of
189  // other elements in the list, if type is Recusrive, we could end up with duplicates in the result.
190  // To fix this we require an initial fetch of @p cols with Base and RetrieveAncestors,
191  // Iterate over that result removing intersections and then perform the Recursive fetch on
192  // the remainder.
193  d->mBasePrefetch = true;
194  // No need to connect to the collectionsReceived signal here. This job is internal. The
195  // result needs to be filtered through filterDescendants before it is useful.
196  new CollectionFetchJob( d->mBaseList, NonOverlappingRoots, this );
197  } else if ( d->mType == NonOverlappingRoots ) {
198  foreach ( const Collection &col, d->mBaseList ) {
199  // No need to connect to the collectionsReceived signal here. This job is internal. The (aggregated)
200  // result needs to be filtered through filterDescendants before it is useful.
201  CollectionFetchJob *subJob = new CollectionFetchJob( col, Base, this );
202  subJob->fetchScope().setAncestorRetrieval( Akonadi::CollectionFetchScope::All );
203  }
204  } else {
205  foreach ( const Collection &col, d->mBaseList ) {
206  CollectionFetchJob *subJob = new CollectionFetchJob( col, d->mType, this );
207  connect( subJob, SIGNAL(collectionsReceived(Akonadi::Collection::List)), SLOT(subJobCollectionReceived(Akonadi::Collection::List)));
208  subJob->setFetchScope( fetchScope() );
209  }
210  }
211  return;
212  }
213 
214  if ( !d->mBase.isValid() && d->mBase.remoteId().isEmpty() ) {
215  setError( Unknown );
216  setErrorText( i18n( "Invalid collection given." ) );
217  emitResult();
218  return;
219  }
220 
221  QByteArray command = d->newTag();
222  if ( !d->mBase.isValid() ) {
223  if ( CollectionUtils::hasValidHierarchicalRID( d->mBase ) ) {
224  command += " HRID";
225  } else {
226  command += " " AKONADI_CMD_RID;
227  }
228  }
229  if ( d->mScope.includeUnsubscribed() ) {
230  command += " LIST ";
231  } else {
232  command += " LSUB ";
233  }
234 
235  if ( d->mBase.isValid() ) {
236  command += QByteArray::number( d->mBase.id() );
237  } else if ( CollectionUtils::hasValidHierarchicalRID( d->mBase ) ) {
238  command += '(' + ProtocolHelper::hierarchicalRidToByteArray( d->mBase ) + ')';
239  } else {
240  command += ImapParser::quote( d->mBase.remoteId().toUtf8() );
241  }
242 
243  command += ' ';
244  switch ( d->mType ) {
245  case Base:
246  command += "0 (";
247  break;
248  case FirstLevel:
249  command += "1 (";
250  break;
251  case Recursive:
252  command += "INF (";
253  break;
254  default:
255  Q_ASSERT( false );
256  }
257 
258  QList<QByteArray> filter;
259  if ( !d->mScope.resource().isEmpty() ) {
260  filter.append( "RESOURCE" );
261  // FIXME: Does this need to be quoted??
262  filter.append( d->mScope.resource().toUtf8() );
263  }
264 
265  if ( !d->mScope.contentMimeTypes().isEmpty() ) {
266  filter.append( "MIMETYPE" );
267  QList<QByteArray> mts;
268  foreach ( const QString &mt, d->mScope.contentMimeTypes() ) {
269  // FIXME: Does this need to be quoted??
270  mts.append( mt.toUtf8() );
271  }
272  filter.append( '(' + ImapParser::join( mts, " " ) + ')' );
273  }
274 
275  QList<QByteArray> options;
276  if ( d->mScope.includeStatistics() ) {
277  options.append( "STATISTICS" );
278  options.append( "true" );
279  }
280  if ( d->mScope.ancestorRetrieval() != CollectionFetchScope::None ) {
281  options.append( "ANCESTORS" );
282  switch ( d->mScope.ancestorRetrieval() ) {
283  case CollectionFetchScope::None:
284  options.append( "0" );
285  break;
286  case CollectionFetchScope::Parent:
287  options.append( "1" );
288  break;
289  case CollectionFetchScope::All:
290  options.append( "INF" );
291  break;
292  default:
293  Q_ASSERT( false );
294  }
295  }
296 
297  command += ImapParser::join( filter, " " ) + ") (" + ImapParser::join( options, " " ) + ")\n";
298  d->writeData( command );
299 }
300 
301 void CollectionFetchJob::doHandleResponse( const QByteArray & tag, const QByteArray & data )
302 {
303  Q_D( CollectionFetchJob );
304 
305  if ( d->mBasePrefetch || d->mType == NonOverlappingRoots ) {
306  return;
307  }
308 
309  if ( tag == "*" ) {
310  Collection collection;
311  ProtocolHelper::parseCollection( data, collection );
312  if ( !collection.isValid() ) {
313  return;
314  }
315 
316  collection.d_ptr->resetChangeLog();
317  d->mCollections.append( collection );
318  d->mPendingCollections.append( collection );
319  if ( !d->mEmitTimer->isActive() ) {
320  d->mEmitTimer->start();
321  }
322  return;
323  }
324  kDebug() << "Unhandled server response" << tag << data;
325 }
326 
327 void CollectionFetchJob::setResource(const QString & resource)
328 {
329  Q_D( CollectionFetchJob );
330 
331  d->mScope.setResource( resource );
332 }
333 
334 static Collection::List filterDescendants( const Collection::List &list )
335 {
336  Collection::List result;
337 
338  QVector<QList<Collection::Id> > ids;
339  foreach ( const Collection &collection, list ) {
340  QList<Collection::Id> ancestors;
341  Collection parent = collection.parentCollection();
342  ancestors << parent.id();
343  if ( parent != Collection::root() ) {
344  while ( parent.parentCollection() != Collection::root() ) {
345  parent = parent.parentCollection();
346  QList<Collection::Id>::iterator i = qLowerBound( ancestors.begin(), ancestors.end(), parent.id() );
347  ancestors.insert( i, parent.id() );
348  }
349  }
350  ids << ancestors;
351  }
352 
353  QSet<Collection::Id> excludeList;
354  foreach ( const Collection &collection, list ) {
355  int i = 0;
356  foreach ( const QList<Collection::Id> &ancestors, ids ) {
357  if ( qBinaryFind( ancestors, collection.id() ) != ancestors.end() ) {
358  excludeList.insert( list.at( i ).id() );
359  }
360  ++i;
361  }
362  }
363 
364  foreach ( const Collection &collection, list ) {
365  if ( !excludeList.contains( collection.id() ) ) {
366  result.append( collection );
367  }
368  }
369 
370  return result;
371 }
372 
373 void CollectionFetchJob::slotResult(KJob * job)
374 {
375  Q_D( CollectionFetchJob );
376 
377  CollectionFetchJob *list = qobject_cast<CollectionFetchJob*>( job );
378  Q_ASSERT( job );
379  if ( d->mBasePrefetch ) {
380  d->mBasePrefetch = false;
381  const Collection::List roots = list->collections();
382  Job::slotResult( job );
383  Q_ASSERT( !hasSubjobs() );
384  if ( !job->error() ) {
385  foreach ( const Collection &col, roots ) {
386  CollectionFetchJob *subJob = new CollectionFetchJob( col, d->mType, this );
387  connect( subJob, SIGNAL(collectionsReceived(Akonadi::Collection::List)), SLOT(subJobCollectionReceived(Akonadi::Collection::List)) );
388  subJob->setFetchScope( fetchScope() );
389  }
390  }
391  // No result yet.
392  } else if ( d->mType == NonOverlappingRoots ) {
393  d->mPrefetchList += list->collections();
394  Job::slotResult( job );
395  if ( !job->error() && !hasSubjobs() ) {
396  const Collection::List result = filterDescendants( d->mPrefetchList );
397  d->mPendingCollections += result;
398  d->mCollections = result;
399  d->flushIterativeResult();
400  emitResult();
401  }
402  } else {
403  // We need to tell the subjob to emit its collectionsReceived signal before
404  // the result signal is emitted. That will populate my mPendingCollections
405  // which will be flushed by calling emitResult which will cause
406  // CollectionFetchJobPrivate::timeout to be called.
407  list->d_func()->flushIterativeResult();
408  d->mCollections += list->collections();
409  // Pending collections should have already been emitted by listening to (and flushing) the job.
410  Job::slotResult( job );
411  if ( !job->error() && !hasSubjobs() ) {
412  emitResult();
413  }
414  }
415 }
416 
417 void CollectionFetchJob::includeUnsubscribed(bool include)
418 {
419  Q_D( CollectionFetchJob );
420 
421  d->mScope.setIncludeUnsubscribed( include );
422 }
423 
424 void CollectionFetchJob::includeStatistics(bool include)
425 {
426  Q_D( CollectionFetchJob );
427 
428  d->mScope.setIncludeStatistics( include );
429 }
430 
431 void CollectionFetchJob::setFetchScope( const CollectionFetchScope &scope )
432 {
433  Q_D( CollectionFetchJob );
434  d->mScope = scope;
435 }
436 
437 CollectionFetchScope& CollectionFetchJob::fetchScope()
438 {
439  Q_D( CollectionFetchJob );
440  return d->mScope;
441 }
442 
443 #include "moc_collectionfetchjob.cpp"
Akonadi::CollectionFetchScope::setAncestorRetrieval
void setAncestorRetrieval(AncestorRetrieval ancestorDepth)
Sets how many levels of ancestor collections should be included in the retrieval. ...
Definition: collectionfetchscope.cpp:134
Akonadi::CollectionFetchJob::collections
Collection::List collections() const
Returns the list of fetched collection.
Definition: collectionfetchjob.cpp:175
Akonadi::CollectionFetchJob::includeUnsubscribed
AKONADI_DEPRECATED void includeUnsubscribed(bool include=true)
Include also unsubscribed collections.
Definition: collectionfetchjob.cpp:417
Akonadi::CollectionFetchJob::CollectionFetchJob
CollectionFetchJob(const Collection &collection, Type type=FirstLevel, QObject *parent=0)
Creates a new collection fetch job.
Definition: collectionfetchjob.cpp:114
Akonadi::CollectionFetchScope
Specifies which parts of a collection should be fetched from the Akonadi storage. ...
Definition: collectionfetchscope.h:68
Akonadi::CollectionFetchJob::Type
Type
Describes the type of fetch depth.
Definition: collectionfetchjob.h:61
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::CollectionFetchJob::fetchScope
CollectionFetchScope & fetchScope()
Returns the collection fetch scope.
Definition: collectionfetchjob.cpp:437
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::CollectionFetchJob::NonOverlappingRoots
List the roots of a list of fetched collections.
Definition: collectionfetchjob.h:65
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::CollectionFetchJob::collectionsReceived
void collectionsReceived(const Akonadi::Collection::List &collections)
This signal is emitted whenever the job has received collections.
Akonadi::CollectionFetchJob::doHandleResponse
virtual void doHandleResponse(const QByteArray &tag, const QByteArray &data)
This method should be reimplemented in the concrete jobs in case you want to handle incoming data...
Definition: collectionfetchjob.cpp:301
Akonadi::CollectionFetchScope::Parent
Only retrieve the immediate parent collection.
Definition: collectionfetchscope.h:76
Akonadi::ProtocolHelper::parseCollection
static int parseCollection(const QByteArray &data, Collection &collection, int start=0)
Parse a collection description.
Definition: protocolhelper.cpp:129
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::Entity::remoteId
QString remoteId() const
Returns the remote id of the entity.
Definition: entity.cpp:82
Akonadi::CollectionFetchScope::All
Retrieve all ancestors, up to Collection::root()
Definition: collectionfetchscope.h:77
Akonadi::ProtocolHelper::hierarchicalRidToByteArray
static QByteArray hierarchicalRidToByteArray(const Collection &col)
Converts the given collection's hierarchical RID into a protocol representation.
Definition: protocolhelper.cpp:282
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::CollectionFetchJob::setFetchScope
void setFetchScope(const CollectionFetchScope &fetchScope)
Sets the collection fetch scope.
Definition: collectionfetchjob.cpp:431
Akonadi::CollectionFetchJob::doStart
virtual void doStart()
This method must be reimplemented in the concrete jobs.
Definition: collectionfetchjob.cpp:182
Akonadi::CollectionFetchJob::includeStatistics
AKONADI_DEPRECATED void includeStatistics(bool include=true)
Include also statistics about the collections.
Definition: collectionfetchjob.cpp:424
Akonadi::JobPrivate
Definition: job_p.h:31
Akonadi::CollectionFetchJob::~CollectionFetchJob
virtual ~CollectionFetchJob()
Destroys the collection fetch job.
Definition: collectionfetchjob.cpp:171
Akonadi::Entity::isValid
bool isValid() const
Returns whether the entity is valid.
Definition: entity.cpp:97
Akonadi::CollectionFetchScope::None
No ancestor retrieval at all (the default)
Definition: collectionfetchscope.h:75
Akonadi::CollectionFetchJob::Recursive
List all sub-collections.
Definition: collectionfetchjob.h:64
Akonadi::Collection::List
QList< Collection > List
Describes a list of collections.
Definition: collection.h:81
Akonadi::CollectionFetchJob::setResource
AKONADI_DEPRECATED void setResource(const QString &resource)
Sets a resource identifier to limit collection listing to one resource.
Definition: collectionfetchjob.cpp:327
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