Changeset View
Changeset View
Standalone View
Standalone View
src/drive/filesearchquery.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | * Copyright (C) 2014 Daniel Vrátil <dvratil@redhat.com> | 2 | * Copyright (C) 2014 Daniel Vrátil <dvratil@redhat.com> | ||
3 | * Copyright (C) 2019 David Barchiesi <david@barchie.si> | ||||
3 | * | 4 | * | ||
4 | * This library is free software; you can redistribute it and/or | 5 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | 6 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | 7 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) version 3, or any | 8 | * version 2.1 of the License, or (at your option) version 3, or any | ||
8 | * later version accepted by the membership of KDE e.V. (or its | 9 | * later version accepted by the membership of KDE e.V. (or its | ||
9 | * successor approved by the membership of KDE e.V.), which shall | 10 | * successor approved by the membership of KDE e.V.), which shall | ||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | 11 | * act as a proxy defined in Section 6 of version 3 of the license. | ||
Show All 10 Lines | |||||
21 | #include "filesearchquery.h" | 22 | #include "filesearchquery.h" | ||
22 | 23 | | |||
23 | #include <QString> | 24 | #include <QString> | ||
24 | #include <QDateTime> | 25 | #include <QDateTime> | ||
25 | 26 | | |||
26 | using namespace KGAPI2; | 27 | using namespace KGAPI2; | ||
27 | using namespace KGAPI2::Drive; | 28 | using namespace KGAPI2::Drive; | ||
28 | 29 | | |||
29 | class Q_DECL_HIDDEN FileSearchQuery::Private : public QSharedData | | |||
30 | { | | |||
31 | public: | | |||
32 | Private(); | | |||
33 | Private(const Private &other); | | |||
34 | ~Private(); | | |||
35 | | ||||
36 | static QString fieldToString(Field field); | | |||
37 | static QString compareOperatorToString(CompareOperator op); | | |||
38 | static QString logicOperatorToString(LogicOperator op); | | |||
39 | static QString valueToString(Field field, const QVariant &var); | | |||
40 | | ||||
41 | QList<FileSearchQuery> subqueries; | | |||
42 | QVariant value; | | |||
43 | Field field; | | |||
44 | CompareOperator compareOp; | | |||
45 | LogicOperator logicOp; | | |||
46 | }; | | |||
47 | | ||||
48 | FileSearchQuery::Private::Private() | | |||
49 | : QSharedData() | | |||
50 | { | | |||
51 | } | | |||
52 | 30 | | |||
53 | FileSearchQuery::Private::Private(const Private &other) | 31 | QString FileSearchQuery::fieldToString(Field field) | ||
54 | : QSharedData(other) | | |||
55 | , subqueries(other.subqueries) | | |||
56 | , value(other.value) | | |||
57 | , field(other.field) | | |||
58 | , compareOp(other.compareOp) | | |||
59 | , logicOp(other.logicOp) | | |||
60 | { | | |||
61 | } | | |||
62 | | ||||
63 | FileSearchQuery::Private::~Private() | | |||
64 | { | | |||
65 | } | | |||
66 | | ||||
67 | | ||||
68 | QString FileSearchQuery::Private::fieldToString(Field field) | | |||
69 | { | 32 | { | ||
70 | switch (field) { | 33 | switch (field) { | ||
71 | case Title: | 34 | case Title: | ||
72 | return QStringLiteral("title"); | 35 | return QStringLiteral("title"); | ||
73 | case FullText: | 36 | case FullText: | ||
74 | return QStringLiteral("fullText"); | 37 | return QStringLiteral("fullText"); | ||
75 | case MimeType: | 38 | case MimeType: | ||
76 | return QStringLiteral("mimeType"); | 39 | return QStringLiteral("mimeType"); | ||
Show All 16 Lines | |||||
93 | case SharedWithMe: | 56 | case SharedWithMe: | ||
94 | return QStringLiteral("sharedWithMe"); | 57 | return QStringLiteral("sharedWithMe"); | ||
95 | } | 58 | } | ||
96 | 59 | | |||
97 | Q_ASSERT(false); | 60 | Q_ASSERT(false); | ||
98 | return QString(); | 61 | return QString(); | ||
99 | } | 62 | } | ||
100 | 63 | | |||
101 | QString FileSearchQuery::Private::compareOperatorToString(CompareOperator op) | 64 | QString FileSearchQuery::valueToString(FileSearchQuery::Field field, const QVariant &var) | ||
102 | { | | |||
103 | switch (op) { | | |||
104 | case Contains: | | |||
105 | return QStringLiteral(" contains "); | | |||
106 | case Equals: | | |||
107 | return QStringLiteral(" = "); | | |||
108 | case NotEquals: | | |||
109 | return QStringLiteral(" != "); | | |||
110 | case Less: | | |||
111 | return QStringLiteral(" < "); | | |||
112 | case LessOrEqual: | | |||
113 | return QStringLiteral(" <= "); | | |||
114 | case Greater: | | |||
115 | return QStringLiteral(" > "); | | |||
116 | case GreaterOrEqual: | | |||
117 | return QStringLiteral(" >= "); | | |||
118 | case In: | | |||
119 | return QStringLiteral(" in "); | | |||
120 | case Has: | | |||
121 | return QStringLiteral(" has "); | | |||
122 | } | | |||
123 | | ||||
124 | Q_ASSERT(false); | | |||
125 | return QString(); | | |||
126 | } | | |||
127 | | ||||
128 | QString FileSearchQuery::Private::logicOperatorToString(FileSearchQuery::LogicOperator op) | | |||
129 | { | | |||
130 | switch (op) { | | |||
131 | case And: | | |||
132 | return QStringLiteral(" and "); | | |||
133 | case Or: | | |||
134 | return QStringLiteral(" or "); | | |||
135 | } | | |||
136 | | ||||
137 | Q_ASSERT(false); | | |||
138 | return QString(); | | |||
139 | } | | |||
140 | | ||||
141 | QString FileSearchQuery::Private::valueToString(FileSearchQuery::Field field, const QVariant &var) | | |||
142 | { | 65 | { | ||
143 | switch (field) { | 66 | switch (field) { | ||
144 | case Title: | 67 | case Title: | ||
145 | case FullText: | 68 | case FullText: | ||
146 | case MimeType: | 69 | case MimeType: | ||
147 | case Parents: | 70 | case Parents: | ||
148 | case Owners: | 71 | case Owners: | ||
149 | case Writers: | 72 | case Writers: | ||
150 | case Readers: | 73 | case Readers: | ||
151 | return QStringLiteral("'%1'").arg(var.toString().replace(QLatin1Char('\''), QLatin1String("\\\'"))); | 74 | return QStringLiteral("'%1'").arg(var.toString().replace(QLatin1Char('\''), QLatin1String("\\\'"))); | ||
152 | case ModifiedDate: | 75 | case ModifiedDate: | ||
153 | case LastViewedByMeDate: | 76 | case LastViewedByMeDate: | ||
154 | return QStringLiteral("'%1'").arg(var.toDateTime().toUTC().toString(QStringLiteral("yyyy-MM-ddThh:mm:ss"))); | 77 | return QStringLiteral("'%1'").arg(var.toDateTime().toUTC().toString(QStringLiteral("yyyy-MM-ddThh:mm:ss"))); | ||
155 | case Trashed: | 78 | case Trashed: | ||
156 | case Starred: | 79 | case Starred: | ||
157 | case SharedWithMe: | 80 | case SharedWithMe: | ||
158 | return (var.toBool() == true ? QStringLiteral("true") : QStringLiteral("false")); | 81 | return (var.toBool() == true ? QStringLiteral("true") : QStringLiteral("false")); | ||
159 | } | 82 | } | ||
160 | 83 | | |||
161 | Q_ASSERT(false); | 84 | Q_ASSERT(false); | ||
162 | return QString(); | 85 | return QString(); | ||
163 | } | 86 | } | ||
164 | 87 | | |||
165 | FileSearchQuery::FileSearchQuery(FileSearchQuery::LogicOperator op) | | |||
166 | : d(new Private) | | |||
167 | { | | |||
168 | d->logicOp = op; | | |||
169 | } | | |||
170 | | ||||
171 | FileSearchQuery::FileSearchQuery(const FileSearchQuery &other) | | |||
172 | : d(other.d) | | |||
173 | { | | |||
174 | } | | |||
175 | | ||||
176 | FileSearchQuery::~FileSearchQuery() | | |||
177 | { | | |||
178 | } | | |||
179 | | ||||
180 | FileSearchQuery &FileSearchQuery::operator=(const FileSearchQuery &other) | | |||
181 | { | | |||
182 | d = other.d; | | |||
183 | return *this; | | |||
184 | } | | |||
185 | | ||||
186 | | ||||
187 | void FileSearchQuery::addQuery(FileSearchQuery::Field field, FileSearchQuery::CompareOperator op, const QVariant &value) | 88 | void FileSearchQuery::addQuery(FileSearchQuery::Field field, FileSearchQuery::CompareOperator op, const QVariant &value) | ||
188 | { | 89 | { | ||
189 | switch (field) { | 90 | switch (field) { | ||
190 | case Title: | 91 | case Title: | ||
191 | case MimeType: | 92 | case MimeType: | ||
192 | Q_ASSERT(op == Contains || op == Equals || op == NotEquals); | 93 | Q_ASSERT(op == Contains || op == Equals || op == NotEquals); | ||
193 | Q_ASSERT(value.canConvert<QString>()); | 94 | Q_ASSERT(value.canConvert<QString>()); | ||
194 | break; | 95 | break; | ||
Show All 16 Lines | |||||
211 | case Owners: | 112 | case Owners: | ||
212 | case Writers: | 113 | case Writers: | ||
213 | case Readers: | 114 | case Readers: | ||
214 | Q_ASSERT(op == In); | 115 | Q_ASSERT(op == In); | ||
215 | Q_ASSERT(value.canConvert<QString>()); | 116 | Q_ASSERT(value.canConvert<QString>()); | ||
216 | break; | 117 | break; | ||
217 | } | 118 | } | ||
218 | 119 | | |||
219 | FileSearchQuery query; | 120 | SearchQuery::addQuery(fieldToString(field), op, valueToString(field, value)); | ||
220 | query.d->field = field; | | |||
221 | query.d->compareOp = op; | | |||
222 | query.d->value = value; | | |||
223 | d->subqueries.append(query); | | |||
224 | } | 121 | } | ||
dvratil: If you just chain up to the parent implementation here, there's no need to have… | |||||
225 | | ||||
226 | void FileSearchQuery::addQuery(const FileSearchQuery &query) | | |||
227 | { | | |||
228 | d->subqueries.append(query); | | |||
229 | } | | |||
230 | | ||||
231 | bool FileSearchQuery::isEmpty() const | | |||
232 | { | | |||
233 | return d->value.isNull() && d->subqueries.isEmpty(); | | |||
234 | } | | |||
235 | | ||||
236 | QString FileSearchQuery::serialize() const | | |||
237 | { | | |||
238 | if (isEmpty()) { | | |||
239 | return QString(); | | |||
240 | } | | |||
241 | | ||||
242 | QString r; | | |||
243 | r = QLatin1Char('('); | | |||
244 | if (d->subqueries.isEmpty()) { | | |||
245 | if (d->compareOp == In) { | | |||
246 | r += QStringLiteral("%1 in %2").arg(Private::valueToString(d->field, d->value), | | |||
247 | Private::fieldToString(d->field)); | | |||
248 | } else { | | |||
249 | r += Private::fieldToString(d->field) % Private::compareOperatorToString(d->compareOp) % Private::valueToString(d->field, d->value); | | |||
250 | } | | |||
251 | } else { | | |||
252 | QList<FileSearchQuery>::ConstIterator iter, end; | | |||
253 | for (iter = d->subqueries.constBegin(), end = d->subqueries.constEnd(); iter != end; ++iter) { | | |||
254 | if (iter != d->subqueries.constBegin()) { | | |||
255 | r += Private::logicOperatorToString(d->logicOp); | | |||
256 | } | | |||
257 | r += (*iter).serialize(); | | |||
258 | } | | |||
259 | } | | |||
260 | r += QLatin1Char(')'); | | |||
261 | | ||||
262 | return r; | | |||
263 | } | | |||
264 | | ||||
265 | |
If you just chain up to the parent implementation here, there's no need to have FileSearchQuery::addQuery() at all.