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.1 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, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors: Lars Uebernickel <lars@uebernic.de>
18 */
19
20#include "gnotification-server.h"
21
22#include <gio/gio.h>
23
24typedef GObjectClass GNotificationServerClass;
25
26struct _GNotificationServer
27{
28 GObject parent;
29
30 GDBusConnection *connection;
31 guint name_owner_id;
32 guint object_id;
33
34 guint is_running;
35
36 /* app_ids -> hashtables of notification ids -> a{sv} */
37 GHashTable *applications;
38};
39
40G_DEFINE_TYPE (GNotificationServer, g_notification_server, G_TYPE_OBJECT)
41
42enum
43{
44 PROP_0,
45 PROP_IS_RUNNING
46};
47
48static GDBusInterfaceInfo *
49org_gtk_Notifications_get_interface (void)
50{
51 static GDBusInterfaceInfo *iface_info;
52
53 if (iface_info == NULL)
54 {
55 GDBusNodeInfo *info;
56 GError *error = NULL;
57
58 info = g_dbus_node_info_new_for_xml (
59 xml_data: "<node>"
60 " <interface name='org.gtk.Notifications'>"
61 " <method name='AddNotification'>"
62 " <arg type='s' direction='in' />"
63 " <arg type='s' direction='in' />"
64 " <arg type='a{sv}' direction='in' />"
65 " </method>"
66 " <method name='RemoveNotification'>"
67 " <arg type='s' direction='in' />"
68 " <arg type='s' direction='in' />"
69 " </method>"
70 " </interface>"
71 "</node>", error: &error);
72
73 if (info == NULL)
74 g_error ("%s", error->message);
75
76 iface_info = g_dbus_node_info_lookup_interface (info, name: "org.gtk.Notifications");
77 g_assert (iface_info);
78
79 g_dbus_interface_info_ref (info: iface_info);
80 g_dbus_node_info_unref (info);
81 }
82
83 return iface_info;
84}
85
86static void
87g_notification_server_notification_added (GNotificationServer *server,
88 const gchar *app_id,
89 const gchar *notification_id,
90 GVariant *notification)
91{
92 GHashTable *notifications;
93
94 notifications = g_hash_table_lookup (hash_table: server->applications, key: app_id);
95 if (notifications == NULL)
96 {
97 notifications = g_hash_table_new_full (hash_func: g_str_hash, key_equal_func: g_str_equal,
98 key_destroy_func: g_free, value_destroy_func: (GDestroyNotify) g_variant_unref);
99 g_hash_table_insert (hash_table: server->applications, key: g_strdup (str: app_id), value: notifications);
100 }
101
102 g_hash_table_replace (hash_table: notifications, key: g_strdup (str: notification_id), value: g_variant_ref (value: notification));
103
104 g_signal_emit_by_name (instance: server, detailed_signal: "notification-received", app_id, notification_id, notification);
105}
106
107static void
108g_notification_server_notification_removed (GNotificationServer *server,
109 const gchar *app_id,
110 const gchar *notification_id)
111{
112 GHashTable *notifications;
113
114 notifications = g_hash_table_lookup (hash_table: server->applications, key: app_id);
115 if (notifications)
116 {
117 g_hash_table_remove (hash_table: notifications, key: notification_id);
118 if (g_hash_table_size (hash_table: notifications) == 0)
119 g_hash_table_remove (hash_table: server->applications, key: app_id);
120 }
121
122 g_signal_emit_by_name (instance: server, detailed_signal: "notification-removed", app_id, notification_id);
123}
124
125static void
126org_gtk_Notifications_method_call (GDBusConnection *connection,
127 const gchar *sender,
128 const gchar *object_path,
129 const gchar *interface_name,
130 const gchar *method_name,
131 GVariant *parameters,
132 GDBusMethodInvocation *invocation,
133 gpointer user_data)
134{
135 GNotificationServer *server = user_data;
136
137 if (g_str_equal (v1: method_name, v2: "AddNotification"))
138 {
139 const gchar *app_id;
140 const gchar *notification_id;
141 GVariant *notification;
142
143 g_variant_get (value: parameters, format_string: "(&s&s@a{sv})", &app_id, &notification_id, &notification);
144 g_notification_server_notification_added (server, app_id, notification_id, notification);
145 g_dbus_method_invocation_return_value (invocation, NULL);
146
147 g_variant_unref (value: notification);
148 }
149 else if (g_str_equal (v1: method_name, v2: "RemoveNotification"))
150 {
151 const gchar *app_id;
152 const gchar *notification_id;
153
154 g_variant_get (value: parameters, format_string: "(&s&s)", &app_id, &notification_id);
155 g_notification_server_notification_removed (server, app_id, notification_id);
156 g_dbus_method_invocation_return_value (invocation, NULL);
157 }
158 else
159 {
160 g_dbus_method_invocation_return_dbus_error (invocation, error_name: "UnknownMethod", error_message: "No such method");
161 }
162}
163
164static void
165g_notification_server_dispose (GObject *object)
166{
167 GNotificationServer *server = G_NOTIFICATION_SERVER (object);
168
169 g_notification_server_stop (server);
170
171 g_clear_pointer (&server->applications, g_hash_table_unref);
172 g_clear_object (&server->connection);
173
174 G_OBJECT_CLASS (g_notification_server_parent_class)->dispose (object);
175}
176
177static void
178g_notification_server_get_property (GObject *object,
179 guint property_id,
180 GValue *value,
181 GParamSpec *pspec)
182{
183 GNotificationServer *server = G_NOTIFICATION_SERVER (object);
184
185 switch (property_id)
186 {
187 case PROP_IS_RUNNING:
188 g_value_set_boolean (value, v_boolean: server->is_running);
189 break;
190
191 default:
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
193 }
194}
195
196static void
197g_notification_server_class_init (GNotificationServerClass *class)
198{
199 GObjectClass *object_class = G_OBJECT_CLASS (class);
200
201 object_class->get_property = g_notification_server_get_property;
202 object_class->dispose = g_notification_server_dispose;
203
204 g_object_class_install_property (oclass: object_class, property_id: PROP_IS_RUNNING,
205 pspec: g_param_spec_boolean (name: "is-running", nick: "", blurb: "", FALSE,
206 flags: G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
207
208 g_signal_new (signal_name: "notification-received", G_TYPE_NOTIFICATION_SERVER, signal_flags: G_SIGNAL_RUN_FIRST,
209 class_offset: 0, NULL, NULL, c_marshaller: g_cclosure_marshal_generic, G_TYPE_NONE, n_params: 3,
210 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_VARIANT);
211
212 g_signal_new (signal_name: "notification-removed", G_TYPE_NOTIFICATION_SERVER, signal_flags: G_SIGNAL_RUN_FIRST,
213 class_offset: 0, NULL, NULL, c_marshaller: g_cclosure_marshal_generic, G_TYPE_NONE, n_params: 2,
214 G_TYPE_STRING, G_TYPE_STRING);
215}
216
217static void
218g_notification_server_bus_acquired (GDBusConnection *connection,
219 const gchar *name,
220 gpointer user_data)
221{
222 const GDBusInterfaceVTable vtable = {
223 org_gtk_Notifications_method_call, NULL, NULL
224 };
225 GNotificationServer *server = user_data;
226
227 server->object_id = g_dbus_connection_register_object (connection, object_path: "/org/gtk/Notifications",
228 interface_info: org_gtk_Notifications_get_interface (),
229 vtable: &vtable, user_data: server, NULL, NULL);
230
231 /* register_object only fails if the same object is exported more than once */
232 g_assert (server->object_id > 0);
233
234 server->connection = g_object_ref (connection);
235}
236
237static void
238g_notification_server_name_acquired (GDBusConnection *connection,
239 const gchar *name,
240 gpointer user_data)
241{
242 GNotificationServer *server = user_data;
243
244 server->is_running = TRUE;
245 g_object_notify (G_OBJECT (server), property_name: "is-running");
246}
247
248static void
249g_notification_server_name_lost (GDBusConnection *connection,
250 const gchar *name,
251 gpointer user_data)
252{
253 GNotificationServer *server = user_data;
254
255 g_notification_server_stop (server);
256
257 if (connection == NULL && server->connection)
258 g_clear_object (&server->connection);
259}
260
261static void
262g_notification_server_init (GNotificationServer *server)
263{
264 server->applications = g_hash_table_new_full (hash_func: g_str_hash, key_equal_func: g_str_equal,
265 key_destroy_func: g_free, value_destroy_func: (GDestroyNotify) g_hash_table_unref);
266
267 server->name_owner_id = g_bus_own_name (bus_type: G_BUS_TYPE_SESSION,
268 name: "org.gtk.Notifications",
269 flags: G_BUS_NAME_OWNER_FLAGS_NONE,
270 bus_acquired_handler: g_notification_server_bus_acquired,
271 name_acquired_handler: g_notification_server_name_acquired,
272 name_lost_handler: g_notification_server_name_lost,
273 user_data: server, NULL);
274}
275
276GNotificationServer *
277g_notification_server_new (void)
278{
279 return g_object_new (G_TYPE_NOTIFICATION_SERVER, NULL);
280}
281
282void
283g_notification_server_stop (GNotificationServer *server)
284{
285 g_return_if_fail (G_IS_NOTIFICATION_SERVER (server));
286
287 if (server->name_owner_id)
288 {
289 g_bus_unown_name (owner_id: server->name_owner_id);
290 server->name_owner_id = 0;
291 }
292
293 if (server->object_id && server->connection)
294 {
295 g_dbus_connection_unregister_object (connection: server->connection, registration_id: server->object_id);
296 server->object_id = 0;
297 }
298
299 if (server->is_running)
300 {
301 server->is_running = FALSE;
302 g_object_notify (G_OBJECT (server), property_name: "is-running");
303 }
304}
305
306gboolean
307g_notification_server_get_is_running (GNotificationServer *server)
308{
309 g_return_val_if_fail (G_IS_NOTIFICATION_SERVER (server), FALSE);
310
311 return server->is_running;
312}
313
314gchar **
315g_notification_server_list_applications (GNotificationServer *server)
316{
317 g_return_val_if_fail (G_IS_NOTIFICATION_SERVER (server), NULL);
318
319 return (gchar **) g_hash_table_get_keys_as_array (hash_table: server->applications, NULL);
320}
321
322gchar **
323g_notification_server_list_notifications (GNotificationServer *server,
324 const gchar *app_id)
325{
326 GHashTable *notifications;
327
328 g_return_val_if_fail (G_IS_NOTIFICATION_SERVER (server), NULL);
329 g_return_val_if_fail (app_id != NULL, NULL);
330
331 notifications = g_hash_table_lookup (hash_table: server->applications, key: app_id);
332
333 if (notifications == NULL)
334 return NULL;
335
336 return (gchar **) g_hash_table_get_keys_as_array (hash_table: notifications, NULL);
337}
338

source code of gtk/subprojects/glib/gio/tests/gnotification-server.c