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; // preferences say the user does not want idle detection.
00063 
00064   _timer->stop();
00065 
00066   QDateTime start = QDateTime::currentDateTime();
00067   QDateTime idleStart = start.addSecs(-60 * _maxIdle);
00068   QString backThen = KGlobal::locale()->formatTime(idleStart.time());
00069 
00070   int id =  QMessageBox::warning( 0, i18n("Idle Detection"),
00071                                      i18n("Desktop has been idle since %1."
00072                                           " What should we do?").arg(backThen),
00073                                      i18n("Revert && Stop"),
00074                                      i18n("Revert && Continue"),
00075                                      i18n("Continue Timing"),0,2);
00076   QDateTime end = QDateTime::currentDateTime();
00077   int diff = start.secsTo(end)/secsPerMinute;
00078 
00079   if (id == 0) 
00080   {
00081     // Revert And Stop
00082     kdDebug(5970) << "Now it is " << QDateTime::currentDateTime() << endl;
00083     kdDebug(5970) << "Reverting timer to " << KGlobal::locale()->formatTime(idleStart.time()).ascii() << endl;
00084     emit(extractTime(idleMinutes+diff)); // we need to subtract the time that has been added during idleness.
00085     emit(stopAllTimersAt(idleStart));
00086   }
00087   else if (id == 1) 
00088   {
00089     // Revert and Continue
00090     emit(extractTime(idleMinutes+diff));
00091     _timer->start(testInterval);
00092   }
00093   else 
00094   {
00095     // Continue
00096     _timer->start(testInterval);
00097   }
00098 }
00099 #endif // HAVE_LIBXSS
00100 
00101 void IdleTimeDetector::startIdleDetection()
00102 {
00103   kdDebug(5970) << "Entering IdleTimeDetector::startIdleDetection" << endl; 
00104 #ifdef HAVE_LIBXSS
00105   kdDebug(5970) << "Starting Timer" << endl;
00106   if (!_timer->isActive())
00107     _timer->start(testInterval);
00108 #endif //HAVE_LIBXSS
00109 }
00110 
00111 void IdleTimeDetector::stopIdleDetection()
00112 {
00113 #ifdef HAVE_LIBXSS
00114   if (_timer->isActive())
00115     _timer->stop();
00116 #endif // HAVE_LIBXSS
00117 }
00118 void IdleTimeDetector::toggleOverAllIdleDetection(bool on)
00119 {
00120   _overAllIdleDetect = on;
00121 }
00122 
00123 #include "idletimedetector.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys