File indexing completed on 2024-04-14 14:23:40

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     /**
0073      * Attempts to simulate user activity. This implies that after calling this
0074      * method, the idle time of the system will become 0 and eventually \link resumingFromIdle \endlink
0075      * will be triggered
0076      *
0077      * @see resumingFromIdle
0078      */
0079     void simulateUserActivity();
0080 
0081 public Q_SLOTS:
0082     /**
0083      * Adds a new timeout to catch. When calling this method, after the system will be idle for
0084      * \c msec milliseconds, the signal \c timeoutReached will be triggered. Please note that until you will
0085      * call \c removeIdleTimeout or \c removeAllIdleTimeouts, the signal will be triggered every
0086      * time the system will be idle for \c msec milliseconds. This function also returns an unique
0087      * token for the timeout just added to allow easier identification.
0088      *
0089      * @param msec the time, in milliseconds, after which the signal will be triggered
0090      *
0091      * @returns an unique identifier for the timeout being added, that will be streamed by timeoutReached
0092      *
0093      * @see removeIdleTimeout
0094      * @see removeAllIdleTimeouts
0095      * @see timeoutReached
0096      *
0097      */
0098     int addIdleTimeout(int msec);
0099 
0100 #if __has_include(<chrono>)
0101     /**
0102      * Convenience overload supporting C++ chrono types. May also be used with chrono literals.
0103      * @since 5.83
0104      */
0105     int addIdleTimeout(std::chrono::milliseconds msec)
0106     {
0107         return addIdleTimeout(int(msec.count()));
0108     }
0109 #endif
0110 
0111     /**
0112      * Stops catching the idle timeout identified by the token \c identifier,
0113      * if it was registered earlier with addIdleTimeout.
0114      * Otherwise does nothing.
0115      *
0116      * @param identifier the token returned from addIdleTimeout of the timeout you want to stop listening to
0117      */
0118     void removeIdleTimeout(int identifier);
0119 
0120     /**
0121      * Stops catching every set timeout (if any). This means that after calling this method, the signal
0122      * \link timeoutReached \endlink won't be called again until you will add another timeout
0123      *
0124      * @see timeoutReached
0125      * @see addIdleTimeout
0126      */
0127     void removeAllIdleTimeouts();
0128 
0129     /**
0130      * Catches the next resume from idle event. This means that whenever user activity will be registered, or
0131      * \link simulateUserActivity \endlink is called, the signal \link resumingFromIdle \endlink will be triggered.
0132      * <p>
0133      * Please note that this method will trigger the signal just for the very first resume event after the call:
0134      * this means you explicitly have to request to track every single resume event you are interested in.
0135      *
0136      * @note This behavior is due to the fact that a resume event happens whenever the user sends an input to the
0137      *       system. This would lead to a massive amount of signals being delivered when the PC is being used.
0138      *       Moreover, you are usually interested in catching just significant resume events, such as the ones after
0139      *       a significant period of inactivity. For tracking user input, you can use the more efficient methods provided
0140      *       by Qt. The purpose of this library is just monitoring the activity of the user.
0141      *
0142      * @see resumingFromIdle
0143      * @see simulateUserActivity
0144      *
0145      */
0146     void catchNextResumeEvent();
0147 
0148     /**
0149      * Stops listening for resume event. This function serves for canceling \c catchNextResumeEvent, as it
0150      * will have effect just when \c catchNextResumeEvent has been called and \c resumingFromIdle not
0151      * yet triggered
0152      *
0153      * @see resumingFromIdle
0154      * @see catchNextResumeEvent
0155      *
0156      */
0157     void stopCatchingResumeEvent();
0158 
0159 Q_SIGNALS:
0160     /**
0161      * Triggered, if KIdleTime is catching resume events, when the system resumes from an idle state. This means
0162      * that either \link simulateUserActivity \endlink was called or the user sent an input to the system.
0163      *
0164      * @see catchNextResumeEvent
0165      */
0166     void resumingFromIdle();
0167 
0168 #if KIDLETIME_ENABLE_DEPRECATED_SINCE(5, 76)
0169     /**
0170      * Triggered when the system has been idle for x milliseconds, identified by the previously set
0171      * timeout.
0172      * <p>
0173      * This signal is triggered whenever each timeout previously registered with \link addIdleTimeout \endlink
0174      * is reached.
0175      *
0176      * @param identifier the identifier of the timeout the system has reached
0177      *
0178      * @see addIdleTimeout
0179      * @see removeIdleTimeout
0180      * @deprecated Since 5.76, use only timeoutReached(int identifier, int msec)
0181      */
0182     KIDLETIME_DEPRECATED_VERSION(5, 76, "Use only timeoutReached(int identifier, int msec)")
0183     void timeoutReached(int identifier); // clazy:exclude=overloaded-signal
0184 #endif
0185 
0186     /**
0187      * Triggered when the system has been idle for x milliseconds, identified by the previously set
0188      * timeout.
0189      *
0190      * This signal is triggered whenever each timeout previously registered with addIdleTimeout(int)
0191      * is reached. It is guaranteed that \p msec will exactly correspond to the identified timeout.
0192      *
0193      * @param identifier the identifier of the timeout the system has reached
0194      * @param msec the time, in milliseconds, the system has been idle for
0195      *
0196      * @see addIdleTimeout
0197      * @see removeIdleTimeout
0198      */
0199     void timeoutReached(int identifier, int msec); // clazy:exclude=overloaded-signal
0200 
0201 private:
0202     KIDLETIME_NO_EXPORT KIdleTime();
0203 
0204     std::unique_ptr<KIdleTimePrivate> const d_ptr;
0205 };
0206 
0207 #endif /* KIDLETIME_H */