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

KUtils

kidletime.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2009 Dario Freddi <drf at kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "kidletime.h"
00020 
00021 #include <config-kidletime.h>
00022 
00023 #ifdef Q_WS_X11
00024 #ifdef HAVE_XSCREENSAVER
00025 #include "xscreensaverbasedpoller.h"
00026 #endif
00027 #ifdef HAVE_XSYNC
00028 #include "xsyncbasedpoller.h"
00029 #endif
00030 #else
00031 #ifdef Q_WS_MAC
00032 #include "macpoller.h"
00033 #else
00034 #include "windowspoller.h"
00035 #endif
00036 #endif
00037 
00038 #include <QWeakPointer>
00039 #include <QSet>
00040 
00041 #include <kglobal.h>
00042 
00043 class KIdleTimeHelper
00044 {
00045 public:
00046     KIdleTimeHelper() : q(0) {}
00047     ~KIdleTimeHelper() {
00048         delete q;
00049     }
00050     KIdleTime *q;
00051 };
00052 
00053 K_GLOBAL_STATIC(KIdleTimeHelper, s_globalKIdleTime)
00054 
00055 KIdleTime *KIdleTime::instance()
00056 {
00057     if (!s_globalKIdleTime->q) {
00058         new KIdleTime;
00059     }
00060 
00061     return s_globalKIdleTime->q;
00062 }
00063 
00064 class KIdleTimePrivate
00065 {
00066     Q_DECLARE_PUBLIC(KIdleTime)
00067     KIdleTime *q_ptr;
00068 public:
00069     KIdleTimePrivate() : catchResume(false), currentId(0) {}
00070 
00071     void loadSystem();
00072     void unloadCurrentSystem();
00073     void _k_resumingFromIdle();
00074     void _k_timeoutReached(int msec);
00075 
00076     QWeakPointer<AbstractSystemPoller> poller;
00077     bool catchResume;
00078 
00079     int currentId;
00080     QHash<int, int> associations;
00081 };
00082 
00083 KIdleTime::KIdleTime()
00084         : QObject(0)
00085         , d_ptr(new KIdleTimePrivate())
00086 {
00087     Q_ASSERT(!s_globalKIdleTime->q);
00088     s_globalKIdleTime->q = this;
00089 
00090     d_ptr->q_ptr = this;
00091 
00092     Q_D(KIdleTime);
00093     d->loadSystem();
00094 
00095     connect(d->poller.data(), SIGNAL(resumingFromIdle()), this, SLOT(_k_resumingFromIdle()));
00096     connect(d->poller.data(), SIGNAL(timeoutReached(int)), this, SLOT(_k_timeoutReached(int)));
00097 }
00098 
00099 KIdleTime::~KIdleTime()
00100 {
00101     Q_D(KIdleTime);
00102     d->unloadCurrentSystem();
00103     delete d_ptr;
00104 }
00105 
00106 void KIdleTime::catchNextResumeEvent()
00107 {
00108     Q_D(KIdleTime);
00109 
00110     if (!d->catchResume) {
00111         d->catchResume = true;
00112         d->poller.data()->catchIdleEvent();
00113     }
00114 }
00115 
00116 void KIdleTime::stopCatchingResumeEvent()
00117 {
00118     Q_D(KIdleTime);
00119 
00120     if (d->catchResume) {
00121         d->catchResume = false;
00122         d->poller.data()->stopCatchingIdleEvents();
00123     }
00124 }
00125 
00126 int KIdleTime::addIdleTimeout(int msec)
00127 {
00128     Q_D(KIdleTime);
00129 
00130     d->poller.data()->addTimeout(msec);
00131 
00132     ++d->currentId;
00133     d->associations[d->currentId] = msec;
00134 
00135     return d->currentId;
00136 }
00137 
00138 void KIdleTime::removeIdleTimeout(int identifier)
00139 {
00140     Q_D(KIdleTime);
00141 
00142     if (!d->associations.contains(identifier)) {
00143         return;
00144     }
00145 
00146     int msec = d->associations[identifier];
00147 
00148     d->associations.remove(identifier);
00149 
00150     if (!d->associations.values().contains(msec)) {
00151         d->poller.data()->removeTimeout(msec);
00152     }
00153 }
00154 
00155 void KIdleTime::removeAllIdleTimeouts()
00156 {
00157     Q_D(KIdleTime);
00158 
00159     QHash< int, int >::iterator i = d->associations.begin();
00160     QSet< int > removed;
00161     removed.reserve(d->associations.size());
00162 
00163     while (i != d->associations.end()) {
00164         int msec = d->associations[i.key()];
00165 
00166         i = d->associations.erase(i);
00167 
00168         if (!removed.contains(msec)) {
00169             d->poller.data()->removeTimeout(msec);
00170             removed.insert(msec);
00171         }
00172     }
00173 }
00174 
00175 void KIdleTimePrivate::loadSystem()
00176 {
00177     if (!poller.isNull()) {
00178         unloadCurrentSystem();
00179     }
00180 
00181     // Priority order
00182 
00183 #ifdef Q_WS_X11
00184 #ifdef HAVE_XSYNC
00185 #ifdef HAVE_XSCREENSAVER
00186     if (XSyncBasedPoller::instance()->isAvailable()) {
00187         poller = XSyncBasedPoller::instance();
00188     } else {
00189         poller = new XScreensaverBasedPoller();
00190     }
00191 #else
00192     poller = XSyncBasedPoller::instance();
00193 #endif
00194 #else
00195 #ifdef HAVE_XSCREENSAVER
00196     poller = new XScreensaverBasedPoller();
00197 #endif
00198 #endif
00199 #else
00200 #ifdef Q_WS_MAC
00201     poller = new MacPoller();
00202 #else
00203     poller = new WindowsPoller();
00204 #endif
00205 #endif
00206 
00207     if (!poller.isNull()) {
00208         poller.data()->setUpPoller();
00209     }
00210 }
00211 
00212 void KIdleTimePrivate::unloadCurrentSystem()
00213 {
00214     if (!poller.isNull()) {
00215         poller.data()->unloadPoller();
00216 #ifdef Q_WS_X11
00217         if (qobject_cast<XSyncBasedPoller*>(poller.data()) == 0) {
00218 #endif
00219             poller.data()->deleteLater();
00220 #ifdef Q_WS_X11
00221         }
00222 #endif
00223     }
00224 }
00225 
00226 void KIdleTimePrivate::_k_resumingFromIdle()
00227 {
00228     Q_Q(KIdleTime);
00229 
00230     if (catchResume) {
00231         emit q->resumingFromIdle();
00232         q->stopCatchingResumeEvent();
00233     }
00234 }
00235 
00236 void KIdleTimePrivate::_k_timeoutReached(int msec)
00237 {
00238     Q_Q(KIdleTime);
00239 
00240     if (associations.values().contains(msec)) {
00241         foreach (int key, associations.keys(msec)) {
00242             emit q->timeoutReached(key);
00243             emit q->timeoutReached(key, msec);
00244         }
00245     }
00246 }
00247 
00248 void KIdleTime::simulateUserActivity()
00249 {
00250     Q_D(KIdleTime);
00251 
00252     d->poller.data()->simulateUserActivity();
00253 }
00254 
00255 int KIdleTime::idleTime() const
00256 {
00257     Q_D(const KIdleTime);
00258 
00259     return d->poller.data()->forcePollRequest();
00260 }
00261 
00262 QHash<int, int> KIdleTime::idleTimeouts() const
00263 {
00264     Q_D(const KIdleTime);
00265 
00266     return d->associations;
00267 }
00268 
00269 #include "kidletime.moc"

KUtils

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.7.3
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal