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