SVN_SILENT made messages (.desktop file)
[kdepim.git] / ktimetracker / idletimedetector.cpp
blob250dda55582f53cfc44242582dee7e07062f5358
1 /*
2 * Copyright (C) 2003 by Scott Monachello <smonach@cox.net>
3 * 2007 the ktimetracker developers
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the
17 * Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor
19 * Boston, MA 02110-1301 USA.
23 #include "idletimedetector.h"
25 #include <QDateTime>
26 #include <QHBoxLayout>
27 #include <QLabel>
28 #include <QTimer>
29 #include <QVBoxLayout>
31 #include <KDialog>
32 #include <KGlobal>
33 #include <KLocale> // i18n
35 #include <kdebug.h>
36 #include <KWindowSystem>
38 #ifdef Q_WS_X11
39 #include <QX11Info>
40 #endif
42 IdleTimeDetector::IdleTimeDetector(int maxIdle)
44 _maxIdle = maxIdle;
46 #if defined(HAVE_LIBXSS) && defined(Q_WS_X11)
47 int event_base, error_base;
48 if(XScreenSaverQueryExtension(QX11Info::display(), &event_base, &error_base)) _idleDetectionPossible = true;
49 else _idleDetectionPossible = false;
50 _timer = new QTimer(this);
51 connect(_timer, SIGNAL(timeout()), this, SLOT(check()));
52 #else
53 _idleDetectionPossible = false;
54 #endif // HAVE_LIBXSS
57 bool IdleTimeDetector::isIdleDetectionPossible()
59 return _idleDetectionPossible;
62 void IdleTimeDetector::check()
64 kDebug(5970) << "Entering function";
65 #if defined(HAVE_LIBXSS) && defined(Q_WS_X11)
66 kDebug(5970) << "kompiled for libxss and x11, idledetectionpossible is " << _idleDetectionPossible;
67 if (_idleDetectionPossible)
69 _mit_info = XScreenSaverAllocInfo();
70 XScreenSaverQueryInfo(QX11Info::display(), QX11Info::appRootWindow(), _mit_info);
71 idleminutes = (_mit_info->idle/1000)/secsPerMinute;
72 kDebug(5970) << "The desktop has been idle for " << idleminutes << " minutes.";
73 kDebug(5970) << "The idle time in miliseconds is " << _mit_info->idle;
74 if (idleminutes >= _maxIdle)
75 informOverrun();
77 #endif // HAVE_LIBXSS
80 void IdleTimeDetector::setMaxIdle(int maxIdle)
82 _maxIdle = maxIdle;
85 void IdleTimeDetector::revert()
87 // revert and stop
88 kDebug(5970) << "Entering function";
89 QDateTime end = QDateTime::currentDateTime();
90 int diff = start.secsTo(end)/secsPerMinute;
91 emit(subtractTime(idleminutes+diff)); // subtract the time that has been added on the display
92 emit(stopAllTimers(idlestart));
95 #if defined(HAVE_LIBXSS) && defined(Q_WS_X11)
96 void IdleTimeDetector::informOverrun()
98 if (!_overAllIdleDetect)
99 return; // In the preferences the user has indicated that he does not want idle detection.
101 _timer->stop();
102 start = QDateTime::currentDateTime();
103 idlestart = start.addSecs(-60 * _maxIdle);
104 QString backThen = KGlobal::locale()->formatTime(idlestart.time());
105 // Create dialog
106 KDialog *dialog=new KDialog(0);
107 QWidget* wid=new QWidget(dialog);
108 dialog->setMainWidget(wid);
109 QVBoxLayout *lay1 = new QVBoxLayout(wid);
110 QHBoxLayout *lay2 = new QHBoxLayout();
111 lay1->addLayout(lay2);
112 QString idlemsg= i18n("Desktop has been idle since %1. What do you want to do ?", backThen);
113 QLabel *label = new QLabel( idlemsg, wid );
114 lay2->addWidget( label );
115 connect( dialog , SIGNAL(cancelClicked()) , this , SLOT(revert()) );
116 connect( wid , SIGNAL(changed(bool)) , wid , SLOT(enabledButtonApply(bool)) );
117 QString explanation=i18n("Continue timing. Timing has started at %1", backThen);
118 QString explanationrevert=i18n("Stop timing and revert back to the time at %1.", backThen);
119 dialog->setButtonText(KDialog::Ok, i18n("Continue timing."));
120 dialog->setButtonText(KDialog::Cancel, i18n("Revert timing"));
121 dialog->setButtonWhatsThis(KDialog::Ok, explanation);
122 dialog->setButtonWhatsThis(KDialog::Cancel, explanationrevert);
123 // The user might be looking at another virtual desktop as where ktimetracker is running
124 KWindowSystem::self()->setOnDesktop( dialog->winId(), KWindowSystem::self()->currentDesktop() );
125 KWindowSystem::self()->demandAttention( dialog->winId() );
126 kDebug(5970) << "Setting WinId " << dialog->winId() << " to deskTop " << KWindowSystem::self()->currentDesktop();
127 dialog->show();
129 #endif // HAVE_LIBXSS
131 void IdleTimeDetector::startIdleDetection()
133 #if defined(HAVE_LIBXSS) && defined(Q_WS_X11)
134 if (!_timer->isActive())
135 _timer->start(testInterval);
136 #endif //HAVE_LIBXSS
139 void IdleTimeDetector::stopIdleDetection()
141 #if defined(HAVE_LIBXSS) && defined(Q_WS_X11)
142 if (_timer->isActive())
143 _timer->stop();
144 #endif // HAVE_LIBXSS
147 void IdleTimeDetector::toggleOverAllIdleDetection(bool on)
149 _overAllIdleDetect = on;