00001
#include "idletimedetector.h"
00002
00003
#include <qdatetime.h>
00004
#include <qmessagebox.h>
00005
#include <qtimer.h>
00006
00007
#include <kglobal.h>
00008
#include <klocale.h>
00009
00010 IdleTimeDetector::IdleTimeDetector(
int maxIdle)
00011 {
00012 _maxIdle = maxIdle;
00013
00014
#ifdef HAVE_LIBXSS
00015
int event_base, error_base;
00016
if(XScreenSaverQueryExtension(qt_xdisplay(), &event_base, &error_base)) {
00017 _idleDetectionPossible =
true;
00018 }
00019
else {
00020 _idleDetectionPossible =
false;
00021 }
00022
00023 _timer =
new QTimer(
this);
00024 connect(_timer, SIGNAL(timeout()),
this, SLOT(check()));
00025
#else
00026
_idleDetectionPossible =
false;
00027
#endif // HAVE_LIBXSS
00028
00029 }
00030
00031 bool IdleTimeDetector::isIdleDetectionPossible()
00032 {
00033
return _idleDetectionPossible;
00034 }
00035
00036
void IdleTimeDetector::check()
00037 {
00038
#ifdef HAVE_LIBXSS
00039
if (_idleDetectionPossible)
00040 {
00041 _mit_info = XScreenSaverAllocInfo ();
00042 XScreenSaverQueryInfo(qt_xdisplay(), qt_xrootwin(), _mit_info);
00043
int idleMinutes = (_mit_info->idle/1000)/secsPerMinute;
00044
if (idleMinutes >= _maxIdle)
00045 informOverrun(idleMinutes);
00046 }
00047
#endif // HAVE_LIBXSS
00048
}
00049
00050 void IdleTimeDetector::setMaxIdle(
int maxIdle)
00051 {
00052 _maxIdle = maxIdle;
00053 }
00054
00055
#ifdef HAVE_LIBXSS
00056
void IdleTimeDetector::informOverrun(
int idleMinutes)
00057 {
00058
if (!_overAllIdleDetect)
00059
return;
00060
00061
00062 _timer->stop();
00063
00064
QDateTime start = QDateTime::currentDateTime();
00065
QDateTime idleStart = start.addSecs(-60 * _maxIdle);
00066
QString backThen = KGlobal::locale()->formatTime(idleStart.time());
00067
00068
int id = QMessageBox::warning( 0, i18n(
"Idle Detection"),
00069 i18n(
"Desktop has been idle since %1."
00070
" What should we do?").arg(backThen),
00071 i18n(
"Revert && Stop"),
00072 i18n(
"Revert && Continue"),
00073 i18n(
"Continue Timing"),0,2);
00074
QDateTime end = QDateTime::currentDateTime();
00075
int diff = start.secsTo(end)/secsPerMinute;
00076
00077
if (
id == 0) {
00078
00079 emit(extractTime(idleMinutes+diff));
00080 emit(
stopAllTimers());
00081 }
00082
else if (
id == 1) {
00083
00084 emit(
extractTime(idleMinutes+diff));
00085 _timer->start(testInterval);
00086 }
00087
else {
00088
00089 _timer->start(testInterval);
00090 }
00091 }
00092
#endif // HAVE_LIBXSS
00093
00094 void IdleTimeDetector::startIdleDetection()
00095 {
00096
#ifdef HAVE_LIBXSS
00097
if (!_timer->isActive())
00098 _timer->start(testInterval);
00099
#endif //HAVE_LIBXSS
00100
}
00101
00102 void IdleTimeDetector::stopIdleDetection()
00103 {
00104
#ifdef HAVE_LIBXSS
00105
if (_timer->isActive())
00106 _timer->stop();
00107
#endif // HAVE_LIBXSS
00108
}
00109 void IdleTimeDetector::toggleOverAllIdleDetection(
bool on)
00110 {
00111 _overAllIdleDetect = on;
00112 }
00113
00114
#include "idletimedetector.moc"