• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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 <KLocale>
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  Q_DECLARE_PUBLIC( ItemFetchJob )
82 
83  Collection mCollection;
84  Item::List mRequestedItems;
85  Item::List mResultItems;
86  ItemFetchScope mFetchScope;
87  Item::List mPendingItems; // items pending for emitting itemsReceived()
88  QTimer* mEmitTimer;
89  ProtocolHelperValuePool *mValuePool;
90 };
91 
92 void ItemFetchJobPrivate::startFetchJob()
93 {
94  Q_Q( ItemFetchJob );
95  QByteArray command = newTag();
96  if ( mRequestedItems.isEmpty() ) {
97  command += " " AKONADI_CMD_ITEMFETCH " 1:*";
98  } else {
99  try {
100  command += ProtocolHelper::entitySetToByteArray( mRequestedItems, AKONADI_CMD_ITEMFETCH );
101  } catch ( const Exception &e ) {
102  q->setError( Job::Unknown );
103  q->setErrorText( QString::fromUtf8( e.what() ) );
104  q->emitResult();
105  return;
106  }
107  }
108 
109  //This is only required for 4.10
110  if ( protocolVersion() < 30 ) {
111  if ( mFetchScope.ignoreRetrievalErrors() ) {
112  kDebug() << "IGNOREERRORS is not available with this akonadi protocol version";
113  }
114  mFetchScope.setIgnoreRetrievalErrors( false );
115  }
116  command += ProtocolHelper::itemFetchScopeToByteArray( mFetchScope );
117 
118  writeData( command );
119 }
120 
121 void ItemFetchJobPrivate::selectDone( KJob * job )
122 {
123  if ( !job->error() )
124  // the collection is now selected, fetch the message(s)
125  startFetchJob();
126 }
127 
128 ItemFetchJob::ItemFetchJob( const Collection &collection, QObject * parent )
129  : Job( new ItemFetchJobPrivate( this ), parent )
130 {
131  Q_D( ItemFetchJob );
132 
133  d->init();
134  d->mCollection = collection;
135  d->mValuePool = new ProtocolHelperValuePool; // only worth it for lots of results
136 }
137 
138 ItemFetchJob::ItemFetchJob( const Item & item, QObject * parent)
139  : Job( new ItemFetchJobPrivate( this ), parent )
140 {
141  Q_D( ItemFetchJob );
142 
143  d->init();
144  d->mRequestedItems.append( item );
145 }
146 
147 ItemFetchJob::ItemFetchJob(const Akonadi::Item::List& items, QObject* parent)
148  : Job( new ItemFetchJobPrivate( this ), parent )
149 {
150  Q_D( ItemFetchJob );
151 
152  d->init();
153  d->mRequestedItems = items;
154 }
155 
156 ItemFetchJob::ItemFetchJob(const QList<Akonadi::Item::Id>& items, QObject* parent)
157  : Job( new ItemFetchJobPrivate( this ), parent )
158 {
159  Q_D( ItemFetchJob );
160 
161  d->init();
162  foreach(Item::Id id, items)
163  d->mRequestedItems.append(Item(id));
164 }
165 
166 
167 ItemFetchJob::~ItemFetchJob()
168 {
169 }
170 
171 void ItemFetchJob::doStart()
172 {
173  Q_D( ItemFetchJob );
174 
175  if ( d->mRequestedItems.isEmpty() ) { // collection content listing
176  if ( d->mCollection == Collection::root() ) {
177  setErrorText( i18n( "Cannot list root collection." ) );
178  setError( Unknown );
179  emitResult();
180  }
181  CollectionSelectJob *job = new CollectionSelectJob( d->mCollection, this );
182  connect( job, SIGNAL(result(KJob*)), SLOT(selectDone(KJob*)) );
183  addSubjob( job );
184  } else
185  d->startFetchJob();
186 }
187 
188 void ItemFetchJob::doHandleResponse( const QByteArray & tag, const QByteArray & data )
189 {
190  Q_D( ItemFetchJob );
191 
192  if ( tag == "*" ) {
193  int begin = data.indexOf( "FETCH" );
194  if ( begin >= 0 ) {
195 
196  // split fetch response into key/value pairs
197  QList<QByteArray> fetchResponse;
198  ImapParser::parseParenthesizedList( data, fetchResponse, begin + 6 );
199 
200  Item item;
201  ProtocolHelper::parseItemFetchResult( fetchResponse, item, d->mValuePool );
202  if ( !item.isValid() )
203  return;
204 
205  d->mResultItems.append( item );
206  d->mPendingItems.append( item );
207  if ( !d->mEmitTimer->isActive() )
208  d->mEmitTimer->start();
209  return;
210  }
211  }
212  kDebug() << "Unhandled response: " << tag << data;
213 }
214 
215 Item::List ItemFetchJob::items() const
216 {
217  Q_D( const ItemFetchJob );
218 
219  return d->mResultItems;
220 }
221 
222 void ItemFetchJob::setFetchScope( ItemFetchScope &fetchScope )
223 {
224  Q_D( ItemFetchJob );
225 
226  d->mFetchScope = fetchScope;
227 }
228 
229 void ItemFetchJob::setFetchScope( const ItemFetchScope &fetchScope )
230 {
231  Q_D( ItemFetchJob );
232 
233  d->mFetchScope = fetchScope;
234 }
235 
236 ItemFetchScope &ItemFetchJob::fetchScope()
237 {
238  Q_D( ItemFetchJob );
239 
240  return d->mFetchScope;
241 }
242 
243 void ItemFetchJob::setCollection(const Akonadi::Collection& collection)
244 {
245  Q_D( ItemFetchJob );
246 
247  d->mCollection = collection;
248 }
249 
250 
251 #include "moc_itemfetchjob.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:38 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