• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • kdelibs
  • KDE Home
  • 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     
00110     QDBusMessage message = QDBusMessage::createMethodCall(
00111             "org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", "Inhibit");
00112     message << determineProgramName();
00113     message << reason;
00114     QDBusReply<uint> reply = QDBusConnection::sessionBus().call(message);
00115     if (reply.isValid()) {
00116         screenSaverDbusCookie = reply.value();
00117         return;
00118     }
00119 #ifdef HAVE_XTEST
00120     if ( !haveXTest ) {
00121         int a,b,c,e;
00122         haveXTest = XTestQueryExtension(QX11Info::display(), &a, &b, &c, &e);
00123 
00124         if ( !haveXTest ) {
00125             kDebug(297) << "--- No XTEST!";
00126             return;
00127         }
00128     }
00129 
00130     if ( !XTestKeyCode ) {
00131         XTestKeyCode = XKeysymToKeycode(QX11Info::display(), XK_Shift_L);
00132 
00133         if ( !XTestKeyCode ) {
00134             kDebug(297) << "--- No XKeyCode for XK_Shift_L!";
00135             return;
00136         }
00137     }
00138 
00139     if ( !screensaverTimer ) {
00140         screensaverTimer = new QTimer( q );
00141         connect( screensaverTimer, SIGNAL(timeout()),
00142                  q, SLOT(screensaverFakeKeyEvent()) );
00143     }
00144 
00145     kDebug(297) << "---- using XTest";
00146     // send a fake event right away in case this got started after a period of
00147     // innactivity leading to the screensaver set to activate in <55s
00148     screensaverFakeKeyEvent();
00149     screensaverTimer->start( 55000 );
00150 #endif // HAVE_XTEST
00151 }
00152 
00153 void KNotificationRestrictions::Private::stopScreenSaverPrevention()
00154 {
00155   
00156     if (screenSaverDbusCookie != -1) {
00157         QDBusMessage message = QDBusMessage::createMethodCall(
00158                 "org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", "UnInhibit");
00159         message << static_cast<uint>(screenSaverDbusCookie);
00160         screenSaverDbusCookie = -1;
00161         if (QDBusConnection::sessionBus().send(message)) {
00162             return;
00163         }
00164     }
00165 #ifdef HAVE_XTEST
00166     delete screensaverTimer;
00167     screensaverTimer = 0;
00168 #endif // HAVE_XTEST
00169 }
00170 
00171 QString KNotificationRestrictions::Private::determineProgramName()
00172 {
00173     QString appName;
00174     if (KGlobal::mainComponent().isValid()) {
00175         appName = KGlobal::mainComponent().aboutData()->programName();
00176     }
00177     if (appName.isEmpty() && qApp) {
00178         appName = QCoreApplication::applicationName();
00179     }
00180     if (appName.isEmpty()) {
00181         appName = i18n("Unknown Application");
00182     }
00183     return appName;
00184 }
00185 
00186 #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
  • 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