File indexing completed on 2024-04-21 05:54:05

0001 /*
0002     SPDX-FileCopyrightText: 2005-2006, 2008-2010 Tom Albers <toma@kde.org>
0003     SPDX-FileCopyrightText: 2005-2006 Bram Schoenmakers <bramschoenmakers@kde.nl>
0004     SPDX-FileCopyrightText: 2010 Juan Luis Baptiste <juan.baptiste@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 
0008 */
0009 
0010 #ifndef RSITimer_H
0011 #define RSITimer_H
0012 
0013 #include <QVector>
0014 #include <memory>
0015 
0016 #include "rsiidletime.h"
0017 #include "rsitimercounter.h"
0018 
0019 /**
0020  * @class RSITimer
0021  * This class controls the timings and arranges the maximizing
0022  * and minimizing of the widget.
0023  * @author Tom Albers <toma.org>
0024  */
0025 class RSITimer : public QObject
0026 {
0027     Q_OBJECT
0028     friend class RSITimerTest;
0029 
0030 public:
0031     /**
0032      * Constructor
0033      * @param parent Parent Widget
0034      * @param name Name
0035      */
0036     explicit RSITimer(QObject *parent = nullptr);
0037 
0038     // Check whether the timer is suspended.
0039     bool isSuspended() const
0040     {
0041         return m_state == TimerState::Suspended;
0042     }
0043 
0044     int tinyLeft() const
0045     {
0046         return m_tinyBreakCounter ? m_tinyBreakCounter->counterLeft() : 0;
0047     };
0048 
0049     int bigLeft() const
0050     {
0051         return m_bigBreakCounter->counterLeft();
0052     };
0053 
0054 public slots:
0055 
0056     /**
0057       Reads the configuration and restarts the timer with slotRestart.
0058     */
0059     void updateConfig(bool doRestart = false);
0060 
0061     /**
0062       Stops the timer activity. This does not imply resetting counters.
0063      */
0064     void slotStop();
0065 
0066     /**
0067       Called when the user suspends RSIBreak from the docker.
0068       @param suspend If true the timer will suspend, if false the timer will unsuspend.
0069     */
0070     void slotSuspended(bool suspend);
0071 
0072     /**
0073       Prepares the timer so that it can start/continue.
0074      */
0075     void slotStart();
0076 
0077     /**
0078       Called when user locks the screen for pause. Resets current timers if currently suggesting.
0079     */
0080     void slotLock();
0081 
0082     /**
0083       When the user presses the Skip button during a break,
0084       this function will be called. It will act like a break has
0085       just passed.
0086     */
0087     void skipBreak();
0088 
0089     /**
0090       When the user presses the postpone break button during a break,
0091       this function will be called. It will postpone the break for the
0092       configured amount of seconds.
0093     */
0094     void postponeBreak();
0095 
0096     /**
0097       Queries X how many seconds the user has been idle. A value of 0
0098       means there was activity during the last second.
0099       @returns The amount of seconds of idling.
0100     */
0101     int idleTime();
0102 
0103 private slots:
0104     /**
0105       The pumping heart of the timer. This will evaluate user's activity and
0106       decide what to do (wait, popup a relax notification or a fullscreen break.
0107     */
0108     virtual void timeout();
0109 
0110 signals:
0111     /** Enforce a fullscreen big break. */
0112     void breakNow();
0113 
0114     /**
0115       Update counters in tooltip.
0116       @param tinyLeft If <=0 a tiny break is active, else it defines how
0117       much time is left until the next tiny break.
0118       @param bigLeft If <=0 a big break is active, else it defines how
0119       much time is left until the next big break.
0120     */
0121     void updateToolTip(const int tinyLeft, const int bigLeft);
0122 
0123     /**
0124       Update the time shown on the fullscreen widget.
0125       @param secondsLeft Shows the user how many seconds are remaining.
0126     */
0127     void updateWidget(int secondsLeft);
0128 
0129     /**
0130       Update the systray icon.
0131       @param v How much time has passed until a tiny break (relative)
0132                Varies from 0 to 100.
0133     */
0134     void updateIdleAvg(double v);
0135 
0136     /**
0137       A request to minimize the fullscreen widget, for example when the
0138       break is over.
0139       @param newImage Load a new image
0140     */
0141     void minimize();
0142 
0143     /**
0144       Pop up a relax notification to the user for @p sec seconds.
0145       @param sec The amount of seconds the user should relax to make the
0146       popup disappear. A value of -1 will hide the relax popup.
0147       @param nextBreakIsBig True if the break after the next break is a big break.
0148       We can warn the user in advance.
0149     */
0150     void relax(int sec, bool nextBreakIsBig);
0151 
0152     /**
0153       Indicates a tinyBreak is skipped because user was enough idle
0154     */
0155     void tinyBreakSkipped();
0156 
0157     /**
0158        Indicates a bigBreak is skipped because user was enough idle
0159      */
0160     void bigBreakSkipped();
0161 
0162     void startLongBreak();
0163     void endLongBreak();
0164     void startShortBreak();
0165     void endShortBreak();
0166 
0167 private:
0168     std::unique_ptr<RSIIdleTime> m_idleTimeInstance;
0169 
0170     bool m_suppressable;
0171     bool m_usePopup;
0172     bool m_useIdleTimers;
0173     QVector<int> m_intervals;
0174 
0175     enum class TimerState {
0176         Suspended = 0, // user has suspended either via dbus or tray.
0177         Monitoring, // normal cycle, waiting for break to trigger.
0178         Suggesting, // politely suggest to take a break with some patience.
0179         Resting // suggestion ignored, waiting out the break.
0180     } m_state;
0181 
0182     std::unique_ptr<RSITimerCounter> m_bigBreakCounter;
0183     std::unique_ptr<RSITimerCounter> m_tinyBreakCounter;
0184     std::unique_ptr<RSITimerCounter> m_pauseCounter;
0185     std::unique_ptr<RSITimerCounter> m_popupCounter;
0186     std::unique_ptr<RSITimerCounter> m_shortInputCounter;
0187 
0188     bool suppressionDetector();
0189     void hibernationDetector(const int totalIdle);
0190     void suggestBreak(const int time);
0191     void defaultUpdateToolTip();
0192     void createTimers();
0193 
0194     // This function is called when a break has passed.
0195     void resetAfterBreak();
0196 
0197     // Start this timer. Used by the constructors.
0198     void run();
0199 
0200     /**
0201       Some internal preparations for a fullscreen break window.
0202       @param breakTime The amount of seconds to break.
0203       @param nextBreakIsBig Whether the next break will be big.
0204     */
0205     void doBreakNow(const int breakTime, const bool nextBreakIsBig);
0206 
0207     // Constructor for tests.
0208     RSITimer(std::unique_ptr<RSIIdleTime> &&_idleTime, const QVector<int> _intervals, const bool _usePopup, const bool _useIdleTimers);
0209 };
0210 
0211 #endif