Make it possible to lock the screen from KDE Connect

Connects to freedesktop ScreenSaver interface and un/locks it upon request.

REVIEW: 124170
This commit is contained in:
Aleix Pol 2015-07-22 03:37:34 +02:00
parent b99ac42ede
commit be2a3252c2
12 changed files with 288 additions and 2 deletions

View file

@ -130,8 +130,11 @@ ApplicationWindow
} );
}
Button {
text: i18n("Remote touch and keyboard")
enabled: false
property var lockIface: LockDeviceDbusInterfaceFactory.create(deviceView.currentDevice.id())
text: lockIface.isLocked ? i18n("Unlock") : i18n("Lock")
onClicked: {
lockIface.isLocked = !lockIface.isLocked;
}
}
Item { Layout.fillHeight: true }

View file

@ -55,6 +55,7 @@ int main(int argc, char** argv)
parser.addOption(QCommandLineOption("ping-msg", i18n("Same as ping but you can set the message to display"), i18n("message")));
parser.addOption(QCommandLineOption("share", i18n("Share a file to a said device"), "path"));
parser.addOption(QCommandLineOption("list-notifications", i18n("Display the notifications on a said device")));
parser.addOption(QCommandLineOption("lock", i18n("Lock the specified device")));
parser.addOption(QCommandLineOption(QStringList("device") << "d", i18n("Device ID"), "dev"));
about.setupCommandLine(&parser);

View file

