Skip to content

Commit

Permalink
Merge pull request #303 from netromdk/linux_sys_events
Browse files Browse the repository at this point in the history
Lock device via Linux system events
  • Loading branch information
raoulh authored Jun 26, 2018
2 parents 467cddd + a2dab43 commit 8a08b67
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 12 deletions.
3 changes: 3 additions & 0 deletions gui.pro
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ mac {
win32 {
LIBS += -luser32
}
linux {
QT += dbus
}

include(src/QtAwesome/QtAwesome/QtAwesome.pri)
include (src/QSimpleUpdater/QSimpleUpdater.pri)
Expand Down
7 changes: 0 additions & 7 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,7 @@ MainWindow::MainWindow(WSClient *client, DbMasterController *mc, QWidget *parent
connect(ui->checkBoxBoot, SIGNAL(toggled(bool)), this, SLOT(checkSettingsChanged()));
connect(ui->checkBoxTuto, SIGNAL(toggled(bool)), this, SLOT(checkSettingsChanged()));

#ifdef Q_OS_LINUX
ui->checkBoxLockDevice->setChecked(false);
ui->checkBoxLockDevice->setVisible(false);
#else
ui->checkBoxLockDevice->setChecked(s.value("settings/LockDeviceOnSystemEvents", true).toBool());
#endif
connect(ui->checkBoxLockDevice, &QCheckBox::toggled, this, &MainWindow::onLockDeviceSystemEventsChanged);

connect(ui->comboBoxScreenBrightness, SIGNAL(currentIndexChanged(int)), this, SLOT(checkSettingsChanged()));
Expand All @@ -562,12 +557,10 @@ MainWindow::MainWindow(WSClient *client, DbMasterController *mc, QWidget *parent
//Setup the confirm view
ui->widgetSpin->setPixmap(AppGui::qtAwesome()->icon(fa::circleonotch).pixmap(QSize(80, 80)));

#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
connect(&eventHandler, &SystemEventHandler::screenLocked, this, &MainWindow::onSystemEvents);
connect(&eventHandler, &SystemEventHandler::loggingOff, this, &MainWindow::onSystemEvents);
connect(&eventHandler, &SystemEventHandler::goingToSleep, this, &MainWindow::onSystemEvents);
connect(&eventHandler, &SystemEventHandler::shuttingDown, this, &MainWindow::onSystemEvents);
#endif

checkAutoStart();

Expand Down
5 changes: 0 additions & 5 deletions src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
#include <QtAwesome.h>
#include "DbMasterController.h"
#include "WindowLog.h"

#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
#include "SystemEventHandler.h"
#endif

#include <DbBackupsTrackerController.h>

Expand Down Expand Up @@ -161,9 +158,7 @@ private slots:
DbMasterController *dbMasterController;
void initHelpLabels();

#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
SystemEventHandler eventHandler;
#endif
};

#endif // MAINWINDOW_H
95 changes: 95 additions & 0 deletions src/SystemEventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#elif defined(Q_OS_WIN)
#include <windows.h>
#include <wtsapi32.h>
#elif defined(Q_OS_LINUX)
#include <QtDBus/QtDBus>
#endif

#include <QCoreApplication>
Expand All @@ -29,6 +31,36 @@ SystemEventHandler::SystemEventHandler()
regFunc((HWND) widget.winId(), 0);
}
}
#elif defined(Q_OS_LINUX)
/* Session events */
auto bus = QDBusConnection::sessionBus();

// Catch Ubuntu events.
bus.connect("", "/com/ubuntu/Upstart", "", "EventEmitted", "sas", this,
SLOT(upstartEventEmitted(QString,QStringList)));

// Catch Gnome shutdown events.
bus.connect("", "", "org.gnome.SessionManager.ClientPrivate", "EndSession", "u", this,
SLOT(clientPrivateEndSession(quint32)));

// Catch org.freedesktop.ScreenSaver event (Used with KDE at least).
bus.connect("", "/org/freedesktop/ScreenSaver", "", "ActiveChanged", "b", this,
SLOT(screenSaverActiveChanged(bool)));

// Catch KDE about-to-suspend event.
bus.connect("", "/org/kde/Solid/PowerManagement/Actions/SuspendSession", "", "aboutToSuspend",
this, SLOT(kdeAboutToSuspend()));

/* System events */
auto sysBus = QDBusConnection::systemBus();

// Catch suspend event from org.freedesktop.login1.
sysBus.connect("", "/org/freedesktop/login1", "", "PrepareForSleep", "b", this,
SLOT(login1PrepareForSleep(bool)));

// Catch shutdown event from org.freedesktop.login1.
sysBus.connect("", "/org/freedesktop/login1", "", "PrepareForShutdown", "b", this,
SLOT(login1PrepareForShutdown(bool)));
#endif
}

Expand All @@ -48,6 +80,22 @@ SystemEventHandler::~SystemEventHandler()
unRegFunc((HWND) widget.winId());
}
}
#elif defined(Q_OS_LINUX)
auto bus = QDBusConnection::sessionBus();
bus.disconnect("", "/com/ubuntu/Upstart", "", "EventEmitted", "sas", this,
SLOT(upstartEventEmitted(QString,QStringList)));
bus.disconnect("", "", "org.gnome.SessionManager.ClientPrivate", "EndSession", "u", this,
SLOT(clientPrivateEndSession(quint32)));
bus.disconnect("", "/org/freedesktop/ScreenSaver", "", "ActiveChanged", "b", this,
SLOT(screenSaverActiveChanged(bool)));
bus.disconnect("", "/org/kde/Solid/PowerManagement/Actions/SuspendSession", "", "aboutToSuspend",
this, SLOT(kdeAboutToSuspend()));

auto sysBus = QDBusConnection::systemBus();
sysBus.disconnect("", "/org/freedesktop/login1", "", "PrepareForSleep", "b", this,
SLOT(login1PrepareForSleep(bool)));
sysBus.connect("", "/org/freedesktop/login1", "", "PrepareForShutdown", "b", this,
SLOT(login1PrepareForShutdown(bool)));
#endif
}

Expand Down Expand Up @@ -128,3 +176,50 @@ void SystemEventHandler::readyToTerminate()
::readyToTerminate();
}
#endif

void SystemEventHandler::upstartEventEmitted(const QString &name, const QStringList &env)
{
if (name == "desktop-lock")
{
emit screenLocked();
}
else if (name == "session-end" && env.size() == 1 && env.first().toLower() == "type=shutdown")
{
emit shuttingDown();
}
}

void SystemEventHandler::clientPrivateEndSession(quint32 id)
{
Q_UNUSED(id);
emit loggingOff();
}

void SystemEventHandler::screenSaverActiveChanged(bool on)
{
if (on)
{
emit screenLocked();
}
}

void SystemEventHandler::kdeAboutToSuspend()
{
emit goingToSleep();
}

void SystemEventHandler::login1PrepareForSleep(bool active)
{
if (active)
{
emit goingToSleep();
}
}

void SystemEventHandler::login1PrepareForShutdown(bool active)
{
if (active)
{
emit shuttingDown();
}
}
8 changes: 8 additions & 0 deletions src/SystemEventHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public slots:
void readyToTerminate();
#endif

private slots:
void upstartEventEmitted(const QString &name, const QStringList &env);
void clientPrivateEndSession(quint32 id);
void screenSaverActiveChanged(bool on);
void kdeAboutToSuspend();
void login1PrepareForSleep(bool active);
void login1PrepareForShutdown(bool active);

private:
#ifdef Q_OS_MAC
void *eventHandler = nullptr;
Expand Down

0 comments on commit 8a08b67

Please sign in to comment.