• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KUtils

  • sources
  • kde-4.12
  • kdelibs
  • kutils
  • kidletime
kidletime.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2009 Dario Freddi <drf at kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "kidletime.h"
20 
21 #include <config-kidletime.h>
22 
23 #ifdef Q_WS_X11
24 #ifdef HAVE_XSCREENSAVER
25 #include "xscreensaverbasedpoller.h"
26 #endif
27 #ifdef HAVE_XSYNC
28 #include "xsyncbasedpoller.h"
29 #endif
30 #else
31 #ifdef Q_WS_MAC
32 #include "macpoller.h"
33 #else
34 #include "windowspoller.h"
35 #endif
36 #endif
37 
38 #include <QWeakPointer>
39 #include <QSet>
40 
41 #include <kglobal.h>
42 
43 class KIdleTimeHelper
44 {
45 public:
46  KIdleTimeHelper() : q(0) {}
47  ~KIdleTimeHelper() {
48  delete q;
49  }
50  KIdleTime *q;
51 };
52 
53 K_GLOBAL_STATIC(KIdleTimeHelper, s_globalKIdleTime)
54 
55 KIdleTime *KIdleTime::instance()
56 {
57  if (!s_globalKIdleTime->q) {
58  new KIdleTime;
59  }
60 
61  return s_globalKIdleTime->q;
62 }
63 
64 class KIdleTimePrivate
65 {
66  Q_DECLARE_PUBLIC(KIdleTime)
67  KIdleTime *q_ptr;
68 public:
69  KIdleTimePrivate() : catchResume(false), currentId(0) {}
70 
71  void loadSystem();
72  void unloadCurrentSystem();
73  void _k_resumingFromIdle();
74  void _k_timeoutReached(int msec);
75 
76  QWeakPointer<AbstractSystemPoller> poller;
77  bool catchResume;
78 
79  int currentId;
80  QHash<int, int> associations;
81 };
82 
83 KIdleTime::KIdleTime()
84  : QObject(0)
85  , d_ptr(new KIdleTimePrivate())
86 {
87  Q_ASSERT(!s_globalKIdleTime->q);
88  s_globalKIdleTime->q = this;
89 
90  d_ptr->q_ptr = this;
91 
92  Q_D(KIdleTime);
93  d->loadSystem();
94 
95  connect(d->poller.data(), SIGNAL(resumingFromIdle()), this, SLOT(_k_resumingFromIdle()));
96  connect(d->poller.data(), SIGNAL(timeoutReached(int)), this, SLOT(_k_timeoutReached(int)));
97 }
98 
99 KIdleTime::~KIdleTime()
100 {
101  Q_D(KIdleTime);
102  d->unloadCurrentSystem();
103  delete d_ptr;
104 }
105 
106 void KIdleTime::catchNextResumeEvent()
107 {
108  Q_D(KIdleTime);
109 
110  if (!d->catchResume) {
111  d->catchResume = true;
112  d->poller.data()->catchIdleEvent();
113  }
114 }
115 
116 void KIdleTime::stopCatchingResumeEvent()
117 {
118  Q_D(KIdleTime);
119 
120  if (d->catchResume) {
121  d->catchResume = false;
122  d->poller.data()->stopCatchingIdleEvents();
123  }
124 }
125 
126 int KIdleTime::addIdleTimeout(int msec)
127 {
128  Q_D(KIdleTime);
129 
130  d->poller.data()->addTimeout(msec);
131 
132  ++d->currentId;
133  d->associations[d->currentId] = msec;
134 
135  return d->currentId;
136 }
137 
138 void KIdleTime::removeIdleTimeout(int identifier)
139 {
140  Q_D(KIdleTime);
141 
142  if (!d->associations.contains(identifier)) {
143  return;
144  }
145 
146  int msec = d->associations[identifier];
147 
148  d->associations.remove(identifier);
149 
150  if (!d->associations.values().contains(msec)) {
151  d->poller.data()->removeTimeout(msec);
152  }
153 }
154 
155 void KIdleTime::removeAllIdleTimeouts()
156 {
157  Q_D(KIdleTime);
158 
159  QHash< int, int >::iterator i = d->associations.begin();
160  QSet< int > removed;
161  removed.reserve(d->associations.size());
162 
163  while (i != d->associations.end()) {
164  int msec = d->associations[i.key()];
165 
166  i = d->associations.erase(i);
167 
168  if (!removed.contains(msec)) {
169  d->poller.data()->removeTimeout(msec);
170  removed.insert(msec);
171  }
172  }
173 }
174 
175 void KIdleTimePrivate::loadSystem()
176 {
177  if (!poller.isNull()) {
178  unloadCurrentSystem();
179  }
180 
181  // Priority order
182 
183 #ifdef Q_WS_X11
184 #ifdef HAVE_XSYNC
185 #ifdef HAVE_XSCREENSAVER
186  if (XSyncBasedPoller::instance()->isAvailable()) {
187  poller = XSyncBasedPoller::instance();
188  } else {
189  poller = new XScreensaverBasedPoller();
190  }
191 #else
192  poller = XSyncBasedPoller::instance();
193 #endif
194 #else
195 #ifdef HAVE_XSCREENSAVER
196  poller = new XScreensaverBasedPoller();
197 #endif
198 #endif
199 #else
200 #ifdef Q_WS_MAC
201  poller = new MacPoller();
202 #else
203  poller = new WindowsPoller();
204 #endif
205 #endif
206 
207  if (!poller.isNull()) {
208  poller.data()->setUpPoller();
209  }
210 }
211 
212 void KIdleTimePrivate::unloadCurrentSystem()
213 {
214  if (!poller.isNull()) {
215  poller.data()->unloadPoller();
216 #ifdef Q_WS_X11
217  if (qobject_cast<XSyncBasedPoller*>(poller.data()) == 0) {
218 #endif
219  poller.data()->deleteLater();
220 #ifdef Q_WS_X11
221  }
222 #endif
223  }
224 }
225 
226 void KIdleTimePrivate::_k_resumingFromIdle()
227 {
228  Q_Q(KIdleTime);
229 
230  if (catchResume) {
231  emit q->resumingFromIdle();
232  q->stopCatchingResumeEvent();
233  }
234 }
235 
236 void KIdleTimePrivate::_k_timeoutReached(int msec)
237 {
238  Q_Q(KIdleTime);
239 
240  if (associations.values().contains(msec)) {
241  foreach (int key, associations.keys(msec)) {
242  emit q->timeoutReached(key);
243  emit q->timeoutReached(key, msec);
244  }
245  }
246 }
247 
248 void KIdleTime::simulateUserActivity()
249 {
250  Q_D(KIdleTime);
251 
252  d->poller.data()->simulateUserActivity();
253 }
254 
255 int KIdleTime::idleTime() const
256 {
257  Q_D(const KIdleTime);
258 
259  return d->poller.data()->forcePollRequest();
260 }
261 
262 QHash<int, int> KIdleTime::idleTimeouts() const
263 {
264  Q_D(const KIdleTime);
265 
266  return d->associations;
267 }
268 
269 #include "kidletime.moc"
KIdleTime
KIdleTime is a singleton reporting information on idle time.
Definition: kidletime.h:39
KIdleTime::removeAllIdleTimeouts
void removeAllIdleTimeouts()
Stops catching every set timeout (if any).
Definition: kidletime.cpp:155
KIdleTime::stopCatchingResumeEvent
void stopCatchingResumeEvent()
Stops listening for resume event.
Definition: kidletime.cpp:116
xsyncbasedpoller.h
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
KIdleTime::~KIdleTime
virtual ~KIdleTime()
The destructor.
Definition: kidletime.cpp:99
QHash< int, int >
QObject
WindowsPoller
Definition: windowspoller.h:26
kglobal.h
KIdleTime::simulateUserActivity
void simulateUserActivity()
Attempts to simulate user activity.
Definition: kidletime.cpp:248
KIdleTime::catchNextResumeEvent
void catchNextResumeEvent()
Catches the next resume from idle event.
Definition: kidletime.cpp:106
XSyncBasedPoller::instance
static XSyncBasedPoller * instance()
Definition: xsyncbasedpoller.cpp:40
xscreensaverbasedpoller.h
KIdleTime::timeoutReached
void timeoutReached(int identifier)
Triggered when the system has been idle for x milliseconds, identified by the previously set timeout...
KIdleTime::idleTimeouts
QHash< int, int > idleTimeouts() const
Returns the list of timeout identifiers associated with their duration, in milliseconds, the library is currently listening to.
Definition: kidletime.cpp:262
QSet
KIdleTime::removeIdleTimeout
void removeIdleTimeout(int identifier)
Stops catching the idle timeout identified by the token identifier, if it was registered earlier with...
Definition: kidletime.cpp:138
MacPoller
Definition: macpoller.h:26
KIdleTime::idleTime
int idleTime() const
Retrieves the idle time of the system, in milliseconds.
Definition: kidletime.cpp:255
macpoller.h
kidletime.h
windowspoller.h
KIdleTime::addIdleTimeout
int addIdleTimeout(int msec)
Adds a new timeout to catch.
Definition: kidletime.cpp:126
XScreensaverBasedPoller
Definition: xscreensaverbasedpoller.h:26
KIdleTime::resumingFromIdle
void resumingFromIdle()
Triggered, if KIdleTime is catching resume events, when the system resumes from an idle state...
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:34 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KUtils

Skip menu "KUtils"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal