21 #include <config-kidletime.h>
24 #ifdef HAVE_XSCREENSAVER
38 #include <QWeakPointer>
46 KIdleTimeHelper() : q(0) {}
57 if (!s_globalKIdleTime->q) {
61 return s_globalKIdleTime->q;
64 class KIdleTimePrivate
69 KIdleTimePrivate() : catchResume(false), currentId(0) {}
72 void unloadCurrentSystem();
73 void _k_resumingFromIdle();
74 void _k_timeoutReached(
int msec);
76 QWeakPointer<AbstractSystemPoller> poller;
83 KIdleTime::KIdleTime()
85 , d_ptr(new KIdleTimePrivate())
87 Q_ASSERT(!s_globalKIdleTime->q);
88 s_globalKIdleTime->q =
this;
95 connect(d->poller.data(), SIGNAL(
resumingFromIdle()),
this, SLOT(_k_resumingFromIdle()));
96 connect(d->poller.data(), SIGNAL(
timeoutReached(
int)),
this, SLOT(_k_timeoutReached(
int)));
102 d->unloadCurrentSystem();
110 if (!d->catchResume) {
111 d->catchResume =
true;
112 d->poller.data()->catchIdleEvent();
120 if (d->catchResume) {
121 d->catchResume =
false;
122 d->poller.data()->stopCatchingIdleEvents();
130 d->poller.data()->addTimeout(msec);
133 d->associations[d->currentId] = msec;
142 if (!d->associations.contains(identifier)) {
146 int msec = d->associations[identifier];
148 d->associations.remove(identifier);
150 if (!d->associations.values().contains(msec)) {
151 d->poller.data()->removeTimeout(msec);
161 removed.reserve(d->associations.size());
163 while (i != d->associations.end()) {
164 int msec = d->associations[i.key()];
166 i = d->associations.erase(i);
168 if (!removed.contains(msec)) {
169 d->poller.data()->removeTimeout(msec);
170 removed.insert(msec);
175 void KIdleTimePrivate::loadSystem()
177 if (!poller.isNull()) {
178 unloadCurrentSystem();
185 #ifdef HAVE_XSCREENSAVER
195 #ifdef HAVE_XSCREENSAVER
207 if (!poller.isNull()) {
208 poller.data()->setUpPoller();
212 void KIdleTimePrivate::unloadCurrentSystem()
214 if (!poller.isNull()) {
215 poller.data()->unloadPoller();
217 if (qobject_cast<XSyncBasedPoller*>(poller.data()) == 0) {
219 poller.data()->deleteLater();
226 void KIdleTimePrivate::_k_resumingFromIdle()
231 emit q->resumingFromIdle();
232 q->stopCatchingResumeEvent();
236 void KIdleTimePrivate::_k_timeoutReached(
int msec)
240 if (associations.values().contains(msec)) {
241 foreach (
int key, associations.keys(msec)) {
242 emit q->timeoutReached(key);
243 emit q->timeoutReached(key, msec);
252 d->poller.data()->simulateUserActivity();
259 return d->poller.data()->forcePollRequest();
266 return d->associations;
269 #include "kidletime.moc"
KIdleTime is a singleton reporting information on idle time.
void removeAllIdleTimeouts()
Stops catching every set timeout (if any).
void stopCatchingResumeEvent()
Stops listening for resume event.
#define K_GLOBAL_STATIC(TYPE, NAME)
virtual ~KIdleTime()
The destructor.
void simulateUserActivity()
Attempts to simulate user activity.
void catchNextResumeEvent()
Catches the next resume from idle event.
static XSyncBasedPoller * instance()
void timeoutReached(int identifier)
Triggered when the system has been idle for x milliseconds, identified by the previously set timeout...
QHash< int, int > idleTimeouts() const
Returns the list of timeout identifiers associated with their duration, in milliseconds, the library is currently listening to.
void removeIdleTimeout(int identifier)
Stops catching the idle timeout identified by the token identifier, if it was registered earlier with...
int idleTime() const
Retrieves the idle time of the system, in milliseconds.
int addIdleTimeout(int msec)
Adds a new timeout to catch.
void resumingFromIdle()
Triggered, if KIdleTime is catching resume events, when the system resumes from an idle state...