KDEUI
knotificationrestrictions.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
00146
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"