Changeset View
Changeset View
Standalone View
Standalone View
src/drive/drivessearchquery.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 David Barchiesi <david@barchie.si> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU Lesser General Public | ||||
6 | * 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 | * 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 | * act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | * | ||||
12 | * This library is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU Lesser General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU Lesser General Public | ||||
18 | * License along with this library. If not, see <https://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "drivessearchquery.h" | ||||
22 | | ||||
23 | #include <QString> | ||||
24 | #include <QDateTime> | ||||
25 | | ||||
26 | using namespace KGAPI2; | ||||
27 | using namespace KGAPI2::Drive; | ||||
28 | | ||||
29 | | ||||
30 | QString DrivesSearchQuery::fieldToString(Field field) | ||||
31 | { | ||||
32 | switch (field) { | ||||
33 | case Name: | ||||
34 | return QStringLiteral("name"); | ||||
35 | case Hidden: | ||||
36 | return QStringLiteral("hidden"); | ||||
37 | case CreatedDate: | ||||
38 | return QStringLiteral("createdDate"); | ||||
39 | case MemberCount: | ||||
40 | return QStringLiteral("memberCount"); | ||||
41 | case OrganizerCount: | ||||
42 | return QStringLiteral("organizerCount"); | ||||
43 | } | ||||
44 | | ||||
45 | Q_ASSERT(false); | ||||
46 | return QString(); | ||||
47 | } | ||||
48 | | ||||
49 | QString DrivesSearchQuery::valueToString(DrivesSearchQuery::Field field, const QVariant &var) | ||||
50 | { | ||||
51 | switch (field) { | ||||
52 | case Name: | ||||
53 | return QStringLiteral("'%1'").arg(var.toString().replace(QLatin1Char('\''), QLatin1String("\\\'"))); | ||||
54 | case Hidden: | ||||
55 | return (var.toBool() == true ? QStringLiteral("true") : QStringLiteral("false")); | ||||
56 | case MemberCount: | ||||
57 | case OrganizerCount: | ||||
58 | return var.toString(); | ||||
59 | case CreatedDate: | ||||
60 | return QStringLiteral("'%1'").arg(var.toDateTime().toUTC().toString(QStringLiteral("yyyy-MM-ddThh:mm:ss"))); | ||||
61 | } | ||||
62 | | ||||
63 | Q_ASSERT(false); | ||||
64 | return QString(); | ||||
65 | } | ||||
66 | | ||||
67 | void DrivesSearchQuery::addQuery(DrivesSearchQuery::Field field, DrivesSearchQuery::CompareOperator op, const QVariant &value) | ||||
68 | { | ||||
69 | switch (field) { | ||||
70 | case Name: | ||||
71 | Q_ASSERT(op == Contains || op == Equals || op == NotEquals); | ||||
72 | Q_ASSERT(value.canConvert<QString>()); | ||||
73 | break; | ||||
74 | case Hidden: | ||||
75 | Q_ASSERT(op == Equals || op == NotEquals); | ||||
76 | Q_ASSERT(value.canConvert<bool>()); | ||||
77 | break; | ||||
78 | case MemberCount: | ||||
79 | case OrganizerCount: | ||||
80 | Q_ASSERT(op == LessOrEqual || op == Less || op == Equals || op == NotEquals || op == Greater || op == GreaterOrEqual); | ||||
81 | Q_ASSERT(value.canConvert<int>()); | ||||
82 | break; | ||||
83 | case CreatedDate: | ||||
84 | Q_ASSERT(op == LessOrEqual || op == Less || op == Equals || op == NotEquals || op == Greater || op == GreaterOrEqual); | ||||
85 | Q_ASSERT(value.canConvert<QDateTime>()); | ||||
86 | break; | ||||
87 | } | ||||
88 | | ||||
89 | SearchQuery::addQuery(fieldToString(field), op, valueToString(field, value)); | ||||
90 | } |