diff --git a/src/drive/CMakeLists.txt b/src/drive/CMakeLists.txt --- a/src/drive/CMakeLists.txt +++ b/src/drive/CMakeLists.txt @@ -10,6 +10,7 @@ childreferencedeletejob.cpp childreferencefetchjob.cpp driveservice.cpp + searchquery.cpp file.cpp fileabstractdatajob.cpp fileabstractmodifyjob.cpp @@ -54,6 +55,7 @@ ChildReferenceCreateJob ChildReferenceDeleteJob ChildReferenceFetchJob + SearchQuery File FileAbstractDataJob FileAbstractModifyJob diff --git a/src/drive/filesearchquery.h b/src/drive/filesearchquery.h --- a/src/drive/filesearchquery.h +++ b/src/drive/filesearchquery.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2014 Daniel Vrátil + * Copyright (C) 2019 David Barchiesi * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -23,6 +24,8 @@ #include "kgapidrive_export.h" +#include "searchquery.h" + #include #include @@ -40,7 +43,7 @@ * * @since 2.3 */ -class KGAPIDRIVE_EXPORT FileSearchQuery +class KGAPIDRIVE_EXPORT FileSearchQuery : public SearchQuery { public: enum Field { @@ -59,38 +62,14 @@ /*Properties FIXME: Not supported atm */ }; - enum CompareOperator { - Contains, - Equals, - NotEquals, - Less, - LessOrEqual, - Greater, - GreaterOrEqual, - In, - Has - }; - - enum LogicOperator { - And, - Or - }; - - explicit FileSearchQuery(LogicOperator op = And); - FileSearchQuery(const FileSearchQuery &other); - FileSearchQuery &operator=(const FileSearchQuery &other); - ~FileSearchQuery(); + using SearchQuery::SearchQuery; + using SearchQuery::addQuery; void addQuery(Field field, CompareOperator op, const QVariant &value); - void addQuery(const FileSearchQuery &query); - - bool isEmpty() const; - - QString serialize() const; - private: - class Private; - QSharedDataPointer d; +private: + QString fieldToString(Field field); + QString valueToString(FileSearchQuery::Field field, const QVariant &var); }; } } diff --git a/src/drive/filesearchquery.cpp b/src/drive/filesearchquery.cpp --- a/src/drive/filesearchquery.cpp +++ b/src/drive/filesearchquery.cpp @@ -1,5 +1,6 @@ /* * Copyright (C) 2014 Daniel Vrátil + * Copyright (C) 2019 David Barchiesi * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -26,46 +27,8 @@ using namespace KGAPI2; using namespace KGAPI2::Drive; -class Q_DECL_HIDDEN FileSearchQuery::Private : public QSharedData -{ -public: - Private(); - Private(const Private &other); - ~Private(); - - static QString fieldToString(Field field); - static QString compareOperatorToString(CompareOperator op); - static QString logicOperatorToString(LogicOperator op); - static QString valueToString(Field field, const QVariant &var); - - QList subqueries; - QVariant value; - Field field; - CompareOperator compareOp; - LogicOperator logicOp; -}; - -FileSearchQuery::Private::Private() - : QSharedData() -{ -} - -FileSearchQuery::Private::Private(const Private &other) - : QSharedData(other) - , subqueries(other.subqueries) - , value(other.value) - , field(other.field) - , compareOp(other.compareOp) - , logicOp(other.logicOp) -{ -} -FileSearchQuery::Private::~Private() -{ -} - - -QString FileSearchQuery::Private::fieldToString(Field field) +QString FileSearchQuery::fieldToString(Field field) { switch (field) { case Title: @@ -98,47 +61,7 @@ return QString(); } -QString FileSearchQuery::Private::compareOperatorToString(CompareOperator op) -{ - switch (op) { - case Contains: - return QStringLiteral(" contains "); - case Equals: - return QStringLiteral(" = "); - case NotEquals: - return QStringLiteral(" != "); - case Less: - return QStringLiteral(" < "); - case LessOrEqual: - return QStringLiteral(" <= "); - case Greater: - return QStringLiteral(" > "); - case GreaterOrEqual: - return QStringLiteral(" >= "); - case In: - return QStringLiteral(" in "); - case Has: - return QStringLiteral(" has "); - } - - Q_ASSERT(false); - return QString(); -} - -QString FileSearchQuery::Private::logicOperatorToString(FileSearchQuery::LogicOperator op) -{ - switch (op) { - case And: - return QStringLiteral(" and "); - case Or: - return QStringLiteral(" or "); - } - - Q_ASSERT(false); - return QString(); -} - -QString FileSearchQuery::Private::valueToString(FileSearchQuery::Field field, const QVariant &var) +QString FileSearchQuery::valueToString(FileSearchQuery::Field field, const QVariant &var) { switch (field) { case Title: @@ -162,28 +85,6 @@ return QString(); } -FileSearchQuery::FileSearchQuery(FileSearchQuery::LogicOperator op) - : d(new Private) -{ - d->logicOp = op; -} - -FileSearchQuery::FileSearchQuery(const FileSearchQuery &other) - : d(other.d) -{ -} - -FileSearchQuery::~FileSearchQuery() -{ -} - -FileSearchQuery &FileSearchQuery::operator=(const FileSearchQuery &other) -{ - d = other.d; - return *this; -} - - void FileSearchQuery::addQuery(FileSearchQuery::Field field, FileSearchQuery::CompareOperator op, const QVariant &value) { switch (field) { @@ -216,50 +117,5 @@ break; } - FileSearchQuery query; - query.d->field = field; - query.d->compareOp = op; - query.d->value = value; - d->subqueries.append(query); + SearchQuery::addQuery(fieldToString(field), op, valueToString(field, value)); } - -void FileSearchQuery::addQuery(const FileSearchQuery &query) -{ - d->subqueries.append(query); -} - -bool FileSearchQuery::isEmpty() const -{ - return d->value.isNull() && d->subqueries.isEmpty(); -} - -QString FileSearchQuery::serialize() const -{ - if (isEmpty()) { - return QString(); - } - - QString r; - r = QLatin1Char('('); - if (d->subqueries.isEmpty()) { - if (d->compareOp == In) { - r += QStringLiteral("%1 in %2").arg(Private::valueToString(d->field, d->value), - Private::fieldToString(d->field)); - } else { - r += Private::fieldToString(d->field) % Private::compareOperatorToString(d->compareOp) % Private::valueToString(d->field, d->value); - } - } else { - QList::ConstIterator iter, end; - for (iter = d->subqueries.constBegin(), end = d->subqueries.constEnd(); iter != end; ++iter) { - if (iter != d->subqueries.constBegin()) { - r += Private::logicOperatorToString(d->logicOp); - } - r += (*iter).serialize(); - } - } - r += QLatin1Char(')'); - - return r; -} - - diff --git a/src/drive/filesearchquery.h b/src/drive/searchquery.h copy from src/drive/filesearchquery.h copy to src/drive/searchquery.h --- a/src/drive/filesearchquery.h +++ b/src/drive/searchquery.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2014 Daniel Vrátil + * Copyright (C) 2019 David Barchiesi * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -18,8 +19,8 @@ * License along with this library. If not, see . */ -#ifndef KGAPI2_DRIVE_FILESEARCHQUERY_H -#define KGAPI2_DRIVE_FILESEARCHQUERY_H +#ifndef KGAPI2_DRIVE_SEARCHQUERY_H +#define KGAPI2_DRIVE_SEARCHQUERY_H #include "kgapidrive_export.h" @@ -32,33 +33,11 @@ { /** - * FileSearchQuery class allows simply building even complex file search queries - * for FileFetchJob. - * - * See https://developers.google.com/drive/web/search-parameters for allowed - * combinations of fields, compare operators, and value types. - * - * @since 2.3 + * SearchQuery class should be used as a base class for building file/team search queries. */ -class KGAPIDRIVE_EXPORT FileSearchQuery +class KGAPIDRIVE_EXPORT SearchQuery { public: - enum Field { - Title, - FullText, - MimeType, - ModifiedDate, - LastViewedByMeDate, - Trashed, - Starred, - Parents, - Owners, - Writers, - Readers, - SharedWithMe, - /*Properties FIXME: Not supported atm */ - }; - enum CompareOperator { Contains, Equals, @@ -76,13 +55,14 @@ Or }; - explicit FileSearchQuery(LogicOperator op = And); - FileSearchQuery(const FileSearchQuery &other); - FileSearchQuery &operator=(const FileSearchQuery &other); - ~FileSearchQuery(); + SearchQuery(LogicOperator op = And); + SearchQuery(const SearchQuery &other); + ~SearchQuery(); + + SearchQuery &operator=(const SearchQuery &other); - void addQuery(Field field, CompareOperator op, const QVariant &value); - void addQuery(const FileSearchQuery &query); + void addQuery(const QString &field, CompareOperator op, const QString &value); + void addQuery(const SearchQuery &query); bool isEmpty() const; @@ -95,4 +75,4 @@ } } -#endif // KGAPI2_DRIVE_FILESEARCHQUERY_H +#endif // KGAPI2_DRIVE_SEARCHQUERY_H diff --git a/src/drive/searchquery.cpp b/src/drive/searchquery.cpp new file mode 100644 --- /dev/null +++ b/src/drive/searchquery.cpp @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2014 Daniel Vrátil + * Copyright (C) 2019 David Barchiesi + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#include "searchquery.h" + +#include +#include + +using namespace KGAPI2; +using namespace KGAPI2::Drive; + +class Q_DECL_HIDDEN SearchQuery::Private : public QSharedData +{ +public: + Private() = default; + Private(const Private &other) = default; + ~Private() = default; + + static QString compareOperatorToString(CompareOperator op); + static QString logicOperatorToString(LogicOperator op); + + QList subqueries; + QString field; + QString value; + CompareOperator compareOp; + LogicOperator logicOp; +}; + +QString SearchQuery::Private::compareOperatorToString(CompareOperator op) +{ + switch (op) { + case Contains: + return QStringLiteral(" contains "); + case Equals: + return QStringLiteral(" = "); + case NotEquals: + return QStringLiteral(" != "); + case Less: + return QStringLiteral(" < "); + case LessOrEqual: + return QStringLiteral(" <= "); + case Greater: + return QStringLiteral(" > "); + case GreaterOrEqual: + return QStringLiteral(" >= "); + case In: + return QStringLiteral(" in "); + case Has: + return QStringLiteral(" has "); + } + + Q_ASSERT(false); + return QString(); +} + +QString SearchQuery::Private::logicOperatorToString(SearchQuery::LogicOperator op) +{ + switch (op) { + case And: + return QStringLiteral(" and "); + case Or: + return QStringLiteral(" or "); + } + + Q_ASSERT(false); + return QString(); +} + +SearchQuery::SearchQuery(SearchQuery::LogicOperator op) + : d(new Private) +{ + d->logicOp = op; +} + +SearchQuery::SearchQuery(const SearchQuery &other) = default; + +SearchQuery::~SearchQuery() = default; + +SearchQuery &SearchQuery::operator=(const SearchQuery &other) = default; + +void SearchQuery::addQuery(const QString &field, SearchQuery::CompareOperator op, const QString &value) +{ + SearchQuery query; + query.d->field = field; + query.d->compareOp = op; + query.d->value = value; + d->subqueries.append(query); +} + +void SearchQuery::addQuery(const SearchQuery &query) +{ + d->subqueries.append(query); +} + +bool SearchQuery::isEmpty() const +{ + return d->value.isNull() && d->subqueries.isEmpty(); +} + +QString SearchQuery::serialize() const +{ + if (isEmpty()) { + return QString(); + } + + QString r; + r = QLatin1Char('('); + if (d->subqueries.isEmpty()) { + if (d->compareOp == In) { + r += QStringLiteral("%1 in %2").arg(d->value, d->field); + } else { + r += d->field % Private::compareOperatorToString(d->compareOp) % d->value; + } + } else { + QList::ConstIterator iter, end; + for (iter = d->subqueries.constBegin(), end = d->subqueries.constEnd(); iter != end; ++iter) { + if (iter != d->subqueries.constBegin()) { + r += Private::logicOperatorToString(d->logicOp); + } + r += (*iter).serialize(); + } + } + r += QLatin1Char(')'); + + return r; +} + +