karm

idletimedetector.cpp
1 #include <time.h>
2 
3 #include "idletimedetector.h"
4 
5 #include <tqdatetime.h>
6 #include <tqmessagebox.h>
7 #include <tqtimer.h>
8 
9 #include <tdeglobal.h>
10 #include <tdelocale.h> // i18n
11 
13 // Trigger a warning after maxIdle minutes
14 {
15  kdDebug(5970) << "Entering IdleTimeDetector::IdleTimeDetector" << endl;
16  _maxIdle = maxIdle;
17 
18 #ifdef HAVE_LIBXSS
19  kdDebug(5970) << "IdleTimeDetector: LIBXSS detected @ compile time" << endl;
20  int event_base, error_base;
21  if(XScreenSaverQueryExtension(tqt_xdisplay(), &event_base, &error_base))
22  {
23  _idleDetectionPossible = true;
24  _mit_info = XScreenSaverAllocInfo ();
25  }
26  else
27  {
28  _idleDetectionPossible = false;
29  }
30 
31  _timer = new TQTimer(this);
32  connect(_timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(check()));
33 #else
34  _idleDetectionPossible = false;
35 #endif // HAVE_LIBXSS
36 
37 }
38 
40 {
41  return _idleDetectionPossible;
42 }
43 
44 void IdleTimeDetector::check()
45 {
46  kdDebug(5970) << "Entering IdleTimeDetector::check" << endl;
47 #ifdef HAVE_LIBXSS
48  if (_idleDetectionPossible)
49  {
50  XScreenSaverQueryInfo(tqt_xdisplay(), tqt_xrootwin(), _mit_info);
51  int idleSeconds = (_mit_info->idle/1000);
52  if (idleSeconds >= _maxIdle)
53  informOverrun(idleSeconds);
54  }
55 #endif // HAVE_LIBXSS
56 }
57 
59 {
60  _maxIdle = maxIdle;
61 }
62 
63 #ifdef HAVE_LIBXSS
64 void IdleTimeDetector::informOverrun(int idleSeconds)
65 {
66  kdDebug(5970) << "Entering IdleTimeDetector::informOverrun" << endl;
67  if (!_overAllIdleDetect)
68  return; // preferences say the user does not want idle detection.
69 
70  _timer->stop();
71 
72  struct timespec tm;
73 
74  clock_gettime(CLOCK_MONOTONIC, &tm);
75  int start = tm.tv_sec - idleSeconds;
76 
77  TQDateTime idleStart = TQDateTime::currentDateTime().addSecs(-idleSeconds);
78  int id = TQMessageBox::warning( 0, i18n("Idle Detection"),
79  i18n("Desktop has been idle since %1."
80  " What should we do?").arg(TDEGlobal::locale()->formatTime(idleStart.time())),
81  i18n("Revert && Stop"),
82  i18n("Revert && Continue"),
83  i18n("Continue Timing"),0,2);
84 
85  clock_gettime(CLOCK_MONOTONIC, &tm);
86  int diff = tm.tv_sec - start;
87 
88  if (id == 0)
89  {
90  // Revert And Stop
91  kdDebug(5970) << "Now it is " << TQDateTime::currentDateTime() << endl;
92  kdDebug(5970) << "Reverting timer to " << TDEGlobal::locale()->formatTime(idleStart.time()).ascii() << endl;
93  emit(extractTime(diff/60));
94  emit(stopAllTimersAt(idleStart));
95  }
96  else if (id == 1)
97  {
98  // Revert and Continue
99  emit(extractTime(diff/60));
100  _timer->start(testInterval);
101  }
102  else
103  {
104  // Continue
105  _timer->start(testInterval);
106  }
107 }
108 #endif // HAVE_LIBXSS
109 
111 {
112  kdDebug(5970) << "Entering IdleTimeDetector::startIdleDetection" << endl;
113 #ifdef HAVE_LIBXSS
114  kdDebug(5970) << "Starting Timer" << endl;
115  if (!_timer->isActive())
116  _timer->start(testInterval);
117 #endif //HAVE_LIBXSS
118 }
119 
121 {
122 #ifdef HAVE_LIBXSS
123  if (_timer->isActive())
124  _timer->stop();
125 #endif // HAVE_LIBXSS
126 }
128 {
129  _overAllIdleDetect = on;
130 }
131 
132 #include "idletimedetector.moc"
void extractTime(int minutes)
Tells the listener to extract time from current timing.
void startIdleDetection()
Starts detecting idle time.
IdleTimeDetector(int maxIdle)
Initializes and idle test timer.
void toggleOverAllIdleDetection(bool on)
Sets whether idle detection should be done at all.
void stopIdleDetection()
Stops detecting idle time.
void setMaxIdle(int maxIdle)
Sets the maximum allowed idle.
bool isIdleDetectionPossible()
Returns true if it is possible to do idle detection.
void stopAllTimersAt(TQDateTime qdt)
Tells the listener to stop timing for TQDateTime.