Kgapi

drivessearchquery.cpp
1 /*
2  * Copyright (C) 2019 David Barchiesi <[email protected]>
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 #include "utils.h"
23 
24 #include <QString>
25 #include <QDateTime>
26 
27 using namespace KGAPI2;
28 using namespace KGAPI2::Drive;
29 
30 
31 QString DrivesSearchQuery::fieldToString(Field field)
32 {
33  switch (field) {
34  case Name:
35  return QStringLiteral("name");
36  case Hidden:
37  return QStringLiteral("hidden");
38  case CreatedDate:
39  return QStringLiteral("createdDate");
40  case MemberCount:
41  return QStringLiteral("memberCount");
42  case OrganizerCount:
43  return QStringLiteral("organizerCount");
44  }
45 
46  Q_ASSERT(false);
47  return QString();
48 }
49 
50 QString DrivesSearchQuery::valueToString(DrivesSearchQuery::Field field, const QVariant &var)
51 {
52  switch (field) {
53  case Name:
54  return QStringLiteral("'%1'").arg(var.toString().replace(QLatin1Char('\''), QLatin1String("\\\'")));
55  case Hidden:
56  return Utils::bool2Str(var.toBool());
57  case MemberCount:
58  case OrganizerCount:
59  return var.toString();
60  case CreatedDate:
61  return QStringLiteral("'%1'").arg(var.toDateTime().toUTC().toString(QStringLiteral("yyyy-MM-ddThh:mm:ss")));
62  }
63 
64  Q_ASSERT(false);
65  return QString();
66 }
67 
68 void DrivesSearchQuery::addQuery(DrivesSearchQuery::Field field, DrivesSearchQuery::CompareOperator op, const QVariant &value)
69 {
70  switch (field) {
71  case Name:
72  Q_ASSERT(op == Contains || op == Equals || op == NotEquals);
73  Q_ASSERT(value.canConvert<QString>());
74  break;
75  case Hidden:
76  Q_ASSERT(op == Equals || op == NotEquals);
77  Q_ASSERT(value.canConvert<bool>());
78  break;
79  case MemberCount:
80  case OrganizerCount:
81  Q_ASSERT(op == LessOrEqual || op == Less || op == Equals || op == NotEquals || op == Greater || op == GreaterOrEqual);
82  Q_ASSERT(value.canConvert<int>());
83  break;
84  case CreatedDate:
85  Q_ASSERT(op == LessOrEqual || op == Less || op == Equals || op == NotEquals || op == Greater || op == GreaterOrEqual);
86  Q_ASSERT(value.canConvert<QDateTime>());
87  break;
88  }
89 
90  SearchQuery::addQuery(fieldToString(field), op, valueToString(field, value));
91 }
bool canConvert(int targetTypeId) const const
QString toString(Qt::DateFormat format) const const
QDateTime toUTC() const const
QDateTime toDateTime() const const
A job to fetch a single map tile described by a StaticMapUrl.
Definition: blog.h:30
QString & replace(int position, int n, QChar after)
bool toBool() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun May 24 2020 22:39:01 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.