• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.1 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 
31 #include <kdebug.h>
32 #include <KLocale>
33 
34 #include <QtCore/QStringList>
35 #include <QtCore/QTimer>
36 
37 using namespace Akonadi;
38 
39 class Akonadi::ItemFetchJobPrivate : public JobPrivate
40 {
41  public:
42  ItemFetchJobPrivate( ItemFetchJob *parent )
43  : JobPrivate( parent ),
44  mEmitTimer( 0 ),
45  mValuePool( 0 )
46  {
47  mCollection = Collection::root();
48  }
49 
50  ~ItemFetchJobPrivate()
51  {
52  delete mValuePool;
53  }
54 
55  void init()
56  {
57  Q_Q( ItemFetchJob );
58  mEmitTimer = new QTimer( q );
59  mEmitTimer->setSingleShot( true );
60  mEmitTimer->setInterval( 100 );
61  q->connect( mEmitTimer, SIGNAL(timeout()), q, SLOT(timeout()) );
62  q->connect( q, SIGNAL(result(KJob*)), q, SLOT(timeout()) );
63  }
64 
65  void timeout()
66  {
67  Q_Q( ItemFetchJob );
68 
69  mEmitTimer->stop(); // in case we are called by result()
70  if ( !mPendingItems.isEmpty() ) {
71  if ( !q->error() )
72  emit q->itemsReceived( mPendingItems );
73  mPendingItems.clear();
74  }
75  }
76 
77  void startFetchJob();
78  void selectDone( KJob * job );
79 
80  Q_DECLARE_PUBLIC( ItemFetchJob )
81 
82  Collection mCollection;
83  Item::List mRequestedItems;
84  Item::List mResultItems;
85  ItemFetchScope mFetchScope;
86  Item::List mPendingItems; // items pending for emitting itemsReceived()
87  QTimer* mEmitTimer;
88  ProtocolHelperValuePool *mValuePool;
89 };
90 
91 void ItemFetchJobPrivate::startFetchJob()
92 {
93  Q_Q( ItemFetchJob );
94  QByteArray command = newTag();
95  if ( mRequestedItems.isEmpty() ) {
96  command += " " AKONADI_CMD_ITEMFETCH " 1:*";
97  } else {
98  try {
99  command += ProtocolHelper::entitySetToByteArray( mRequestedItems, AKONADI_CMD_ITEMFETCH );
100  } catch ( const Exception &e ) {
101  q->setError( Job::Unknown );
102  q->setErrorText( QString::fromUtf8( e.what() ) );
103  q->emitResult();
104  return;
105  }
106  }
107 
108  command += ProtocolHelper::itemFetchScopeToByteArray( mFetchScope );
109 
110  writeData( command );
111 }
112 
113 void ItemFetchJobPrivate::selectDone( KJob * job )
114 {
115  if ( !job->error() )
116  // the collection is now selected, fetch the message(s)
117  startFetchJob();
118 }
119 
120 ItemFetchJob::ItemFetchJob( const Collection &collection, QObject * parent )
121  : Job( new ItemFetchJobPrivate( this ), parent )
122 {
123  Q_D( ItemFetchJob );
124 
125  d->init();
126  d->mCollection = collection;
127  d->mValuePool = new ProtocolHelperValuePool; // only worth it for lots of results
128 }
129 
130 ItemFetchJob::ItemFetchJob( const Item & item, QObject * parent)
131  : Job( new ItemFetchJobPrivate( this ), parent )
132 {
133  Q_D( ItemFetchJob );
134 
135  d->init();
136  d->mRequestedItems.append( item );
137 }
138 
139 ItemFetchJob::ItemFetchJob(const Akonadi::Item::List& items, QObject* parent)
140  : Job( new ItemFetchJobPrivate( this ), parent )
141 {
142  Q_D( ItemFetchJob );
143 
144  d->init();
145  d->mRequestedItems = items;
146 }
147 
148 ItemFetchJob::ItemFetchJob(const QList<Akonadi::Item::Id>& items, QObject* parent)
149  : Job( new ItemFetchJobPrivate( this ), parent )
150 {
151  Q_D( ItemFetchJob );
152 
153  d->init();
154  foreach(Item::Id id, items)
155  d->mRequestedItems.append(Item(id));
156 }
157 
158 
159 ItemFetchJob::~ItemFetchJob()
160 {
161 }
162 
163 void ItemFetchJob::doStart()
164 {
165  Q_D( ItemFetchJob );
166 
167  if ( d->mRequestedItems.isEmpty() ) { // collection content listing
168  if ( d->mCollection == Collection::root() ) {
169  setErrorText( i18n( "Cannot list root collection." ) );
170  setError( Unknown );
171  emitResult();
172  }
173  CollectionSelectJob *job = new CollectionSelectJob( d->mCollection, this );
174  connect( job, SIGNAL(result(KJob*)), SLOT(selectDone(KJob*)) );
175  addSubjob( job );
176  } else
177  d->startFetchJob();
178 }
179 
180 void ItemFetchJob::doHandleResponse( const QByteArray & tag, const QByteArray & data )
181 {
182  Q_D( ItemFetchJob );
183 
184  if ( tag == "*" ) {
185  int begin = data.indexOf( "FETCH" );
186  if ( begin >= 0 ) {
187 
188  // split fetch response into key/value pairs
189  QList<QByteArray> fetchResponse;
190  ImapParser::parseParenthesizedList( data, fetchResponse, begin + 6 );
191 
192  Item item;
193  ProtocolHelper::parseItemFetchResult( fetchResponse, item, d->mValuePool );
194  if ( !item.isValid() )
195  return;
196 
197  d->mResultItems.append( item );
198  d->mPendingItems.append( item );
199  if ( !d->mEmitTimer->isActive() )
200  d->mEmitTimer->start();
201  return;
202  }
203  }
204  kDebug() << "Unhandled response: " << tag << data;
205 }
206 
207 Item::List ItemFetchJob::items() const
208 {
209  Q_D( const ItemFetchJob );
210 
211  return d->mResultItems;
212 }
213 
214 void ItemFetchJob::setFetchScope( ItemFetchScope &fetchScope )
215 {
216  Q_D( ItemFetchJob );
217 
218  d->mFetchScope = fetchScope;
219 }
220 
221 void ItemFetchJob::setFetchScope( const ItemFetchScope &fetchScope )
222 {
223  Q_D( ItemFetchJob );
224 
225  d->mFetchScope = fetchScope;
226 }
227 
228 ItemFetchScope &ItemFetchJob::fetchScope()
229 {
230  Q_D( ItemFetchJob );
231 
232  return d->mFetchScope;
233 }
234 
235 void ItemFetchJob::setCollection(const Akonadi::Collection& collection)
236 {
237  Q_D( ItemFetchJob );
238 
239  d->mCollection = collection;
240 }
241 
242 
243 #include "moc_itemfetchjob.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Wed Mar 27 2013 08:57:23 by doxygen 1.8.3 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.1 API Reference

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