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

akonadi

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

Skip menu "kdepimlibs-4.13.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • 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