Tests: add session_bus_run() and use it where possible
[glib.git] / gio / ggtknotificationbackend.c
blobbc13b7d932777cb95b78fb9c60971bc179aa20c8
1 /*
2 * Copyright © 2013 Lars Uebernickel
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.
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.
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.
19 * Authors: Lars Uebernickel <lars@uebernic.de>
22 #include "gnotificationbackend.h"
24 #include "giomodule-priv.h"
25 #include "gdbusconnection.h"
26 #include "gapplication.h"
27 #include "gnotification-private.h"
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))
32 typedef struct _GGtkNotificationBackend GGtkNotificationBackend;
33 typedef GNotificationBackendClass GGtkNotificationBackendClass;
35 struct _GGtkNotificationBackend
37 GNotificationBackend parent;
40 GType g_gtk_notification_backend_get_type (void);
42 G_DEFINE_TYPE_WITH_CODE (GGtkNotificationBackend, g_gtk_notification_backend, G_TYPE_NOTIFICATION_BACKEND,
43 _g_io_modules_ensure_extension_points_registered ();
44 g_io_extension_point_implement (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME,
45 g_define_type_id, "gtk", 100))
47 static gboolean
48 g_gtk_notification_backend_is_supported (void)
50 GDBusConnection *session_bus;
51 GVariant *reply;
53 /* Find out if the notification server is running. This is a
54 * synchronous call because gio extension points don't support asnyc
55 * backend verification. This is only run once and only contacts the
56 * dbus daemon. */
58 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
59 if (session_bus == NULL)
60 return FALSE;
62 reply = g_dbus_connection_call_sync (session_bus, "org.freedesktop.DBus", "/", "org.freedesktop.DBus",
63 "GetNameOwner", g_variant_new ("(s)", "org.gtk.Notifications"),
64 G_VARIANT_TYPE ("(s)"), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
66 g_object_unref (session_bus);
68 if (reply)
70 g_variant_unref (reply);
71 return TRUE;
73 else
74 return FALSE;
77 static void
78 g_gtk_notification_backend_send_notification (GNotificationBackend *backend,
79 const gchar *id,
80 GNotification *notification)
82 GVariant *params;
84 params = g_variant_new ("(ss@a{sv})", g_application_get_application_id (backend->application),
85 id,
86 g_notification_serialize (notification));
88 g_dbus_connection_call (backend->dbus_connection,
89 "org.gtk.Notifications", "/org/gtk/Notifications",
90 "org.gtk.Notifications", "AddNotification", params,
91 G_VARIANT_TYPE_UNIT,
92 G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
95 static void
96 g_gtk_notification_backend_withdraw_notification (GNotificationBackend *backend,
97 const gchar *id)
99 GVariant *params;
101 params = g_variant_new ("(ss)", g_application_get_application_id (backend->application), id);
103 g_dbus_connection_call (backend->dbus_connection, "org.gtk.Notifications",
104 "/org/gtk/Notifications", "org.gtk.Notifications",
105 "RemoveNotification", params, G_VARIANT_TYPE_UNIT,
106 G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
109 static void
110 g_gtk_notification_backend_init (GGtkNotificationBackend *backend)
114 static void
115 g_gtk_notification_backend_class_init (GGtkNotificationBackendClass *class)
117 GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class);
119 backend_class->is_supported = g_gtk_notification_backend_is_supported;
120 backend_class->send_notification = g_gtk_notification_backend_send_notification;
121 backend_class->withdraw_notification = g_gtk_notification_backend_withdraw_notification;