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

KDEUI

knotificationrestrictions.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright (C) 2006 Aaron Seigo <aseigo@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 as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "knotificationrestrictions.h"
00021 
00022 #include <kaboutdata.h>
00023 #include <kcomponentdata.h>
00024 #include <kdebug.h>
00025 #include <kglobal.h>
00026 #include <klocale.h>
00027 
00028 #include <QtGui/QApplication>
00029 #include <QtDBus/QDBusConnection>
00030 #include <QtDBus/QDBusMessage>
00031 #include <QtDBus/QDBusReply>
00032 
00033 #include <config.h>
00034 
00035 #ifdef HAVE_XTEST
00036 #include <QTimer>
00037 #include <QX11Info>
00038 
00039 #include <X11/keysym.h>
00040 #include <X11/extensions/XTest.h>
00041 #endif // HAVE_XTEST
00042 
00043 class KNotificationRestrictions::Private
00044 {
00045     public:
00046         Private( KNotificationRestrictions* qq, Services c )
00047             : q( qq ),
00048               control(c)
00049             , screenSaverDbusCookie(-1)
00050 #ifdef HAVE_XTEST
00051              ,screensaverTimer(0),
00052               haveXTest(0),
00053               XTestKeyCode(0)
00054 #endif // HAVE_XTEST
00055         {
00056         }
00057 
00058         void screensaverFakeKeyEvent();
00059         void startScreenSaverPrevention();
00060         void stopScreenSaverPrevention();
00061 
00062         static QString determineProgramName();
00063 
00064         KNotificationRestrictions* q;
00065         Services control;
00066         int screenSaverDbusCookie;
00067         QString reason;
00068 #ifdef HAVE_XTEST
00069         QTimer* screensaverTimer;
00070         int haveXTest;
00071         int XTestKeyCode;
00072 #endif // HAVE_XTEST
00073 };
00074 
00075 KNotificationRestrictions::KNotificationRestrictions( Services control,
00076                                                       QObject* parent )
00077     : QObject(parent),
00078       d( new Private( this, control ) )
00079 {
00080     if (d->control & ScreenSaver) {
00081         d->startScreenSaverPrevention();
00082     }
00083 }
00084 
00085 KNotificationRestrictions::~KNotificationRestrictions()
00086 {
00087     if (d->control & ScreenSaver) {
00088         d->stopScreenSaverPrevention();
00089     }
00090 
00091     delete d;
00092 }
00093 
00094 void KNotificationRestrictions::Private::screensaverFakeKeyEvent()
00095 {
00096     kDebug(297);
00097 #ifdef HAVE_XTEST
00098     kDebug(297) << "---- using XTestFakeKeyEvent";
00099     Display* display = QX11Info::display();
00100     XTestFakeKeyEvent(display, XTestKeyCode, true, CurrentTime);
00101     XTestFakeKeyEvent(display, XTestKeyCode, false, CurrentTime);
00102     XSync(display, false);
00103 #endif // HAVE_XTEST
00104 }
00105 
00106 void KNotificationRestrictions::Private::startScreenSaverPrevention()
00107 {
00108     kDebug(297);
00109     QDBusMessage message = QDBusMessage::createMethodCall(
00110             "org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", "Inhibit");
00111     message << determineProgramName();
00112     message << reason;
00113     QDBusReply<uint> reply = QDBusConnection::sessionBus().call(message);
00114     if (reply.isValid()) {
00115         screenSaverDbusCookie = reply.value();
00116         return;
00117     }
00118 #ifdef HAVE_XTEST
00119     if ( !haveXTest ) {
00120         int a,b,c,e;
00121         haveXTest = XTestQueryExtension(QX11Info::display(), &a, &b, &c, &e);
00122 
00123         if ( !haveXTest ) {
00124             kDebug(297) << "--- No XTEST!";
00125             return;
00126         }
00127     }
00128 
00129     if ( !XTestKeyCode ) {
00130         XTestKeyCode = XKeysymToKeycode(QX11Info::display(), XK_Shift_L);
00131 
00132         if ( !XTestKeyCode ) {
00133             kDebug(297) << "--- No XKeyCode for XK_Shift_L!";
00134             return;
00135         }
00136     }
00137 
00138     if ( !screensaverTimer ) {
00139         screensaverTimer = new QTimer( q );
00140         connect( screensaverTimer, SIGNAL(timeout()),
00141                  q, SLOT(screensaverFakeKeyEvent()) );
00142     }
00143 
00144     kDebug(297) << "---- using XTest";
00145     // send a fake event right away in case this got started after a period of
00146     // innactivity leading to the screensaver set to activate in <55s
00147     screensaverFakeKeyEvent();
00148     screensaverTimer->start( 55000 );
00149 #endif // HAVE_XTEST
00150 }
00151 
00152 void KNotificationRestrictions::Private::stopScreenSaverPrevention()
00153 {
00154     if (screenSaverDbusCookie != -1) {
00155         QDBusMessage message = QDBusMessage::createMethodCall(
00156                 "org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", "UnInhibit");
00157         message << static_cast<uint>(screenSaverDbusCookie);
00158         screenSaverDbusCookie = -1;
00159         if (QDBusConnection::sessionBus().send(message)) {
00160             return;
00161         }
00162     }
00163 #ifdef HAVE_XTEST
00164     delete screensaverTimer;
00165     screensaverTimer = 0;
00166 #endif // HAVE_XTEST
00167 }
00168 
00169 QString KNotificationRestrictions::Private::determineProgramName()
00170 {
00171     QString appName;
00172     if (KGlobal::mainComponent().isValid()) {
00173         appName = KGlobal::mainComponent().aboutData()->programName();
00174     }
00175     if (appName.isEmpty() && qApp) {
00176         appName = QCoreApplication::applicationName();
00177     }
00178     if (appName.isEmpty()) {
00179         appName = i18n("Unknown Application");
00180     }
00181     return appName;
00182 }
00183 
00184 #include "knotificationrestrictions.moc"

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • 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
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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