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