@ -41,6 +41,7 @@ geninterface(${CMAKE_SOURCE_DIR}/plugins/notifications/notificationsdbusinterfac
geninterface(${CMAKE_SOURCE_DIR}/plugins/notifications/notification.h notificationinterface)
geninterface(${CMAKE_SOURCE_DIR}/plugins/mprisremote/mprisremoteplugin.h mprisremoteinterface)
geninterface(${CMAKE_SOURCE_DIR}/plugins/remotecontrol/remotecontrolplugin.h remotecontrolinterface)
geninterface(${CMAKE_SOURCE_DIR}/plugins/lockdevice/lockdeviceplugin.h lockdeviceinterface)
add_library(kdeconnectinterfaces SHARED ${libkdeconnect_SRC})
set_target_properties(kdeconnectinterfaces PROPERTIES

View file

@ -122,3 +122,16 @@ RemoteControlDbusInterface::RemoteControlDbusInterface(const QString& id, QObjec
RemoteControlDbusInterface::~RemoteControlDbusInterface()
{
}
LockDeviceDbusInterface::LockDeviceDbusInterface(const QString& id, QObject* parent)
: OrgKdeKdeconnectDeviceLockdeviceInterface(DaemonDbusInterface::activatedService(), "/modules/kdeconnect/devices/" + id + "/lockdevice", QDBusConnection::sessionBus(), parent)
{
connect(this, &OrgKdeKdeconnectDeviceLockdeviceInterface::lockedChanged, this, &LockDeviceDbusInterface::lockedChangedProxy);
Q_ASSERT(isValid());
}
LockDeviceDbusInterface::~LockDeviceDbusInterface()
{
}
#include "dbusinterfaces.moc"

View file

@ -31,6 +31,7 @@
#include "interfaces/notificationinterface.h"
#include "interfaces/mprisremoteinterface.h"
#include "interfaces/remotecontrolinterface.h"
#include "interfaces/lockdeviceinterface.h"
/**
* Using these "proxy" classes just in case we need to rename the
@ -134,4 +135,17 @@ public:
~RemoteControlDbusInterface() override;
};
class KDECONNECTINTERFACES_EXPORT LockDeviceDbusInterface
: public OrgKdeKdeconnectDeviceLockdeviceInterface
{
Q_OBJECT
Q_PROPERTY(bool isLocked READ isLocked WRITE setIsLocked NOTIFY lockedChangedProxy)
public:
LockDeviceDbusInterface(const QString& deviceId, QObject* parent = 0);
virtual ~LockDeviceDbusInterface();
Q_SIGNALS:
void lockedChangedProxy(bool isLocked);
};
#endif

View file

@ -59,6 +59,12 @@ QObject* createMprisInterface(QVariant deviceId)
return new MprisDbusInterface(deviceId.toString());
}
QObject* createDeviceLockInterface(QVariant deviceId)
{
Q_ASSERT(!deviceId.toString().isEmpty());
return new LockDeviceDbusInterface(deviceId.toString());
}
QObject* createDBusResponse()
{
return new DBusAsyncResponse();
@ -72,6 +78,7 @@ void KdeConnectDeclarativePlugin::registerTypes(const char* uri)
qmlRegisterType<ProcessRunner>(uri, 1, 0, "ProcessRunner");
qmlRegisterType<DevicesSortProxyModel>(uri, 1, 0, "DevicesSortProxyModel");
qmlRegisterUncreatableType<MprisDbusInterface>(uri, 1, 0, "MprisDbusInterface", QStringLiteral("You're not supposed to instantiate interfacess"));
qmlRegisterUncreatableType<LockDeviceDbusInterface>(uri, 1, 0, "LockDeviceDbusInterface", QStringLiteral("You're not supposed to instantiate interfacess"));
qmlRegisterUncreatableType<DeviceDbusInterface>(uri, 1, 0, "DeviceDbusInterface", QStringLiteral("You're not supposed to instantiate interfacess"));
}
@ -93,6 +100,9 @@ void KdeConnectDeclarativePlugin::initializeEngine(QQmlEngine* engine, const cha
engine->rootContext()->setContextProperty("RemoteControlDbusInterfaceFactory"
, new ObjectFactory(engine, createRemoteControlInterface));
engine->rootContext()->setContextProperty("LockDeviceDbusInterfaceFactory"
, new ObjectFactory(engine, createDeviceLockInterface));
engine->rootContext()->setContextProperty("DBusResponseFactory"
, new ObjectFactory(engine, createDBusResponse));

View file

@ -17,6 +17,7 @@ add_subdirectory(screensaver-inhibit)
if(EXPERIMENTALAPP_ENABLED)
add_subdirectory(mprisremote)
add_subdirectory(remotecontrol)
add_subdirectory(lockdevice)
endif()
#FIXME: If we split notifications in several files, they won't appear in the same group in the Notifications KCM

View file

@ -0,0 +1,9 @@
qt5_add_dbus_interface(lockdevice_SRCS org.freedesktop.ScreenSaver.xml screensaverdbusinterface)
kdeconnect_add_plugin(kdeconnect_lockdevice JSON kdeconnect_lockdevice.json SOURCES lockdeviceplugin.cpp ${lockdevice_SRCS})
target_link_libraries(kdeconnect_lockdevice
kdeconnectcore
Qt5::DBus
KF5::I18n
)

View file

@ -0,0 +1,24 @@
{
"Encoding": "UTF-8",
"KPlugin": {
"Authors": [
{
"Email": "aleixpol@kde.org",
"Name": "Aleix Pol"
}
],
"Description": "Locks your systems",
"EnabledByDefault": true,
"Icon": "applications-miscelaneaous",
"Id": "kdeconnect_lockdevice",
"License": "GPL",
"Name": "LockDevice",
"ServiceTypes": [
"KdeConnect/Plugin"
],
"Version": "0.1",
"Website": "https://kde.org"
},
"X-KdeConnect-OutgoingPackageType": [ "kdeconnect.lock" ],
"X-KdeConnect-SupportedPackageType": [ "kdeconnect.lock" ]
}

View file

@ -0,0 +1,108 @@
/**
* Copyright 2015 Aleix Pol Gonzalez <aleixpol@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lockdeviceplugin.h"
#include <KLocalizedString>
#include <KPluginFactory>
#include <QDebug>
#include <QDBusConnection>
#include <QLoggingCategory>
#include "screensaverdbusinterface.h"
#include <core/device.h>
K_PLUGIN_FACTORY_WITH_JSON( KdeConnectLockPluginFactory, "kdeconnect_lockdevice.json", registerPlugin<LockDevicePlugin>(); )
Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_LOCKREMOTE, "kdeconnect.plugin.lock")
LockDevicePlugin::LockDevicePlugin(QObject* parent, const QVariantList& args)
: KdeConnectPlugin(parent, args)
, m_remoteLocked(false)
, m_iface(nullptr)
{
}
LockDevicePlugin::~LockDevicePlugin()
{
delete m_iface;
}
bool LockDevicePlugin::isLocked() const
{
return m_remoteLocked;
}
void LockDevicePlugin::setLocked(bool locked)
{
NetworkPackage np(PACKAGE_TYPE_LOCK);
np.set("setLocked", locked);
sendPackage(np);
}
bool LockDevicePlugin::receivePackage(const NetworkPackage & np)
{
if (np.has("isLocked")) {
bool locked = np.get<bool>("isLocked");
if (m_remoteLocked != locked) {
m_remoteLocked = locked;
Q_EMIT lockedChanged(locked);
}
}
bool sendState = np.has("requestLocked");
if (np.has("setLocked")) {
iface()->SetActive(np.get<bool>("setLocked"));
sendState = true;
}
if (sendState) {
NetworkPackage np(PACKAGE_TYPE_LOCK);
np.set("isLocked", iface()->GetActive());
sendPackage(np);
}
return true;
}
OrgFreedesktopScreenSaverInterface* LockDevicePlugin::iface()
{
if (!m_iface) {
m_iface = new OrgFreedesktopScreenSaverInterface("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver", QDBusConnection::sessionBus());
if(!m_iface->isValid())
qCWarning(KDECONNECT_PLUGIN_LOCKREMOTE) << "Couldn't connect to the ScreenSaver interface";
}
return m_iface;
}
void LockDevicePlugin::connected()
{
QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportAllContents);
NetworkPackage np(PACKAGE_TYPE_LOCK);
np.set("requestLocked", QVariant());
sendPackage(np);
}
QString LockDevicePlugin::dbusPath() const
{
return "/modules/kdeconnect/devices/" + device()->id() + "/lockdevice";
}
#include "lockdeviceplugin.moc"

View file

@ -0,0 +1,61 @@
/**
* Copyright 2015 Aleix Pol Gonzalez <aleixpol@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LOCKDEVICEPLUGIN_H
#define LOCKDEVICEPLUGIN_H
#include <QObject>
#include <core/kdeconnectplugin.h>
class OrgFreedesktopScreenSaverInterface;
#define PACKAGE_TYPE_LOCK QLatin1String("kdeconnect.lock")
class Q_DECL_EXPORT LockDevicePlugin
: public KdeConnectPlugin
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device.lockdevice")
Q_PROPERTY(bool isLocked READ isLocked WRITE setLocked NOTIFY lockedChanged)
public:
explicit LockDevicePlugin(QObject *parent, const QVariantList &args);
virtual ~LockDevicePlugin();
bool isLocked() const;
void setLocked(bool b);
void connected() override;
bool receivePackage(const NetworkPackage & np) override;
Q_SIGNALS:
void lockedChanged(bool locked);
private:
QString dbusPath() const;
bool m_remoteLocked;
OrgFreedesktopScreenSaverInterface* iface();
OrgFreedesktopScreenSaverInterface* m_iface;
};
#endif

View file

@ -0,0 +1,41 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="org.freedesktop.ScreenSaver">
<signal name="ActiveChanged">
<arg type="b"/>
</signal>
<method name="Lock">
</method>
<method name="SimulateUserActivity">
</method>
<method name="GetActive">
<arg type="b" direction="out"/>
</method>
<method name="GetActiveTime">
<arg name="seconds" type="u" direction="out"/>
</method>
<method name="GetSessionIdleTime">
<arg name="seconds" type="u" direction="out"/>
</method>
<method name="SetActive">
<arg type="b" direction="out"/>
<arg name="e" type="b" direction="in"/>
</method>
<method name="Inhibit">
<arg name="application_name" type="s" direction="in"/>
<arg name="reason_for_inhibit" type="s" direction="in"/>
<arg name="cookie" type="u" direction="out"/>
</method>
<method name="UnInhibit">
<arg name="cookie" type="u" direction="in"/>
</method>
<method name="Throttle">
<arg name="application_name" type="s" direction="in"/>
<arg name="reason_for_inhibit" type="s" direction="in"/>
<arg name="cookie" type="u" direction="out"/>
</method>
<method name="UnThrottle">
<arg name="cookie" type="u" direction="in"/>
</method>
</interface>
</node>