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-gtk-notifications"
5 : :
6 : : #include "config.h"
7 : :
8 : : #include <time.h>
9 : :
10 : : #include <gio/gdesktopappinfo.h>
11 : : #include <gio/gio.h>
12 : : #include <valent.h>
13 : :
14 : : #include "valent-gtk-notifications.h"
15 : :
16 : :
17 : : struct _ValentGtkNotifications
18 : : {
19 : : ValentNotificationsAdapter parent_instance;
20 : :
21 : : GDBusInterfaceVTable vtable;
22 : : GDBusNodeInfo *node_info;
23 : : GDBusInterfaceInfo *iface_info;
24 : : GDBusConnection *monitor;
25 : : unsigned int monitor_id;
26 : : char *name_owner;
27 : : unsigned int name_owner_id;
28 : : };
29 : :
30 : : static void g_async_initable_iface_init (GAsyncInitableIface *iface);
31 : :
32 [ + + + - ]: 10 : G_DEFINE_FINAL_TYPE_WITH_CODE (ValentGtkNotifications, valent_gtk_notifications, VALENT_TYPE_NOTIFICATIONS_ADAPTER,
33 : : G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, g_async_initable_iface_init))
34 : :
35 : :
36 : : /*
37 : : * GDBusInterfaceSkeleton
38 : : */
39 : : static const char interface_xml[] =
40 : : "<node>"
41 : : " <interface name='org.gtk.Notifications'>"
42 : : " <method name='AddNotification'>"
43 : : " <arg name='applicationId' type='s' direction='in'/>"
44 : : " <arg name='notificationId' type='s' direction='in'/>"
45 : : " <arg name='parameters' type='a{sv}' direction='in'/>"
46 : : " </method>"
47 : : " <method name='RemoveNotification'>"
48 : : " <arg name='applicationId' type='s' direction='in'/>"
49 : : " <arg name='notificationId' type='s' direction='in'/>"
50 : : " </method>"
51 : : " </interface>"
52 : : "</node>";
53 : :
54 : : static const char *interface_matches[] = {
55 : : "interface='org.gtk.Notifications',member='AddNotification',type='method_call'",
56 : : "interface='org.gtk.Notifications',member='RemoveNotification',type='method_call'",
57 : : NULL
58 : : };
59 : :
60 : :
61 : : static void
62 : 1 : _add_notification (ValentNotificationsAdapter *adapter,
63 : : GVariant *parameters)
64 : : {
65 : 1 : g_autoptr (ValentNotification) notification = NULL;
66 [ + - ]: 1 : g_autofree char *desktop_id = NULL;
67 : 1 : g_autoptr (GDesktopAppInfo) desktop_info = NULL;
68 [ - + ]: 1 : g_autoptr (GVariant) props = NULL;
69 : 1 : const char *app_id;
70 : 1 : const char *notif_id;
71 : :
72 : : /* Extract what we need from the parameters */
73 : 1 : g_variant_get (parameters, "(&s&s@a{sv})", &app_id, ¬if_id, &props);
74 : :
75 : : /* Ignore our own notifications */
76 [ - + ]: 1 : if (g_str_equal (app_id, APPLICATION_ID))
77 [ # # ]: 0 : return;
78 : :
79 : : /* Deserialize GNotification into ValentNotification */
80 : 1 : notification = valent_notification_deserialize (props);
81 : 1 : valent_notification_set_id (notification, notif_id);
82 : :
83 : : /* Set a timestamp */
84 : 1 : valent_notification_set_time (notification, valent_timestamp_ms ());
85 : :
86 : : /* Try and get an application name */
87 : 1 : desktop_id = g_strdup_printf ("%s.desktop", app_id);
88 : :
89 [ - + ]: 1 : if ((desktop_info = g_desktop_app_info_new (desktop_id)))
90 : : {
91 : 0 : const char *app_name;
92 : :
93 : 0 : app_name = g_app_info_get_display_name (G_APP_INFO (desktop_info));
94 : 0 : valent_notification_set_application (notification, app_name);
95 : : }
96 : :
97 [ + - ]: 1 : valent_notifications_adapter_notification_added (adapter, notification);
98 : : }
99 : :
100 : : static void
101 : 1 : _remove_notification (ValentNotificationsAdapter *adapter,
102 : : GVariant *parameters)
103 : : {
104 : 1 : const char *app_id;
105 : 1 : const char *notif_id;
106 : :
107 : 1 : g_variant_get (parameters, "(&s&s)", &app_id, ¬if_id);
108 : :
109 : : /* Ignore our own notifications */
110 [ - + ]: 1 : if (g_str_equal (app_id, APPLICATION_ID))
111 : 0 : return;
112 : :
113 : 1 : valent_notifications_adapter_notification_removed (adapter, notif_id);
114 : : }
115 : :
116 : : static void
117 : 2 : valent_gtk_notifications_method_call (GDBusConnection *connection,
118 : : const char *sender,
119 : : const char *object_path,
120 : : const char *interface_name,
121 : : const char *method_name,
122 : : GVariant *parameters,
123 : : GDBusMethodInvocation *invocation,
124 : : gpointer user_data)
125 : : {
126 : 2 : ValentNotificationsAdapter *adapter = VALENT_NOTIFICATIONS_ADAPTER (user_data);
127 : 2 : ValentGtkNotifications *self = VALENT_GTK_NOTIFICATIONS (user_data);
128 : 2 : GDBusMessage *message;
129 : 2 : const char *destination;
130 : :
131 [ - + ]: 2 : g_assert (VALENT_IS_GTK_NOTIFICATIONS (adapter));
132 : :
133 : 2 : message = g_dbus_method_invocation_get_message (invocation);
134 : 2 : destination = g_dbus_message_get_destination (message);
135 : :
136 [ - + - - ]: 2 : if (g_strcmp0 ("org.gtk.Notifications", destination) != 0 &&
137 : 0 : g_strcmp0 (self->name_owner, destination) != 0)
138 : 0 : goto out;
139 : :
140 [ + + ]: 2 : if (g_strcmp0 (method_name, "AddNotification") == 0)
141 : 1 : _add_notification (adapter, parameters);
142 : :
143 [ - + ]: 1 : else if (g_strcmp0 (method_name, "RemoveNotification") == 0)
144 : 1 : _remove_notification (adapter, parameters);
145 : :
146 : 0 : out:
147 : 2 : g_object_unref (invocation);
148 : 2 : }
149 : :
150 : : /*
151 : : * Setup
152 : : */
153 : : static void
154 : 1 : on_name_appeared (GDBusConnection *connection,
155 : : const char *name,
156 : : const char *name_owner,
157 : : gpointer user_data)
158 : : {
159 : 1 : ValentGtkNotifications *self = VALENT_GTK_NOTIFICATIONS (user_data);
160 : :
161 [ - + ]: 1 : g_assert (VALENT_IS_GTK_NOTIFICATIONS (self));
162 : :
163 [ - + ]: 1 : self->name_owner = g_strdup (name_owner);
164 : 1 : valent_extension_plugin_state_changed (VALENT_EXTENSION (self),
165 : : VALENT_PLUGIN_STATE_ACTIVE,
166 : : NULL);
167 : 1 : }
168 : :
169 : : static void
170 : 0 : on_name_vanished (GDBusConnection *connection,
171 : : const char *name,
172 : : gpointer user_data)
173 : : {
174 : 0 : ValentGtkNotifications *self = VALENT_GTK_NOTIFICATIONS (user_data);
175 : :
176 [ # # ]: 0 : g_assert (VALENT_IS_GTK_NOTIFICATIONS (self));
177 : :
178 [ # # ]: 0 : g_clear_pointer (&self->name_owner, g_free);
179 : 0 : valent_extension_plugin_state_changed (VALENT_EXTENSION (self),
180 : : VALENT_PLUGIN_STATE_INACTIVE,
181 : : NULL);
182 : 0 : }
183 : :
184 : : static void
185 : 1 : become_monitor_cb (GDBusConnection *connection,
186 : : GAsyncResult *result,
187 : : gpointer user_data)
188 : : {
189 : 1 : g_autoptr (GTask) task = G_TASK (user_data);
190 : 1 : ValentGtkNotifications *self = g_task_get_source_object (task);
191 [ - - + - ]: 1 : g_autoptr (GVariant) reply = NULL;
192 [ - - ]: 1 : g_autoptr (GError) error = NULL;
193 : :
194 : 1 : reply = g_dbus_connection_call_finish (connection, result, &error);
195 [ - + ]: 1 : if (reply == NULL)
196 : : {
197 : 0 : valent_extension_plugin_state_changed (VALENT_EXTENSION (self),
198 : : VALENT_PLUGIN_STATE_ERROR,
199 : : error);
200 [ # # ]: 0 : g_clear_object (&self->monitor);
201 : 0 : g_dbus_error_strip_remote_error (error);
202 : 0 : g_task_return_error (task, g_steal_pointer (&error));
203 [ # # ]: 0 : return;
204 : : }
205 : :
206 : : /* Watch the true name owner*/
207 : 1 : self->name_owner_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
208 : : "org.gtk.Notifications",
209 : : G_BUS_NAME_WATCHER_FLAGS_NONE,
210 : : on_name_appeared,
211 : : on_name_vanished,
212 : : self, NULL);
213 : :
214 [ - + ]: 1 : g_task_return_boolean (task, TRUE);
215 : : }
216 : :
217 : : static void
218 : 1 : new_for_address_cb (GObject *object,
219 : : GAsyncResult *result,
220 : : gpointer user_data)
221 : : {
222 : 1 : g_autoptr (GTask) task = G_TASK (user_data);
223 : 1 : ValentGtkNotifications *self = g_task_get_source_object (task);
224 : 1 : GCancellable *cancellable = g_task_get_cancellable (task);
225 [ - - ]: 1 : g_autoptr (GError) error = NULL;
226 : :
227 : 1 : self->monitor = g_dbus_connection_new_for_address_finish (result, &error);
228 : :
229 [ - + ]: 1 : if (self->monitor == NULL)
230 : : {
231 : 0 : valent_extension_plugin_state_changed (VALENT_EXTENSION (self),
232 : : VALENT_PLUGIN_STATE_ERROR,
233 : : error);
234 : 0 : g_dbus_error_strip_remote_error (error);
235 : 0 : g_task_return_error (task, g_steal_pointer (&error));
236 : 0 : return;
237 : : }
238 : :
239 : : /* Export the interface and register as a monitor
240 : : */
241 : 2 : self->monitor_id =
242 : 1 : g_dbus_connection_register_object (self->monitor,
243 : : "/org/gtk/Notifications",
244 : : self->iface_info,
245 : 1 : &self->vtable,
246 : : self, NULL,
247 : : &error);
248 : :
249 [ - + ]: 1 : if (self->monitor_id == 0)
250 : : {
251 : 0 : valent_extension_plugin_state_changed (VALENT_EXTENSION (self),
252 : : VALENT_PLUGIN_STATE_ERROR,
253 : : error);
254 [ # # ]: 0 : g_clear_object (&self->monitor);
255 : 0 : g_dbus_error_strip_remote_error (error);
256 : 0 : g_task_return_error (task, g_steal_pointer (&error));
257 : 0 : return;
258 : : }
259 : :
260 [ - + ]: 1 : g_dbus_connection_call (self->monitor,
261 : : "org.freedesktop.DBus",
262 : : "/org/freedesktop/DBus",
263 : : "org.freedesktop.DBus.Monitoring",
264 : : "BecomeMonitor",
265 : : g_variant_new ("(^asu)", interface_matches, 0),
266 : : NULL,
267 : : G_DBUS_CALL_FLAGS_NONE,
268 : : -1,
269 : : cancellable,
270 : : (GAsyncReadyCallback)become_monitor_cb,
271 : : g_steal_pointer (&task));
272 : : }
273 : :
274 : :
275 : : /*
276 : : * GAsyncInitable
277 : : */
278 : : static void
279 : 1 : valent_gtk_notifications_init_async (GAsyncInitable *initable,
280 : : int io_priority,
281 : : GCancellable *cancellable,
282 : : GAsyncReadyCallback callback,
283 : : gpointer user_data)
284 : : {
285 : 1 : g_autoptr (GTask) task = NULL;
286 [ - - ]: 1 : g_autofree char *address = NULL;
287 : 1 : g_autoptr (GError) error = NULL;
288 : :
289 [ - + ]: 1 : g_assert (VALENT_IS_GTK_NOTIFICATIONS (initable));
290 : :
291 : 1 : task = g_task_new (initable, cancellable, callback, user_data);
292 : 1 : g_task_set_priority (task, io_priority);
293 [ + - ]: 1 : g_task_set_source_tag (task, valent_gtk_notifications_init_async);
294 : :
295 : : /* Get a bus address and dedicated monitoring connection
296 : : */
297 : 1 : address = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION,
298 : : cancellable,
299 : : &error);
300 [ - + ]: 1 : if (address == NULL)
301 : : {
302 : 0 : valent_extension_plugin_state_changed (VALENT_EXTENSION (initable),
303 : : VALENT_PLUGIN_STATE_ERROR,
304 : : error);
305 : 0 : g_dbus_error_strip_remote_error (error);
306 : 0 : g_task_return_error (task, g_steal_pointer (&error));
307 [ # # ]: 0 : return;
308 : : }
309 : :
310 [ - + ]: 1 : g_dbus_connection_new_for_address (address,
311 : : G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
312 : : G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
313 : : NULL,
314 : : cancellable,
315 : : (GAsyncReadyCallback)new_for_address_cb,
316 : : g_steal_pointer (&task));
317 : : }
318 : :
319 : : static void
320 : 2 : g_async_initable_iface_init (GAsyncInitableIface *iface)
321 : : {
322 : 2 : iface->init_async = valent_gtk_notifications_init_async;
323 : 2 : }
324 : :
325 : : /*
326 : : * ValentObject
327 : : */
328 : : static void
329 : 2 : valent_gtk_notifications_destroy (ValentObject *object)
330 : : {
331 : 2 : ValentGtkNotifications *self = VALENT_GTK_NOTIFICATIONS (object);
332 : :
333 [ + + ]: 2 : if (self->name_owner_id > 0)
334 : : {
335 : 1 : g_clear_handle_id (&self->name_owner_id, g_bus_unwatch_name);
336 [ + - ]: 1 : g_clear_pointer (&self->name_owner, g_free);
337 : : }
338 : :
339 [ + + ]: 2 : if (self->monitor_id != 0)
340 : : {
341 : 1 : g_dbus_connection_unregister_object (self->monitor, self->monitor_id);
342 : 1 : self->monitor_id = 0;
343 : : }
344 : :
345 [ + + ]: 2 : g_clear_object (&self->monitor);
346 : :
347 : 2 : VALENT_OBJECT_CLASS (valent_gtk_notifications_parent_class)->destroy (object);
348 : 2 : }
349 : :
350 : : /*
351 : : * GObject
352 : : */
353 : : static void
354 : 1 : valent_gtk_notifications_finalize (GObject *object)
355 : : {
356 : 1 : ValentGtkNotifications *self = VALENT_GTK_NOTIFICATIONS (object);
357 : :
358 [ + - ]: 1 : g_clear_pointer (&self->node_info, g_dbus_node_info_unref);
359 : :
360 : 1 : G_OBJECT_CLASS (valent_gtk_notifications_parent_class)->finalize(object);
361 : 1 : }
362 : :
363 : : static void
364 : 2 : valent_gtk_notifications_class_init (ValentGtkNotificationsClass *klass)
365 : : {
366 : 2 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
367 : 2 : ValentObjectClass *vobject_class = VALENT_OBJECT_CLASS (klass);
368 : :
369 : 2 : object_class->finalize = valent_gtk_notifications_finalize;
370 : :
371 : 2 : vobject_class->destroy = valent_gtk_notifications_destroy;
372 : : }
373 : :
374 : : static void
375 : 1 : valent_gtk_notifications_init (ValentGtkNotifications *self)
376 : : {
377 : 1 : self->node_info = g_dbus_node_info_new_for_xml (interface_xml, NULL);
378 : 1 : self->iface_info = self->node_info->interfaces[0];
379 : :
380 : 1 : self->vtable.method_call = valent_gtk_notifications_method_call;
381 : 1 : self->vtable.get_property = NULL;
382 : 1 : self->vtable.set_property = NULL;
383 : 1 : }
384 : :
|