2 * Copyright © 2013 Lars Uebernickel
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2 of the licence or (at
7 * 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 "gnotification-server.h"
26 typedef GObjectClass GNotificationServerClass;
28 struct _GNotificationServer
32 GDBusConnection *connection;
38 /* app_ids -> hashtables of notification ids -> a{sv} */
39 GHashTable *applications;
42 G_DEFINE_TYPE (GNotificationServer, g_notification_server, G_TYPE_OBJECT);
50 static GDBusInterfaceInfo *
51 org_gtk_Notifications_get_interface (void)
53 static GDBusInterfaceInfo *iface_info;
55 if (iface_info == NULL)
60 info = g_dbus_node_info_new_for_xml (
62 " <interface name='org.gtk.Notifications'>"
63 " <method name='AddNotification'>"
64 " <arg type='s' direction='in' />"
65 " <arg type='s' direction='in' />"
66 " <arg type='a{sv}' direction='in' />"
68 " <method name='RemoveNotification'>"
69 " <arg type='s' direction='in' />"
70 " <arg type='s' direction='in' />"
76 g_error ("%s", error->message);
78 iface_info = g_dbus_node_info_lookup_interface (info, "org.gtk.Notifications");
79 g_assert (iface_info);
81 g_dbus_interface_info_ref (iface_info);
82 g_dbus_node_info_unref (info);
89 g_notification_server_notification_added (GNotificationServer *server,
91 const gchar *notification_id,
92 GVariant *notification)
94 GHashTable *notifications;
96 notifications = g_hash_table_lookup (server->applications, app_id);
97 if (notifications == NULL)
99 notifications = g_hash_table_new_full (g_str_hash, g_str_equal,
100 g_free, (GDestroyNotify) g_variant_unref);
101 g_hash_table_insert (server->applications, g_strdup (app_id), notifications);
104 g_hash_table_replace (notifications, g_strdup (notification_id), g_variant_ref (notification));
106 g_signal_emit_by_name (server, "notification-received", app_id, notification_id, notification);
110 g_notification_server_notification_removed (GNotificationServer *server,
112 const gchar *notification_id)
114 GHashTable *notifications;
116 notifications = g_hash_table_lookup (server->applications, app_id);
119 g_hash_table_remove (notifications, notification_id);
120 if (g_hash_table_size (notifications) == 0)
121 g_hash_table_remove (server->applications, app_id);
124 g_signal_emit_by_name (server, "notification-removed", app_id, notification_id);
128 org_gtk_Notifications_method_call (GDBusConnection *connection,
130 const gchar *object_path,
131 const gchar *interface_name,
132 const gchar *method_name,
133 GVariant *parameters,
134 GDBusMethodInvocation *invocation,
137 GNotificationServer *server = user_data;
139 if (g_str_equal (method_name, "AddNotification"))
142 const gchar *notification_id;
143 GVariant *notification;
145 g_variant_get (parameters, "(&s&s@a{sv})", &app_id, ¬ification_id, ¬ification);
146 g_notification_server_notification_added (server, app_id, notification_id, notification);
147 g_dbus_method_invocation_return_value (invocation, NULL);
149 g_variant_unref (notification);
151 else if (g_str_equal (method_name, "RemoveNotification"))
154 const gchar *notification_id;
156 g_variant_get (parameters, "(&s&s)", &app_id, ¬ification_id);
157 g_notification_server_notification_removed (server, app_id, notification_id);
158 g_dbus_method_invocation_return_value (invocation, NULL);
162 g_dbus_method_invocation_return_dbus_error (invocation, "UnknownMethod", "No such method");
167 g_notification_server_dispose (GObject *object)
169 GNotificationServer *server = G_NOTIFICATION_SERVER (object);
171 g_notification_server_stop (server);
173 g_clear_pointer (&server->applications, g_hash_table_unref);
174 g_clear_object (&server->connection);
176 G_OBJECT_CLASS (g_notification_server_parent_class)->dispose (object);
180 g_notification_server_get_property (GObject *object,
185 GNotificationServer *server = G_NOTIFICATION_SERVER (object);
189 case PROP_IS_RUNNING:
190 g_value_set_boolean (value, server->is_running);
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
199 g_notification_server_class_init (GNotificationServerClass *class)
201 GObjectClass *object_class = G_OBJECT_CLASS (class);
203 object_class->get_property = g_notification_server_get_property;
204 object_class->dispose = g_notification_server_dispose;
206 g_object_class_install_property (object_class, PROP_IS_RUNNING,
207 g_param_spec_boolean ("is-running", "", "", FALSE,
208 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
210 g_signal_new ("notification-received", G_TYPE_NOTIFICATION_SERVER, G_SIGNAL_RUN_FIRST,
211 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 3,
212 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_VARIANT);
214 g_signal_new ("notification-removed", G_TYPE_NOTIFICATION_SERVER, G_SIGNAL_RUN_FIRST,
215 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
216 G_TYPE_STRING, G_TYPE_STRING);
220 g_notification_server_bus_acquired (GDBusConnection *connection,
224 const GDBusInterfaceVTable vtable = {
225 org_gtk_Notifications_method_call, NULL, NULL
227 GNotificationServer *server = user_data;
229 server->object_id = g_dbus_connection_register_object (connection, "/org/gtk/Notifications",
230 org_gtk_Notifications_get_interface (),
231 &vtable, server, NULL, NULL);
233 /* register_object only fails if the same object is exported more than once */
234 g_assert (server->object_id > 0);
236 server->connection = g_object_ref (connection);
240 g_notification_server_name_acquired (GDBusConnection *connection,
244 GNotificationServer *server = user_data;
246 server->is_running = TRUE;
247 g_object_notify (G_OBJECT (server), "is-running");
251 g_notification_server_name_lost (GDBusConnection *connection,
255 GNotificationServer *server = user_data;
257 g_notification_server_stop (server);
259 if (connection == NULL && server->connection)
260 g_clear_object (&server->connection);
264 g_notification_server_init (GNotificationServer *server)
266 server->applications = g_hash_table_new_full (g_str_hash, g_str_equal,
267 g_free, (GDestroyNotify) g_hash_table_unref);
269 server->name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
270 "org.gtk.Notifications",
271 G_BUS_NAME_OWNER_FLAGS_NONE,
272 g_notification_server_bus_acquired,
273 g_notification_server_name_acquired,
274 g_notification_server_name_lost,
278 GNotificationServer *
279 g_notification_server_new (void)
281 return g_object_new (G_TYPE_NOTIFICATION_SERVER, NULL);
285 g_notification_server_stop (GNotificationServer *server)
287 g_return_if_fail (G_IS_NOTIFICATION_SERVER (server));
289 if (server->name_owner_id)
291 g_bus_unown_name (server->name_owner_id);
292 server->name_owner_id = 0;
295 if (server->object_id && server->connection)
297 g_dbus_connection_unregister_object (server->connection, server->object_id);
298 server->object_id = 0;
301 if (server->is_running)
303 server->is_running = FALSE;
304 g_object_notify (G_OBJECT (server), "is-running");
309 g_notification_server_get_is_running (GNotificationServer *server)
311 g_return_val_if_fail (G_IS_NOTIFICATION_SERVER (server), FALSE);
313 return server->is_running;
317 g_notification_server_list_applications (GNotificationServer *server)
319 g_return_val_if_fail (G_IS_NOTIFICATION_SERVER (server), NULL);
321 return (gchar **) g_hash_table_get_keys_as_array (server->applications, NULL);
325 g_notification_server_list_notifications (GNotificationServer *server,
328 GHashTable *notifications;
330 g_return_val_if_fail (G_IS_NOTIFICATION_SERVER (server), NULL);
331 g_return_val_if_fail (app_id != NULL, NULL);
333 notifications = g_hash_table_lookup (server->applications, app_id);
335 if (notifications == NULL)
338 return (gchar **) g_hash_table_get_keys_as_array (notifications, NULL);