on xenial help sign in
wrap org.freedesktop.ScreenSaver service

put the common code into a separate private class/file

commit5db553ac9e8fb99ef5318e1cbe3208c7d8bbd42e authorLukáš Tinkl <lukas.tinkl@canonical.com> dateThu 18 Jun 2015, 00:15 (11 years ago) parent7560621
5 files changed+81−132
plugins/Unity/Session/CMakeLists.txt+1 plugins/Unity/Session/dbusunitysessionservice.cpp+34−124 plugins/Unity/Session/dbusunitysessionservice.h+34 tests/plugins/Unity/Session/CMakeLists.txt+1 tests/plugins/Unity/Session/sessionbackendtest.cpp+11−8
plugins/Unity/Session/CMakeLists.txt+1
@@ -9,6 +9,7 @@ include_directories(
99set(QMLSESSIONPLUGIN_SRC
1010 plugin.cpp
1111 dbusunitysessionservice.cpp
12+ dbusunitysessionservice_p.cpp
1213 orientationlock.cpp
1314 )
1415
plugins/Unity/Session/dbusunitysessionservice.cpp+34−124
@@ -15,128 +15,12 @@
1515 *
1616 * Authors: Lukáš Tinkl <ltinkl@canonical.com>
1717 * Christopher Townsend <christopher.townsend@canonical.com>
18− * Paul Liu <paul.liu@canonical.com>
18+ * Ying-Chun Liu (PaulLiu) <paul.liu@canonical.com>
1919 */
2020
2121// local
2222#include "dbusunitysessionservice.h"
23−
24−// system
25−#include <sys/types.h>
26−#include <unistd.h>
27−#include <pwd.h>
28−
29−// Qt
30−#include <QDebug>
31−#include <QDBusPendingCall>
32−#include <QDBusReply>
33−#include <QElapsedTimer>
34−
35−#define LOGIN1_SERVICE QStringLiteral("org.freedesktop.login1")
36−#define LOGIN1_PATH QStringLiteral("/org/freedesktop/login1")
37−#define LOGIN1_IFACE QStringLiteral("org.freedesktop.login1.Manager")
38−#define LOGIN1_SESSION_IFACE QStringLiteral("org.freedesktop.login1.Session")
39−
40−#define ACTIVE_KEY QStringLiteral("Active")
41−
42−class DBusUnitySessionServicePrivate: public QObject
43−{
44− Q_OBJECT
45−public:
46− QString logindSessionPath;
47− bool isActive = true;
48− QElapsedTimer screensaverActiveTimer;
49−
50− DBusUnitySessionServicePrivate(): QObject() {
51− init();
52− checkActive();
53− }
54−
55− void init()
56− {
57− // get our logind session path
58− QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE,
59− LOGIN1_PATH,
60− LOGIN1_IFACE,
61− "GetSessionByPID");
62− msg << (quint32) getpid();
63−
64− QDBusReply<QDBusObjectPath> reply = QDBusConnection::systemBus().asyncCall(msg);
65− if (reply.isValid()) {
66− logindSessionPath = reply.value().path();
67− //qDebug() << "session path" << logindSessionPath;
68−
69− // start watching the Active property
70− QDBusConnection::systemBus().connect(LOGIN1_SERVICE, logindSessionPath, "org.freedesktop.DBus.Properties", "PropertiesChanged",
71− this, SLOT(onPropertiesChanged(QString,QVariantMap,QStringList)));
72− } else {
73− qWarning() << "Failed to get logind session path" << reply.error().message();
74− }
75− }
76−
77− bool checkLogin1Call(const QString &method) const
78− {
79− QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE, LOGIN1_PATH, LOGIN1_IFACE, method);
80− QDBusReply<QString> reply = QDBusConnection::systemBus().asyncCall(msg);
81− return reply.isValid() && (reply == QStringLiteral("yes") || reply == QStringLiteral("challenge"));
82− }
83−
84− void makeLogin1Call(const QString &method, const QVariantList &args)
85− {
86− QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE,
87− LOGIN1_PATH,
88− LOGIN1_IFACE,
89− method);
90− msg.setArguments(args);
91− QDBusConnection::systemBus().asyncCall(msg);
92− }
93−
94− void checkActive()
95− {
96− if (logindSessionPath.isEmpty()) {
97− qWarning() << "Invalid session path";
98− return;
99− }
100−
101− QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE,
102− logindSessionPath,
103− "org.freedesktop.DBus.Properties",
104− "Get");
105− msg << LOGIN1_SESSION_IFACE;
106− msg << ACTIVE_KEY;
107−
108− QDBusReply<QVariant> reply = QDBusConnection::systemBus().asyncCall(msg);
109− if (reply.isValid()) {
110− isActive = reply.value().toBool();
111− qDebug() << "Session" << logindSessionPath << "is active:" << isActive;
112− } else {
113− qWarning() << "Failed to get Active property" << reply.error().message();
114− }
115− }
116−
117−private Q_SLOTS:
118− void onPropertiesChanged(const QString &iface, const QVariantMap &changedProps, const QStringList &invalidatedProps)
119− {
120− Q_UNUSED(iface)
121−
122− if (changedProps.contains(ACTIVE_KEY)) {
123− isActive = changedProps.value(ACTIVE_KEY).toBool();
124− } else if (invalidatedProps.contains(ACTIVE_KEY)) {
125− checkActive();
126− }
127−
128− Q_EMIT screensaverActiveChanged(!isActive);
129−
130− if (isActive) {
131− screensaverActiveTimer.invalidate();
132− } else {
133− screensaverActiveTimer.start();
134− }
135− }
136−
137−Q_SIGNALS:
138− void screensaverActiveChanged(bool active);
139−};
23+#include "dbusunitysessionservice_p.h"
14024
14125Q_GLOBAL_STATIC(DBusUnitySessionServicePrivate, d)
14226
@@ -258,7 +142,7 @@ void DBusUnitySessionService::Lock()
258142
259143bool DBusUnitySessionService::IsLocked() const
260144{
261− return !d->isActive;
145+ return !d->isSessionActive;
262146}
263147
264148void DBusUnitySessionService::RequestLogout()
@@ -319,6 +203,7 @@ void performAsyncUnityCall(const QString &method)
319203 QDBusConnection::sessionBus().asyncCall(msg);
320204}
321205
206+
322207DBusGnomeSessionManagerWrapper::DBusGnomeSessionManagerWrapper()
323208 : UnityDBusObject("/org/gnome/SessionManager/EndSessionDialog", "com.canonical.Unity")
324209{
@@ -358,7 +243,7 @@ DBusGnomeScreensaverWrapper::DBusGnomeScreensaverWrapper()
358243
359244bool DBusGnomeScreensaverWrapper::GetActive() const
360245{
361− return !d->isActive; // return whether the session is not active
246+ return !d->isSessionActive; // return whether the session is not active
362247}
363248
364249void DBusGnomeScreensaverWrapper::SetActive(bool lock)
@@ -375,11 +260,36 @@ void DBusGnomeScreensaverWrapper::Lock()
375260
376261quint32 DBusGnomeScreensaverWrapper::GetActiveTime() const
377262{
378− if (GetActive() && d->screensaverActiveTimer.isValid()) {
379− return d->screensaverActiveTimer.elapsed() / 1000;
263+ return d->activeTime();
264+}
265+
266+
267+DBusScreensaverWrapper::DBusScreensaverWrapper()
268+ : UnityDBusObject("/org/freedesktop/ScreenSaver", "org.freedesktop.ScreenSaver")
269+{
270+ connect(d, &DBusUnitySessionServicePrivate::screensaverActiveChanged, this, &DBusScreensaverWrapper::ActiveChanged);
271+}
272+
273+bool DBusScreensaverWrapper::GetActive() const
274+{
275+ return !d->isSessionActive; // return whether the session is not active
276+}
277+
278+bool DBusScreensaverWrapper::SetActive(bool lock)
279+{
280+ if (lock) {
281+ Lock();
282+ return true;
380283 }
284+ return false;
285+}
381286
382− return 0;
287+void DBusScreensaverWrapper::Lock()
288+{
289+ performAsyncUnityCall("Lock");
383290}
384291
385−#include "dbusunitysessionservice.moc"
292+quint32 DBusScreensaverWrapper::GetActiveTime() const
293+{
294+ return d->activeTime();
295+}
plugins/Unity/Session/dbusunitysessionservice.h+34
@@ -300,4 +300,38 @@ Q_SIGNALS:
300300 void ActiveChanged(bool active);
301301};
302302
303+class DBusScreensaverWrapper: public UnityDBusObject
304+{
305+ Q_OBJECT
306+ Q_CLASSINFO("D-Bus Interface", "org.freedesktop.ScreenSaver")
307+
308+public:
309+ DBusScreensaverWrapper();
310+ ~DBusScreensaverWrapper() = default;
311+
312+public Q_SLOTS:
313+ /**
314+ * @return whether the session is currently locked (screensaver is on)
315+ */
316+ Q_SCRIPTABLE bool GetActive() const;
317+
318+ /**
319+ * Locks the session immediately if @p lock is true
320+ */
321+ Q_SCRIPTABLE bool SetActive(bool lock);
322+
323+ /**
324+ * Locks the session immediately
325+ */
326+ Q_SCRIPTABLE void Lock();
327+
328+ /**
329+ * @return the number of seconds elapsed since the screensaver had been active
330+ */
331+ Q_SCRIPTABLE quint32 GetActiveTime() const;
332+
333+Q_SIGNALS:
334+ void ActiveChanged(bool active);
335+};
336+
303337#endif // DBUSUNITYSESSIONSERVICE_H
tests/plugins/Unity/Session/CMakeLists.txt+1
@@ -17,6 +17,7 @@ add_custom_target(sessionbackendtest ${testBackendCommand})
1717add_executable(sessionbackendtestExec
1818 sessionbackendtest.cpp
1919 ${CMAKE_SOURCE_DIR}/plugins/Unity/Session/dbusunitysessionservice.cpp
20+ ${CMAKE_SOURCE_DIR}/plugins/Unity/Session/dbusunitysessionservice_p.cpp
2021)
2122target_link_libraries(sessionbackendtestExec
2223 unity8-private
tests/plugins/Unity/Session/sessionbackendtest.cpp+11−8
@@ -84,8 +84,8 @@ private Q_SLOTS:
8484 QTest::addColumn<QString>("signal");
8585
8686 QTest::newRow("Logout") << (uint)Action::LOGOUT << "LogoutRequested(bool)";
87− QTest::newRow("Shutdown") << (unsigned)Action::SHUTDOWN << "ShutdownRequested(bool)";
88− QTest::newRow("Reboot") << (unsigned)Action::REBOOT << "RebootRequested(bool)";
87+ QTest::newRow("Shutdown") << (uint)Action::SHUTDOWN << "ShutdownRequested(bool)";
88+ QTest::newRow("Reboot") << (uint)Action::REBOOT << "RebootRequested(bool)";
8989 }
9090
9191 void testGnomeSessionWrapper() {
@@ -158,15 +158,18 @@ private Q_SLOTS:
158158
159159// void testLock() {
160160// DBusUnitySessionService dbusUnitySessionService;
161−// QDBusInterface login1face("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", QDBusConnection::systemBus());
161+// DBusScreensaverWrapper saverIface;
162+// QSignalSpy spy(&saverIface, &DBusScreensaverWrapper::ActiveChanged);
162163// QCoreApplication::processEvents(); // to let the service register on DBus
163164// qDebug() << "Locking up...";
164165// dbusUnitySessionService.Lock();
165−// qDebug() << "Unlocking...";
166−// QDBusReply<void> unlockReply = login1face.call("UnlockSessions");
167−// if (!unlockReply.isValid()) {
168−// qWarning() << "Failed to unlock sessions" << unlockReply.error().message();
169−// }
166+
167+// QTRY_COMPARE(spy.count(), 1);
168+// QList<QVariant> arguments = spy.takeFirst(); // take the first signal
169+// QVERIFY(arguments.at(0).toBool() == true); // verify the first argument
170+
171+// // verify the session is locked
172+// QCOMPARE(saverIface.GetActive(), true);
170173// }
171174
172175private: