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

akonadi

  • akonadi
itemsearchjob.cpp
1 /*
2  Copyright (c) 2009 Tobias Koenig <tokoe@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 "itemsearchjob.h"
21 
22 #include "imapparser_p.h"
23 #include "itemfetchscope.h"
24 #include "job_p.h"
25 #include "protocolhelper_p.h"
26 #include "searchquery.h"
27 
28 #include <QtCore/QTimer>
29 #include <QThreadStorage>
30 
31 using namespace Akonadi;
32 
33 class Akonadi::ItemSearchJobPrivate : public JobPrivate
34 {
35  public:
36  ItemSearchJobPrivate( ItemSearchJob *parent, const SearchQuery &query )
37  : JobPrivate( parent ), mQuery( query ), mRecursive( false ), mRemote( false ), mEmitTimer( 0 )
38  {
39  }
40 
41  void init()
42  {
43  Q_Q( ItemSearchJob );
44  mEmitTimer = new QTimer( q );
45  mEmitTimer->setSingleShot( true );
46  mEmitTimer->setInterval( 100 );
47  q->connect( mEmitTimer, SIGNAL(timeout()), q, SLOT(timeout()) );
48  q->connect( q, SIGNAL(result(KJob*)), q, SLOT(timeout()) );
49  }
50 
51  void timeout()
52  {
53  Q_Q( Akonadi::ItemSearchJob );
54 
55  mEmitTimer->stop(); // in case we are called by result()
56  if ( !mPendingItems.isEmpty() ) {
57  if ( !q->error() )
58  emit q->itemsReceived( mPendingItems );
59  mPendingItems.clear();
60  }
61  }
62 
63  QString jobDebuggingString() const /*Q_DECL_OVERRIDE*/ {
64  QStringList flags;
65  if ( mRecursive ) {
66  flags.append( QLatin1String( "recursive" ) );
67  }
68  if ( mRemote ) {
69  flags.append( QLatin1String( "remote" ) );
70  }
71  if ( mCollections.isEmpty() ) {
72  flags.append( QLatin1String( "all collections" ) );
73  } else {
74  flags.append( QString::fromLatin1( "%1 collections" ).arg( mCollections.count() ) );
75  }
76  return QString::fromLatin1( "%1,json=%2" ).arg( flags.join(QLatin1String(","))).arg(QString::fromUtf8(mQuery.toJSON()));
77  }
78 
79  Q_DECLARE_PUBLIC( ItemSearchJob )
80 
81  SearchQuery mQuery;
82  Collection::List mCollections;
83  QStringList mMimeTypes;
84  bool mRecursive;
85  bool mRemote;
86  ItemFetchScope mFetchScope;
87 
88  Item::List mItems;
89  Item::List mPendingItems; // items pending for emitting itemsReceived()
90 
91  QTimer* mEmitTimer;
92 };
93 
94 QThreadStorage<Session *> instances;
95 
96 static Session *defaultSearchSession()
97 {
98  if ( !instances.hasLocalData() ) {
99  const QByteArray sessionName = Session::defaultSession()->sessionId() + "-SearchSession";
100  instances.setLocalData( new Session( sessionName ) );
101  }
102  return instances.localData();
103 }
104 
105 static QObject *sessionForJob( QObject *parent )
106 {
107  if ( qobject_cast<Job *>(parent) || qobject_cast<Session *>(parent) )
108  return parent;
109  return defaultSearchSession();
110 }
111 
112 ItemSearchJob::ItemSearchJob( const SearchQuery &query, QObject *parent )
113  : Job( new ItemSearchJobPrivate( this, query ), sessionForJob( parent ) )
114 {
115  Q_D( ItemSearchJob );
116 
117  d->init();
118 }
119 
120 
121 ItemSearchJob::ItemSearchJob( const QString &query, QObject * parent )
122  : Job( new ItemSearchJobPrivate( this, SearchQuery::fromJSON( query.toUtf8()) ), sessionForJob( parent ) )
123 {
124  Q_D( ItemSearchJob );
125 
126  d->init();
127 }
128 
129 ItemSearchJob::~ItemSearchJob()
130 {
131 }
132 
133 void ItemSearchJob::setQuery( const QString &query )
134 {
135  Q_D( ItemSearchJob );
136 
137  d->mQuery = SearchQuery::fromJSON( query.toUtf8() );
138 }
139 
140 void ItemSearchJob::setQuery( const SearchQuery &query )
141 {
142  Q_D( ItemSearchJob );
143 
144  d->mQuery = query;
145 }
146 
147 
148 void ItemSearchJob::setFetchScope( const ItemFetchScope &fetchScope )
149 {
150  Q_D( ItemSearchJob );
151 
152  d->mFetchScope = fetchScope;
153 }
154 
155 ItemFetchScope &ItemSearchJob::fetchScope()
156 {
157  Q_D( ItemSearchJob );
158 
159  return d->mFetchScope;
160 }
161 
162 void ItemSearchJob::setSearchCollections( const Collection::List &collections )
163 {
164  Q_D( ItemSearchJob );
165 
166  d->mCollections = collections;
167 }
168 
169 Collection::List ItemSearchJob::searchCollections() const
170 {
171  return d_func()->mCollections;
172 }
173 
174 void ItemSearchJob::setMimeTypes( const QStringList &mimeTypes )
175 {
176  Q_D( ItemSearchJob );
177 
178  d->mMimeTypes = mimeTypes;
179 }
180 
181 QStringList ItemSearchJob::mimeTypes() const
182 {
183  return d_func()->mMimeTypes;
184 }
185 
186 void ItemSearchJob::setRecursive( bool recursive )
187 {
188  Q_D( ItemSearchJob );
189 
190  d->mRecursive = recursive;
191 }
192 
193 bool ItemSearchJob::isRecursive() const
194 {
195  return d_func()->mRecursive;
196 }
197 
198 void ItemSearchJob::setRemoteSearchEnabled(bool enabled)
199 {
200  Q_D( ItemSearchJob );
201 
202  d->mRemote = enabled;
203 }
204 
205 bool ItemSearchJob::isRemoteSearchEnabled() const
206 {
207  return d_func()->mRemote;
208 }
209 
210 void ItemSearchJob::doStart()
211 {
212  Q_D( ItemSearchJob );
213 
214  QByteArray command = d->newTag() + " SEARCH ";
215  if ( !d->mMimeTypes.isEmpty() ) {
216  command += "MIMETYPE (" + d->mMimeTypes.join( QLatin1String( " ") ).toLatin1() + ") ";
217  }
218  if ( !d->mCollections.isEmpty() ) {
219  command += "COLLECTIONS (";
220  Q_FOREACH (const Collection &collection, d->mCollections ) {
221  command += QByteArray::number( collection.id() ) + ' ';
222  }
223  command += ") ";
224  }
225  if ( d->mRecursive ) {
226  command += "RECURSIVE ";
227  }
228  if ( d->mRemote ) {
229  command += "REMOTE ";
230  }
231 
232  command += "QUERY " + ImapParser::quote( d->mQuery.toJSON() );
233  command += ' ' + ProtocolHelper::itemFetchScopeToByteArray( d->mFetchScope );
234  command += '\n';
235  d->writeData( command );
236 }
237 
238 void ItemSearchJob::doHandleResponse( const QByteArray & tag, const QByteArray & data )
239 {
240  Q_D( ItemSearchJob );
241 
242  if ( tag == "*" ) {
243  int begin = data.indexOf( "SEARCH" );
244  if ( begin >= 0 ) {
245 
246  // split fetch response into key/value pairs
247  QList<QByteArray> fetchResponse;
248  ImapParser::parseParenthesizedList( data, fetchResponse, begin + 7 );
249 
250  Item item;
251  ProtocolHelper::parseItemFetchResult( fetchResponse, item );
252  if ( !item.isValid() )
253  return;
254 
255  d->mItems.append( item );
256  d->mPendingItems.append( item );
257  if ( !d->mEmitTimer->isActive() )
258  d->mEmitTimer->start();
259  return;
260  }
261  }
262  kDebug() << "Unhandled response: " << tag << data;
263 }
264 
265 Item::List ItemSearchJob::items() const
266 {
267  Q_D( const ItemSearchJob );
268 
269  return d->mItems;
270 }
271 
272 QUrl ItemSearchJob::akonadiItemIdUri()
273 {
274  return QUrl( QLatin1String( "http://akonadi-project.org/ontologies/aneo#akonadiItemId" ) );
275 }
276 
277 #include "moc_itemsearchjob.cpp"
Akonadi::ItemSearchJob::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: itemsearchjob.cpp:238
Akonadi::ItemSearchJob::mimeTypes
QStringList mimeTypes() const
Returns list of mime types to search in.
Definition: itemsearchjob.cpp:181
Akonadi::ItemSearchJob::setRemoteSearchEnabled
void setRemoteSearchEnabled(bool enabled)
Sets whether resources should be queried too.
Definition: itemsearchjob.cpp:198
Akonadi::ItemSearchJob::ItemSearchJob
AKONADI_DEPRECATED ItemSearchJob(const QString &query, QObject *parent=0)
Creates an item search job.
Definition: itemsearchjob.cpp:121
Akonadi::ItemSearchJob::items
Item::List items() const
Returns the items that matched the search query.
Definition: itemsearchjob.cpp:265
Akonadi::ProtocolHelper::itemFetchScopeToByteArray
static QByteArray itemFetchScopeToByteArray(const ItemFetchScope &fetchScope)
Converts a given ItemFetchScope object into a protocol representation.
Definition: protocolhelper.cpp:355
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::ItemSearchJob::isRemoteSearchEnabled
bool isRemoteSearchEnabled() const
Returns whether remote search is enabled.
Definition: itemsearchjob.cpp:205
Akonadi::ItemSearchJob::setSearchCollections
void setSearchCollections(const Collection::List &collections)
Search only in given collections.
Definition: itemsearchjob.cpp:162
Akonadi::Job
Base class for all actions in the Akonadi storage.
Definition: job.h:86
Akonadi::ItemSearchJob::searchCollections
Collection::List searchCollections() const
Returns list of collections to search.
Definition: itemsearchjob.cpp:169
Akonadi::Session::defaultSession
static Session * defaultSession()
Returns the default session for this thread.
Definition: session.cpp:479
Akonadi::ItemSearchJob::setFetchScope
void setFetchScope(const ItemFetchScope &fetchScope)
Sets the item fetch scope.
Definition: itemsearchjob.cpp:148
Akonadi::Session::sessionId
QByteArray sessionId() const
Returns the session identifier.
Definition: session.cpp:457
Akonadi::ItemSearchJob::setRecursive
void setRecursive(bool recursive)
Sets whether the search should recurse into collections.
Definition: itemsearchjob.cpp:186
Akonadi::ItemSearchJob::fetchScope
ItemFetchScope & fetchScope()
Returns the item fetch scope.
Definition: itemsearchjob.cpp:155
Akonadi::Session
A communication session with the Akonadi storage.
Definition: session.h:59
Akonadi::ItemSearchJob::akonadiItemIdUri
static AKONADI_DEPRECATED QUrl akonadiItemIdUri()
Returns an URI that represents a predicate that is always added to the Nepomuk resource by the Akonad...
Definition: itemsearchjob.cpp:272
Akonadi::Entity::id
Id id() const
Returns the unique identifier of the entity.
Definition: entity.cpp:72
Akonadi::ItemSearchJob::doStart
void doStart()
This method must be reimplemented in the concrete jobs.
Definition: itemsearchjob.cpp:210
Akonadi::SearchQuery
A query that can be passed to ItemSearchJob or others.
Definition: searchquery.h:129
Akonadi::ItemFetchScope
Specifies which parts of an item should be fetched from the Akonadi storage.
Definition: itemfetchscope.h:68
Akonadi::ItemSearchJob::~ItemSearchJob
~ItemSearchJob()
Destroys the item search job.
Definition: itemsearchjob.cpp:129
Akonadi::JobPrivate
Definition: job_p.h:31
Akonadi::ItemSearchJob::setQuery
void AKONADI_DEPRECATED setQuery(const QString &query)
Sets the search query in Akonadi search metalanguage format (JSON)
Definition: itemsearchjob.cpp:133
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::ItemSearchJob::isRecursive
bool isRecursive() const
Returns whether the search is recursive.
Definition: itemsearchjob.cpp:193
Akonadi::ItemSearchJob::setMimeTypes
void setMimeTypes(const QStringList &mimeTypes)
Search only for items of given mime types.
Definition: itemsearchjob.cpp:174
Akonadi::ItemSearchJob
Job that searches for items in the Akonadi storage.
Definition: itemsearchjob.h:67
Akonadi::Collection::List
QList< Collection > List
Describes a list of collections.
Definition: collection.h:81
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