File indexing completed on 2024-05-19 16:38:21

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         QString agent() const; /// Contains the initiating agent for this resource
0076 
0077         void setResource(QString resource);
0078         void setTitle(QString title);
0079         void setMimetype(QString mimetype);
0080         void setScore(double score);
0081         void setLastUpdate(uint lastUpdate);
0082         void setFirstUpdate(uint firstUpdate);
0083         void setLinkStatus(LinkStatus linkedStatus);
0084         void setLinkedActivities(QStringList activities);
0085         void setAgent(QString agent);
0086 
0087     private:
0088         ResultSet_ResultPrivate *d;
0089     };
0090 
0091     /**
0092      * ResultSet is a container. This notifies the generic algorithms
0093      * from STL and others of the contained type.
0094      */
0095     typedef Result value_type;
0096 
0097     /**
0098      * Creates the ResultSet from the specified query
0099      */
0100     ResultSet(Query query);
0101 
0102     ResultSet(ResultSet &&source);
0103     ResultSet(const ResultSet &source);
0104     ResultSet &operator=(ResultSet source);
0105     ~ResultSet();
0106 
0107     /**
0108      * @returns a result at the specified index
0109      * @param index of the result
0110      * @note You should use iterators instead
0111      */
0112     Result at(int index) const;
0113 
0114     // Iterators
0115 
0116     /**
0117      * An STL-style constant forward iterator for accessing the results in a ResultSet
0118      * TODO: Consider making this to be more than just forward iterator.
0119      *       Maybe even a random-access one.
0120      */
0121     class const_iterator
0122     {
0123     public:
0124         typedef std::random_access_iterator_tag iterator_category;
0125         typedef int difference_type;
0126 
0127         typedef const Result value_type;
0128         typedef const Result &reference;
0129         typedef const Result *pointer;
0130 
0131         const_iterator();
0132         const_iterator(const const_iterator &source);
0133         const_iterator &operator=(const const_iterator &source);
0134 
0135         ~const_iterator();
0136 
0137         bool isSourceValid() const;
0138 
0139         reference operator*() const;
0140         pointer operator->() const;
0141 
0142         // prefix
0143         const_iterator &operator++();
0144         // postfix
0145         const_iterator operator++(int);
0146 
0147         // prefix
0148         const_iterator &operator--();
0149         // postfix
0150         const_iterator operator--(int);
0151 
0152         const_iterator operator+(difference_type n) const;
0153         const_iterator &operator+=(difference_type n);
0154 
0155         const_iterator operator-(difference_type n) const;
0156         const_iterator &operator-=(difference_type n);
0157 
0158         reference operator[](difference_type n) const;
0159 
0160         KACTIVITIESSTATS_EXPORT friend bool operator==(const const_iterator &left, const const_iterator &right);
0161         KACTIVITIESSTATS_EXPORT friend bool operator!=(const const_iterator &left, const const_iterator &right);
0162 
0163         KACTIVITIESSTATS_EXPORT friend bool operator<(const const_iterator &left, const const_iterator &right);
0164         KACTIVITIESSTATS_EXPORT friend bool operator>(const const_iterator &left, const const_iterator &right);
0165 
0166         KACTIVITIESSTATS_EXPORT friend bool operator<=(const const_iterator &left, const const_iterator &right);
0167         KACTIVITIESSTATS_EXPORT friend bool operator>=(const const_iterator &left, const const_iterator &right);
0168 
0169         KACTIVITIESSTATS_EXPORT friend difference_type operator-(const const_iterator &left, const const_iterator &right);
0170 
0171     private:
0172         const_iterator(const ResultSet *resultSet, int currentRow);
0173 
0174         friend class ResultSet;
0175 
0176         ResultSet_IteratorPrivate *const d;
0177     };
0178 
0179     /**
0180      * @returns a constant iterator pointing to the start of the collection
0181      * (to the first item)
0182      * @note as usual in C++, the range of the collection is [begin, end)
0183      */
0184     const_iterator begin() const;
0185     /**
0186      * @returns a constant iterator pointing to the end of the collection
0187      * (after the last item)
0188      * @note as usual in C++, the range of the collection is [begin, end)
0189      */
0190     const_iterator end() const;
0191 
0192     /**
0193      * Alias for begin
0194      */
0195     inline const_iterator cbegin() const
0196     {
0197         return begin();
0198     }
0199     /**
0200      * Alias for end
0201      */
0202     inline const_iterator cend() const
0203     {
0204         return end();
0205     }
0206 
0207     /**
0208      * Alias for begin
0209      */
0210     inline const_iterator constBegin() const
0211     {
0212         return cbegin();
0213     }
0214     /**
0215      * Alias for end
0216      */
0217     inline const_iterator constEnd() const
0218     {
0219         return cend();
0220     }
0221 
0222 private:
0223     friend class ResultSet_IteratorPrivate;
0224     ResultSetPrivate *d;
0225 };
0226 
0227 bool KACTIVITIESSTATS_EXPORT operator==(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
0228 bool KACTIVITIESSTATS_EXPORT operator!=(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
0229 
0230 bool KACTIVITIESSTATS_EXPORT operator<(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
0231 bool KACTIVITIESSTATS_EXPORT operator>(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
0232 
0233 bool KACTIVITIESSTATS_EXPORT operator<=(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
0234 bool KACTIVITIESSTATS_EXPORT operator>=(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
0235 
0236 ResultSet::const_iterator::difference_type KACTIVITIESSTATS_EXPORT operator-(const ResultSet::const_iterator &left, const ResultSet::const_iterator &right);
0237 
0238 inline QDebug operator<<(QDebug out, const ResultSet::Result &result)
0239 {
0240     return out << (result.linkStatus() == ResultSet::Result::Linked          ? "⊤"
0241                        : result.linkStatus() == ResultSet::Result::NotLinked ? "⊥"
0242                                                                              : "?")
0243                << result.score() << (result.title() != result.resource() ? result.title() : QString()) << result.lastUpdate()
0244                << QStringView(result.resource()).right(20);
0245 }
0246 
0247 } // namespace Stats
0248 } // namespace KActivities
0249 
0250 #endif // KACTIVITIES_STATS_RESULTSET