• 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
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  }
50 
51  ~ItemFetchJobPrivate()
52  {
53  delete mValuePool;
54  }
55 
56  void init()
57  {
58  Q_Q( ItemFetchJob );
59  mEmitTimer = new QTimer( q );
60  mEmitTimer->setSingleShot( true );
61  mEmitTimer->setInterval( 100 );
62  q->connect( mEmitTimer, SIGNAL(timeout()), q, SLOT(timeout()) );
63  q->connect( q, SIGNAL(result(KJob*)), q, SLOT(timeout()) );
64  }
65 
66  void timeout()
67  {
68  Q_Q( ItemFetchJob );
69 
70  mEmitTimer->stop(); // in case we are called by result()
71  if ( !mPendingItems.isEmpty() ) {
72  if ( !q->error() )
73  emit q->itemsReceived( mPendingItems );
74  mPendingItems.clear();
75  }
76  }
77 
78  void startFetchJob();
79  void selectDone( KJob * job );
80 
81  QString jobDebuggingString() const /*Q_DECL_OVERRIDE*/ {
82  if ( mRequestedItems.isEmpty() ) {
83  return QString::fromLatin1( "All items from collection %1" ).arg( mCollection.id() );
84  } else {
85  try {
86  return QString::fromLatin1( ProtocolHelper::entitySetToByteArray( mRequestedItems, AKONADI_CMD_ITEMFETCH ) );
87  } catch ( const Exception &e ) {
88  return QString::fromUtf8( e.what() );
89  }
90  }
91  }
92 
93  Q_DECLARE_PUBLIC( ItemFetchJob )
94 
95  Collection mCollection;
96  Item::List mRequestedItems;
97  Item::List mResultItems;
98  ItemFetchScope mFetchScope;
99  Item::List mPendingItems; // items pending for emitting itemsReceived()
100  QTimer* mEmitTimer;
101  ProtocolHelperValuePool *mValuePool;
102 };
103 
104 void ItemFetchJobPrivate::startFetchJob()
105 {
106  Q_Q( ItemFetchJob );
107  QByteArray command = newTag();
108  if ( mRequestedItems.isEmpty() ) {
109  command += " " AKONADI_CMD_ITEMFETCH " 1:*";
110  } else {
111  try {
112  command += ProtocolHelper::entitySetToByteArray( mRequestedItems, AKONADI_CMD_ITEMFETCH );
113  } catch ( const Exception &e ) {
114  q->setError( Job::Unknown );
115  q->setErrorText( QString::fromUtf8( e.what() ) );
116  q->emitResult();
117  return;
118  }
119  }
120 
121  //This is only required for 4.10
122  if ( protocolVersion() < 30 ) {
123  if ( mFetchScope.ignoreRetrievalErrors() ) {
124  kDebug() << "IGNOREERRORS is not available with this akonadi protocol version";
125  }
126  mFetchScope.setIgnoreRetrievalErrors( false );
127  }
128  command += ProtocolHelper::itemFetchScopeToByteArray( mFetchScope );
129 
130  writeData( command );
131 }
132 
133 void ItemFetchJobPrivate::selectDone( KJob * job )
134 {
135  if ( !job->error() )
136  // the collection is now selected, fetch the message(s)
137  startFetchJob();
138 }
139 
140 ItemFetchJob::ItemFetchJob( const Collection &collection, QObject * parent )
141  : Job( new ItemFetchJobPrivate( this ), parent )
142 {
143  Q_D( ItemFetchJob );
144 
145  d->init();
146  d->mCollection = collection;
147  d->mValuePool = new ProtocolHelperValuePool; // only worth it for lots of results
148 }
149 
150 ItemFetchJob::ItemFetchJob( const Item & item, QObject * parent)
151  : Job( new ItemFetchJobPrivate( this ), parent )
152 {
153  Q_D( ItemFetchJob );
154 
155  d->init();
156  d->mRequestedItems.append( item );
157 }
158 
159 ItemFetchJob::ItemFetchJob(const Akonadi::Item::List& items, QObject* parent)
160  : Job( new ItemFetchJobPrivate( this ), parent )
161 {
162  Q_D( ItemFetchJob );
163 
164  d->init();
165  d->mRequestedItems = items;
166 }
167 
168 ItemFetchJob::ItemFetchJob(const QList<Akonadi::Item::Id>& items, QObject* parent)
169  : Job( new ItemFetchJobPrivate( this ), parent )
170 {
171  Q_D( ItemFetchJob );
172 
173  d->init();
174  foreach(Item::Id id, items)
175  d->mRequestedItems.append(Item(id));
176 }
177 
178 ItemFetchJob::~ItemFetchJob()
179 {
180 }
181 
182 void ItemFetchJob::doStart()
183 {
184  Q_D( ItemFetchJob );
185 
186  if ( d->mCollection == Collection::root() ) {
187  if ( d->mRequestedItems.isEmpty() ) { // collection content listing
188  setErrorText( i18n( "Cannot list root collection." ) );
189  setError( Unknown );
190  emitResult();
191  } else {
192  d->startFetchJob();
193  }
194  } else {
195  CollectionSelectJob *job = new CollectionSelectJob( d->mCollection, this );
196  connect( job, SIGNAL(result(KJob*)), SLOT(selectDone(KJob*)) );
197  addSubjob( job );
198  }
199 }
200 
201 void ItemFetchJob::doHandleResponse( const QByteArray & tag, const QByteArray & data )
202 {
203  Q_D( ItemFetchJob );
204 
205  if ( tag == "*" ) {
206  int begin = data.indexOf( "FETCH" );
207  if ( begin >= 0 ) {
208 
209  // split fetch response into key/value pairs
210  QList<QByteArray> fetchResponse;
211  ImapParser::parseParenthesizedList( data, fetchResponse, begin + 6 );
212 
213  Item item;
214  ProtocolHelper::parseItemFetchResult( fetchResponse, item, d->mValuePool );
215  if ( !item.isValid() )
216  return;
217 
218  d->mResultItems.append( item );
219  d->mPendingItems.append( item );
220  if ( !d->mEmitTimer->isActive() )
221  d->mEmitTimer->start();
222  return;
223  }
224  }
225  kDebug() << "Unhandled response: " << tag << data;
226 }
227 
228 Item::List ItemFetchJob::items() const
229 {
230  Q_D( const ItemFetchJob );
231 
232  return d->mResultItems;
233 }
234 
235 void ItemFetchJob::clearItems()
236 {
237  Q_D( ItemFetchJob );
238 
239  d->mResultItems.clear();
240 }
241 
242 void ItemFetchJob::setFetchScope( ItemFetchScope &fetchScope )
243 {
244  Q_D( ItemFetchJob );
245 
246  d->mFetchScope = fetchScope;
247 }
248 
249 void ItemFetchJob::setFetchScope( const ItemFetchScope &fetchScope )
250 {
251  Q_D( ItemFetchJob );
252 
253  d->mFetchScope = fetchScope;
254 }
255 
256 ItemFetchScope &ItemFetchJob::fetchScope()
257 {
258  Q_D( ItemFetchJob );
259 
260  return d->mFetchScope;
261 }
262 
263 void ItemFetchJob::setCollection(const Akonadi::Collection& collection)
264 {
265  Q_D( ItemFetchJob );
266 
267  d->mCollection = collection;
268 }
269 
270 #include "moc_itemfetchjob.cpp"
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::ProtocolHelper::itemFetchScopeToByteArray
static QByteArray itemFetchScopeToByteArray(const ItemFetchScope &fetchScope)
Converts a given ItemFetchScope object into a protocol representation.
Definition: protocolhelper.cpp:298
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:122
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
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:228
Akonadi::ItemFetchJob::fetchScope
ItemFetchScope & fetchScope()
Returns the item fetch scope.
Definition: itemfetchjob.cpp:256
Akonadi::ItemFetchJob::setFetchScope
void setFetchScope(ItemFetchScope &fetchScope)
Sets the item fetch scope.
Definition: itemfetchjob.cpp:242
Akonadi::Job::addSubjob
virtual bool addSubjob(KJob *job)
Adds the given job as a subjob to this job.
Definition: job.cpp:318
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:201
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::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:178
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:140
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:347
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:235
Akonadi::ItemFetchJob::setCollection
void setCollection(const Collection &collection)
Specifies the collection the item is in.
Definition: itemfetchjob.cpp:263
Akonadi::ItemFetchJob::doStart
virtual void doStart()
This method must be reimplemented in the concrete jobs.
Definition: itemfetchjob.cpp:182
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 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