Branch data Line data Source code
1 : : // SPDX-License-Identifier: GPL-3.0-or-later
2 : : // SPDX-FileCopyrightText: Andy Holmes <andrew.g.r.holmes@gmail.com>
3 : :
4 : : #define G_LOG_DOMAIN "valent-fdo-session"
5 : :
6 : : #include "config.h"
7 : :
8 : : #include <libpeas.h>
9 : : #include <valent.h>
10 : :
11 : : #include "valent-fdo-session.h"
12 : :
13 : :
14 : : struct _ValentFdoSession
15 : : {
16 : : ValentSessionAdapter parent_instance;
17 : :
18 : : GDBusProxy *proxy;
19 : :
20 : : gboolean active;
21 : : gboolean locked;
22 : : };
23 : :
24 : : static void g_async_initable_iface_init (GAsyncInitableIface *iface);
25 : :
26 [ + + + - ]: 15 : G_DEFINE_FINAL_TYPE_WITH_CODE (ValentFdoSession, valent_fdo_session, VALENT_TYPE_SESSION_ADAPTER,
27 : : G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, g_async_initable_iface_init))
28 : :
29 : :
30 : : /*
31 : : * DBus Callbacks
32 : : */
33 : : static void
34 : 1 : on_properties_changed (GDBusProxy *proxy,
35 : : GVariant *changed_properties,
36 : : GStrv invalidated_properties,
37 : : ValentFdoSession *self)
38 : : {
39 [ - + ]: 1 : g_assert (VALENT_IS_FDO_SESSION (self));
40 : :
41 [ + - ]: 1 : if (!g_variant_lookup (changed_properties, "Active", "b", &self->active))
42 : : return;
43 : :
44 : 1 : g_object_notify (G_OBJECT (self), "active");
45 : : }
46 : :
47 : : static void
48 : 1 : on_signal (GDBusProxy *proxy,
49 : : const char *sender_name,
50 : : const char *signal_name,
51 : : GVariant *parameters,
52 : : ValentFdoSession *self)
53 : : {
54 [ - + ]: 1 : g_assert (VALENT_IS_FDO_SESSION (self));
55 : :
56 [ + - ]: 1 : if (g_strcmp0 (signal_name, "Lock") == 0)
57 : 1 : self->locked = TRUE;
58 [ # # ]: 0 : else if (g_strcmp0 (signal_name, "Unlock") == 0)
59 : 0 : self->locked = FALSE;
60 : :
61 : 1 : g_object_notify (G_OBJECT (self), "locked");
62 : 1 : }
63 : :
64 : : /*
65 : : * ValentSessionAdapter
66 : : */
67 : : static gboolean
68 : 2 : valent_fdo_session_get_active (ValentSessionAdapter *adapter)
69 : : {
70 : 2 : ValentFdoSession *self = VALENT_FDO_SESSION (adapter);
71 : :
72 [ - + ]: 2 : g_assert (VALENT_IS_FDO_SESSION (self));
73 : :
74 : 2 : return self->active;
75 : : }
76 : :
77 : : static gboolean
78 : 2 : valent_fdo_session_get_locked (ValentSessionAdapter *adapter)
79 : : {
80 : 2 : ValentFdoSession *self = VALENT_FDO_SESSION (adapter);
81 : :
82 [ - + ]: 2 : g_assert (VALENT_IS_FDO_SESSION (self));
83 : :
84 : 2 : return self->locked;
85 : : }
86 : :
87 : : static void
88 : 1 : valent_fdo_session_set_locked (ValentSessionAdapter *adapter,
89 : : gboolean state)
90 : : {
91 : 1 : ValentFdoSession *self = VALENT_FDO_SESSION (adapter);
92 : :
93 [ - + ]: 1 : g_assert (VALENT_IS_FDO_SESSION (self));
94 [ + - + - : 1 : g_return_if_fail (G_IS_DBUS_PROXY (self->proxy));
- + - - ]
95 : :
96 [ - + ]: 1 : g_dbus_proxy_call (self->proxy,
97 : : state ? "Lock" : "Unlock",
98 : : NULL,
99 : : G_DBUS_CALL_FLAGS_NONE,
100 : : -1,
101 : : NULL,
102 : : NULL,
103 : : NULL);
104 : : }
105 : :
106 : : static void
107 : 1 : new_session_cb (GObject *object,
108 : : GAsyncResult *result,
109 : : gpointer user_data)
110 : : {
111 : 1 : g_autoptr (GTask) task = G_TASK (user_data);
112 : 1 : ValentFdoSession *self = g_task_get_source_object (task);
113 : 1 : g_autoptr (GVariant) active = NULL;
114 [ + - ]: 1 : g_autoptr (GVariant) locked = NULL;
115 [ + - ]: 1 : g_autoptr (GError) error = NULL;
116 : :
117 [ + - + - : 1 : g_assert (G_IS_TASK (task));
- + - - ]
118 [ + - ]: 1 : g_assert (VALENT_IS_FDO_SESSION (self));
119 : :
120 : 1 : self->proxy = g_dbus_proxy_new_for_bus_finish (result, &error);
121 [ - + ]: 1 : if (self->proxy == NULL)
122 : : {
123 : 0 : valent_extension_plugin_state_changed (VALENT_EXTENSION (self),
124 : : VALENT_PLUGIN_STATE_ERROR,
125 : : error);
126 : 0 : g_dbus_error_strip_remote_error (error);
127 : 0 : g_task_return_error (task, g_steal_pointer (&error));
128 [ # # ]: 0 : return;
129 : : }
130 : :
131 : : /* Preload properties and watch for changes
132 : : */
133 : 1 : active = g_dbus_proxy_get_cached_property (self->proxy, "Active");
134 [ + - ]: 1 : if (active != NULL)
135 : 1 : self->active = g_variant_get_boolean (active);
136 : :
137 : 1 : locked = g_dbus_proxy_get_cached_property (self->proxy, "LockedHint");
138 [ + - ]: 1 : if (locked != NULL)
139 : 1 : self->locked = g_variant_get_boolean (locked);
140 : :
141 : 1 : g_signal_connect_object (self->proxy,
142 : : "g-properties-changed",
143 : : G_CALLBACK (on_properties_changed),
144 : : self,
145 : : G_CONNECT_DEFAULT);
146 : 1 : g_signal_connect_object (self->proxy,
147 : : "g-signal",
148 : : G_CALLBACK (on_signal),
149 : : self,
150 : : G_CONNECT_DEFAULT);
151 : :
152 : 1 : valent_extension_plugin_state_changed (VALENT_EXTENSION (self),
153 : : VALENT_PLUGIN_STATE_ACTIVE,
154 : : NULL);
155 [ - + ]: 1 : g_task_return_boolean (task, TRUE);
156 : : }
157 : :
158 : : static void
159 : 1 : get_display_cb (GDBusConnection *connection,
160 : : GAsyncResult *result,
161 : : gpointer user_data)
162 : : {
163 : 1 : g_autoptr (GTask) task = G_TASK (user_data);
164 : 1 : ValentFdoSession *self = g_task_get_source_object (task);
165 : 1 : g_autoptr (GError) error = NULL;
166 [ - + ]: 1 : g_autoptr (GVariant) reply = NULL;
167 [ - - ]: 1 : g_autoptr (GVariant) value = NULL;
168 : 1 : const char *session_id, *object_path;
169 : :
170 [ + - + - : 1 : g_assert (G_IS_TASK (task));
- + - - ]
171 : :
172 : 1 : reply = g_dbus_connection_call_finish (connection, result, &error);
173 [ - + ]: 1 : if (reply == NULL)
174 : : {
175 : 0 : valent_extension_plugin_state_changed (VALENT_EXTENSION (self),
176 : : VALENT_PLUGIN_STATE_ERROR,
177 : : error);
178 : 0 : g_dbus_error_strip_remote_error (error);
179 : 0 : g_task_return_error (task, g_steal_pointer (&error));
180 [ # # ]: 0 : return;
181 : : }
182 : :
183 : 1 : g_variant_get (reply, "(v)", &value);
184 : 1 : g_variant_get (value, "(&s&o)", &session_id, &object_path);
185 [ + - ]: 1 : g_dbus_proxy_new (connection,
186 : : G_DBUS_PROXY_FLAGS_NONE,
187 : : NULL,
188 : : "org.freedesktop.login1",
189 : : object_path,
190 : : "org.freedesktop.login1.Session",
191 : : g_task_get_cancellable (task),
192 : : (GAsyncReadyCallback)new_session_cb,
193 : : g_object_ref (task));
194 : : }
195 : :
196 : : static void
197 : 1 : get_user_cb (GDBusConnection *connection,
198 : : GAsyncResult *result,
199 : : gpointer user_data)
200 : : {
201 : 1 : g_autoptr (GTask) task = G_TASK (user_data);
202 : 1 : ValentFdoSession *self = g_task_get_source_object (task);
203 : 1 : g_autoptr (GError) error = NULL;
204 [ - - - + ]: 1 : g_autoptr (GVariant) reply = NULL;
205 : 1 : const char *object_path;
206 : :
207 [ + - + - : 1 : g_assert (G_IS_TASK (task));
- + - - ]
208 : :
209 : 1 : reply = g_dbus_connection_call_finish (connection, result, &error);
210 [ - + ]: 1 : if (reply == NULL)
211 : : {
212 : 0 : valent_extension_plugin_state_changed (VALENT_EXTENSION (self),
213 : : VALENT_PLUGIN_STATE_ERROR,
214 : : error);
215 : 0 : g_dbus_error_strip_remote_error (error);
216 : 0 : g_task_return_error (task, g_steal_pointer (&error));
217 [ # # ]: 0 : return;
218 : : }
219 : :
220 : 1 : g_variant_get (reply, "(&o)", &object_path);
221 : 1 : g_dbus_connection_call (connection,
222 : : "org.freedesktop.login1",
223 : : object_path,
224 : : "org.freedesktop.DBus.Properties",
225 : : "Get",
226 : : g_variant_new ("(ss)",
227 : : "org.freedesktop.login1.User",
228 : : "Display"),
229 : : G_VARIANT_TYPE ("(v)"),
230 : : G_DBUS_CALL_FLAGS_NONE,
231 : : -1,
232 : : g_task_get_cancellable (task),
233 : : (GAsyncReadyCallback)get_display_cb,
234 : : g_object_ref (task));
235 : : }
236 : :
237 : : /*
238 : : * GAsyncInitable
239 : : */
240 : : static void
241 : 1 : valent_fdo_notifications_init_async (GAsyncInitable *initable,
242 : : int io_priority,
243 : : GCancellable *cancellable,
244 : : GAsyncReadyCallback callback,
245 : : gpointer user_data)
246 : : {
247 : 1 : g_autoptr (GTask) task = NULL;
248 [ - - ]: 1 : g_autoptr (GDBusConnection) connection = NULL;
249 [ - - ]: 1 : g_autoptr (GError) error = NULL;
250 : :
251 [ - + ]: 1 : g_assert (VALENT_IS_FDO_SESSION (initable));
252 : :
253 : 1 : task = g_task_new (initable, cancellable, callback, user_data);
254 : 1 : g_task_set_priority (task, io_priority);
255 [ + - ]: 1 : g_task_set_source_tag (task, valent_fdo_notifications_init_async);
256 : :
257 : : /* Get a system connection and the current user
258 : : */
259 : 1 : connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, &error);
260 [ - + ]: 1 : if (connection == NULL)
261 : : {
262 : 0 : valent_extension_plugin_state_changed (VALENT_EXTENSION (initable),
263 : : VALENT_PLUGIN_STATE_ERROR,
264 : : error);
265 : 0 : g_dbus_error_strip_remote_error (error);
266 : 0 : g_task_return_error (task, g_steal_pointer (&error));
267 [ # # ]: 0 : return;
268 : : }
269 : :
270 [ - + ]: 1 : g_dbus_connection_call (connection,
271 : : "org.freedesktop.login1",
272 : : "/org/freedesktop/login1",
273 : : "org.freedesktop.login1.Manager",
274 : : "GetUser",
275 : : g_variant_new ("(u)", geteuid ()),
276 : : G_VARIANT_TYPE ("(o)"),
277 : : G_DBUS_CALL_FLAGS_NONE,
278 : : -1,
279 : : cancellable,
280 : : (GAsyncReadyCallback)get_user_cb,
281 : : g_steal_pointer (&task));
282 : : }
283 : :
284 : : static void
285 : 2 : g_async_initable_iface_init (GAsyncInitableIface *iface)
286 : : {
287 : 2 : iface->init_async = valent_fdo_notifications_init_async;
288 : 2 : }
289 : :
290 : : /*
291 : : * ValentObject
292 : : */
293 : : static void
294 : 2 : valent_fdo_session_destroy (ValentObject *object)
295 : : {
296 : 2 : ValentFdoSession *self = VALENT_FDO_SESSION (object);
297 : :
298 [ + + ]: 2 : g_clear_object (&self->proxy);
299 : :
300 : 2 : VALENT_OBJECT_CLASS (valent_fdo_session_parent_class)->destroy (object);
301 : 2 : }
302 : :
303 : : /*
304 : : * GObject
305 : : */
306 : : static void
307 : 2 : valent_fdo_session_class_init (ValentFdoSessionClass *klass)
308 : : {
309 : 2 : ValentObjectClass *vobject_class = VALENT_OBJECT_CLASS (klass);
310 : 2 : ValentSessionAdapterClass *session_class = VALENT_SESSION_ADAPTER_CLASS (klass);
311 : :
312 : 2 : vobject_class->destroy = valent_fdo_session_destroy;
313 : :
314 : 2 : session_class->get_active = valent_fdo_session_get_active;
315 : 2 : session_class->get_locked = valent_fdo_session_get_locked;
316 : 2 : session_class->set_locked = valent_fdo_session_set_locked;
317 : : }
318 : :
319 : : static void
320 : 1 : valent_fdo_session_init (ValentFdoSession *self)
321 : : {
322 : 1 : }
323 : :
|