PlasmaActivitiesStats

resultset.h
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#ifndef KACTIVITIES_STATS_RESULTSET
8#define KACTIVITIES_STATS_RESULTSET
9
10#include "query.h"
11
12#include <QDebug>
13
14namespace KActivities
15{
16namespace Stats
17{
18class ResultSetPrivate;
19class ResultSet_ResultPrivate;
20class ResultSet_IteratorPrivate;
21
22/**
23 * @class KActivities::Stats::ResultSet resultset.h <KActivities/Stats/ResultSet>
24 *
25 * Class that can query the KActivities usage tracking mechanism
26 * for resources.
27 *
28 * Note: It is important to note that you should not create
29 * long-living instances of ResultSet. It might lock the database
30 * and break proper updating mechanisms. If you want a list of results
31 * that automatically get updated, use ResultModel.
32 *
33 * ResultSet is meant to be used when you just need to fetch a few results
34 * like this:
35 *
36 * @code
37 * auto results = ResultSet(AllResources | Agent("org.kde.kate"));
38 * for (const auto &result: results) {
39 * // ...
40 * }
41 * @endcode
42 */
43class PLASMAACTIVITIESSTATS_EXPORT ResultSet
44{
45public:
46 /**
47 * Structure containing data of one of the results
48 */
49 class Result
50 {
51 public:
52 Result();
53 ~Result();
54
55 Result(Result &&result);
56 Result(const Result &result);
57 Result &operator=(Result result);
58
59 enum LinkStatus {
60 NotLinked = 0,
61 Unknown = 1,
62 Linked = 2,
63 };
64
65 // TODO: KF6 rething the function names, and maybe their signature, perhaps leverage std::variant or std::optional to add semantics to the API
66 QString resource() const; ///< String representation of resource (can represent an url or a path)
67 QUrl url() const; ///< Url representation of a resource based on internal resource, readonly, @since 5.64
68 QString title() const; ///< Title of the resource, or URL if title is not known
69 QString mimetype() const; ///< Mimetype of the resource, or URL if title is not known
70 double score() const; ///< The score calculated based on the usage statistics
71 uint lastUpdate() const; ///< Timestamp of the last update
72 uint firstUpdate() const; ///< Timestamp of the first update
73 LinkStatus linkStatus() const; ///< Differentiates between linked and non-linked resources in mixed queries
74 QStringList linkedActivities() const; ///< Contains the activities this resource is linked to for the queries that care about resource linking
75 QString agent() const; /// Contains the initiating agent for this resource
76
78 void setTitle(QString title);
79 void setMimetype(QString mimetype);
80 void setScore(double score);
81 void setLastUpdate(uint lastUpdate);
82 void setFirstUpdate(uint firstUpdate);
83 void setLinkStatus(LinkStatus linkedStatus);
84 void setLinkedActivities(QStringList activities);
85 void setAgent(QString agent);
86
87 private:
88 ResultSet_ResultPrivate *d;
89 };
90
91 /**
92 * ResultSet is a container. This notifies the generic algorithms
93 * from STL and others of the contained type.
94 */
96
97 /**
98 * Creates the ResultSet from the specified query
99 */
100 ResultSet(Query query);
101
102 ResultSet(ResultSet &&source);
103 ResultSet(const ResultSet &source) = delete;
104 ResultSet &operator=(ResultSet source) = delete;
105 ~ResultSet();
106
107 /**
108 * @returns a result at the specified index
109 * @param index of the result
110 * @note You should use iterators instead
111 */
112 Result at(int index) const;
113
114 // Iterators
115
116 /**
117 * An STL-style constant forward iterator for accessing the results in a ResultSet
118 * TODO: Consider making this to be more than just forward iterator.
119 * Maybe even a random-access one.
120 */
121 class const_iterator
122 {
123 public:
124 typedef std::random_access_iterator_tag iterator_category;
125 typedef int difference_type;
126
127 typedef const Result value_type;
128 typedef const Result &reference;
129 typedef const Result *pointer;
130
131 const_iterator();
132 const_iterator(const const_iterator &source);
133 const_iterator &operator=(const const_iterator &source);
134
135 ~const_iterator();
136
137 bool isSourceValid() const;
138
139 reference operator*() const;
140 pointer operator->() const;
141
142 // prefix
143 const_iterator &operator++();
144 // postfix
145 const_iterator operator++(int);
146
147 // prefix
148 const_iterator &operator--();
149 // postfix
150 const_iterator operator--(int);
151
152 const_iterator operator+(difference_type n) const;
153 const_iterator &operator+=(difference_type n);
154
155 const_iterator operator-(difference_type n) const;
156 const_iterator &operator-=(difference_type n);
157
158 reference operator[](difference_type n) const;
159
160 PLASMAACTIVITIESSTATS_EXPORT friend bool operator==(const const_iterator &left, const const_iterator &right);
161 PLASMAACTIVITIESSTATS_EXPORT friend bool operator!=(const const_iterator &left, const const_iterator &right);
162
163 PLASMAACTIVITIESSTATS_EXPORT friend bool operator<(const const_iterator &left, const const_iterator &right);
164 PLASMAACTIVITIESSTATS_EXPORT friend bool operator>(const const_iterator &left, const const_iterator &right);
165
166 PLASMAACTIVITIESSTATS_EXPORT friend bool operator<=(const const_iterator &left, const const_iterator &right);
167 PLASMAACTIVITIESSTATS_EXPORT friend bool operator>=(const const_iterator &left, const const_iterator &right);
168
169 PLASMAACTIVITIESSTATS_EXPORT friend difference_type operator-(const const_iterator &left, const const_iterator &right);
170
171 private:
172 const_iterator(const ResultSet *resultSet, int currentRow);
173
174 friend class ResultSet;
175
176 ResultSet_IteratorPrivate *const d;
177 };
178
179 /**
180 * @returns a constant iterator pointing to the start of the collection
181 * (to the first item)
182 * @note as usual in C++, the range of the collection is [begin, end)
183 */
184 const_iterator begin() const;
185 /**
186 * @returns a constant iterator pointing to the end of the collection
187 * (after the last item)
188 * @note as usual in C++, the range of the collection is [begin, end)
189 */
190 const_iterator end() const;
191
192 /**
193 * Alias for begin
194 */
195 inline const_iterator cbegin() const
196 {
197 return begin();
198 }
199 /**
200 * Alias for end
201 */
202 inline const_iterator cend() const
203 {
204 return end();
205 }
206
207 /**
208 * Alias for begin
209 */
210 inline const_iterator constBegin() const
211 {
212 return cbegin();
213 }
214 /**
215 * Alias for end
216 */
217 inline const_iterator constEnd() const
218 {
219 return cend();
220 }
221
222private:
223 friend class ResultSet_IteratorPrivate;
224 ResultSetPrivate *d;
225};
226
227bool PLASMAACTIVITIESSTATS_EXPORT operator==(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
228bool PLASMAACTIVITIESSTATS_EXPORT operator!=(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
229
230bool PLASMAACTIVITIESSTATS_EXPORT operator<(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
231bool PLASMAACTIVITIESSTATS_EXPORT operator>(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
232
233bool PLASMAACTIVITIESSTATS_EXPORT operator<=(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
234bool PLASMAACTIVITIESSTATS_EXPORT operator>=(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
235
236ResultSet::const_iterator::difference_type PLASMAACTIVITIESSTATS_EXPORT operator-(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
237
238inline QDebug operator<<(QDebug out, const ResultSet::Result &result)
239{
240 return out << (result.linkStatus() == ResultSet::Result::Linked ? "⊤"
241 : result.linkStatus() == ResultSet::Result::NotLinked ? "⊥"
242 : "?")
243 << result.score() << (result.title() != result.resource() ? result.title() : QString()) << result.lastUpdate()
244 << QStringView(result.resource()).right(20);
245}
246
247} // namespace Stats
248} // namespace KActivities
249
250#endif // KACTIVITIES_STATS_RESULTSET
The activities system tracks resources (documents, contacts, etc.) that the user has used.
Definition query.h:54
Structure containing data of one of the results.
Definition resultset.h:50
uint lastUpdate() const
Timestamp of the last update.
double score() const
The score calculated based on the usage statistics.
void setResource(QString resource)
Contains the initiating agent for this resource.
QString mimetype() const
Mimetype of the resource, or URL if title is not known.
QUrl url() const
Url representation of a resource based on internal resource, readonly,.
QString title() const
Title of the resource, or URL if title is not known.
uint firstUpdate() const
Timestamp of the first update.
LinkStatus linkStatus() const
Differentiates between linked and non-linked resources in mixed queries.
QStringList linkedActivities() const
Contains the activities this resource is linked to for the queries that care about resource linking.
QString resource() const
String representation of resource (can represent an url or a path)
const_iterator cend() const
Alias for end.
Definition resultset.h:202
Result value_type
ResultSet is a container.
Definition resultset.h:95
Result at(int index) const
const_iterator cbegin() const
Alias for begin.
Definition resultset.h:195
const_iterator constBegin() const
Alias for begin.
Definition resultset.h:210
ResultSet(Query query)
Creates the ResultSet from the specified query.
const_iterator constEnd() const
Alias for end.
Definition resultset.h:217
QStringView right(qsizetype length) const const
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.