karm

idletimedetector.cpp

00001 #include "idletimedetector.h"
00002 
00003 #include <tqdatetime.h>
00004 #include <tqmessagebox.h>
00005 #include <tqtimer.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) << "Entering IdleTimeDetector::IdleTimeDetector" << endl;
00014   _maxIdle = maxIdle;
00015 
00016 #ifdef HAVE_LIBXSS
00017   kdDebug(5970) << "IdleTimeDetector: LIBXSS detected @ compile time" << endl;
00018   int event_base, error_base;
00019   if(XScreenSaverQueryExtension(qt_xdisplay(), &event_base, &error_base)) 
00020   {
00021     _idleDetectionPossible = true;
00022   }
00023   else 
00024   {
00025     _idleDetectionPossible = false;
00026   }
00027 
00028   _timer = new TQTimer(this);
00029   connect(_timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(check()));
00030 #else
00031   _idleDetectionPossible = false;
00032 #endif // HAVE_LIBXSS
00033 
00034 }
00035 
00036 bool IdleTimeDetector::isIdleDetectionPossible()
00037 {
00038   return _idleDetectionPossible;
00039 }
00040 
00041 void IdleTimeDetector::check()
00042 {
00043   kdDebug(5970) << "Entering IdleTimeDetector::check" << endl;
00044 #ifdef HAVE_LIBXSS
00045   if (_idleDetectionPossible)
00046   {
00047     _mit_info = XScreenSaverAllocInfo ();
00048     XScreenSaverQueryInfo(qt_xdisplay(), qt_xrootwin(), _mit_info);
00049     int idleSeconds = (_mit_info->idle/1000);
00050     if (idleSeconds >= _maxIdle)
00051       informOverrun(idleSeconds);
00052   }
00053 #endif // HAVE_LIBXSS
00054 }
00055 
00056 void IdleTimeDetector::setMaxIdle(int maxIdle)
00057 {
00058   _maxIdle = maxIdle;
00059 }
00060 
00061 #ifdef HAVE_LIBXSS
00062 void IdleTimeDetector::informOverrun(int idleSeconds)
00063 {
00064   kdDebug(5970) << "Entering IdleTimeDetector::informOverrun" << endl;
00065   if (!_overAllIdleDetect)
00066     return; // preferences say the user does not want idle detection.
00067 
00068   _timer->stop();
00069 
00070   TQDateTime idleStart = TQDateTime::currentDateTime().addSecs(-idleSeconds);
00071   TQString idleStartQString = KGlobal::locale()->formatTime(idleStart.time());
00072 
00073   int id =  TQMessageBox::warning( 0, i18n("Idle Detection"),
00074                                      i18n("Desktop has been idle since %1."
00075                                           " What should we do?").arg(idleStartQString),
00076                                      i18n("Revert && Stop"),
00077                                      i18n("Revert && Continue"),
00078                                      i18n("Continue Timing"),0,2);
00079   TQDateTime end = TQDateTime::currentDateTime();
00080   int diff = idleStart.secsTo(end)/secsPerMinute;
00081 
00082   if (id == 0) 
00083   {
00084     // Revert And Stop
00085     kdDebug(5970) << "Now it is " << TQDateTime::currentDateTime() << endl;
00086     kdDebug(5970) << "Reverting timer to " << KGlobal::locale()->formatTime(idleStart.time()).ascii() << endl;
00087     emit(extractTime(idleSeconds/60+diff)); // we need to subtract the time that has been added during idleness.
00088     emit(stopAllTimersAt(idleStart));
00089   }
00090   else if (id == 1) 
00091   {
00092     // Revert and Continue
00093     emit(extractTime(idleSeconds/60+diff));
00094     _timer->start(testInterval);
00095   }
00096   else 
00097   {
00098     // Continue
00099     _timer->start(testInterval);
00100   }
00101 }
00102 #endif // HAVE_LIBXSS
00103 
00104 void IdleTimeDetector::startIdleDetection()
00105 {
00106   kdDebug(5970) << "Entering IdleTimeDetector::startIdleDetection" << endl; 
00107 #ifdef HAVE_LIBXSS
00108   kdDebug(5970) << "Starting Timer" << endl;
00109   if (!_timer->isActive())
00110     _timer->start(testInterval);
00111 #endif //HAVE_LIBXSS
00112 }
00113 
00114 void IdleTimeDetector::stopIdleDetection()
00115 {
00116 #ifdef HAVE_LIBXSS
00117   if (_timer->isActive())
00118     _timer->stop();
00119 #endif // HAVE_LIBXSS
00120 }
00121 void IdleTimeDetector::toggleOverAllIdleDetection(bool on)
00122 {
00123   _overAllIdleDetect = on;
00124 }
00125 
00126 #include "idletimedetector.moc"