gio/gdbusaddress: Clean up Win32 code a bit
[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 "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 static void
185 call_notify (GDBusConnection     *con,
186              GApplication        *app,
187              guint32              replace_id,
188              GNotification       *notification,
189              GAsyncReadyCallback  callback,
190              gpointer             user_data)
191 {
192   GVariantBuilder action_builder;
193   guint n_buttons;
194   guint i;
195   GVariantBuilder hints_builder;
196   GIcon *icon;
197   GVariant *parameters;
198   const gchar *body;
199
200   g_variant_builder_init (&action_builder, G_VARIANT_TYPE_STRING_ARRAY);
201   if (g_notification_get_default_action (notification, NULL, NULL))
202     {
203       g_variant_builder_add (&action_builder, "s", "default");
204       g_variant_builder_add (&action_builder, "s", "");
205     }
206
207   n_buttons = g_notification_get_n_buttons (notification);
208   for (i = 0; i < n_buttons; i++)
209     {
210       gchar *label;
211       gchar *action;
212       GVariant *target;
213       gchar *detailed_name;
214
215       g_notification_get_button (notification, i, &label, &action, &target);
216       detailed_name = g_action_print_detailed_name (action, target);
217
218       /* Actions named 'default' collide with libnotify's naming of the
219        * default action. Rewriting them to something unique is enough,
220        * because those actions can never be activated (they aren't
221        * prefixed with 'app.').
222        */
223       if (g_str_equal (detailed_name, "default"))
224         {
225           g_free (detailed_name);
226           detailed_name = g_dbus_generate_guid ();
227         }
228
229       g_variant_builder_add_value (&action_builder, g_variant_new_take_string (detailed_name));
230       g_variant_builder_add_value (&action_builder, g_variant_new_take_string (label));
231
232       g_free (action);
233       if (target)
234         g_variant_unref (target);
235     }
236
237   g_variant_builder_init (&hints_builder, G_VARIANT_TYPE ("a{sv}"));
238   g_variant_builder_add (&hints_builder, "{sv}", "desktop-entry",
239                          g_variant_new_string (g_application_get_application_id (app)));
240   if (g_notification_get_urgent (notification))
241     g_variant_builder_add (&hints_builder, "{sv}", "urgency", g_variant_new_byte (2));
242   icon = g_notification_get_icon (notification);
243   if (icon != NULL && G_IS_FILE_ICON (icon))
244     {
245       GFile *file;
246
247       file = g_file_icon_get_file (G_FILE_ICON (icon));
248       g_variant_builder_add (&hints_builder, "{sv}", "image-path",
249                              g_variant_new_take_string (g_file_get_path (file)));
250     }
251
252   body = g_notification_get_body (notification);
253
254   parameters = g_variant_new ("(susssasa{sv}i)",
255                               "",           /* app name */
256                               replace_id,
257                               "",           /* app icon */
258                               g_notification_get_title (notification),
259                               body ? body : "",
260                               &action_builder,
261                               &hints_builder,
262                               -1);          /* expire_timeout */
263
264   g_dbus_connection_call (con, "org.freedesktop.Notifications", "/org/freedesktop/Notifications",
265                           "org.freedesktop.Notifications", "Notify",
266                           parameters, G_VARIANT_TYPE ("(u)"),
267                           G_DBUS_CALL_FLAGS_NONE, -1, NULL,
268                           callback, user_data);
269 }
270
271 static void
272 notification_sent (GObject      *source_object,
273                    GAsyncResult *result,
274                    gpointer      user_data)
275 {
276   FreedesktopNotification *n = user_data;
277   GVariant *val;
278   GError *error = NULL;
279   static gboolean warning_printed = FALSE;
280
281   val = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), result, &error);
282   if (val)
283     {
284       g_variant_get (val, "(u)", &n->notify_id);
285       g_variant_unref (val);
286     }
287   else
288     {
289       if (!warning_printed)
290         {
291           g_warning ("unable to send notifications through org.freedesktop.Notifications: %s",
292                      error->message);
293           warning_printed = TRUE;
294         }
295
296       n->backend->notifications = g_slist_remove (n->backend->notifications, n);
297       freedesktop_notification_free (n);
298
299       g_error_free (error);
300     }
301 }
302
303 static void
304 g_fdo_notification_backend_dispose (GObject *object)
305 {
306   GFdoNotificationBackend *backend = G_FDO_NOTIFICATION_BACKEND (object);
307
308   if (backend->notify_subscription)
309     {
310       GDBusConnection *session_bus;
311
312       session_bus = G_NOTIFICATION_BACKEND (backend)->dbus_connection;
313       g_dbus_connection_signal_unsubscribe (session_bus, backend->notify_subscription);
314       backend->notify_subscription = 0;
315     }
316
317   if (backend->notifications)
318     {
319       g_slist_free_full (backend->notifications, freedesktop_notification_free);
320       backend->notifications = NULL;
321     }
322
323   G_OBJECT_CLASS (g_fdo_notification_backend_parent_class)->dispose (object);
324 }
325
326 static gboolean
327 g_fdo_notification_backend_is_supported (void)
328 {
329   /* This is the fallback backend with the lowest priority. To avoid an
330    * unnecessary synchronous dbus call to check for
331    * org.freedesktop.Notifications, this function always succeeds. A
332    * warning will be printed when sending the first notification fails.
333    */
334   return TRUE;
335 }
336
337 static void
338 g_fdo_notification_backend_send_notification (GNotificationBackend *backend,
339                                               const gchar          *id,
340                                               GNotification        *notification)
341 {
342   GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
343   FreedesktopNotification *n;
344
345   if (self->notify_subscription == 0)
346     {
347       self->notify_subscription =
348         g_dbus_connection_signal_subscribe (backend->dbus_connection,
349                                             "org.freedesktop.Notifications",
350                                             "org.freedesktop.Notifications", NULL,
351                                             "/org/freedesktop/Notifications", NULL,
352                                             G_DBUS_SIGNAL_FLAGS_NONE,
353                                             notify_signal, backend, NULL);
354     }
355
356   n = g_fdo_notification_backend_find_notification (self, id);
357   if (n == NULL)
358     {
359       n = g_slice_new0 (FreedesktopNotification);
360       n->backend = self;
361       n->id = g_strdup (id);
362       n->notify_id = 0;
363
364       n->backend->notifications = g_slist_prepend (n->backend->notifications, n);
365     }
366   else
367     {
368       /* Only clear default action. All other fields are still valid */
369       g_clear_pointer (&n->default_action, g_free);
370       g_clear_pointer (&n->default_action_target, g_variant_unref);
371     }
372
373   g_notification_get_default_action (notification, &n->default_action, &n->default_action_target);
374
375   call_notify (backend->dbus_connection, backend->application, n->notify_id, notification, notification_sent, n);
376 }
377
378 static void
379 g_fdo_notification_backend_withdraw_notification (GNotificationBackend *backend,
380                                                   const gchar          *id)
381 {
382   GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
383   FreedesktopNotification *n;
384
385   n = g_fdo_notification_backend_find_notification (self, id);
386   if (n)
387     {
388       if (n->notify_id > 0)
389         {
390           g_dbus_connection_call (backend->dbus_connection,
391                                   "org.freedesktop.Notifications",
392                                   "/org/freedesktop/Notifications",
393                                   "org.freedesktop.Notifications", "CloseNotification",
394                                   g_variant_new ("(u)", n->id), NULL,
395                                   G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
396         }
397
398       self->notifications = g_slist_remove (self->notifications, n);
399       freedesktop_notification_free (n);
400     }
401 }
402
403 static void
404 g_fdo_notification_backend_init (GFdoNotificationBackend *backend)
405 {
406 }
407
408 static void
409 g_fdo_notification_backend_class_init (GFdoNotificationBackendClass *class)
410 {
411   GObjectClass *object_class = G_OBJECT_CLASS (class);
412   GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class);
413
414   object_class->dispose = g_fdo_notification_backend_dispose;
415
416   backend_class->is_supported = g_fdo_notification_backend_is_supported;
417   backend_class->send_notification = g_fdo_notification_backend_send_notification;
418   backend_class->withdraw_notification = g_fdo_notification_backend_withdraw_notification;
419 }