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

Skip menu "kdepimlibs-4.10.5 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