Warning, file /frameworks/kactivities-stats/src/resultset.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2015, 2016 Ivan Cukic <ivan.cukic(at)kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef KACTIVITIES_STATS_RESULTSET
0008 #define KACTIVITIES_STATS_RESULTSET
0009 
0010 #include "query.h"
0011 
0012 #include <QDebug>
0013 
0014 namespace KActivities
0015 {
0016 namespace Stats
0017 {
0018 class ResultSetPrivate;
0019 class ResultSet_ResultPrivate;
0020 class ResultSet_IteratorPrivate;
0021 
0022 /**
0023  * @class KActivities::Stats::ResultSet resultset.h <KActivities/Stats/ResultSet>
0024  *
0025  * Class that can query the KActivities usage tracking mechanism
0026  * for resources.
0027  *
0028  * Note: It is important to note that you should not create
0029  * long-living instances of ResultSet. It might lock the database
0030  * and break proper updating mechanisms. If you want a list of results
0031  * that automatically get updated, use ResultModel.
0032  *
0033  * ResultSet is meant to be used when you just need to fetch a few results
0034  * like this:
0035  *
0036  * @code
0037  * auto results = ResultSet(AllResources | Agent("org.kde.kate"));
0038  * for (const auto &result: results) {
0039  *     // ...
0040  * }
0041  * @endcode
0042  */
0043 class KACTIVITIESSTATS_EXPORT ResultSet
0044 {
0045 public:
0046     /**
0047      * Structure containing data of one of the results
0048      */
0049     class Result
0050     {
0051     public:
0052         Result();
0053         ~Result();
0054 
0055         Result(Result &&result);
0056         Result(const Result &result);
0057         Result &operator=(Result result);
0058 
0059         enum LinkStatus {
0060             NotLinked = 0,
0061             Unknown = 1,
0062             Linked = 2,
0063         };
0064 
0065         // TODO: KF6 rething the function names, and maybe their signature, perhaps leverage std::variant or std::optional to add semantics to the API
0066         QString resource() const; ///< String representation of resource (can represent an url or a path)
0067         QUrl url() const; ///< Url representation of a resource based on internal resource, readonly, @since 5.64
0068         QString title() const; ///< Title of the resource, or URL if title is not known
0069         QString mimetype() const; ///< Mimetype of the resource, or URL if title is not known
0070         double score() const; ///< The score calculated based on the usage statistics
0071         uint lastUpdate() const; ///< Timestamp of the last update
0072         uint firstUpdate() const; ///< Timestamp of the first update
0073         LinkStatus linkStatus() const; ///< Differentiates between linked and non-linked resources in mixed queries
0074         QStringList linkedActivities() const; ///< Contains the activities this resource is linked to for the queries that care about resource linking
0075 
0076         void setResource(QString resource);
0077         void setTitle(QString title);
0078         void setMimetype(QString mimetype);
0079         void setScore(double score);
0080         void setLastUpdate(uint lastUpdate);
0081         void setFirstUpdate(uint firstUpdate);
0082         void setLinkStatus(LinkStatus linkedStatus);
0083         void setLinkedActivities(QStringList activities);
0084 
0085     private:
0086         ResultSet_ResultPrivate *d;
0087     };
0088 
0089     /**
0090      * ResultSet is a container. This notifies the generic algorithms
0091      * from STL and others of the contained type.
0092      */
0093     typedef Result value_type;
0094 
0095     /**
0096      * Creates the ResultSet from the specified query
0097      */
0098     ResultSet(Query query);
0099 
0100     ResultSet(ResultSet &&source);
0101     ResultSet(const ResultSet &source);
0102     ResultSet &operator=(ResultSet source);
0103     ~ResultSet();
0104 
0105     /**
0106      * @returns a result at the specified index
0107      * @param index of the result
0108      * @note You should use iterators instead
0109      */
0110     Result at(int index) const;
0111 
0112     // Iterators
0113 
0114     /**
0115      * An STL-style constant forward iterator for accessing the results in a ResultSet
0116      * TODO: Consider making this to be more than just forward iterator.
0117      *       Maybe even a random-access one.
0118      */
0119     class const_iterator
0120     {
0121     public:
0122         typedef std::random_access_iterator_tag iterator_category;
0123         typedef int difference_type;
0124 
0125         typedef const Result value_type;
0126         typedef const Result &reference;
0127         typedef const Result *pointer;
0128 
0129         const_iterator();
0130         const_iterator(const const_iterator &source);
0131         const_iterator &operator=(const const_iterator &source);
0132 
0133         ~const_iterator();
0134 
0135         bool isSourceValid() const;
0136 
0137         reference operator*() const;
0138         pointer operator->() const;
0139 
0140         // prefix
0141         const_iterator &operator++();
0142         // postfix
0143         const_iterator operator++(int);
0144 
0145         // prefix
0146         const_iterator &operator--();
0147         // postfix
0148         const_iterator operator--(int);
0149 
0150         const_iterator operator+(difference_type n) const;
0151         const_iterator &operator+=(difference_type n);
0152 
0153         const_iterator operator-(difference_type n) const;
0154         const_iterator &operator-=(difference_type n);
0155 
0156         reference operator[](difference_type n) const;
0157 
0158         KACTIVITIESSTATS_EXPORT friend bool operator==(const const_iterator &left, const const_iterator &right);
0159         KACTIVITIESSTATS_EXPORT friend bool operator!=(const const_iterator &left, const const_iterator &right);
0160 
0161         KACTIVITIESSTATS_EXPORT friend bool operator<(const const_iterator &left, const const_iterator &right);
0162         KACTIVITIESSTATS_EXPORT friend bool operator>(const const_iterator &left, const const_iterator &right);
0163 
0164         KACTIVITIESSTATS_EXPORT friend bool operator<=(const const_iterator &left, const const_iterator &right);
0165         KACTIVITIESSTATS_EXPORT friend bool operator>=(const const_iterator &left, const const_iterator &right);
0166 
0167         KACTIVITIESSTATS_EXPORT friend difference_type operator-(const const_iterator &left, const const_iterator &right);
0168 
0169     private:
0170         const_iterator(const ResultSet *resultSet, int currentRow);
0171 
0172         friend class ResultSet;
0173 
0174         ResultSet_IteratorPrivate *const d;
0175     };
0176 
0177     /**
0178      * @returns a constant iterator pointing to the start of the collection
0179      * (to the first item)
0180      * @note as usual in C++, the range of the collection is [begin, end)
0181      */
0182     const_iterator begin() const;
0183     /**
0184      * @returns a constant iterator pointing to the end of the collection
0185      * (after the last item)
0186      * @note as usual in C++, the range of the collection is [begin, end)
0187      */
0188     const_iterator end() const;
0189 
0190     /**
0191      * Alias for begin
0192      */
0193     inline const_iterator cbegin() const
0194     {
0195         return begin();
0196     }
0197     /**
0198      * Alias for end
0199      */
0200     inline const_iterator cend() const
0201     {
0202         return end();
0203     }
0204 
0205     /**
0206      * Alias for begin
0207      */
0208     inline const_iterator constBegin() const
0209     {
0210         return cbegin();
0211     }
0212     /**
0213      * Alias for end
0214      */
0215     inline const_iterator constEnd() const
0216     {
0217         return cend();
0218     }
0219 
0220 private:
0221     friend class ResultSet_IteratorPrivate;
0222     ResultSetPrivate *d;
0223 };
0224 
0225 bool KACTIVITIESSTATS_EXPORT operator==(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
0226 bool KACTIVITIESSTATS_EXPORT operator!=(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
0227 
0228 bool KACTIVITIESSTATS_EXPORT operator<(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
0229 bool KACTIVITIESSTATS_EXPORT operator>(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
0230 
0231 bool KACTIVITIESSTATS_EXPORT operator<=(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
0232 bool KACTIVITIESSTATS_EXPORT operator>=(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
0233 
0234 ResultSet::const_iterator::difference_type KACTIVITIESSTATS_EXPORT operator-(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
0235 
0236 inline QDebug operator<<(QDebug out, const ResultSet::Result &result)
0237 {
0238     return out << (result.linkStatus() == ResultSet::Result::Linked          ? "⊤"
0239                        : result.linkStatus() == ResultSet::Result::NotLinked ? "⊥"
0240                                                                              : "?")
0241                << result.score() << (result.title() != result.resource() ? result.title() : QString()) << result.lastUpdate()
0242                << QStringView(result.resource()).right(20);
0243 }
0244 
0245 } // namespace Stats
0246 } // namespace KActivities
0247 
0248 #endif // KACTIVITIES_STATS_RESULTSET