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

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