f02597d0158fcd1de805c5c8b7c5771bc643e993
[platform/upstream/glib.git] / gio / ggtknotificationbackend.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 "config.h"
23 #include "gnotificationbackend.h"
24
25 #include "giomodule-priv.h"
26 #include "gdbusconnection.h"
27 #include "gapplication.h"
28 #include "gnotification-private.h"
29
30 #define G_TYPE_GTK_NOTIFICATION_BACKEND  (g_gtk_notification_backend_get_type ())
31 #define G_GTK_NOTIFICATION_BACKEND(o)    (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_GTK_NOTIFICATION_BACKEND, GGtkNotificationBackend))
32
33 typedef struct _GGtkNotificationBackend GGtkNotificationBackend;
34 typedef GNotificationBackendClass       GGtkNotificationBackendClass;
35
36 struct _GGtkNotificationBackend
37 {
38   GNotificationBackend parent;
39 };
40
41 GType g_gtk_notification_backend_get_type (void);
42
43 G_DEFINE_TYPE_WITH_CODE (GGtkNotificationBackend, g_gtk_notification_backend, G_TYPE_NOTIFICATION_BACKEND,
44   _g_io_modules_ensure_extension_points_registered ();
45   g_io_extension_point_implement (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME,
46                                  g_define_type_id, "gtk", 100))
47
48 static gboolean
49 g_gtk_notification_backend_is_supported (void)
50 {
51   GDBusConnection *session_bus;
52   GVariant *reply;
53
54   /* Find out if the notification server is running. This is a
55    * synchronous call because gio extension points don't support asnyc
56    * backend verification. This is only run once and only contacts the
57    * dbus daemon. */
58
59   session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
60   if (session_bus == NULL)
61     return FALSE;
62
63   reply = g_dbus_connection_call_sync (session_bus, "org.freedesktop.DBus", "/", "org.freedesktop.DBus",
64                                        "GetNameOwner", g_variant_new ("(s)", "org.gtk.Notifications"),
65                                        G_VARIANT_TYPE ("(s)"), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
66
67   g_object_unref (session_bus);
68
69   if (reply)
70     {
71       g_variant_unref (reply);
72       return TRUE;
73     }
74   else
75     return FALSE;
76 }
77
78 static void
79 g_gtk_notification_backend_send_notification (GNotificationBackend *backend,
80                                               const gchar          *id,
81                                               GNotification        *notification)
82 {
83   GVariant *params;
84
85   params = g_variant_new ("(ss@a{sv})", g_application_get_application_id (backend->application),
86                                         id,
87                                         g_notification_serialize (notification));
88
89   g_dbus_connection_call (backend->dbus_connection,
90                           "org.gtk.Notifications", "/org/gtk/Notifications",
91                           "org.gtk.Notifications", "AddNotification", params,
92                           G_VARIANT_TYPE_UNIT,
93                           G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
94 }
95
96 static void
97 g_gtk_notification_backend_withdraw_notification (GNotificationBackend *backend,
98                                                   const gchar          *id)
99 {
100   GVariant *params;
101
102   params = g_variant_new ("(ss)", g_application_get_application_id (backend->application), id);
103
104   g_dbus_connection_call (backend->dbus_connection, "org.gtk.Notifications",
105                           "/org/gtk/Notifications", "org.gtk.Notifications",
106                           "RemoveNotification", params, G_VARIANT_TYPE_UNIT,
107                           G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
108 }
109
110 static void
111 g_gtk_notification_backend_init (GGtkNotificationBackend *backend)
112 {
113 }
114
115 static void
116 g_gtk_notification_backend_class_init (GGtkNotificationBackendClass *class)
117 {
118   GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class);
119
120   backend_class->is_supported = g_gtk_notification_backend_is_supported;
121   backend_class->send_notification = g_gtk_notification_backend_send_notification;
122   backend_class->withdraw_notification = g_gtk_notification_backend_withdraw_notification;
123 }