1 | /* |
2 | Copyright (c) 2010 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 "persistentsearchattribute.h" |
21 | #include "collection.h" |
22 | |
23 | #include <akonadi/private/imapparser_p.h> |
24 | |
25 | #include <QtCore/QString> |
26 | #include <QtCore/QStringList> |
27 | |
28 | using namespace Akonadi; |
29 | |
30 | class PersistentSearchAttribute::Private |
31 | { |
32 | public: |
33 | Private() |
34 | : remote(true) |
35 | , recursive(false) |
36 | { |
37 | } |
38 | |
39 | QString queryString; |
40 | QList<qint64> queryCollections; |
41 | bool remote; |
42 | bool recursive; |
43 | }; |
44 | |
45 | PersistentSearchAttribute::PersistentSearchAttribute() |
46 | : d(new Private) |
47 | { |
48 | } |
49 | |
50 | PersistentSearchAttribute::~PersistentSearchAttribute() |
51 | { |
52 | delete d; |
53 | } |
54 | |
55 | QString PersistentSearchAttribute::queryLanguage() const |
56 | { |
57 | return QLatin1String("SPARQL" ); |
58 | } |
59 | |
60 | void PersistentSearchAttribute::setQueryLanguage(const QString &language) |
61 | { |
62 | Q_UNUSED(language); |
63 | } |
64 | |
65 | QString PersistentSearchAttribute::queryString() const |
66 | { |
67 | return d->queryString; |
68 | } |
69 | |
70 | void PersistentSearchAttribute::setQueryString(const QString &query) |
71 | { |
72 | d->queryString = query; |
73 | } |
74 | |
75 | QList<qint64> PersistentSearchAttribute::queryCollections() const |
76 | { |
77 | return d->queryCollections; |
78 | } |
79 | |
80 | void PersistentSearchAttribute::setQueryCollections(const QList<Collection> &collections) |
81 | { |
82 | d->queryCollections.clear(); |
83 | Q_FOREACH (const Collection &collection, collections) { |
84 | d->queryCollections << collection.id(); |
85 | } |
86 | } |
87 | |
88 | void PersistentSearchAttribute::setQueryCollections(const QList<qint64> &collectionsIds) |
89 | { |
90 | d->queryCollections = collectionsIds; |
91 | } |
92 | |
93 | bool PersistentSearchAttribute::isRecursive() const |
94 | { |
95 | return d->recursive; |
96 | } |
97 | |
98 | void PersistentSearchAttribute::setRecursive(bool recursive) |
99 | { |
100 | d->recursive = recursive; |
101 | } |
102 | |
103 | bool PersistentSearchAttribute::isRemoteSearchEnabled() const |
104 | { |
105 | return d->remote; |
106 | } |
107 | |
108 | void PersistentSearchAttribute::setRemoteSearchEnabled(bool enabled) |
109 | { |
110 | d->remote = enabled; |
111 | } |
112 | |
113 | QByteArray PersistentSearchAttribute::type() const |
114 | { |
115 | return "PERSISTENTSEARCH" ; // ### should eventually be AKONADI_PARAM_PERSISTENTSEARCH |
116 | } |
117 | |
118 | Attribute *PersistentSearchAttribute::clone() const |
119 | { |
120 | PersistentSearchAttribute *attr = new PersistentSearchAttribute; |
121 | attr->setQueryString(queryString()); |
122 | attr->setQueryCollections(queryCollections()); |
123 | attr->setRecursive(isRecursive()); |
124 | attr->setRemoteSearchEnabled(isRemoteSearchEnabled()); |
125 | return attr; |
126 | } |
127 | |
128 | QByteArray PersistentSearchAttribute::serialized() const |
129 | { |
130 | QStringList cols; |
131 | Q_FOREACH (qint64 colId, d->queryCollections) { |
132 | cols << QString::number(colId); |
133 | } |
134 | |
135 | QList<QByteArray> l; |
136 | // ### eventually replace with the AKONADI_PARAM_PERSISTENTSEARCH_XXX constants |
137 | l.append("QUERYSTRING" ); |
138 | l.append(ImapParser::quote(d->queryString.toUtf8())); |
139 | l.append("QUERYCOLLECTIONS" ); |
140 | l.append("(" + cols.join(QLatin1String(" " )).toLatin1() + ")" ); |
141 | if (d->remote) { |
142 | l.append("REMOTE" ); |
143 | } |
144 | if (d->recursive) { |
145 | l.append("RECURSIVE" ); |
146 | } |
147 | return "(" + ImapParser::join(l, " " ) + ')'; //krazy:exclude=doublequote_chars |
148 | } |
149 | |
150 | void PersistentSearchAttribute::deserialize(const QByteArray &data) |
151 | { |
152 | QList<QByteArray> l; |
153 | ImapParser::parseParenthesizedList(data, l); |
154 | for (int i = 0; i < l.size() - 1; ++i) { |
155 | const QByteArray key = l.at(i); |
156 | if (key == "QUERYLANGUAGE" ) { |
157 | // Skip the value |
158 | ++i; |
159 | } else if (key == "QUERYSTRING" ) { |
160 | d->queryString = QString::fromUtf8(l.at(i + 1)); |
161 | ++i; |
162 | } else if (key == "QUERYCOLLECTIONS" ) { |
163 | QList<QByteArray> ids; |
164 | ImapParser::parseParenthesizedList(l.at(i + 1), ids); |
165 | d->queryCollections.clear(); |
166 | Q_FOREACH (const QByteArray &id, ids) { |
167 | d->queryCollections << id.toLongLong(); |
168 | } |
169 | ++i; |
170 | } else if (key == "REMOTE" ) { |
171 | d->remote = true; |
172 | } else if (key == "RECURSIVE" ) { |
173 | d->recursive = true; |
174 | } |
175 | } |
176 | } |
177 | |