nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * Copyright © 2013 Lars Uebernickel |
||
3 | * |
||
4 | * This library is free software; you can redistribute it and/or |
||
5 | * modify it under the terms of the GNU Lesser General Public |
||
6 | * License as published by the Free Software Foundation; either |
||
7 | * version 2 of the License, or (at your option) any later version. |
||
8 | * |
||
9 | * This library is distributed in the hope that it will be useful, |
||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||
12 | * Lesser General Public License for more details. |
||
13 | * |
||
14 | * You should have received a copy of the GNU Lesser General |
||
15 | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
||
16 | * |
||
17 | * Authors: Lars Uebernickel <lars@uebernic.de> |
||
18 | */ |
||
19 | |||
20 | #include "config.h" |
||
21 | |||
22 | #include "gnotificationbackend.h" |
||
23 | |||
24 | #include "gapplication.h" |
||
25 | #include "giomodule-priv.h" |
||
26 | #include "gnotification-private.h" |
||
27 | #include "gdbusconnection.h" |
||
28 | #include "gactiongroup.h" |
||
29 | #include "gaction.h" |
||
30 | #include "gthemedicon.h" |
||
31 | #include "gfileicon.h" |
||
32 | #include "gfile.h" |
||
33 | #include "gdbusutils.h" |
||
34 | |||
35 | #define G_TYPE_FDO_NOTIFICATION_BACKEND (g_fdo_notification_backend_get_type ()) |
||
36 | #define G_FDO_NOTIFICATION_BACKEND(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_FDO_NOTIFICATION_BACKEND, GFdoNotificationBackend)) |
||
37 | |||
38 | typedef struct _GFdoNotificationBackend GFdoNotificationBackend; |
||
39 | typedef GNotificationBackendClass GFdoNotificationBackendClass; |
||
40 | |||
41 | struct _GFdoNotificationBackend |
||
42 | { |
||
43 | GNotificationBackend parent; |
||
44 | |||
45 | guint notify_subscription; |
||
46 | GSList *notifications; |
||
47 | }; |
||
48 | |||
49 | GType g_fdo_notification_backend_get_type (void); |
||
50 | |||
51 | G_DEFINE_TYPE_WITH_CODE (GFdoNotificationBackend, g_fdo_notification_backend, G_TYPE_NOTIFICATION_BACKEND, |
||
52 | _g_io_modules_ensure_extension_points_registered (); |
||
53 | g_io_extension_point_implement (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME, |
||
54 | g_define_type_id, "freedesktop", 0)) |
||
55 | |||
56 | typedef struct |
||
57 | { |
||
58 | GFdoNotificationBackend *backend; |
||
59 | gchar *id; |
||
60 | guint32 notify_id; |
||
61 | gchar *default_action; |
||
62 | GVariant *default_action_target; |
||
63 | } FreedesktopNotification; |
||
64 | |||
65 | |||
66 | static void |
||
67 | freedesktop_notification_free (gpointer data) |
||
68 | { |
||
69 | FreedesktopNotification *n = data; |
||
70 | |||
71 | g_free (n->id); |
||
72 | g_free (n->default_action); |
||
73 | if (n->default_action_target) |
||
74 | g_variant_unref (n->default_action_target); |
||
75 | |||
76 | g_slice_free (FreedesktopNotification, n); |
||
77 | } |
||
78 | |||
79 | static FreedesktopNotification * |
||
80 | g_fdo_notification_backend_find_notification (GFdoNotificationBackend *backend, |
||
81 | const gchar *id) |
||
82 | { |
||
83 | GSList *it; |
||
84 | |||
85 | for (it = backend->notifications; it != NULL; it = it->next) |
||
86 | { |
||
87 | FreedesktopNotification *n = it->data; |
||
88 | if (g_str_equal (n->id, id)) |
||
89 | return n; |
||
90 | } |
||
91 | |||
92 | return NULL; |
||
93 | } |
||
94 | |||
95 | static FreedesktopNotification * |
||
96 | g_fdo_notification_backend_find_notification_by_notify_id (GFdoNotificationBackend *backend, |
||
97 | guint32 id) |
||
98 | { |
||
99 | GSList *it; |
||
100 | |||
101 | for (it = backend->notifications; it != NULL; it = it->next) |
||
102 | { |
||
103 | FreedesktopNotification *n = it->data; |
||
104 | if (n->notify_id == id) |
||
105 | return n; |
||
106 | } |
||
107 | |||
108 | return NULL; |
||
109 | } |
||
110 | |||
111 | static void |
||
112 | activate_action (GFdoNotificationBackend *backend, |
||
113 | const gchar *name, |
||
114 | GVariant *parameter) |
||
115 | { |
||
116 | GNotificationBackend *g_backend = G_NOTIFICATION_BACKEND (backend); |
||
117 | |||
118 | if (name) |
||
119 | { |
||
120 | if (g_str_has_prefix (name, "app.")) |
||
121 | g_action_group_activate_action (G_ACTION_GROUP (g_backend->application), name + 4, parameter); |
||
122 | } |
||
123 | else |
||
124 | { |
||
125 | g_application_activate (g_backend->application); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | static void |
||
130 | notify_signal (GDBusConnection *connection, |
||
131 | const gchar *sender_name, |
||
132 | const gchar *object_path, |
||
133 | const gchar *interface_name, |
||
134 | const gchar *signal_name, |
||
135 | GVariant *parameters, |
||
136 | gpointer user_data) |
||
137 | { |
||
138 | GFdoNotificationBackend *backend = user_data; |
||
139 | guint32 id = 0; |
||
140 | const gchar *action = NULL; |
||
141 | FreedesktopNotification *n; |
||
142 | |||
143 | if (g_str_equal (signal_name, "NotificationClosed") && |
||
144 | g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(uu)"))) |
||
145 | { |
||
146 | g_variant_get (parameters, "(uu)", &id, NULL); |
||
147 | } |
||
148 | else if (g_str_equal (signal_name, "ActionInvoked") && |
||
149 | g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(us)"))) |
||
150 | { |
||
151 | g_variant_get (parameters, "(u&s)", &id, &action); |
||
152 | } |
||
153 | else |
||
154 | return; |
||
155 | |||
156 | n = g_fdo_notification_backend_find_notification_by_notify_id (backend, id); |
||
157 | if (n == NULL) |
||
158 | return; |
||
159 | |||
160 | if (action) |
||
161 | { |
||
162 | if (g_str_equal (action, "default")) |
||
163 | { |
||
164 | activate_action (backend, n->default_action, n->default_action_target); |
||
165 | } |
||
166 | else |
||
167 | { |
||
168 | gchar *name; |
||
169 | GVariant *target; |
||
170 | |||
171 | if (g_action_parse_detailed_name (action, &name, &target, NULL)) |
||
172 | { |
||
173 | activate_action (backend, name, target); |
||
174 | g_free (name); |
||
175 | if (target) |
||
176 | g_variant_unref (target); |
||
177 | } |
||
178 | } |
||
179 | } |
||
180 | |||
181 | backend->notifications = g_slist_remove (backend->notifications, n); |
||
182 | freedesktop_notification_free (n); |
||
183 | } |
||
184 | |||
185 | /* Converts a GNotificationPriority to an urgency level as defined by |
||
186 | * the freedesktop spec (0: low, 1: normal, 2: critical). |
||
187 | */ |
||
188 | static guchar |
||
189 | urgency_from_priority (GNotificationPriority priority) |
||
190 | { |
||
191 | switch (priority) |
||
192 | { |
||
193 | case G_NOTIFICATION_PRIORITY_LOW: |
||
194 | return 0; |
||
195 | |||
196 | default: |
||
197 | case G_NOTIFICATION_PRIORITY_NORMAL: |
||
198 | return 1; |
||
199 | |||
200 | case G_NOTIFICATION_PRIORITY_HIGH: |
||
201 | case G_NOTIFICATION_PRIORITY_URGENT: |
||
202 | return 2; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | static void |
||
207 | call_notify (GDBusConnection *con, |
||
208 | GApplication *app, |
||
209 | guint32 replace_id, |
||
210 | GNotification *notification, |
||
211 | GAsyncReadyCallback callback, |
||
212 | gpointer user_data) |
||
213 | { |
||
214 | GVariantBuilder action_builder; |
||
215 | guint n_buttons; |
||
216 | guint i; |
||
217 | GVariantBuilder hints_builder; |
||
218 | GIcon *icon; |
||
219 | GVariant *parameters; |
||
220 | const gchar *body; |
||
221 | guchar urgency; |
||
222 | |||
223 | g_variant_builder_init (&action_builder, G_VARIANT_TYPE_STRING_ARRAY); |
||
224 | if (g_notification_get_default_action (notification, NULL, NULL)) |
||
225 | { |
||
226 | g_variant_builder_add (&action_builder, "s", "default"); |
||
227 | g_variant_builder_add (&action_builder, "s", ""); |
||
228 | } |
||
229 | |||
230 | n_buttons = g_notification_get_n_buttons (notification); |
||
231 | for (i = 0; i < n_buttons; i++) |
||
232 | { |
||
233 | gchar *label; |
||
234 | gchar *action; |
||
235 | GVariant *target; |
||
236 | gchar *detailed_name; |
||
237 | |||
238 | g_notification_get_button (notification, i, &label, &action, &target); |
||
239 | detailed_name = g_action_print_detailed_name (action, target); |
||
240 | |||
241 | /* Actions named 'default' collide with libnotify's naming of the |
||
242 | * default action. Rewriting them to something unique is enough, |
||
243 | * because those actions can never be activated (they aren't |
||
244 | * prefixed with 'app.'). |
||
245 | */ |
||
246 | if (g_str_equal (detailed_name, "default")) |
||
247 | { |
||
248 | g_free (detailed_name); |
||
249 | detailed_name = g_dbus_generate_guid (); |
||
250 | } |
||
251 | |||
252 | g_variant_builder_add_value (&action_builder, g_variant_new_take_string (detailed_name)); |
||
253 | g_variant_builder_add_value (&action_builder, g_variant_new_take_string (label)); |
||
254 | |||
255 | g_free (action); |
||
256 | if (target) |
||
257 | g_variant_unref (target); |
||
258 | } |
||
259 | |||
260 | g_variant_builder_init (&hints_builder, G_VARIANT_TYPE ("a{sv}")); |
||
261 | g_variant_builder_add (&hints_builder, "{sv}", "desktop-entry", |
||
262 | g_variant_new_string (g_application_get_application_id (app))); |
||
263 | urgency = urgency_from_priority (g_notification_get_priority (notification)); |
||
264 | g_variant_builder_add (&hints_builder, "{sv}", "urgency", g_variant_new_byte (urgency)); |
||
265 | icon = g_notification_get_icon (notification); |
||
266 | if (icon != NULL) |
||
267 | { |
||
268 | if (G_IS_FILE_ICON (icon)) |
||
269 | { |
||
270 | GFile *file; |
||
271 | |||
272 | file = g_file_icon_get_file (G_FILE_ICON (icon)); |
||
273 | g_variant_builder_add (&hints_builder, "{sv}", "image-path", |
||
274 | g_variant_new_take_string (g_file_get_path (file))); |
||
275 | } |
||
276 | else if (G_IS_THEMED_ICON (icon)) |
||
277 | { |
||
278 | const gchar* const* icon_names = g_themed_icon_get_names(G_THEMED_ICON (icon)); |
||
279 | /* Take first name from GThemedIcon */ |
||
280 | g_variant_builder_add (&hints_builder, "{sv}", "image-path", |
||
281 | g_variant_new_string (icon_names[0])); |
||
282 | } |
||
283 | } |
||
284 | |||
285 | body = g_notification_get_body (notification); |
||
286 | |||
287 | parameters = g_variant_new ("(susssasa{sv}i)", |
||
288 | "", /* app name */ |
||
289 | replace_id, |
||
290 | "", /* app icon */ |
||
291 | g_notification_get_title (notification), |
||
292 | body ? body : "", |
||
293 | &action_builder, |
||
294 | &hints_builder, |
||
295 | -1); /* expire_timeout */ |
||
296 | |||
297 | g_dbus_connection_call (con, "org.freedesktop.Notifications", "/org/freedesktop/Notifications", |
||
298 | "org.freedesktop.Notifications", "Notify", |
||
299 | parameters, G_VARIANT_TYPE ("(u)"), |
||
300 | G_DBUS_CALL_FLAGS_NONE, -1, NULL, |
||
301 | callback, user_data); |
||
302 | } |
||
303 | |||
304 | static void |
||
305 | notification_sent (GObject *source_object, |
||
306 | GAsyncResult *result, |
||
307 | gpointer user_data) |
||
308 | { |
||
309 | FreedesktopNotification *n = user_data; |
||
310 | GVariant *val; |
||
311 | GError *error = NULL; |
||
312 | static gboolean warning_printed = FALSE; |
||
313 | |||
314 | val = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), result, &error); |
||
315 | if (val) |
||
316 | { |
||
317 | g_variant_get (val, "(u)", &n->notify_id); |
||
318 | g_variant_unref (val); |
||
319 | } |
||
320 | else |
||
321 | { |
||
322 | if (!warning_printed) |
||
323 | { |
||
324 | g_warning ("unable to send notifications through org.freedesktop.Notifications: %s", |
||
325 | error->message); |
||
326 | warning_printed = TRUE; |
||
327 | } |
||
328 | |||
329 | n->backend->notifications = g_slist_remove (n->backend->notifications, n); |
||
330 | freedesktop_notification_free (n); |
||
331 | |||
332 | g_error_free (error); |
||
333 | } |
||
334 | } |
||
335 | |||
336 | static void |
||
337 | g_fdo_notification_backend_dispose (GObject *object) |
||
338 | { |
||
339 | GFdoNotificationBackend *backend = G_FDO_NOTIFICATION_BACKEND (object); |
||
340 | |||
341 | if (backend->notify_subscription) |
||
342 | { |
||
343 | GDBusConnection *session_bus; |
||
344 | |||
345 | session_bus = G_NOTIFICATION_BACKEND (backend)->dbus_connection; |
||
346 | g_dbus_connection_signal_unsubscribe (session_bus, backend->notify_subscription); |
||
347 | backend->notify_subscription = 0; |
||
348 | } |
||
349 | |||
350 | if (backend->notifications) |
||
351 | { |
||
352 | g_slist_free_full (backend->notifications, freedesktop_notification_free); |
||
353 | backend->notifications = NULL; |
||
354 | } |
||
355 | |||
356 | G_OBJECT_CLASS (g_fdo_notification_backend_parent_class)->dispose (object); |
||
357 | } |
||
358 | |||
359 | static gboolean |
||
360 | g_fdo_notification_backend_is_supported (void) |
||
361 | { |
||
362 | /* This is the fallback backend with the lowest priority. To avoid an |
||
363 | * unnecessary synchronous dbus call to check for |
||
364 | * org.freedesktop.Notifications, this function always succeeds. A |
||
365 | * warning will be printed when sending the first notification fails. |
||
366 | */ |
||
367 | return TRUE; |
||
368 | } |
||
369 | |||
370 | static void |
||
371 | g_fdo_notification_backend_send_notification (GNotificationBackend *backend, |
||
372 | const gchar *id, |
||
373 | GNotification *notification) |
||
374 | { |
||
375 | GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend); |
||
376 | FreedesktopNotification *n; |
||
377 | |||
378 | if (self->notify_subscription == 0) |
||
379 | { |
||
380 | self->notify_subscription = |
||
381 | g_dbus_connection_signal_subscribe (backend->dbus_connection, |
||
382 | "org.freedesktop.Notifications", |
||
383 | "org.freedesktop.Notifications", NULL, |
||
384 | "/org/freedesktop/Notifications", NULL, |
||
385 | G_DBUS_SIGNAL_FLAGS_NONE, |
||
386 | notify_signal, backend, NULL); |
||
387 | } |
||
388 | |||
389 | n = g_fdo_notification_backend_find_notification (self, id); |
||
390 | if (n == NULL) |
||
391 | { |
||
392 | n = g_slice_new0 (FreedesktopNotification); |
||
393 | n->backend = self; |
||
394 | n->id = g_strdup (id); |
||
395 | n->notify_id = 0; |
||
396 | |||
397 | n->backend->notifications = g_slist_prepend (n->backend->notifications, n); |
||
398 | } |
||
399 | else |
||
400 | { |
||
401 | /* Only clear default action. All other fields are still valid */ |
||
402 | g_clear_pointer (&n->default_action, g_free); |
||
403 | g_clear_pointer (&n->default_action_target, g_variant_unref); |
||
404 | } |
||
405 | |||
406 | g_notification_get_default_action (notification, &n->default_action, &n->default_action_target); |
||
407 | |||
408 | call_notify (backend->dbus_connection, backend->application, n->notify_id, notification, notification_sent, n); |
||
409 | } |
||
410 | |||
411 | static void |
||
412 | g_fdo_notification_backend_withdraw_notification (GNotificationBackend *backend, |
||
413 | const gchar *id) |
||
414 | { |
||
415 | GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend); |
||
416 | FreedesktopNotification *n; |
||
417 | |||
418 | n = g_fdo_notification_backend_find_notification (self, id); |
||
419 | if (n) |
||
420 | { |
||
421 | if (n->notify_id > 0) |
||
422 | { |
||
423 | g_dbus_connection_call (backend->dbus_connection, |
||
424 | "org.freedesktop.Notifications", |
||
425 | "/org/freedesktop/Notifications", |
||
426 | "org.freedesktop.Notifications", "CloseNotification", |
||
427 | g_variant_new ("(u)", n->id), NULL, |
||
428 | G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL); |
||
429 | } |
||
430 | |||
431 | self->notifications = g_slist_remove (self->notifications, n); |
||
432 | freedesktop_notification_free (n); |
||
433 | } |
||
434 | } |
||
435 | |||
436 | static void |
||
437 | g_fdo_notification_backend_init (GFdoNotificationBackend *backend) |
||
438 | { |
||
439 | } |
||
440 | |||
441 | static void |
||
442 | g_fdo_notification_backend_class_init (GFdoNotificationBackendClass *class) |
||
443 | { |
||
444 | GObjectClass *object_class = G_OBJECT_CLASS (class); |
||
445 | GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class); |
||
446 | |||
447 | object_class->dispose = g_fdo_notification_backend_dispose; |
||
448 | |||
449 | backend_class->is_supported = g_fdo_notification_backend_is_supported; |
||
450 | backend_class->send_notification = g_fdo_notification_backend_send_notification; |
||
451 | backend_class->withdraw_notification = g_fdo_notification_backend_withdraw_notification; |
||
452 | } |