karm

idletimedetector.cpp
1 #include "idletimedetector.h"
2 
3 #include <tqdatetime.h>
4 #include <tqmessagebox.h>
5 #include <tqtimer.h>
6 
7 #include <kglobal.h>
8 #include <klocale.h> // i18n
9 
11 // Trigger a warning after maxIdle minutes
12 {
13  kdDebug(5970) << "Entering IdleTimeDetector::IdleTimeDetector" << endl;
14  _maxIdle = maxIdle;
15 
16 #ifdef HAVE_LIBXSS
17  kdDebug(5970) << "IdleTimeDetector: LIBXSS detected @ compile time" << endl;
18  int event_base, error_base;
19  if(XScreenSaverQueryExtension(qt_xdisplay(), &event_base, &error_base))
20  {
21  _idleDetectionPossible = true;
22  _mit_info = XScreenSaverAllocInfo ();
23  }
24  else
25  {
26  _idleDetectionPossible = false;
27  }
28 
29  _timer = new TQTimer(this);
30  connect(_timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(check()));
31 #else
32  _idleDetectionPossible = false;
33 #endif // HAVE_LIBXSS
34 
35 }
36 
38 {
39  return _idleDetectionPossible;
40 }
41 
42 void IdleTimeDetector::check()
43 {
44  kdDebug(5970) << "Entering IdleTimeDetector::check" << endl;
45 #ifdef HAVE_LIBXSS
46  if (_idleDetectionPossible)
47  {
48  XScreenSaverQueryInfo(qt_xdisplay(), qt_xrootwin(), _mit_info);
49  int idleSeconds = (_mit_info->idle/1000);
50  if (idleSeconds >= _maxIdle)
51  informOverrun(idleSeconds);
52  }
53 #endif // HAVE_LIBXSS
54 }
55 
57 {
58  _maxIdle = maxIdle;
59 }
60 
61 #ifdef HAVE_LIBXSS
62 void IdleTimeDetector::informOverrun(int idleSeconds)
63 {
64  kdDebug(5970) << "Entering IdleTimeDetector::informOverrun" << endl;
65  if (!_overAllIdleDetect)
66  return; // preferences say the user does not want idle detection.
67 
68  _timer->stop();
69 
70  TQDateTime idleStart = TQDateTime::currentDateTime().addSecs(-idleSeconds);
71  TQString idleStartTQString = KGlobal::locale()->formatTime(idleStart.time());
72 
73  int id = TQMessageBox::warning( 0, i18n("Idle Detection"),
74  i18n("Desktop has been idle since %1."
75  " What should we do?").arg(idleStartTQString),
76  i18n("Revert && Stop"),
77  i18n("Revert && Continue"),
78  i18n("Continue Timing"),0,2);
79  TQDateTime end = TQDateTime::currentDateTime();
80  int diff = idleStart.secsTo(end)/secsPerMinute;
81 
82  if (id == 0)
83  {
84  // Revert And Stop
85  kdDebug(5970) << "Now it is " << TQDateTime::currentDateTime() << endl;
86  kdDebug(5970) << "Reverting timer to " << KGlobal::locale()->formatTime(idleStart.time()).ascii() << endl;
87  emit(extractTime(idleSeconds/60+diff)); // we need to subtract the time that has been added during idleness.
88  emit(stopAllTimersAt(idleStart));
89  }
90  else if (id == 1)
91  {
92  // Revert and Continue
93  emit(extractTime(idleSeconds/60+diff));
94  _timer->start(testInterval);
95  }
96  else
97  {
98  // Continue
99  _timer->start(testInterval);
100  }
101 }
102 #endif // HAVE_LIBXSS
103 
105 {
106  kdDebug(5970) << "Entering IdleTimeDetector::startIdleDetection" << endl;
107 #ifdef HAVE_LIBXSS
108  kdDebug(5970) << "Starting Timer" << endl;
109  if (!_timer->isActive())
110  _timer->start(testInterval);
111 #endif //HAVE_LIBXSS
112 }
113 
115 {
116 #ifdef HAVE_LIBXSS
117  if (_timer->isActive())
118  _timer->stop();
119 #endif // HAVE_LIBXSS
120 }
122 {
123  _overAllIdleDetect = on;
124 }
125 
126 #include "idletimedetector.moc"