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

akonadi

  • akonadi
searchquery.cpp
1 /*
2  Copyright (c) 2014 Daniel Vrátil <dvratil@redhat.com>
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 "searchquery.h"
21 
22 #include <QtCore/QVariant>
23 
24 #include <KDebug>
25 
26 #include <qjson/parser.h>
27 #include <qjson/serializer.h>
28 
29 using namespace Akonadi;
30 
31 class SearchTerm::Private: public QSharedData
32 {
33  public:
34  Private():
35  QSharedData(),
36  condition( SearchTerm::CondEqual ),
37  relation( SearchTerm::RelAnd ),
38  isNegated( false )
39  {
40  }
41 
42  Private( const Private &other ):
43  QSharedData( other ),
44  key( other.key ),
45  value( other.value ),
46  condition( other.condition ),
47  relation( other.relation ),
48  terms( other.terms ),
49  isNegated( other.isNegated )
50  {
51  }
52 
53  bool operator==( const Private &other ) const
54  {
55  return relation == other.relation
56  && isNegated == other.isNegated
57  && terms == other.terms
58  && key == other.key
59  && value == other.value
60  && condition == other.condition;
61  }
62 
63  QString key;
64  QVariant value;
65  Condition condition;
66  Relation relation;
67  QList<SearchTerm> terms;
68  bool isNegated;
69 };
70 
71 class SearchQuery::Private: public QSharedData
72 {
73  public:
74  Private():
75  QSharedData(),
76  limit(-1)
77  {
78  }
79 
80  Private( const Private &other ):
81  QSharedData( other ),
82  rootTerm( other.rootTerm ),
83  limit( other.limit )
84  {
85  }
86 
87  bool operator==( const Private &other ) const
88  {
89  return rootTerm == other.rootTerm && limit == other.limit;
90  }
91 
92  static QVariantMap termToJSON( const SearchTerm &term )
93  {
94  const QList<SearchTerm> &subTerms = term.subTerms();
95  QVariantMap termJSON;
96  termJSON.insert( QLatin1String( "negated"), term.isNegated() );
97  if ( subTerms.isEmpty() ) {
98  termJSON.insert( QLatin1String( "key" ), term.key() );
99  termJSON.insert( QLatin1String( "value" ), term.value() );
100  termJSON.insert( QLatin1String( "cond" ), static_cast<int>( term.condition() ) );
101  } else {
102  termJSON.insert( QLatin1String( "rel" ), static_cast<int>( term.relation() ) );
103  QVariantList subTermsJSON;
104  Q_FOREACH ( const SearchTerm &term, subTerms ) {
105  subTermsJSON.append( termToJSON( term ) );
106  }
107  termJSON.insert( QLatin1String( "subTerms" ), subTermsJSON );
108  }
109 
110  return termJSON;
111  }
112 
113  static SearchTerm JSONToTerm( const QVariantMap &json )
114  {
115  if ( json.contains( QLatin1String( "key" ) ) ) {
116  SearchTerm term( json[QLatin1String( "key" )].toString(),
117  json[QLatin1String( "value" )],
118  static_cast<SearchTerm::Condition>( json[QLatin1String( "cond" )].toInt() ) );
119  term.setIsNegated( json[QLatin1String( "negated" )].toBool() );
120  return term;
121  } else if ( json.contains( QLatin1String( "rel" ) ) ) {
122  SearchTerm term( static_cast<SearchTerm::Relation>( json[QLatin1String( "rel" )].toInt() ) );
123  term.setIsNegated( json[QLatin1String( "negated" )].toBool() );
124  const QVariantList subTermsJSON = json[QLatin1String( "subTerms" )].toList();
125  Q_FOREACH ( const QVariant &subTermJSON, subTermsJSON ) {
126  term.addSubTerm( JSONToTerm( subTermJSON.toMap() ) );
127  }
128  return term;
129  } else {
130  kWarning() << "Invalid JSON for term: "<< json;
131  return SearchTerm();
132  }
133  }
134 
135  SearchTerm rootTerm;
136  int limit;
137 };
138 
139 SearchTerm::SearchTerm( SearchTerm::Relation relation ):
140  d( new Private )
141 {
142  d->relation = relation;
143 }
144 
145 SearchTerm::SearchTerm( const QString &key, const QVariant &value, SearchTerm::Condition condition ):
146  d( new Private )
147 {
148  d->relation = RelAnd;
149  d->key = key;
150  d->value = value;
151  d->condition = condition;
152 }
153 
154 SearchTerm::SearchTerm( const SearchTerm &other ):
155  d( other.d )
156 {
157 }
158 
159 SearchTerm::~SearchTerm()
160 {
161 }
162 
163 SearchTerm& SearchTerm::operator=( const SearchTerm &other )
164 {
165  d = other.d;
166  return *this;
167 }
168 
169 bool SearchTerm::operator==( const SearchTerm &other ) const
170 {
171  return *d == *other.d;
172 }
173 
174 bool SearchTerm::isNull() const
175 {
176  return d->key.isEmpty() && d->value.isNull() && d->terms.isEmpty();
177 }
178 
179 QString SearchTerm::key() const
180 {
181  return d->key;
182 }
183 
184 QVariant SearchTerm::value() const
185 {
186  return d->value;
187 }
188 
189 SearchTerm::Condition SearchTerm::condition() const
190 {
191  return d->condition;
192 }
193 
194 void SearchTerm::setIsNegated( bool negated )
195 {
196  d->isNegated = negated;
197 }
198 
199 bool SearchTerm::isNegated() const
200 {
201  return d->isNegated;
202 }
203 
204 void SearchTerm::addSubTerm( const SearchTerm &term )
205 {
206  d->terms << term;
207 }
208 
209 QList< SearchTerm > SearchTerm::subTerms() const
210 {
211  return d->terms;
212 }
213 
214 SearchTerm::Relation SearchTerm::relation() const
215 {
216  return d->relation;
217 }
218 
219 
220 SearchQuery::SearchQuery( SearchTerm::Relation rel ):
221  d( new Private )
222 {
223  d->rootTerm = SearchTerm( rel );
224 }
225 
226 SearchQuery::SearchQuery( const SearchQuery &other ):
227  d( other.d )
228 {
229 }
230 
231 SearchQuery::~SearchQuery()
232 {
233 }
234 
235 SearchQuery& SearchQuery::operator=( const SearchQuery &other )
236 {
237  d = other.d;
238  return *this;
239 }
240 
241 bool SearchQuery::operator==( const SearchQuery &other ) const
242 {
243  return *d == *other.d;
244 }
245 
246 bool SearchQuery::isNull() const
247 {
248  return d->rootTerm.isNull();
249 }
250 
251 SearchTerm SearchQuery::term() const
252 {
253  return d->rootTerm;
254 }
255 
256 void SearchQuery::addTerm( const QString &key, const QVariant &value, SearchTerm::Condition condition )
257 {
258  addTerm( SearchTerm( key, value, condition ) );
259 }
260 
261 void SearchQuery::addTerm( const SearchTerm &term )
262 {
263  d->rootTerm.addSubTerm( term );
264 }
265 
266 void SearchQuery::setTerm( const SearchTerm& term )
267 {
268  d->rootTerm = term;
269 }
270 
271 void SearchQuery::setLimit( int limit )
272 {
273  d->limit = limit;
274 }
275 
276 int SearchQuery::limit() const
277 {
278  return d->limit;
279 }
280 
281 QByteArray SearchQuery::toJSON() const
282 {
283  QVariantMap root = Private::termToJSON( d->rootTerm );
284  root.insert( QLatin1String( "limit" ), d->limit );
285 
286  QJson::Serializer serializer;
287  return serializer.serialize( root );
288 }
289 
290 SearchQuery SearchQuery::fromJSON( const QByteArray &jsonData )
291 {
292  QJson::Parser parser;
293  bool ok = false;
294  const QVariant json = parser.parse( jsonData, &ok );
295  if ( !ok || json.isNull() ) {
296  return SearchQuery();
297  }
298 
299  const QVariantMap map = json.toMap();
300  SearchQuery query;
301  query.d->rootTerm = Private::JSONToTerm( map );
302  if ( map.contains( QLatin1String("limit") ) ) {
303  query.d->limit = map.value( QLatin1String("limit") ).toInt();
304  }
305  return query;
306 }
307 
308 QMap<EmailSearchTerm::EmailSearchField, QString> initializeMapping()
309 {
310  QMap<EmailSearchTerm::EmailSearchField, QString> mapping;
311  mapping.insert(EmailSearchTerm::Body, QLatin1String("body"));
312  mapping.insert(EmailSearchTerm::Headers, QLatin1String("headers"));
313  mapping.insert(EmailSearchTerm::Subject, QLatin1String("subject"));
314  mapping.insert(EmailSearchTerm::Message, QLatin1String("message"));
315  mapping.insert(EmailSearchTerm::HeaderFrom, QLatin1String("from"));
316  mapping.insert(EmailSearchTerm::HeaderTo, QLatin1String("to"));
317  mapping.insert(EmailSearchTerm::HeaderCC, QLatin1String("cc"));
318  mapping.insert(EmailSearchTerm::HeaderBCC, QLatin1String("bcc"));
319  mapping.insert(EmailSearchTerm::HeaderReplyTo, QLatin1String("replyto"));
320  mapping.insert(EmailSearchTerm::HeaderOrganization, QLatin1String("organization"));
321  mapping.insert(EmailSearchTerm::HeaderListId, QLatin1String("listid"));
322  mapping.insert(EmailSearchTerm::HeaderResentFrom, QLatin1String("resentfrom"));
323  mapping.insert(EmailSearchTerm::HeaderXLoop, QLatin1String("xloop"));
324  mapping.insert(EmailSearchTerm::HeaderXMailingList, QLatin1String("xmailinglist"));
325  mapping.insert(EmailSearchTerm::HeaderXSpamFlag, QLatin1String("xspamflag"));
326  mapping.insert(EmailSearchTerm::HeaderDate, QLatin1String("date"));
327  mapping.insert(EmailSearchTerm::HeaderOnlyDate, QLatin1String("onlydate"));
328  mapping.insert(EmailSearchTerm::MessageStatus, QLatin1String("messagestatus"));
329  mapping.insert(EmailSearchTerm::MessageTag, QLatin1String("messagetag"));
330  mapping.insert(EmailSearchTerm::ByteSize, QLatin1String("size"));
331  mapping.insert(EmailSearchTerm::Attachment, QLatin1String("attachment"));
332  return mapping;
333 }
334 
335 static QMap<EmailSearchTerm::EmailSearchField, QString> emailSearchFieldMapping = initializeMapping();
336 
337 EmailSearchTerm::EmailSearchTerm( EmailSearchTerm::EmailSearchField field, const QVariant& value, SearchTerm::Condition condition )
338  : SearchTerm( toKey( field ), value, condition )
339 {
340 
341 }
342 
343 QString EmailSearchTerm::toKey( EmailSearchTerm::EmailSearchField field )
344 {
345  return emailSearchFieldMapping.value( field );
346 }
347 
348 EmailSearchTerm::EmailSearchField EmailSearchTerm::fromKey( const QString &key )
349 {
350  return emailSearchFieldMapping.key(key);
351 }
352 
353 QMap<ContactSearchTerm::ContactSearchField, QString> initializeContactMapping()
354 {
355  QMap<ContactSearchTerm::ContactSearchField, QString> mapping;
356  mapping.insert(ContactSearchTerm::Name, QLatin1String("name"));
357  mapping.insert(ContactSearchTerm::Nickname, QLatin1String("nickname"));
358  mapping.insert(ContactSearchTerm::Email, QLatin1String("email"));
359  mapping.insert(ContactSearchTerm::Uid, QLatin1String("uid"));
360  mapping.insert(ContactSearchTerm::All, QLatin1String("all"));
361  return mapping;
362 }
363 
364 static QMap<ContactSearchTerm::ContactSearchField, QString> contactSearchFieldMapping = initializeContactMapping();
365 
366 ContactSearchTerm::ContactSearchTerm( ContactSearchTerm::ContactSearchField field, const QVariant& value, SearchTerm::Condition condition )
367  : SearchTerm( toKey( field ), value, condition )
368 {
369 
370 }
371 
372 QString ContactSearchTerm::toKey( ContactSearchTerm::ContactSearchField field )
373 {
374  return contactSearchFieldMapping.value( field );
375 }
376 
377 ContactSearchTerm::ContactSearchField ContactSearchTerm::fromKey( const QString &key )
378 {
379  return contactSearchFieldMapping.key(key);
380 }
Akonadi::SearchTerm::setIsNegated
void setIsNegated(bool negated)
Sets whether the entire term is negated.
Definition: searchquery.cpp:194
Akonadi::SearchQuery::setTerm
void setTerm(const SearchTerm &term)
Sets the root term.
Definition: searchquery.cpp:266
Akonadi::SearchTerm::isNegated
bool isNegated() const
Returns whether the entire term is negated.
Definition: searchquery.cpp:199
Akonadi::SearchTerm::addSubTerm
void addSubTerm(const SearchTerm &term)
Adds a new subterm to this term.
Definition: searchquery.cpp:204
Akonadi::EmailSearchTerm::EmailSearchTerm
EmailSearchTerm(EmailSearchField field, const QVariant &value, SearchTerm::Condition condition=SearchTerm::CondEqual)
Constructs an email end term.
Definition: searchquery.cpp:337
Akonadi::EmailSearchTerm::toKey
static QString toKey(EmailSearchField)
Translates field to key.
Definition: searchquery.cpp:343
Akonadi::SearchQuery::addTerm
void addTerm(const QString &key, const QVariant &value, SearchTerm::Condition condition=SearchTerm::CondEqual)
Adds a new term.
Definition: searchquery.cpp:256
Akonadi::SearchTerm::condition
SearchTerm::Condition condition() const
Returns relation between key and value.
Definition: searchquery.cpp:189
Akonadi::SearchTerm::key
QString key() const
Returns key of this end term.
Definition: searchquery.cpp:179
Akonadi::SearchTerm::SearchTerm
SearchTerm(SearchTerm::Relation relation=SearchTerm::RelAnd)
Constructs a term where all subterms will be in given relation.
Definition: searchquery.cpp:139
Akonadi::EmailSearchTerm::fromKey
static EmailSearchField fromKey(const QString &key)
Translates key to field.
Definition: searchquery.cpp:348
Akonadi::ContactSearchTerm::toKey
static QString toKey(ContactSearchField)
Translates field to key.
Definition: searchquery.cpp:372
Akonadi::SearchQuery
A query that can be passed to ItemSearchJob or others.
Definition: searchquery.h:129
Akonadi::SearchQuery::term
SearchTerm term() const
Returns the root term.
Definition: searchquery.cpp:251
Akonadi::SearchQuery::setLimit
void setLimit(int limit)
Sets the maximum number of results.
Definition: searchquery.cpp:271
Akonadi::SearchTerm::subTerms
QList< SearchTerm > subTerms() const
Returns all subterms, or an empty list if this is an end term.
Definition: searchquery.cpp:209
Akonadi::SearchTerm::value
QVariant value() const
Returns value of this end term.
Definition: searchquery.cpp:184
Akonadi::SearchQuery::SearchQuery
SearchQuery(SearchTerm::Relation rel=SearchTerm::RelAnd)
Constructs query where all added terms will be in given relation.
Definition: searchquery.cpp:220
Akonadi::SearchTerm
Search term represents the actual condition within query.
Definition: searchquery.h:40
Akonadi::SearchQuery::limit
int limit() const
Returns the maximum number of results.
Definition: searchquery.cpp:276
Akonadi::ContactSearchTerm::fromKey
static ContactSearchField fromKey(const QString &key)
Translates key to field.
Definition: searchquery.cpp:377
Akonadi::SearchTerm::relation
SearchTerm::Relation relation() const
Returns relation in which all subterms are.
Definition: searchquery.cpp:214
Akonadi::EmailSearchTerm::EmailSearchField
EmailSearchField
All fields expect a search string unless noted otherwise.
Definition: searchquery.h:202
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Mon Jul 21 2014 08:03:55 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