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