karm

idletimedetector.cpp

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>    // i18n
00009 
00010 IdleTimeDetector::IdleTimeDetector(int maxIdle)
00011 // Trigger a warning after maxIdle minutes
00012 {
00013   kdDebug(5970) << "IdleTimeDetector::IdleTimeDetector" << endl;
00014   _maxIdle = maxIdle;
00015 
00016 #ifdef HAVE_LIBXSS
00017   int event_base, error_base;
00018   if(XScreenSaverQueryExtension(qt_xdisplay(), &event_base, &error_base)) {
00019     _idleDetectionPossible = true;
00020   }
00021   else {
00022     _idleDetectionPossible = false;
00023   }
00024 
00025   _timer = new QTimer(this);
00026   connect(_timer, SIGNAL(timeout()), this, SLOT(check()));
00027 #else
00028   _idleDetectionPossible = false;
00029 #endif // HAVE_LIBXSS
00030 
00031 }
00032 
00033 bool IdleTimeDetector::isIdleDetectionPossible()
00034 {
00035   return _idleDetectionPossible;
00036 }
00037 
00038 void IdleTimeDetector::check()
00039 {
00040   kdDebug(5970) << "Entering IdleTimeDetector::check" << endl;
00041 #ifdef HAVE_LIBXSS
00042   if (_idleDetectionPossible)
00043   {
00044     _mit_info = XScreenSaverAllocInfo ();
00045     XScreenSaverQueryInfo(qt_xdisplay(), qt_xrootwin(), _mit_info);
00046     int idleMinutes = (_mit_info->idle/1000)/secsPerMinute;
00047     if (idleMinutes >= _maxIdle)
00048       informOverrun(idleMinutes);
00049   }
00050 #endif // HAVE_LIBXSS
00051 }
00052 
00053 void IdleTimeDetector::setMaxIdle(int maxIdle)
00054 {
00055   _maxIdle = maxIdle;
00056 }
00057 
00058 #ifdef HAVE_LIBXSS
00059 void IdleTimeDetector::informOverrun(int idleMinutes)
00060 {
00061   if (!_overAllIdleDetect)
00062     return; // In the preferences the user has indicated that he do not
00063             // want idle detection.
00064 
00065   _timer->stop();
00066 
00067   QDateTime start = QDateTime::currentDateTime();
00068   QDateTime idleStart = start.addSecs(-60 * _maxIdle);
00069   QString backThen = KGlobal::locale()->formatTime(idleStart.time());
00070 
00071   int id =  QMessageBox::warning( 0, i18n("Idle Detection"),
00072                                      i18n("Desktop has been idle since %1."
00073                                           " What should we do?").arg(backThen),
00074                                      i18n("Revert && Stop"),
00075                                      i18n("Revert && Continue"),
00076                                      i18n("Continue Timing"),0,2);
00077   QDateTime end = QDateTime::currentDateTime();
00078   int diff = start.secsTo(end)/secsPerMinute;
00079 
00080   if (id == 0) {
00081     // Revert And Stop
00082     kdDebug(5970) << "Now it is " << KGlobal::locale()->formatTime(QDateTime::currentDateTime().time()).ascii() << endl;
00083     kdDebug(5970) << "Reverting timer to " << KGlobal::locale()->formatTime(idleStart.time()).ascii() << endl;
00084     emit(stopAllTimersAt(idleStart));
00085   }
00086   else if (id == 1) {
00087     // Revert and Continue
00088     emit(extractTime(idleMinutes+diff));
00089     _timer->start(testInterval);
00090   }
00091   else {
00092     // Continue
00093     _timer->start(testInterval);
00094   }
00095 }
00096 #endif // HAVE_LIBXSS
00097 
00098 void IdleTimeDetector::startIdleDetection()
00099 {
00100   kdDebug(5970) << "Entering IdleTimeDetector::startIdleDetection" << endl; 
00101 #ifdef HAVE_LIBXSS
00102   kdDebug(5970) << "Starting Timer" << endl;
00103   if (!_timer->isActive())
00104     _timer->start(testInterval);
00105 #endif //HAVE_LIBXSS
00106 }
00107 
00108 void IdleTimeDetector::stopIdleDetection()
00109 {
00110 #ifdef HAVE_LIBXSS
00111   if (_timer->isActive())
00112     _timer->stop();
00113 #endif // HAVE_LIBXSS
00114 }
00115 void IdleTimeDetector::toggleOverAllIdleDetection(bool on)
00116 {
00117   _overAllIdleDetect = on;
00118 }
00119 
00120 #include "idletimedetector.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys