File indexing completed on 2024-05-12 05:21:39

0001 /*
0002  * Copyright (C) 2003 by Scott Monachello <smonach@cox.net>
0003  * Copyright (C) 2019  Alexander Potashev <aspotashev@gmail.com>
0004  *
0005  *   This program is free software; you can redistribute it and/or modify
0006  *   it under the terms of the GNU General Public License as published by
0007  *   the Free Software Foundation; either version 2 of the License, or
0008  *   (at your option) any later version.
0009  *
0010  *   This program is distributed in the hope that it will be useful,
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  *   GNU General Public License for more details.
0014  *
0015  *   You should have received a copy of the GNU General Public License along
0016  *   with this program; if not, write to the
0017  *      Free Software Foundation, Inc.
0018  *      51 Franklin Street, Fifth Floor
0019  *      Boston, MA  02110-1301  USA.
0020  *
0021  */
0022 
0023 #ifndef KTIMETRACKER_IDLE_TIME_DETECTOR_H
0024 #define KTIMETRACKER_IDLE_TIME_DETECTOR_H
0025 
0026 #include <QDateTime>
0027 #include <QObject>
0028 
0029 /**
0030  * Keep track of how long the computer has been idle.
0031  */
0032 
0033 class IdleTimeDetector : public QObject
0034 {
0035     Q_OBJECT
0036 
0037 public:
0038     /**
0039        Initializes and idle test timer
0040        @param maxIdle minutes before the idle timer will go off.
0041     **/
0042     explicit IdleTimeDetector(int maxIdle);
0043 
0044     /**
0045        Returns true if it is possible to do idle detection.
0046        Idle detection relys on a feature in the X server, which might not
0047        always be present.
0048     **/
0049     static bool isIdleDetectionPossible();
0050 
0051 Q_SIGNALS:
0052     /**
0053        Tells the listener to subtract time from current timing.
0054        The time to subtract is due to the idle time since the dialog wass
0055        shown, and until the user answers the dialog.
0056        @param minutes Minutes to subtract.
0057     **/
0058     void subtractTime(int64_t minutes);
0059 
0060     /**
0061         Tells the listener to stop timing
0062      **/
0063     void stopAllTimers(QDateTime time);
0064 
0065 public Q_SLOTS:
0066     /**
0067        Sets the maximum allowed idle.
0068        @param maxIdle Maximum allowed idle time in minutes
0069     **/
0070     void setMaxIdle(int maxIdle);
0071 
0072     /**
0073        Starts detecting idle time
0074     **/
0075     void startIdleDetection();
0076 
0077     /**
0078         Stops detecting idle time.
0079     **/
0080     void stopIdleDetection();
0081 
0082     /**
0083        Sets whether idle detection should be done at all
0084        @param on If true idle detection is done based on
0085        startIdleDetection and @ref stopIdleDetection
0086     **/
0087     void toggleOverAllIdleDetection(bool on);
0088 
0089 protected Q_SLOTS:
0090     void timeoutReached(int id, int timeout);
0091 
0092 private:
0093     void revert(const QDateTime &dialogStart, const QDateTime &idleStart, int idleMinutes);
0094 
0095     bool m_overAllIdleDetect; // Based on preferences.
0096     int m_maxIdle;
0097     int m_timeoutId;
0098 };
0099 
0100 #endif // KTIMETRACKER_IDLE_TIME_DETECTOR_H