• 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
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 
27 #include <QtCore/QTimer>
28 #include <QThreadStorage>
29 
30 using namespace Akonadi;
31 
32 class Akonadi::ItemSearchJobPrivate : public JobPrivate
33 {
34  public:
35  ItemSearchJobPrivate( ItemSearchJob *parent, const QString &query )
36  : JobPrivate( parent ), mQuery( query ), mEmitTimer( 0 )
37  {
38  }
39 
40  void timeout()
41  {
42  Q_Q( Akonadi::ItemSearchJob );
43 
44  mEmitTimer->stop(); // in case we are called by result()
45  if ( !mPendingItems.isEmpty() ) {
46  if ( !q->error() )
47  emit q->itemsReceived( mPendingItems );
48  mPendingItems.clear();
49  }
50  }
51 
52  Q_DECLARE_PUBLIC( ItemSearchJob )
53 
54  QString mQuery;
55  Item::List mItems;
56  ItemFetchScope mFetchScope;
57  Item::List mPendingItems; // items pending for emitting itemsReceived()
58  QTimer* mEmitTimer;
59 };
60 
61 QThreadStorage<Session *> instances;
62 
63 static Session *defaultSearchSession()
64 {
65  if ( !instances.hasLocalData() ) {
66  const QByteArray sessionName = Session::defaultSession()->sessionId() + "-SearchSession";
67  instances.setLocalData( new Session( sessionName ) );
68  }
69  return instances.localData();
70 }
71 
72 static QObject *sessionForJob( QObject *parent )
73 {
74  if ( qobject_cast<Job *>(parent) || qobject_cast<Session *>(parent) )
75  return parent;
76  return defaultSearchSession();
77 }
78 
79 ItemSearchJob::ItemSearchJob( const QString & query, QObject * parent )
80  : Job( new ItemSearchJobPrivate( this, query ), sessionForJob( parent ) )
81 {
82  Q_D( ItemSearchJob );
83 
84  d->mEmitTimer = new QTimer( this );
85  d->mEmitTimer->setSingleShot( true );
86  d->mEmitTimer->setInterval( 100 );
87  connect( d->mEmitTimer, SIGNAL(timeout()), this, SLOT(timeout()) );
88  connect( this, SIGNAL(result(KJob*)), this, SLOT(timeout()) );
89 }
90 
91 ItemSearchJob::~ItemSearchJob()
92 {
93 }
94 
95 void ItemSearchJob::setQuery( const QString &query )
96 {
97  Q_D( ItemSearchJob );
98 
99  d->mQuery = query;
100 }
101 
102 void ItemSearchJob::setFetchScope( const ItemFetchScope &fetchScope )
103 {
104  Q_D( ItemSearchJob );
105 
106  d->mFetchScope = fetchScope;
107 }
108 
109 ItemFetchScope &ItemSearchJob::fetchScope()
110 {
111  Q_D( ItemSearchJob );
112 
113  return d->mFetchScope;
114 }
115 
116 void ItemSearchJob::doStart()
117 {
118  Q_D( ItemSearchJob );
119 
120  QByteArray command = d->newTag() + " SEARCH ";
121  command += ImapParser::quote( d->mQuery.toUtf8() );
122  command += ' ' + ProtocolHelper::itemFetchScopeToByteArray( d->mFetchScope );
123  command += '\n';
124  d->writeData( command );
125 }
126 
127 void ItemSearchJob::doHandleResponse( const QByteArray & tag, const QByteArray & data )
128 {
129  Q_D( ItemSearchJob );
130 
131  if ( tag == "*" ) {
132  int begin = data.indexOf( "SEARCH" );
133  if ( begin >= 0 ) {
134 
135  // split fetch response into key/value pairs
136  QList<QByteArray> fetchResponse;
137  ImapParser::parseParenthesizedList( data, fetchResponse, begin + 7 );
138 
139  Item item;
140  ProtocolHelper::parseItemFetchResult( fetchResponse, item );
141  if ( !item.isValid() )
142  return;
143 
144  d->mItems.append( item );
145  d->mPendingItems.append( item );
146  if ( !d->mEmitTimer->isActive() )
147  d->mEmitTimer->start();
148  return;
149  }
150  }
151  kDebug() << "Unhandled response: " << tag << data;
152 }
153 
154 Item::List ItemSearchJob::items() const
155 {
156  Q_D( const ItemSearchJob );
157 
158  return d->mItems;
159 }
160 
161 QUrl ItemSearchJob::akonadiItemIdUri()
162 {
163  return QUrl( QLatin1String( "http://akonadi-project.org/ontologies/aneo#akonadiItemId" ) );
164 }
165 
166 #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:127
Akonadi::ItemSearchJob::ItemSearchJob
ItemSearchJob(const QString &query, QObject *parent=0)
Creates an item search job.
Definition: itemsearchjob.cpp:79
Akonadi::ItemSearchJob::items
Item::List items() const
Returns the items that matched the search query.
Definition: itemsearchjob.cpp:154
Akonadi::ProtocolHelper::itemFetchScopeToByteArray
static QByteArray itemFetchScopeToByteArray(const ItemFetchScope &fetchScope)
Converts a given ItemFetchScope object into a protocol representation.
Definition: protocolhelper.cpp:298
Akonadi::Job
Base class for all actions in the Akonadi storage.
Definition: job.h:86
Akonadi::Session::defaultSession
static Session * defaultSession()
Returns the default session for this thread.
Definition: session.cpp:444
Akonadi::ItemSearchJob::setFetchScope
void setFetchScope(const ItemFetchScope &fetchScope)
Sets the item fetch scope.
Definition: itemsearchjob.cpp:102
Akonadi::Session::sessionId
QByteArray sessionId() const
Returns the session identifier.
Definition: session.cpp:422
Akonadi::ItemSearchJob::fetchScope
ItemFetchScope & fetchScope()
Returns the item fetch scope.
Definition: itemsearchjob.cpp:109
Akonadi::Session
A communication session with the Akonadi storage.
Definition: session.h:59
Akonadi::ItemSearchJob::akonadiItemIdUri
static QUrl akonadiItemIdUri()
Returns an URI that represents a predicate that is always added to the Nepomuk resource by the Akonad...
Definition: itemsearchjob.cpp:161
Akonadi::ItemSearchJob::doStart
void doStart()
This method must be reimplemented in the concrete jobs.
Definition: itemsearchjob.cpp:116
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:91
Akonadi::JobPrivate
Definition: job_p.h:31
Akonadi::ItemSearchJob::setQuery
void setQuery(const QString &query)
Sets the search query in SPARQL format.
Definition: itemsearchjob.cpp:95
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::ItemSearchJob
Job that searches for items in the Akonadi storage.
Definition: itemsearchjob.h:70
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