12#include "searchstore.h"
15#include <QSharedPointer>
21#include <QJsonDocument>
26const int defaultLimit = 100000;
28class Akonadi::Search::QueryPrivate
35 uint m_limit = defaultLimit;
38 int m_yearFilter = -1;
39 int m_monthFilter = -1;
44 QVariantMap m_customOptions;
52Query::Query(
const Term &t)
58Query::Query(
const Query &rhs)
59 : d(new QueryPrivate(*rhs.d))
63Query::~Query() =
default;
65void Query::setTerm(
const Term &t)
70Term Query::term()
const
82 for (
const QString &type : typeList) {
87void Query::setType(
const QString &type)
103QString Query::searchString()
const
105 return d->m_searchString;
110 d->m_searchString = str;
113uint Query::limit()
const
123uint Query::offset()
const
128void Query::setOffset(uint offset)
130 d->m_offset = offset;
135 d->m_yearFilter = year;
136 d->m_monthFilter = month;
137 d->m_dayFilter = day;
140int Query::yearFilter()
const
142 return d->m_yearFilter;
145int Query::monthFilter()
const
147 return d->m_monthFilter;
150int Query::dayFilter()
const
152 return d->m_dayFilter;
157 d->m_sortingOption = option;
162 return d->m_sortingOption;
168 d->m_sortingProperty = property;
171QString Query::sortingProperty()
const
173 return d->m_sortingProperty;
178 d->m_customOptions.insert(option, value);
183 return d->m_customOptions.value(option);
186QVariantMap Query::customOptions()
const
188 return d->m_customOptions;
191void Query::removeCustomOption(
const QString &option)
193 d->m_customOptions.remove(option);
201 Q_ASSERT_X(!types().isEmpty(),
"Akonadi::Search::Query::exec",
"A query is being initialized without a type");
202 if (types().isEmpty()) {
209 const auto typeList{types()};
210 for (
const QString &type : typeList) {
211 if (!store->types().contains(type)) {
218 storeMatch = store.data();
227 int id = storeMatch->
exec(*
this);
228 return {id, storeMatch};
235 if (!d->m_types.isEmpty()) {
236 map[QStringLiteral(
"type")] = d->m_types;
239 if (d->m_limit != defaultLimit) {
240 map[QStringLiteral(
"limit")] = d->m_limit;
244 map[QStringLiteral(
"offset")] = d->m_offset;
247 if (!d->m_searchString.isEmpty()) {
248 map[QStringLiteral(
"searchString")] = d->m_searchString;
251 if (d->m_term.isValid()) {
252 map[QStringLiteral(
"term")] =
QVariant(d->m_term.toVariantMap());
255 if (d->m_yearFilter >= 0) {
256 map[QStringLiteral(
"yearFilter")] = d->m_yearFilter;
258 if (d->m_monthFilter >= 0) {
259 map[QStringLiteral(
"monthFilter")] = d->m_monthFilter;
261 if (d->m_dayFilter >= 0) {
262 map[QStringLiteral(
"dayFilter")] = d->m_dayFilter;
265 if (d->m_sortingOption !=
SortAuto) {
266 map[QStringLiteral(
"sortingOption")] =
static_cast<int>(d->m_sortingOption);
268 if (!d->m_sortingProperty.isEmpty()) {
269 map[QStringLiteral(
"sortingProperty")] = d->m_sortingProperty;
272 if (!d->m_customOptions.isEmpty()) {
273 map[QStringLiteral(
"customOptions")] = d->m_customOptions;
289 query.d->m_types =
map[QStringLiteral(
"type")].toStringList();
291 if (
map.contains(QStringLiteral(
"limit"))) {
292 query.d->m_limit =
map[QStringLiteral(
"limit")].toUInt();
294 query.d->m_limit = defaultLimit;
297 query.d->m_offset =
map[QStringLiteral(
"offset")].toUInt();
298 query.d->m_searchString =
map[QStringLiteral(
"searchString")].toString();
299 query.d->m_term = Term::fromVariantMap(map[QStringLiteral(
"term")].
toMap());
301 if (
map.contains(QStringLiteral(
"yearFilter"))) {
302 query.d->m_yearFilter =
map[QStringLiteral(
"yearFilter")].toInt();
304 if (
map.contains(QStringLiteral(
"monthFilter"))) {
305 query.d->m_monthFilter =
map[QStringLiteral(
"monthFilter")].toInt();
307 if (
map.contains(QStringLiteral(
"dayFilter"))) {
308 query.d->m_dayFilter =
map[QStringLiteral(
"dayFilter")].toInt();
311 if (
map.contains(QStringLiteral(
"sortingOption"))) {
312 int option =
map.value(QStringLiteral(
"sortingOption")).toInt();
313 query.d->m_sortingOption =
static_cast<SortingOption
>(option);
316 if (
map.contains(QStringLiteral(
"sortingProperty"))) {
317 query.d->m_sortingProperty =
map.value(QStringLiteral(
"sortingProperty")).toString();
320 if (
map.contains(QStringLiteral(
"customOptions"))) {
321 QVariant var =
map[QStringLiteral(
"customOptions")];
323 query.d->m_customOptions =
map[QStringLiteral(
"customOptions")].toMap();
325 QVariantHash hash = var.
toHash();
329 for (; it !=
end; ++it) {
330 query.d->m_customOptions.
insert(it.key(), it.value());
341 url.
setScheme(QStringLiteral(
"akonadisearch"));
354Query Query::fromSearchUrl(
const QUrl &url)
356 if (url.
scheme() !=
"akonadisearch"_L1) {
362 return Query::fromJSON(jsonString.
toUtf8());
371bool Query::operator==(
const Query &rhs)
const
373 if (rhs.d->m_limit != d->m_limit || rhs.d->m_offset != d->m_offset || rhs.d->m_dayFilter != d->m_dayFilter || rhs.d->m_monthFilter != d->m_monthFilter
374 || rhs.d->m_yearFilter != d->m_yearFilter || rhs.d->m_customOptions != d->m_customOptions || rhs.d->m_searchString != d->m_searchString
375 || rhs.d->m_sortingProperty != d->m_sortingProperty || rhs.d->m_sortingOption != d->m_sortingOption) {
379 if (rhs.d->m_types.size() != d->m_types.size()) {
383 for (
const QString &type : std::as_const(rhs.d->m_types)) {
384 if (!d->m_types.contains(type)) {
389 return d->m_term == rhs.d->m_term;
void setLimit(uint limit)
Only a maximum of limit results will be returned.
void addCustomOption(const QString &option, const QVariant &value)
Adds a custom option which any search backend could use to configure the query result.
void setDateFilter(int year, int month=-1, int day=-1)
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.
void setSortingProperty(const QString &property)
Sets the property that should be used for sorting.
@ SortProperty
The results are returned based on the explicit property specified.
@ SortAuto
The results are returned in the order the SearchStore decides should be ideal.
virtual int exec(const Query &query)=0
Executes the particular query synchronously.
static List searchStores()
Gives a list of available search stores.
Akonadi search infrastructure.
const QVariantMap toMap(const MODEL &model)
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
QAction * end(const QObject *recvr, const char *slot, QObject *parent)
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
iterator insert(const_iterator before, parameter_type value)
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
QByteArray toUtf8() const const
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QString scheme() const const
void setQuery(const QString &query, ParsingMode mode)
void setScheme(const QString &scheme)
void addQueryItem(const QString &key, const QString &value)
QString queryItemValue(const QString &key, QUrl::ComponentFormattingOptions encoding) const const
QHash< QString, QVariant > toHash() const const
int userType() const const