10#include "advancedqueryparser.h"
11#include "searchstore.h"
12#include "baloodebug.h"
18#include <QJsonDocument>
23const int defaultLimit = -1;
25class BALOO_CORE_NO_EXPORT
Baloo::Query::Private {
31 int m_limit = defaultLimit;
35 int m_monthFilter = 0;
38 SortingOption m_sortingOption = SortAuto;
47Query::Query(
const Query& rhs)
48 : d(new Private(*rhs.d))
52Query::~Query() =
default;
61 for (
const QString& type : typeList) {
66void Query::setType(
const QString& type)
82QString Query::searchString()
const
84 return d->m_searchString;
89 d->m_searchString = str;
94uint Query::limit()
const
104uint Query::offset()
const
109void Query::setOffset(uint offset)
111 d->m_offset = offset;
116 d->m_yearFilter = year;
117 d->m_monthFilter = month;
118 d->m_dayFilter = day;
121int Query::yearFilter()
const
123 return d->m_yearFilter;
126int Query::monthFilter()
const
128 return d->m_monthFilter;
131int Query::dayFilter()
const
133 return d->m_dayFilter;
138 d->m_sortingOption = option;
143 return d->m_sortingOption;
146QString Query::includeFolder()
const
148 return d->m_includeFolder;
153 d->m_includeFolder = folder;
156ResultIterator Query::exec()
158 if (!d->m_searchString.isEmpty()) {
159 if (d->m_term.isValid()) {
160 qCDebug(BALOO) <<
"Term already set";
162 AdvancedQueryParser parser;
163 d->m_term = parser.parse(d->m_searchString);
166 Term term(d->m_term);
167 if (!d->m_types.isEmpty()) {
168 for (
const QString& type : std::as_const(d->m_types)) {
169 term = term && Term(QStringLiteral(
"type"), type);
173 if (!d->m_includeFolder.isEmpty()) {
174 term = term && Term(QStringLiteral(
"includefolder"), d->m_includeFolder);
177 if (d->m_yearFilter || d->m_monthFilter || d->m_dayFilter) {
179 if (d->m_monthFilter < 10) {
183 if (d->m_dayFilter < 10) {
188 term = term && Term(QStringLiteral(
"modified"), ba, Term::Equal);
191 SearchStore searchStore;
192 auto results = searchStore.exec(term, d->m_offset, d->m_limit, d->m_sortingOption ==
SortAuto);
193 return ResultIterator(std::move(results));
200 if (!d->m_types.isEmpty()) {
201 map[QStringLiteral(
"type")] = d->m_types;
204 if (d->m_limit != defaultLimit) {
205 map[QStringLiteral(
"limit")] = d->m_limit;
209 map[QStringLiteral(
"offset")] = d->m_offset;
212 if (!d->m_searchString.isEmpty()) {
213 map[QStringLiteral(
"searchString")] = d->m_searchString;
216 if (d->m_term.isValid()) {
217 map[QStringLiteral(
"term")] =
QVariant(d->m_term.toVariantMap());
220 if (d->m_yearFilter > 0) {
221 map[QStringLiteral(
"yearFilter")] = d->m_yearFilter;
223 if (d->m_monthFilter > 0) {
224 map[QStringLiteral(
"monthFilter")] = d->m_monthFilter;
226 if (d->m_dayFilter > 0) {
227 map[QStringLiteral(
"dayFilter")] = d->m_dayFilter;
230 if (d->m_sortingOption !=
SortAuto) {
231 map[QStringLiteral(
"sortingOption")] =
static_cast<int>(d->m_sortingOption);
234 if (!d->m_includeFolder.isEmpty()) {
235 map[QStringLiteral(
"includeFolder")] = d->m_includeFolder;
241 return jdoc.
toJson(QJsonDocument::JsonFormat::Compact);
251 query.d->m_types =
map[QStringLiteral(
"type")].toStringList();
253 if (
map.contains(QStringLiteral(
"limit"))) {
254 query.d->m_limit =
map[QStringLiteral(
"limit")].toUInt();
256 query.d->m_limit = defaultLimit;
259 query.d->m_offset =
map[QStringLiteral(
"offset")].toUInt();
260 query.d->m_searchString =
map[QStringLiteral(
"searchString")].toString();
261 query.d->m_term = Term::fromVariantMap(map[QStringLiteral(
"term")].
toMap());
263 if (
map.contains(QStringLiteral(
"yearFilter"))) {
264 query.d->m_yearFilter =
map[QStringLiteral(
"yearFilter")].toInt();
266 if (
map.contains(QStringLiteral(
"monthFilter"))) {
267 query.d->m_monthFilter =
map[QStringLiteral(
"monthFilter")].toInt();
269 if (
map.contains(QStringLiteral(
"dayFilter"))) {
270 query.d->m_dayFilter =
map[QStringLiteral(
"dayFilter")].toInt();
273 if (
map.contains(QStringLiteral(
"sortingOption"))) {
274 int option =
map.value(QStringLiteral(
"sortingOption")).toInt();
275 query.d->m_sortingOption =
static_cast<SortingOption
>(option);
278 if (
map.contains(QStringLiteral(
"includeFolder"))) {
279 query.d->m_includeFolder =
map.value(QStringLiteral(
"includeFolder")).toString();
282 if (!
query.d->m_searchString.isEmpty() &&
query.d->m_term.isValid()) {
283 qCWarning(BALOO) <<
"Only one of 'searchString' and 'term' should be set:" << arr;
291 url.
setScheme(QStringLiteral(
"baloosearch"));
309 return QStringLiteral(
"{\"type\":[\"Document\"]}");
311 return QStringLiteral(
"{\"type\":[\"Image\"]}");
313 return QStringLiteral(
"{\"type\":[\"Audio\"]}");
315 return QStringLiteral(
"{\"type\":[\"Video\"]}");
321Query Query::fromSearchUrl(
const QUrl& url)
331 return Query::fromJSON(jsonString.
toUtf8());
341 const QString jsonString = jsonQueryFromUrl(url);
343 return Query::fromJSON(jsonString.
toUtf8());
355bool Query::operator==(
const Query& rhs)
const
357 if (rhs.d->m_limit != d->m_limit || rhs.d->m_offset != d->m_offset ||
358 rhs.d->m_dayFilter != d->m_dayFilter || rhs.d->m_monthFilter != d->m_monthFilter ||
359 rhs.d->m_yearFilter != d->m_yearFilter || rhs.d->m_includeFolder != d->m_includeFolder ||
360 rhs.d->m_searchString != d->m_searchString ||
361 rhs.d->m_sortingOption != d->m_sortingOption)
366 if (rhs.d->m_types.size() != d->m_types.size()) {
370 for (
const QString& type : std::as_const(rhs.d->m_types)) {
371 if (!d->m_types.contains(type)) {
376 return d->m_term == rhs.d->m_term;
379bool Query::operator!=(
const Query& rhs)
const
381 return !(*
this == rhs);
The Query class is the central class to query to search for files from the Index.
@ SortAuto
The results are returned in the order Baloo decides should be ideal.
void setLimit(uint limit)
Only a maximum of limit results will be returned.
void setIncludeFolder(const QString &folder)
Only files in this folder will be returned.
void setDateFilter(int year, int month=0, int day=0)
Filter the results in the specified date range.
void addType(const QString &type)
Add a type to the results of the query.
void setSearchString(const QString &str)
Set some text which should be used to search for Items.
std::optional< QSqlQuery > query(const QString &queryStatement)
Implements storage for docIds without any associated data Instantiated for:
const QVariantMap toMap(const MODEL &model)
QString path(const QString &relativePath)
QByteArray number(double n, char format, int precision)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
void setObject(const QJsonObject &object)
QByteArray toJson(JsonFormat format) const const
QJsonObject fromVariantMap(const QVariantMap &map)
QVariantMap toVariantMap() const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
QByteArray toUtf8() const const
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QString path(ComponentFormattingOptions options) const const
QString scheme() const const
void setQuery(const QString &query, ParsingMode mode)
void setScheme(const QString &scheme)
void addQueryItem(const QString &key, const QString &value)
bool hasQueryItem(const QString &key) const const
QString queryItemValue(const QString &key, QUrl::ComponentFormattingOptions encoding) const const