20#include "searchquery.h"
22#include <QtCore/QVariant>
26#include <qjson/parser.h>
27#include <qjson/serializer.h>
31class SearchTerm::Private :
public QSharedData
42 Private(
const Private &other)
46 , condition(other.condition)
47 , relation(other.relation)
49 , isNegated(other.isNegated)
53 bool operator==(
const Private &other)
const
55 return relation == other.relation
56 && isNegated == other.isNegated
57 && terms == other.terms
59 && value == other.value
60 && condition == other.condition;
67 QList<SearchTerm> terms;
71class SearchQuery::Private :
public QSharedData
80 Private(
const Private &other)
82 , rootTerm(other.rootTerm)
87 bool operator==(
const Private &other)
const
89 return rootTerm == other.rootTerm && limit == other.limit;
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()));
102 termJSON.insert(QLatin1String(
"rel"),
static_cast<int>(
term.
relation()));
103 QVariantList subTermsJSON;
105 subTermsJSON.append(termToJSON(
term));
107 termJSON.insert(QLatin1String(
"subTerms"), subTermsJSON);
113 static SearchTerm JSONToTerm(
const QVariantMap &json)
115 if (json.contains(QLatin1String(
"key"))) {
117 json[QLatin1String(
"value")],
118 static_cast<SearchTerm::Condition
>(json[QLatin1String(
"cond")].toInt()));
121 }
else if (json.contains(QLatin1String(
"rel"))) {
122 SearchTerm term(
static_cast<SearchTerm::Relation
>(json[QLatin1String(
"rel")].toInt()));
124 const QVariantList subTermsJSON = json[QLatin1String(
"subTerms")].toList();
125 Q_FOREACH (
const QVariant &subTermJSON, subTermsJSON) {
130 kWarning() <<
"Invalid JSON for term: " << json;
148 d->relation = RelAnd;
159SearchTerm::~SearchTerm()
169bool SearchTerm::operator==(
const SearchTerm &other)
const
171 return *d == *other.d;
174bool SearchTerm::isNull()
const
176 return d->key.isEmpty() && d->value.isNull() && d->terms.isEmpty();
196 d->isNegated = negated;
230SearchQuery::~SearchQuery()
240bool SearchQuery::operator==(
const SearchQuery &other)
const
242 return *d == *other.d;
245bool SearchQuery::isNull()
const
247 return d->rootTerm.isNull();
262 d->rootTerm.addSubTerm(
term);
280QByteArray SearchQuery::toJSON()
const
282 QVariantMap root = Private::termToJSON(d->rootTerm);
283 root.insert(QLatin1String(
"limit"), d->limit);
285 QJson::Serializer serializer;
286 return serializer.serialize(root);
289SearchQuery SearchQuery::fromJSON(
const QByteArray &jsonData)
291 QJson::Parser parser;
293 const QVariant json = parser.parse(jsonData, &ok);
294 if (!ok || json.isNull()) {
298 const QVariantMap map = json.toMap();
300 query.d->rootTerm = Private::JSONToTerm(map);
301 if (map.contains(QLatin1String(
"limit"))) {
302 query.d->limit = map.value(QLatin1String(
"limit")).toInt();
307QMap<EmailSearchTerm::EmailSearchField, QString> initializeMapping()
309 QMap<EmailSearchTerm::EmailSearchField, QString> mapping;
310 mapping.insert(EmailSearchTerm::Body, QLatin1String(
"body"));
311 mapping.insert(EmailSearchTerm::Headers, QLatin1String(
"headers"));
312 mapping.insert(EmailSearchTerm::Subject, QLatin1String(
"subject"));
313 mapping.insert(EmailSearchTerm::Message, QLatin1String(
"message"));
314 mapping.insert(EmailSearchTerm::HeaderFrom, QLatin1String(
"from"));
315 mapping.insert(EmailSearchTerm::HeaderTo, QLatin1String(
"to"));
316 mapping.insert(EmailSearchTerm::HeaderCC, QLatin1String(
"cc"));
317 mapping.insert(EmailSearchTerm::HeaderBCC, QLatin1String(
"bcc"));
318 mapping.insert(EmailSearchTerm::HeaderReplyTo, QLatin1String(
"replyto"));
319 mapping.insert(EmailSearchTerm::HeaderOrganization, QLatin1String(
"organization"));
320 mapping.insert(EmailSearchTerm::HeaderListId, QLatin1String(
"listid"));
321 mapping.insert(EmailSearchTerm::HeaderResentFrom, QLatin1String(
"resentfrom"));
322 mapping.insert(EmailSearchTerm::HeaderXLoop, QLatin1String(
"xloop"));
323 mapping.insert(EmailSearchTerm::HeaderXMailingList, QLatin1String(
"xmailinglist"));
324 mapping.insert(EmailSearchTerm::HeaderXSpamFlag, QLatin1String(
"xspamflag"));
325 mapping.insert(EmailSearchTerm::HeaderDate, QLatin1String(
"date"));
326 mapping.insert(EmailSearchTerm::HeaderOnlyDate, QLatin1String(
"onlydate"));
327 mapping.insert(EmailSearchTerm::MessageStatus, QLatin1String(
"messagestatus"));
328 mapping.insert(EmailSearchTerm::MessageTag, QLatin1String(
"messagetag"));
329 mapping.insert(EmailSearchTerm::ByteSize, QLatin1String(
"size"));
330 mapping.insert(EmailSearchTerm::Attachment, QLatin1String(
"attachment"));
334static QMap<EmailSearchTerm::EmailSearchField, QString> emailSearchFieldMapping = initializeMapping();
344 return emailSearchFieldMapping.value(field);
349 return emailSearchFieldMapping.key(
key);
352QMap<ContactSearchTerm::ContactSearchField, QString> initializeContactMapping()
354 QMap<ContactSearchTerm::ContactSearchField, QString> mapping;
355 mapping.insert(ContactSearchTerm::Name, QLatin1String(
"name"));
356 mapping.insert(ContactSearchTerm::Nickname, QLatin1String(
"nickname"));
357 mapping.insert(ContactSearchTerm::Email, QLatin1String(
"email"));
358 mapping.insert(ContactSearchTerm::Uid, QLatin1String(
"uid"));
359 mapping.insert(ContactSearchTerm::All, QLatin1String(
"all"));
363static QMap<ContactSearchTerm::ContactSearchField, QString> contactSearchFieldMapping = initializeContactMapping();
365ContactSearchTerm::ContactSearchTerm(ContactSearchTerm::ContactSearchField field,
const QVariant &value, SearchTerm::Condition condition)
373 return contactSearchFieldMapping.value(field);
378 return contactSearchFieldMapping.key(
key);
static EmailSearchField fromKey(const QString &key)
Translates key to field.
EmailSearchTerm(EmailSearchField field, const QVariant &value, SearchTerm::Condition condition=SearchTerm::CondEqual)
Constructs an email end term.
static QString toKey(EmailSearchField)
Translates field to key.
EmailSearchField
All fields expect a search string unless noted otherwise.
A query that can be passed to ItemSearchJob or others.
void setTerm(const SearchTerm &term)
Sets the root term.
void addTerm(const QString &key, const QVariant &value, SearchTerm::Condition condition=SearchTerm::CondEqual)
Adds a new term.
void setLimit(int limit)
Sets the maximum number of results.
int limit() const
Returns the maximum number of results.
SearchQuery(SearchTerm::Relation rel=SearchTerm::RelAnd)
Constructs query where all added terms will be in given relation.
SearchTerm term() const
Returns the root term.
Search term represents the actual condition within query.
QVariant value() const
Returns value of this end term.
QString key() const
Returns key of this end term.
void addSubTerm(const SearchTerm &term)
Adds a new subterm to this term.
void setIsNegated(bool negated)
Sets whether the entire term is negated.
SearchTerm::Condition condition() const
Returns relation between key and value.
SearchTerm(SearchTerm::Relation relation=SearchTerm::RelAnd)
Constructs a term where all subterms will be in given relation.
bool isNegated() const
Returns whether the entire term is negated.
QList< SearchTerm > subTerms() const
Returns all subterms, or an empty list if this is an end term.
SearchTerm::Relation relation() const
Returns relation in which all subterms are.
FreeBusyManager::Singleton.