fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kdeui / util / knotificationrestrictions.cpp
blob2017d6ed06377a314ff7239538314a98c5cb42ed
1 /* This file is part of the KDE libraries
2 Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
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 as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "knotificationrestrictions.h"
22 #include <kaboutdata.h>
23 #include <kcomponentdata.h>
24 #include <kdebug.h>
25 #include <kglobal.h>
26 #include <klocale.h>
28 #include <QtGui/QApplication>
29 #include <QtDBus/QDBusConnection>
30 #include <QtDBus/QDBusMessage>
31 #include <QtDBus/QDBusReply>
33 #include <config.h>
35 #ifdef HAVE_XTEST
36 #include <QTimer>
37 #include <QX11Info>
39 #include <X11/keysym.h>
40 #include <X11/extensions/XTest.h>
41 #endif // HAVE_XTEST
43 class KNotificationRestrictions::Private
45 public:
46 Private( KNotificationRestrictions* qq, Services c )
47 : q( qq ),
48 control(c)
49 , screenSaverDbusCookie(-1)
50 #ifdef HAVE_XTEST
51 ,screensaverTimer(0),
52 haveXTest(0),
53 XTestKeyCode(0)
54 #endif // HAVE_XTEST
58 void screensaverFakeKeyEvent();
59 void startScreenSaverPrevention();
60 void stopScreenSaverPrevention();
62 static QString determineProgramName();
64 KNotificationRestrictions* q;
65 Services control;
66 int screenSaverDbusCookie;
67 QString reason;
68 #ifdef HAVE_XTEST
69 QTimer* screensaverTimer;
70 int haveXTest;
71 int XTestKeyCode;
72 #endif // HAVE_XTEST
75 KNotificationRestrictions::KNotificationRestrictions( Services control,
76 QObject* parent )
77 : QObject(parent),
78 d( new Private( this, control ) )
80 if (d->control & ScreenSaver) {
81 d->startScreenSaverPrevention();
85 KNotificationRestrictions::~KNotificationRestrictions()
87 if (d->control & ScreenSaver) {
88 d->stopScreenSaverPrevention();
91 delete d;
94 void KNotificationRestrictions::Private::screensaverFakeKeyEvent()
96 kDebug(297);
97 #ifdef HAVE_XTEST
98 kDebug(297) << "---- using XTestFakeKeyEvent";
99 Display* display = QX11Info::display();
100 XTestFakeKeyEvent(display, XTestKeyCode, true, CurrentTime);
101 XTestFakeKeyEvent(display, XTestKeyCode, false, CurrentTime);
102 XSync(display, false);
103 #endif // HAVE_XTEST
106 void KNotificationRestrictions::Private::startScreenSaverPrevention()
108 kDebug(297);
109 QDBusMessage message = QDBusMessage::createMethodCall(
110 "org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", "Inhibit");
111 message << determineProgramName();
112 message << reason;
113 QDBusReply<uint> reply = QDBusConnection::sessionBus().call(message);
114 if (reply.isValid()) {
115 screenSaverDbusCookie = reply.value();
116 return;
118 #ifdef HAVE_XTEST
119 if ( !haveXTest ) {
120 int a,b,c,e;
121 haveXTest = XTestQueryExtension(QX11Info::display(), &a, &b, &c, &e);
123 if ( !haveXTest ) {
124 kDebug(297) << "--- No XTEST!";
125 return;
129 if ( !XTestKeyCode ) {
130 XTestKeyCode = XKeysymToKeycode(QX11Info::display(), XK_Shift_L);
132 if ( !XTestKeyCode ) {
133 kDebug(297) << "--- No XKeyCode for XK_Shift_L!";
134 return;
138 if ( !screensaverTimer ) {
139 screensaverTimer = new QTimer( q );
140 connect( screensaverTimer, SIGNAL(timeout()),
141 q, SLOT(screensaverFakeKeyEvent()) );
144 kDebug(297) << "---- using XTest";
145 // send a fake event right away in case this got started after a period of
146 // innactivity leading to the screensaver set to activate in <55s
147 screensaverFakeKeyEvent();
148 screensaverTimer->start( 55000 );
149 #endif // HAVE_XTEST
152 void KNotificationRestrictions::Private::stopScreenSaverPrevention()
154 if (screenSaverDbusCookie != -1) {
155 QDBusMessage message = QDBusMessage::createMethodCall(
156 "org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver", "UnInhibit");
157 message << static_cast<uint>(screenSaverDbusCookie);
158 screenSaverDbusCookie = -1;
159 if (QDBusConnection::sessionBus().send(message)) {
160 return;
163 #ifdef HAVE_XTEST
164 delete screensaverTimer;
165 screensaverTimer = 0;
166 #endif // HAVE_XTEST
169 QString KNotificationRestrictions::Private::determineProgramName()
171 QString appName;
172 if (KGlobal::mainComponent().isValid()) {
173 appName = KGlobal::mainComponent().aboutData()->programName();
175 if (appName.isEmpty() && qApp) {
176 appName = QCoreApplication::applicationName();
178 if (appName.isEmpty()) {
179 appName = i18n("Unknown Application");
181 return appName;
184 #include "knotificationrestrictions.moc"