File indexing completed on 2024-12-22 05:13:39

0001 /*
0002     SPDX-FileCopyrightText: 2010-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 ACTIVITIES_INFO_H
0008 #define ACTIVITIES_INFO_H
0009 
0010 #include <QFuture>
0011 #include <QObject>
0012 #include <QString>
0013 
0014 #include "plasma_activities_export.h"
0015 
0016 #include <memory>
0017 
0018 namespace KActivities
0019 {
0020 class InfoPrivate;
0021 
0022 /**
0023  * This class provides info about an activity. Most methods in it require a
0024  * semantic backend running to function properly.
0025  *
0026  * This class is not thread-safe.
0027  *
0028  * @see Consumer for info about activities
0029  *
0030  * The API of the class is synchronous, but the most used properties
0031  * are pre-fetched and cached. This means that, in order to get the least
0032  * amount of d-bus related locks, you should declare long-lived instances
0033  * of this class.
0034  *
0035  * Before relying on the values retrieved by the class, make sure that the
0036  * state is not Info::Unknown. You can get invalid data either because the
0037  * service is not functioning properly (or at all) or because the class did
0038  * not have enough time to synchronize the data with it.
0039  *
0040  * For example, if this is the only existing instance of the Info class, the
0041  * name method will return an empty string.
0042  *
0043  * For example, this is wrong (works, but blocks):
0044  * @code
0045  * void someMethod(const QString & activity) {
0046  *     // Do not copy. This approach is not a good one!
0047  *     Info info(activity);
0048  *     doSomethingWith(info.name());
0049  * }
0050  * @endcode
0051  *
0052  * Instances of the Info class should be long-lived. For example, members
0053  * of the classes that use them, and you should listen for the changes in the
0054  * provided properties.
0055  *
0056  * @since 4.5
0057  */
0058 class PLASMA_ACTIVITIES_EXPORT Info : public QObject
0059 {
0060     Q_OBJECT
0061 
0062     Q_PROPERTY(QString id READ id)
0063     Q_PROPERTY(QString name READ name NOTIFY nameChanged)
0064     Q_PROPERTY(QString description READ description NOTIFY descriptionChanged)
0065     Q_PROPERTY(QString icon READ icon NOTIFY iconChanged)
0066     Q_PROPERTY(bool isCurrent READ isCurrent NOTIFY isCurrentChanged)
0067     Q_PROPERTY(Info::State state READ state NOTIFY stateChanged)
0068 
0069 public:
0070     explicit Info(const QString &activity, QObject *parent = nullptr);
0071     ~Info() override;
0072 
0073     /**
0074      * @return true if the activity represented by this object exists and is valid
0075      */
0076     bool isValid() const;
0077 
0078     /**
0079      * Specifies which parts of this class are functional
0080      */
0081     enum Availability {
0082         Nothing = 0, ///< No activity info provided (isValid is false)
0083         BasicInfo = 1, ///< Basic info is provided
0084         Everything = 2, ///< Everything is available
0085     };
0086 
0087     /**
0088      * State of the activity
0089      */
0090     enum State {
0091         Invalid = 0, ///< This activity does not exist
0092         Unknown = 1, ///< Information is not yet retrieved from the service
0093         Running = 2, ///< Activity is running
0094         Starting = 3, ///< Activity is begin started
0095         Stopped = 4, ///< Activity is stopped
0096         Stopping = 5, ///< Activity is begin started
0097     };
0098 
0099     /**
0100      * @returns what info is provided by this instance of Info
0101      */
0102     Availability availability() const;
0103 
0104     /**
0105      * @returns the URI of this activity. The same URI is used by activities
0106      * KIO worker.
0107      */
0108     QString uri() const;
0109 
0110     /**
0111      * @returns the id of the activity
0112      */
0113     QString id() const;
0114 
0115     /**
0116      * @returns whether this activity is the current one
0117      */
0118     bool isCurrent() const;
0119 
0120     /**
0121      * @returns the name of the activity
0122      */
0123     QString name() const;
0124 
0125     /**
0126      * @returns the description of the activity
0127      */
0128     QString description() const;
0129 
0130     /**
0131      * @returns the icon of the activity. Icon can be a freedesktop.org name or
0132      * a file path. Or empty if no icon is set.
0133      */
0134     QString icon() const;
0135 
0136     /**
0137      * @returns the state of the activity
0138      */
0139     State state() const;
0140 
0141     /**
0142      * Links the specified resource to the activity
0143      * @param resourceUri resource URI
0144      * @note This method is <b>asynchronous</b>. It will return before the
0145      * resource is actually linked to the activity.
0146      */
0147     // QFuture<void> linkResource(const QString &resourceUri);
0148 
0149     /**
0150      * Unlinks the specified resource from the activity
0151      * @param resourceUri resource URI
0152      * @note This method is <b>asynchronous</b>. It will return before the
0153      * resource is actually unlinked from the activity.
0154      */
0155     // QFuture<void> unlinkResource(const QString &resourceUri);
0156 
0157     /**
0158      * @returns whether a resource is linked to this activity
0159      * @note This QFuture is not thread-based, you can not call synchronous
0160      * methods like waitForFinished, cancel, pause on it.
0161      * @since 5.0
0162      */
0163     // QFuture<bool> isResourceLinked(const QString &resourceUri);
0164 
0165 Q_SIGNALS:
0166     /**
0167      * Emitted when the activity's name, icon or some custom property is changed
0168      */
0169     void infoChanged();
0170 
0171     /**
0172      * Emitted when the name is changed
0173      */
0174     void nameChanged(const QString &name);
0175 
0176     /**
0177      * Emitted when the activity becomes the current one, or when it stops
0178      * being the current one
0179      */
0180     void isCurrentChanged(bool current);
0181 
0182     /**
0183      * Emitted when the description is changed
0184      */
0185     void descriptionChanged(const QString &description);
0186 
0187     /**
0188      * Emitted when the icon was changed
0189      */
0190     void iconChanged(const QString &icon);
0191 
0192     /**
0193      * Emitted when the activity is added
0194      */
0195     void added();
0196 
0197     /**
0198      * Emitted when the activity is removed
0199      */
0200     void removed();
0201 
0202     /**
0203      * Emitted when the activity is started
0204      */
0205     void started();
0206 
0207     /**
0208      * Emitted when the activity is stopped
0209      */
0210     void stopped();
0211 
0212     /**
0213      * Emitted when the activity changes state
0214      * @param state new state of the activity
0215      */
0216     void stateChanged(KActivities::Info::State state);
0217 
0218 private:
0219     const std::unique_ptr<InfoPrivate> d;
0220 
0221     Q_PRIVATE_SLOT(d, void activityStateChanged(const QString &, int))
0222     Q_PRIVATE_SLOT(d, void added(const QString &))
0223     Q_PRIVATE_SLOT(d, void removed(const QString &))
0224     Q_PRIVATE_SLOT(d, void started(const QString &))
0225     Q_PRIVATE_SLOT(d, void stopped(const QString &))
0226     Q_PRIVATE_SLOT(d, void infoChanged(const QString &))
0227     Q_PRIVATE_SLOT(d, void nameChanged(const QString &, const QString &))
0228     Q_PRIVATE_SLOT(d, void descriptionChanged(const QString &, const QString &))
0229     Q_PRIVATE_SLOT(d, void iconChanged(const QString &, const QString &))
0230     Q_PRIVATE_SLOT(d, void setServiceStatus(Consumer::ServiceStatus))
0231     Q_PRIVATE_SLOT(d, void setCurrentActivity(const QString &))
0232 
0233     friend class InfoPrivate;
0234 };
0235 
0236 } // namespace KActivities
0237 
0238 #endif // ACTIVITIES_INFO_H