summaryrefslogtreecommitdiffstats
path: root/karm/idletimedetector.cpp
blob: dd2c4d565efcae18a0b80d04675123c82e56498f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <time.h>

#include "idletimedetector.h"

#include <tqdatetime.h>
#include <tqmessagebox.h>
#include <tqtimer.h>

#include <tdeglobal.h>
#include <tdelocale.h>    // i18n

IdleTimeDetector::IdleTimeDetector(int maxIdle)
// Trigger a warning after maxIdle minutes
{
  kdDebug(5970) << "Entering IdleTimeDetector::IdleTimeDetector" << endl;
  _maxIdle = maxIdle;

#ifdef HAVE_LIBXSS
  kdDebug(5970) << "IdleTimeDetector: LIBXSS detected @ compile time" << endl;
  int event_base, error_base;
  if(XScreenSaverQueryExtension(tqt_xdisplay(), &event_base, &error_base)) 
  {
    _idleDetectionPossible = true;
    _mit_info = XScreenSaverAllocInfo ();
  }
  else 
  {
    _idleDetectionPossible = false;
  }

  _timer = new TQTimer(this);
  connect(_timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(check()));
#else
  _idleDetectionPossible = false;
#endif // HAVE_LIBXSS

}

bool IdleTimeDetector::isIdleDetectionPossible()
{
  return _idleDetectionPossible;
}

void IdleTimeDetector::check()
{
  kdDebug(5970) << "Entering IdleTimeDetector::check" << endl;
#ifdef HAVE_LIBXSS
  if (_idleDetectionPossible)
  {
    XScreenSaverQueryInfo(tqt_xdisplay(), tqt_xrootwin(), _mit_info);
    int idleSeconds = (_mit_info->idle/1000);
    if (idleSeconds >= _maxIdle)
      informOverrun(idleSeconds);
  }
#endif // HAVE_LIBXSS
}

void IdleTimeDetector::setMaxIdle(int maxIdle)
{
  _maxIdle = maxIdle;
}

#ifdef HAVE_LIBXSS
void IdleTimeDetector::informOverrun(int idleSeconds)
{
  kdDebug(5970) << "Entering IdleTimeDetector::informOverrun" << endl;
  if (!_overAllIdleDetect)
    return; // preferences say the user does not want idle detection.

  _timer->stop();

  struct timespec tm;

  clock_gettime(CLOCK_MONOTONIC, &tm);
  int start = tm.tv_sec - idleSeconds;

  TQDateTime idleStart = TQDateTime::currentDateTime().addSecs(-idleSeconds);
  int id =  TQMessageBox::warning( 0, i18n("Idle Detection"),
                                     i18n("Desktop has been idle since %1."
                                          " What should we do?").arg(TDEGlobal::locale()->formatTime(idleStart.time())),
                                     i18n("Revert && Stop"),
                                     i18n("Revert && Continue"),
                                     i18n("Continue Timing"),0,2);

  clock_gettime(CLOCK_MONOTONIC, &tm);
  int diff = tm.tv_sec - start;

  if (id == 0) 
  {
    // Revert And Stop
    kdDebug(5970) << "Now it is " << TQDateTime::currentDateTime() << endl;
    kdDebug(5970) << "Reverting timer to " << TDEGlobal::locale()->formatTime(idleStart.time()).ascii() << endl;
    emit(extractTime(diff/60));
    emit(stopAllTimersAt(idleStart));
  }
  else if (id == 1) 
  {
    // Revert and Continue
    emit(extractTime(diff/60));
    _timer->start(testInterval);
  }
  else 
  {
    // Continue
    _timer->start(testInterval);
  }
}
#endif // HAVE_LIBXSS

void IdleTimeDetector::startIdleDetection()
{
  kdDebug(5970) << "Entering IdleTimeDetector::startIdleDetection" << endl; 
#ifdef HAVE_LIBXSS
  kdDebug(5970) << "Starting Timer" << endl;
  if (!_timer->isActive())
    _timer->start(testInterval);
#endif //HAVE_LIBXSS
}

void IdleTimeDetector::stopIdleDetection()
{
#ifdef HAVE_LIBXSS
  if (_timer->isActive())
    _timer->stop();
#endif // HAVE_LIBXSS
}
void IdleTimeDetector::toggleOverAllIdleDetection(bool on)
{
  _overAllIdleDetect = on;
}

#include "idletimedetector.moc"