on xenial help sign in
Add a wrapper for handling the /org/gnome/SessionManager/EndSessionDialog DBus object used by indicator-session. This allows indicator-session to properly talk to the Unity 8 session management functions. Fixes: #1392187

Approved by: Albert Astals Cid

commitda3f75a0a4394abaaed9175007909906f9a09763 authorChris Townsend <christopher.townsend@canonical.com> dateMon 2 Mar 2015, 12:11 (12 years ago) parent7f8c6efb822ed2
4 files changed+152−13
plugins/Unity/Session/dbusunitysessionservice.cpp+49 plugins/Unity/Session/dbusunitysessionservice.h+20−3 plugins/Unity/Session/plugin.cpp+1 tests/plugins/Unity/Session/sessionbackendtest.cpp+82−10
plugins/Unity/Session/dbusunitysessionservice.cpp+49
@@ -82,3 +82,52 @@ void DBusUnitySessionService::RequestShutdown()
8282{
8383 Q_EMIT shutdownRequested(false);
8484}
85+
86+enum class Action : unsigned
87+{
88+ LOGOUT = 0,
89+ SHUTDOWN,
90+ REBOOT,
91+ NONE
92+};
93+
94+DBusGnomeSessionManagerWrapper::DBusGnomeSessionManagerWrapper()
95+ : UnityDBusObject("/org/gnome/SessionManager/EndSessionDialog", "com.canonical.Unity")
96+{
97+}
98+
99+DBusGnomeSessionManagerWrapper::~DBusGnomeSessionManagerWrapper()
100+{
101+}
102+
103+void DBusGnomeSessionManagerWrapper::Open(const unsigned type, const unsigned arg_1, const unsigned max_wait, const QList<QDBusObjectPath> &inhibitors)
104+{
105+ Q_UNUSED(arg_1);
106+ Q_UNUSED(max_wait);
107+ Q_UNUSED(inhibitors);
108+
109+ QDBusConnection connection = QDBusConnection::sessionBus();
110+ QDBusInterface iface1 ("com.canonical.Unity",
111+ "/com/canonical/Unity/Session",
112+ "com.canonical.Unity.Session",
113+ connection);
114+
115+ Action action = (Action)type;
116+
117+ switch (action)
118+ {
119+ case Action::LOGOUT:
120+ iface1.call("RequestLogout");
121+ break;
122+
123+ case Action::REBOOT:
124+ iface1.call("RequestShutdown");
125+ break;
126+
127+ case Action::SHUTDOWN:
128+ break;
129+
130+ default:
131+ break;
132+ }
133+}
plugins/Unity/Session/dbusunitysessionservice.h+20−3
@@ -17,8 +17,13 @@
1717#ifndef DBUSUNITYSESSIONSERVICE_H
1818#define DBUSUNITYSESSIONSERVICE_H
1919
20+#include <QDBusObjectPath>
21+
2022#include "unitydbusobject.h"
2123
24+typedef QList<QDBusObjectPath> QDbusList;
25+Q_DECLARE_METATYPE(QList<QDBusObjectPath>)
26+
2227/**
2328 * DBusUnitySessionService provides com.canonical.Unity.Session dbus
2429 * interface.
@@ -50,7 +55,7 @@ Q_SIGNALS:
5055 * @param have_inhibitors if there are any special running applications
5156 * which inhibit the logout.
5257 */
53− void logoutRequested(bool have_inhibitors);
58+ Q_SCRIPTABLE void logoutRequested(bool have_inhibitors);
5459
5560 /**
5661 * rebootRequested signal
@@ -60,7 +65,7 @@ Q_SIGNALS:
6065 * @param have_inhibitors if there are any special running applications
6166 * which inhibit the reboot.
6267 */
63− void rebootRequested(bool have_inhibitors);
68+ Q_SCRIPTABLE void rebootRequested(bool have_inhibitors);
6469
6570 /**
6671 * shutdownRequested signal
@@ -70,7 +75,7 @@ Q_SIGNALS:
7075 * @param have_inhibitors if there are any special running applications
7176 * which inhibit the shutdown.
7277 */
73− void shutdownRequested(bool have_inhibitors);
78+ Q_SCRIPTABLE void shutdownRequested(bool have_inhibitors);
7479
7580
7681 /**
@@ -149,4 +154,16 @@ public Q_SLOTS:
149154 Q_SCRIPTABLE void EndSession();
150155};
151156
157+class DBusGnomeSessionManagerWrapper : public UnityDBusObject
158+{
159+ Q_OBJECT
160+ Q_CLASSINFO("D-Bus Interface", "org.gnome.SessionManager.EndSessionDialog")
161+
162+public:
163+ DBusGnomeSessionManagerWrapper();
164+ ~DBusGnomeSessionManagerWrapper();
165+
166+public Q_SLOTS:
167+ Q_SCRIPTABLE void Open(const unsigned int type, const unsigned int arg_1, const unsigned int max_wait, const QList<QDBusObjectPath> &inhibitors);
168+};
152169#endif // DBUSUNITYSESSIONSERVICE_H
plugins/Unity/Session/plugin.cpp+1
@@ -26,6 +26,7 @@
2626
2727static QObject *dbusunitysessionservice_provider(QQmlEngine */*engine*/, QJSEngine */*jsEngine*/)
2828{
29+ new DBusGnomeSessionManagerWrapper();
2930 return new DBusUnitySessionService();
3031}
3132
tests/plugins/Unity/Session/sessionbackendtest.cpp+82−10
@@ -26,6 +26,14 @@
2626
2727#include "dbusunitysessionservice.h"
2828
29+enum class Action : unsigned
30+{
31+ LOGOUT = 0,
32+ SHUTDOWN,
33+ REBOOT,
34+ NONE
35+};
36+
2937class SessionBackendTest : public QObject
3038{
3139 Q_OBJECT
@@ -33,32 +41,96 @@ class SessionBackendTest : public QObject
3341private Q_SLOTS:
3442
3543 void initTestCase() {
44+ dbusUnitySession = new QDBusInterface ("com.canonical.Unity",
45+ "/com/canonical/Unity/Session",
46+ "com.canonical.Unity.Session",
47+ QDBusConnection::sessionBus());
3648 }
3749
38− void testDbusIfaceMethods_data() {
50+ void testUnitySessionLogoutRequested_data() {
3951 QTest::addColumn<QString>("method");
4052
4153 QTest::newRow("Logout") << "RequestLogout";
4254 }
4355
44− void testDbusIfaceMethods() {
56+ void testUnitySessionLogoutRequested() {
4557 QFETCH(QString, method);
4658
4759 DBusUnitySessionService dbusUnitySessionService;
4860 QCoreApplication::processEvents(); // to let the service register on DBus
4961
50− QDBusConnection con = QDBusConnection::sessionBus();
51− QDBusInterface interface ("com.canonical.Unity",
52− "/com/canonical/Unity/Session",
53− "com.canonical.Unity.Session",
54− con);
55− QDBusReply<void> reply;
62+ QSignalSpy spy(&dbusUnitySessionService, SIGNAL(logoutRequested(bool)));
63+
64+ QDBusReply<void> reply = dbusUnitySession->call(method);
65+ QCOMPARE(reply.isValid(), true);
66+
67+ QCOMPARE(spy.count(), 1);
68+ }
69+
70+ void testGnomeSessionWrapperLogoutRequested() {
71+ DBusUnitySessionService dbusUnitySessionService;
72+ QCoreApplication::processEvents(); // to let the service register on DBus
73+
74+ // Spy on the logoutRequested signal on the /com/canonical/Unity/Session object
75+ // as proof we are actually calling the actual method.
76+ QSignalSpy spy(&dbusUnitySessionService, SIGNAL(logoutRequested(bool)));
77+
78+ DBusGnomeSessionManagerWrapper dbusGnomeSessionManagerWrapper;
79+ QCoreApplication::processEvents(); // to let the service register on DBus
80+
81+ QDBusInterface dbusGnomeSessionWrapper ("com.canonical.Unity",
82+ "/org/gnome/SessionManager/EndSessionDialog",
83+ "org.gnome.SessionManager.EndSessionDialog",
84+ QDBusConnection::sessionBus());
85+
86+ QCOMPARE(dbusGnomeSessionWrapper.isValid(), true);
87+
88+ // Set the QVariant as a QList<QDBusObjectPath> type
89+ QDbusList var;
90+ QVariant inhibitors;
91+ inhibitors.setValue(var);
92+
93+ QDBusReply<void> reply = dbusGnomeSessionWrapper.call("Open", (unsigned)Action::LOGOUT, (unsigned)0, (unsigned)0, inhibitors);
94+ QCOMPARE(reply.isValid(), true);
95+
96+ // Make sure we see the signal being emitted.
97+ QCOMPARE(spy.count(), 1);
98+ }
99+
100+ void testGnomeSessionWrapperShutdownRequested() {
101+ DBusUnitySessionService dbusUnitySessionService;
102+ QCoreApplication::processEvents(); // to let the service register on DBus
103+
104+ // Spy on the shutdownRequested signal on the /com/canonical/Unity/Session object
105+ // as proof we are actually calling the actual method.
106+ QSignalSpy spy(&dbusUnitySessionService, SIGNAL(shutdownRequested(bool)));
107+
108+ DBusGnomeSessionManagerWrapper dbusGnomeSessionManagerWrapper;
109+ QCoreApplication::processEvents(); // to let the service register on DBus
110+
111+ QDBusInterface dbusGnomeSessionWrapper ("com.canonical.Unity",
112+ "/org/gnome/SessionManager/EndSessionDialog",
113+ "org.gnome.SessionManager.EndSessionDialog",
114+ QDBusConnection::sessionBus());
56115
57− QCOMPARE(interface.isValid(), true);
58− reply = interface.call(method);
116+ QCOMPARE(dbusGnomeSessionWrapper.isValid(), true);
117+
118+ // Set the QVariant as a QList<QDBusObjectPath> type
119+ QDbusList var;
120+ QVariant inhibitors;
121+ inhibitors.setValue(var);
122+
123+ // * Reboot action translates to the shutdown signal due to some weird idiosyncracy
124+ // in the indicator-session/Unity interaction. *
125+ QDBusReply<void> reply = dbusGnomeSessionWrapper.call("Open", (unsigned)Action::REBOOT, (unsigned)0, (unsigned)0, inhibitors);
59126 QCOMPARE(reply.isValid(), true);
60127
128+ // Make sure we see the signal being emitted.
129+ QCOMPARE(spy.count(), 1);
61130 }
131+
132+private:
133+ QDBusInterface *dbusUnitySession;
62134};
63135
64136QTEST_GUILESS_MAIN(SessionBackendTest)