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"
26 #include <QHBoxLayout>
29 #include <QVBoxLayout>
33 #include <KLocale> // i18n
36 #include <KWindowSystem>
42 IdleTimeDetector::IdleTimeDetector(int 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()));
53 _idleDetectionPossible
= false;
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
)
80 void IdleTimeDetector::setMaxIdle(int maxIdle
)
85 void IdleTimeDetector::revert()
88 kDebug(5970) << "Entering function";
89 QDateTime end
= QDateTime::currentDateTime();
90 int diff
= start
.secsTo(end
)/secsPerMinute
;
91 emit(extractTime(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 do not
100 // want idle detection.
103 start
= QDateTime::currentDateTime();
104 idlestart
= start
.addSecs(-60 * _maxIdle
);
105 QString backThen
= KGlobal::locale()->formatTime(idlestart
.time());
107 KDialog
*dialog
=new KDialog( 0 );
108 QWidget
* wid
=new QWidget(dialog
);
109 dialog
->setMainWidget( wid
);
110 QVBoxLayout
*lay1
= new QVBoxLayout(wid
);
111 QHBoxLayout
*lay2
= new QHBoxLayout();
112 lay1
->addLayout(lay2
);
113 QString idlemsg
=QString( "Desktop has been idle since %1. What do you want to do ?" ).arg(backThen
);
114 QLabel
*label
= new QLabel( idlemsg
, wid
);
115 lay2
->addWidget( label
);
116 connect( dialog
, SIGNAL(cancelClicked()) , this , SLOT(revert()) );
117 connect( wid
, SIGNAL(changed(bool)) , wid
, SLOT(enabledButtonApply(bool)) );
118 QString explanation
=i18n("Continue timing. Timing has started at %1", backThen
);
119 QString explanationrevert
=i18n("Stop timing and revert back to the time at %1.", backThen
);
120 dialog
->setButtonText(KDialog::Ok
, i18n("Continue timing."));
121 dialog
->setButtonText(KDialog::Cancel
, i18n("Revert timing"));
122 dialog
->setButtonWhatsThis(KDialog::Ok
, explanation
);
123 dialog
->setButtonWhatsThis(KDialog::Cancel
, explanationrevert
);
124 // The user might be looking at another virtual desktop as where ktimetracker is running
125 KWindowSystem::self()->setOnDesktop( dialog
->winId(), KWindowSystem::self()->currentDesktop() );
126 KWindowSystem::self()->demandAttention( dialog
->winId() );
127 kDebug(5970) << "Setting WinId " << dialog
->winId() << " to deskTop " << KWindowSystem::self()->currentDesktop();
130 #endif // HAVE_LIBXSS
132 void IdleTimeDetector::startIdleDetection()
134 #if defined(HAVE_LIBXSS) && defined(Q_WS_X11)
135 if (!_timer
->isActive())
136 _timer
->start(testInterval
);
140 void IdleTimeDetector::stopIdleDetection()
142 #if defined(HAVE_LIBXSS) && defined(Q_WS_X11)
143 if (_timer
->isActive())
145 #endif // HAVE_LIBXSS
148 void IdleTimeDetector::toggleOverAllIdleDetection(bool on
)
150 _overAllIdleDetect
= on
;
153 #include "idletimedetector.moc"