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

akonadi

  • akonadi
itemfetchjob.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 "itemfetchjob.h"
21 
22 #include "attributefactory.h"
23 #include "collection.h"
24 #include "collectionselectjob_p.h"
25 #include "imapparser_p.h"
26 #include "itemfetchscope.h"
27 #include "job_p.h"
28 #include "protocol_p.h"
29 #include "protocolhelper_p.h"
30 #include "session_p.h"
31 
32 #include <kdebug.h>
33 #include <KLocalizedString>
34 
35 #include <QtCore/QStringList>
36 #include <QtCore/QTimer>
37 
38 using namespace Akonadi;
39 
40 class Akonadi::ItemFetchJobPrivate : public JobPrivate
41 {
42  public:
43  ItemFetchJobPrivate( ItemFetchJob *parent )
44  : JobPrivate( parent ),
45  mEmitTimer( 0 ),
46  mValuePool( 0 )
47  {
48  mCollection = Collection::root();
49  mDeliveryOptions = ItemFetchJob::Default;
50  }
51 
52  ~ItemFetchJobPrivate()
53  {
54  delete mValuePool;
55  }
56 
57  void init()
58  {
59  Q_Q( ItemFetchJob );
60  mEmitTimer = new QTimer( q );
61  mEmitTimer->setSingleShot( true );
62  mEmitTimer->setInterval( 100 );
63  q->connect( mEmitTimer, SIGNAL(timeout()), q, SLOT(timeout()) );
64  }
65 
66  void aboutToFinish()
67  {
68  timeout();
69  }
70 
71  void timeout()
72  {
73  Q_Q( ItemFetchJob );
74 
75  mEmitTimer->stop(); // in case we are called by aboutToFinish()
76  if ( !mPendingItems.isEmpty() ) {
77  if ( !q->error() )
78  emit q->itemsReceived( mPendingItems );
79  mPendingItems.clear();
80  }
81  }
82 
83  void startFetchJob();
84  void selectDone( KJob * job );
85 
86  QString jobDebuggingString() const /*Q_DECL_OVERRIDE*/ {
87  if ( mRequestedItems.isEmpty() ) {
88  QString str = QString::fromLatin1( "All items from collection %1" ).arg( mCollection.id() );
89  if ( mFetchScope.fetchChangedSince().isValid() )
90  str += QString::fromLatin1( " changed since %1" ).arg( mFetchScope.fetchChangedSince().toString() );
91  return str;
92  } else {
93  try {
94  return QString::fromLatin1( ProtocolHelper::entitySetToByteArray( mRequestedItems, AKONADI_CMD_ITEMFETCH ) );
95  } catch ( const Exception &e ) {
96  return QString::fromUtf8( e.what() );
97  }
98  }
99  }
100 
101  Q_DECLARE_PUBLIC( ItemFetchJob )
102 
103  Collection mCollection;
104  Item::List mRequestedItems;
105  Item::List mResultItems;
106  ItemFetchScope mFetchScope;
107  Item::List mPendingItems; // items pending for emitting itemsReceived()
108  QTimer* mEmitTimer;
109  ProtocolHelperValuePool *mValuePool;
110  ItemFetchJob::DeliveryOptions mDeliveryOptions;
111 };
112 
113 void ItemFetchJobPrivate::startFetchJob()
114 {
115  Q_Q( ItemFetchJob );
116  QByteArray command = newTag();
117  if ( mRequestedItems.isEmpty() ) {
118  command += " " AKONADI_CMD_ITEMFETCH " 1:*";
119  } else {
120  try {
121  command += ProtocolHelper::entitySetToByteArray( mRequestedItems, AKONADI_CMD_ITEMFETCH );
122  } catch ( const Exception &e ) {
123  q->setError( Job::Unknown );
124  q->setErrorText( QString::fromUtf8( e.what() ) );
125  q->emitResult();
126  return;
127  }
128  }
129 
130  //This is only required for 4.10
131  if ( protocolVersion() < 30 ) {
132  if ( mFetchScope.ignoreRetrievalErrors() ) {
133  kDebug() << "IGNOREERRORS is not available with this akonadi protocol version";
134  }
135  mFetchScope.setIgnoreRetrievalErrors( false );
136  }
137  command += ProtocolHelper::itemFetchScopeToByteArray( mFetchScope );
138 
139  writeData( command );
140 }
141 
142 void ItemFetchJobPrivate::selectDone( KJob * job )
143 {
144  if ( !job->error() )
145  // the collection is now selected, fetch the message(s)
146  startFetchJob();
147 }
148 
149 ItemFetchJob::ItemFetchJob( const Collection &collection, QObject * parent )
150  : Job( new ItemFetchJobPrivate( this ), parent )
151 {
152  Q_D( ItemFetchJob );
153 
154  d->init();
155  d->mCollection = collection;
156  d->mValuePool = new ProtocolHelperValuePool; // only worth it for lots of results
157 }
158 
159 ItemFetchJob::ItemFetchJob( const Item & item, QObject * parent)
160  : Job( new ItemFetchJobPrivate( this ), parent )
161 {
162  Q_D( ItemFetchJob );
163 
164  d->init();
165  d->mRequestedItems.append( item );
166 }
167 
168 ItemFetchJob::ItemFetchJob(const Akonadi::Item::List& items, QObject* parent)
169  : Job( new ItemFetchJobPrivate( this ), parent )
170 {
171  Q_D( ItemFetchJob );
172 
173  d->init();
174  d->mRequestedItems = items;
175 }
176 
177 ItemFetchJob::ItemFetchJob(const QList<Akonadi::Item::Id>& items, QObject* parent)
178  : Job( new ItemFetchJobPrivate( this ), parent )
179 {
180  Q_D( ItemFetchJob );
181 
182  d->init();
183  foreach(Item::Id id, items)
184  d->mRequestedItems.append(Item(id));
185 }
186 
187 ItemFetchJob::~ItemFetchJob()
188 {
189 }
190 
191 void ItemFetchJob::doStart()
192 {
193  Q_D( ItemFetchJob );
194 
195  if ( d->mCollection == Collection::root() ) {
196  if ( d->mRequestedItems.isEmpty() ) { // collection content listing
197  setErrorText( i18n( "Cannot list root collection." ) );
198  setError( Unknown );
199  emitResult();
200  } else {
201  d->startFetchJob();
202  }
203  } else {
204  CollectionSelectJob *job = new CollectionSelectJob( d->mCollection, this );
205  connect( job, SIGNAL(result(KJob*)), SLOT(selectDone(KJob*)) );
206  addSubjob( job );
207  }
208 }
209 
210 void ItemFetchJob::doHandleResponse( const QByteArray & tag, const QByteArray & data )
211 {
212  Q_D( ItemFetchJob );
213 
214  if ( tag == "*" ) {
215  int begin = data.indexOf( "FETCH" );
216  if ( begin >= 0 ) {
217 
218  // split fetch response into key/value pairs
219  QList<QByteArray> fetchResponse;
220  ImapParser::parseParenthesizedList( data, fetchResponse, begin + 6 );
221 
222  Item item;
223  ProtocolHelper::parseItemFetchResult( fetchResponse, item, d->mValuePool );
224  if ( !item.isValid() )
225  return;
226 
227  if ( d->mDeliveryOptions & ItemGetter ) {
228  d->mResultItems.append( item );
229  }
230 
231  if ( d->mDeliveryOptions & EmitItemsInBatches ) {
232  d->mPendingItems.append( item );
233  if ( !d->mEmitTimer->isActive() )
234  d->mEmitTimer->start();
235  }
236  else if ( d->mDeliveryOptions & EmitItemsIndividually ) {
237  emit itemsReceived( Item::List() << item );
238  }
239  return;
240  }
241  }
242  kDebug() << "Unhandled response: " << tag << data;
243 }
244 
245 Item::List ItemFetchJob::items() const
246 {
247  Q_D( const ItemFetchJob );
248 
249  return d->mResultItems;
250 }
251 
252 void ItemFetchJob::clearItems()
253 {
254  Q_D( ItemFetchJob );
255 
256  d->mResultItems.clear();
257 }
258 
259 void ItemFetchJob::setFetchScope( ItemFetchScope &fetchScope )
260 {
261  Q_D( ItemFetchJob );
262 
263  d->mFetchScope = fetchScope;
264 }
265 
266 void ItemFetchJob::setFetchScope( const ItemFetchScope &fetchScope )
267 {
268  Q_D( ItemFetchJob );
269 
270  d->mFetchScope = fetchScope;
271 }
272 
273 ItemFetchScope &ItemFetchJob::fetchScope()
274 {
275  Q_D( ItemFetchJob );
276 
277  return d->mFetchScope;
278 }
279 
280 void ItemFetchJob::setCollection(const Akonadi::Collection& collection)
281 {
282  Q_D( ItemFetchJob );
283 
284  d->mCollection = collection;
285 }
286 
287 void ItemFetchJob::setDeliveryOption(DeliveryOptions options)
288 {
289  Q_D( ItemFetchJob );
290 
291  d->mDeliveryOptions = options;
292 }
293 
294 ItemFetchJob::DeliveryOptions ItemFetchJob::deliveryOptions() const
295 {
296  Q_D( const ItemFetchJob );
297 
298  return d->mDeliveryOptions;
299 }
300 
301 
302 #include "moc_itemfetchjob.cpp"
Akonadi::ItemFetchJob::itemsReceived
void itemsReceived(const Akonadi::Item::List &items)
This signal is emitted whenever new items have been fetched completely.
Akonadi::ItemFetchScope::setIgnoreRetrievalErrors
void setIgnoreRetrievalErrors(bool enabled)
Ignore retrieval errors while fetching items, and always deliver what is available.
Definition: itemfetchscope.cpp:153
Akonadi::CollectionSelectJob
Definition: collectionselectjob_p.h:34
Akonadi::ItemFetchJob::ItemGetter
items available through items()
Definition: itemfetchjob.h:198
Akonadi::ProtocolHelper::itemFetchScopeToByteArray
static QByteArray itemFetchScopeToByteArray(const ItemFetchScope &fetchScope)
Converts a given ItemFetchScope object into a protocol representation.
Definition: protocolhelper.cpp:355
Akonadi::Job::Unknown
Unknown error.
Definition: job.h:109
Akonadi::ProtocolHelper::entitySetToByteArray
static QByteArray entitySetToByteArray(const QList< T > &_objects, const QByteArray &command)
Converts the given set of items into a protocol representation.
Definition: protocolhelper_p.h:125
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::ItemFetchJob::setDeliveryOption
void setDeliveryOption(DeliveryOptions options)
Sets the mechanisms by which the items should be fetched.
Definition: itemfetchjob.cpp:287
Akonadi::Job
Base class for all actions in the Akonadi storage.
Definition: job.h:86
Akonadi::ItemFetchJob::items
Item::List items() const
Returns the fetched items.
Definition: itemfetchjob.cpp:245
Akonadi::ItemFetchJob::EmitItemsIndividually
emitted via signal upon reception
Definition: itemfetchjob.h:199
Akonadi::ItemFetchJob::fetchScope
ItemFetchScope & fetchScope()
Returns the item fetch scope.
Definition: itemfetchjob.cpp:273
Akonadi::JobPrivate::aboutToFinish
virtual void aboutToFinish()
This method is called right before result() and finished() signals are emitted.
Akonadi::ItemFetchJob::setFetchScope
void setFetchScope(ItemFetchScope &fetchScope)
Sets the item fetch scope.
Definition: itemfetchjob.cpp:259
Akonadi::Job::addSubjob
virtual bool addSubjob(KJob *job)
Adds the given job as a subjob to this job.
Definition: job.cpp:322
Akonadi::ItemFetchJob::deliveryOptions
DeliveryOptions deliveryOptions() const
Returns the delivery options.
Definition: itemfetchjob.cpp:294
Akonadi::Collection::root
static Collection root()
Returns the root collection.
Definition: collection.cpp:192
Akonadi::JobPrivate::newTag
QByteArray newTag()
Returns a new unique command tag for communication with the backend.
Akonadi::ItemFetchJob::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: itemfetchjob.cpp:210
Akonadi::ItemFetchScope
Specifies which parts of an item should be fetched from the Akonadi storage.
Definition: itemfetchscope.h:68
Akonadi::Exception
Base class for exceptions used by the Akonadi library.
Definition: exception.h:35
Akonadi::ItemFetchJob
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:82
Akonadi::ItemFetchJob::EmitItemsInBatches
emitted via signal in bulk (collected and emitted delayed via timer)
Definition: itemfetchjob.h:200
Akonadi::JobPrivate::writeData
void writeData(const QByteArray &data)
Sends raw data to the backend.
Akonadi::JobPrivate
Definition: job_p.h:31
Akonadi::ItemFetchJob::~ItemFetchJob
virtual ~ItemFetchJob()
Destroys the item fetch job.
Definition: itemfetchjob.cpp:187
Akonadi::ItemFetchScope::ignoreRetrievalErrors
bool ignoreRetrievalErrors() const
Returns whether retrieval errors should be ignored.
Definition: itemfetchscope.cpp:158
Akonadi::ItemFetchJob::ItemFetchJob
ItemFetchJob(const Collection &collection, QObject *parent=0)
Creates a new item fetch job that retrieves all items inside the given collection.
Definition: itemfetchjob.cpp:149
Akonadi::ProtocolHelper::parseItemFetchResult
static void parseItemFetchResult(const QList< QByteArray > &lineTokens, Item &item, ProtocolHelperValuePool *valuePool=0)
Parses a single line from an item fetch job result into an Item object.
Definition: protocolhelper.cpp:406
Akonadi::Exception::what
const char * what() const
Returns the error message associated with this exception.
Definition: exception.cpp:92
Akonadi::ItemFetchJob::clearItems
void clearItems()
Save memory by clearing the fetched items.
Definition: itemfetchjob.cpp:252
Akonadi::ItemFetchJob::setCollection
void setCollection(const Collection &collection)
Specifies the collection the item is in.
Definition: itemfetchjob.cpp:280
Akonadi::ItemFetchJob::doStart
virtual void doStart()
This method must be reimplemented in the concrete jobs.
Definition: itemfetchjob.cpp:191
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Mon Jul 21 2014 08:03:53 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