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

0001 /* This file is part of the KDE libraries
0002  * SPDX-FileCopyrightText: 2009 Dario Freddi <drf at kde.org>
0003  * SPDX-FileCopyrightText: 2021 Harald Sitter <sitter@kde.org>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-or-later
0006  */
0007 
0008 #ifndef KIDLETIME_H
0009 #define KIDLETIME_H
0010 
0011 #include <QHash>
0012 #include <QObject>
0013 #include <kidletime_export.h>
0014 #include <memory>
0015 
0016 #if __has_include(<chrono>)
0017 #include <chrono>
0018 #endif
0019 
0020 class KIdleTimePrivate;
0021 
0022 /**
0023  * @class KIdleTime kidletime.h KIdleTime
0024  *
0025  * KIdleTime is a singleton reporting information on idle time. It is useful not
0026  * only for finding out about the current idle time of the PC, but also for getting
0027  * notified upon idle time events, such as custom timeouts, or user activity.
0028  *
0029  * @note All the intervals and times in this library are in milliseconds, unless
0030  *       specified otherwise
0031  *
0032  * @author Dario Freddi
0033  *
0034  * @since 4.4
0035  */
0036 class KIDLETIME_EXPORT KIdleTime : public QObject
0037 {
0038     Q_OBJECT
0039     Q_DECLARE_PRIVATE(KIdleTime)
0040     Q_DISABLE_COPY(KIdleTime)
0041 
0042 public:
0043     /**
0044      * Returns the singleton instance. Use this method to access KIdleTime
0045      *
0046      * @returns the instance of KIdleTime
0047      */
0048     static KIdleTime *instance();
0049 
0050     /**
0051      * The destructor
0052      */
0053     ~KIdleTime() override;
0054 
0055     /**
0056      * Retrieves the idle time of the system, in milliseconds
0057      *
0058      * @returns the idle time of the system
0059      */
0060     int idleTime() const;
0061 
0062     /**
0063      * Returns the list of timeout identifiers associated with their duration, in milliseconds,
0064      * the library is currently listening to.
0065      *
0066      * @see addIdleTimeout
0067      * @see removeIdleTimeout
0068      * @see timeoutReached
0069      */
0070     QHash<int, int> idleTimeouts() const;
0071 
0072 #if KIDLETIME_ENABLE_DEPRECATED_SINCE(5, 100)
0073     /**
0074      * Attempts to simulate user activity. This implies that after calling this
0075      * method, the idle time of the system will become 0 and eventually \link resumingFromIdle \endlink
0076      * will be triggered
0077      *
0078      * @see resumingFromIdle
0079      */
0080     KIDLETIME_DEPRECATED_VERSION(5, 100, "No known users and not available on every platform")
0081     void simulateUserActivity();
0082 #endif
0083 
0084 public Q_SLOTS:
0085     /**
0086      * Adds a new timeout to catch. When calling this method, after the system will be idle for
0087      * \c msec milliseconds, the signal \c timeoutReached will be triggered. Please note that until you will
0088      * call \c removeIdleTimeout or \c removeAllIdleTimeouts, the signal will be triggered every
0089      * time the system will be idle for \c msec milliseconds. This function also returns an unique
0090      * token for the timeout just added to allow easier identification.
0091      *
0092      * @param msec the time, in milliseconds, after which the signal will be triggered
0093      *
0094      * @returns an unique identifier for the timeout being added, that will be streamed by timeoutReached
0095      *
0096      * @see removeIdleTimeout
0097      * @see removeAllIdleTimeouts
0098      * @see timeoutReached
0099      *
0100      */
0101     int addIdleTimeout(int msec);
0102 
0103 #if __has_include(<chrono>)
0104     /**
0105      * Convenience overload supporting C++ chrono types. May also be used with chrono literals.
0106      * @since 5.83
0107      */
0108     int addIdleTimeout(std::chrono::milliseconds msec)
0109     {
0110         return addIdleTimeout(int(msec.count()));
0111     }
0112 #endif
0113 
0114     /**
0115      * Stops catching the idle timeout identified by the token \c identifier,
0116      * if it was registered earlier with addIdleTimeout.
0117      * Otherwise does nothing.
0118      *
0119      * @param identifier the token returned from addIdleTimeout of the timeout you want to stop listening to
0120      */
0121     void removeIdleTimeout(int identifier);
0122 
0123     /**
0124      * Stops catching every set timeout (if any). This means that after calling this method, the signal
0125      * \link timeoutReached \endlink won't be called again until you will add another timeout
0126      *
0127      * @see timeoutReached
0128      * @see addIdleTimeout
0129      */
0130     void removeAllIdleTimeouts();
0131 
0132     /**
0133      * Catches the next resume from idle event. This means that whenever user activity will be registered, or
0134      * \link simulateUserActivity \endlink is called, the signal \link resumingFromIdle \endlink will be triggered.
0135      * <p>
0136      * Please note that this method will trigger the signal just for the very first resume event after the call:
0137      * this means you explicitly have to request to track every single resume event you are interested in.
0138      *
0139      * @note This behavior is due to the fact that a resume event happens whenever the user sends an input to the
0140      *       system. This would lead to a massive amount of signals being delivered when the PC is being used.
0141      *       Moreover, you are usually interested in catching just significant resume events, such as the ones after
0142      *       a significant period of inactivity. For tracking user input, you can use the more efficient methods provided
0143      *       by Qt. The purpose of this library is just monitoring the activity of the user.
0144      *
0145      * @see resumingFromIdle
0146      * @see simulateUserActivity
0147      *
0148      */
0149     void catchNextResumeEvent();
0150 
0151     /**
0152      * Stops listening for resume event. This function serves for canceling \c catchNextResumeEvent, as it
0153      * will have effect just when \c catchNextResumeEvent has been called and \c resumingFromIdle not
0154      * yet triggered
0155      *
0156      * @see resumingFromIdle
0157      * @see catchNextResumeEvent
0158      *
0159      */
0160     void stopCatchingResumeEvent();
0161 
0162 Q_SIGNALS:
0163     /**
0164      * Triggered, if KIdleTime is catching resume events, when the system resumes from an idle state. This means
0165      * that either \link simulateUserActivity \endlink was called or the user sent an input to the system.
0166      *
0167      * @see catchNextResumeEvent
0168      */
0169     void resumingFromIdle();
0170 
0171 #if KIDLETIME_ENABLE_DEPRECATED_SINCE(5, 76)
0172     /**
0173      * Triggered when the system has been idle for x milliseconds, identified by the previously set
0174      * timeout.
0175      * <p>
0176      * This signal is triggered whenever each timeout previously registered with \link addIdleTimeout \endlink
0177      * is reached.
0178      *
0179      * @param identifier the identifier of the timeout the system has reached
0180      *
0181      * @see addIdleTimeout
0182      * @see removeIdleTimeout
0183      * @deprecated Since 5.76, use only timeoutReached(int identifier, int msec)
0184      */
0185     KIDLETIME_DEPRECATED_VERSION(5, 76, "Use only timeoutReached(int identifier, int msec)")
0186     void timeoutReached(int identifier); // clazy:exclude=overloaded-signal
0187 #endif
0188 
0189     /**
0190      * Triggered when the system has been idle for x milliseconds, identified by the previously set
0191      * timeout.
0192      *
0193      * This signal is triggered whenever each timeout previously registered with addIdleTimeout(int)
0194      * is reached. It is guaranteed that \p msec will exactly correspond to the identified timeout.
0195      *
0196      * @param identifier the identifier of the timeout the system has reached
0197      * @param msec the time, in milliseconds, the system has been idle for
0198      *
0199      * @see addIdleTimeout
0200      * @see removeIdleTimeout
0201      */
0202     void timeoutReached(int identifier, int msec); // clazy:exclude=overloaded-signal
0203 
0204 private:
0205     KIdleTime();
0206 
0207     std::unique_ptr<KIdleTimePrivate> const d_ptr;
0208 };
0209 
0210 #endif /* KIDLETIME_H */