PlasmaActivitiesStats

query.cpp
1/*
2 SPDX-FileCopyrightText: 2015, 2016 Ivan Cukic <ivan.cukic(at)kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "query.h"
8#include "common/specialvalues.h"
9#include <QDate>
10#include <QDebug>
11
12constexpr int s_defaultCacheSize = 50;
13
14namespace KActivities
15{
16namespace Stats
17{
18namespace details
19{
20inline void validateTypes(QStringList &types)
21{
22 // Nothing at the moment
23 Q_UNUSED(types);
24}
25
26inline void validateAgents(QStringList &agents)
27{
28 // Nothing at the moment
29 Q_UNUSED(agents);
30}
31
32inline void validateActivities(QStringList &activities)
33{
34 // Nothing at the moment
35 Q_UNUSED(activities);
36}
37
38inline void validateFilters(QStringList &filters)
39{
40 for (auto it = filters.begin(), end = filters.end(); it != end; ++it) {
41 it->replace(QLatin1String("'"), QLatin1String(""));
42 }
43}
44inline void validateUrlFilters(QStringList &urlFilters)
45{
46 validateFilters(urlFilters);
47}
48
49inline void validateTitleFilters(QStringList &titleFilters)
50{
51 validateFilters(titleFilters);
52}
53
54} // namespace details
55
56class QueryPrivate
57{
58public:
59 QueryPrivate()
60 : ordering(Terms::HighScoredFirst)
61 , limit(s_defaultCacheSize)
62 , offset(0)
63 {
64 }
65
66 Terms::Select selection;
67 QStringList types;
68 QStringList agents;
69 QStringList activities;
70 QStringList urlFilters;
71 QStringList titleFilters;
72 Terms::Order ordering;
73 QDate start, end;
74 int limit;
75 int offset;
76};
77
78Query::Query(Terms::Select selection)
79 : d(new QueryPrivate())
80{
81 d->selection = selection;
82}
83
84Query::Query(Query &&source)
85 : d(nullptr)
86{
87 std::swap(d, source.d);
88}
89
90Query::Query(const Query &source)
91 : d(new QueryPrivate(*source.d))
92{
93}
94
95Query &Query::operator=(Query source)
96{
97 std::swap(d, source.d);
98 return *this;
99}
100
101Query::~Query()
102{
103 delete d;
104}
105
106bool Query::operator==(const Query &right) const
107{
108 return selection() == right.selection() //
109 && types() == right.types() //
110 && agents() == right.agents() //
111 && activities() == right.activities() //
112 && selection() == right.selection() //
113 && urlFilters() == right.urlFilters() //
114 && dateStart() == right.dateStart() //
115 && dateEnd() == right.dateEnd();
116}
117
118bool Query::operator!=(const Query &right) const
119{
120 return !(*this == right);
121}
122
123#define IMPLEMENT_QUERY_LIST_FIELD(WHAT, What, Term, Default) \
124 void Query::add##WHAT(const QStringList &What) \
125 { \
126 d->What << What; \
127 details::validate##WHAT(d->What); \
128 } \
129 \
130 void Query::set##WHAT(const Terms::Term &What) \
131 { \
132 d->What = What.values; \
133 details::validate##WHAT(d->What); \
134 } \
135 \
136 QStringList Query::What() const \
137 { \
138 return d->What.size() ? d->What : Default; \
139 } \
140 \
141 void Query::clear##WHAT() \
142 { \
143 d->What.clear(); \
144 }
145
146IMPLEMENT_QUERY_LIST_FIELD(Types, types, Type, QStringList(ANY_TYPE_TAG))
147IMPLEMENT_QUERY_LIST_FIELD(Agents, agents, Agent, QStringList(CURRENT_AGENT_TAG))
148IMPLEMENT_QUERY_LIST_FIELD(Activities, activities, Activity, QStringList(CURRENT_ACTIVITY_TAG))
149IMPLEMENT_QUERY_LIST_FIELD(UrlFilters, urlFilters, Url, QStringList(QStringLiteral("*")))
150IMPLEMENT_QUERY_LIST_FIELD(TitleFilters, titleFilters, Title, QStringList(QStringLiteral("*")))
151
152#undef IMPLEMENT_QUERY_LIST_FIELD
153
154void Query::setOrdering(Terms::Order ordering)
155{
156 d->ordering = ordering;
157}
158
159void Query::setSelection(Terms::Select selection)
160{
161 d->selection = selection;
162}
163
164void Query::setLimit(int limit)
165{
166 d->limit = limit;
167}
168
169void Query::setOffset(int offset)
170{
171 d->offset = offset;
172}
173
175{
176 d->start = date.start;
177 d->end = date.end;
178}
179
180void Query::setDateStart(QDate start)
181{
182 d->start = start;
183}
184
185void Query::setDateEnd(QDate end)
186{
187 d->end = end;
188}
189
190Terms::Order Query::ordering() const
191{
192 return d->ordering;
193}
194
195Terms::Select Query::selection() const
196{
197 return d->selection;
198}
199
200int Query::limit() const
201{
202 return d->limit;
203}
204
205int Query::offset() const
206{
207 Q_ASSERT_X(d->limit > 0, "Query::offset", "Offset can only be specified if limit is set");
208 return d->offset;
209}
210
211QDate Query::dateStart() const
212{
213 return d->start;
214}
215
216QDate Query::dateEnd() const
217{
218 return d->end;
219}
220} // namespace Stats
221} // namespace KActivities
222
223namespace KAStats = KActivities::Stats;
224
225QDebug operator<<(QDebug dbg, const KAStats::Query &query)
226{
227 using namespace KAStats::Terms;
228
229 // clang-format off
230 dbg.nospace()
231 << "Query { "
232 << query.selection()
233 << ", " << Type(query.types())
234 << ", " << Agent(query.agents())
235 << ", " << Activity(query.activities())
236 << ", " << Url(query.urlFilters())
237 << ", " << Date(query.dateStart(), query.dateEnd())
238 << ", " << query.ordering()
239 << ", Limit: " << query.limit()
240 << " }";
241 // clang-format on
242
243 return dbg;
244}
The activities system tracks resources (documents, contacts, etc.) that the user has used.
Definition query.h:54
void setDate(const Terms::Date &date)
Definition query.cpp:174
Q_SCRIPTABLE QString start(QString train="")
Provides enums and strucss to use.for building queries with Query.
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
QDebug & nospace()
iterator begin()
iterator end()
QTextStream & right(QTextStream &stream)
On which start access date do you want to filter ?
Definition terms.h:226
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 12:01:02 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.