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
24 typedef GObjectClass GNotificationServerClass;
25
26 struct _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
40 G_DEFINE_TYPE (GNotificationServer, g_notification_server, G_TYPE_OBJECT)
41
42 enum
43 {
44 PROP_0,
45 PROP_IS_RUNNING
46 };
47
48 static GDBusInterfaceInfo *
org_gtk_Notifications_get_interface(void)49 org_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 "<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);
72
73 if (info == NULL)
74 g_error ("%s", error->message);
75
76 iface_info = g_dbus_node_info_lookup_interface (info, "org.gtk.Notifications");
77 g_assert (iface_info);
78
79 g_dbus_interface_info_ref (iface_info);
80 g_dbus_node_info_unref (info);
81 }
82
83 return iface_info;
84 }
85
86 static void
g_notification_server_notification_added(GNotificationServer * server,const gchar * app_id,const gchar * notification_id,GVariant * notification)87 g_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 (server->applications, app_id);
95 if (notifications == NULL)
96 {
97 notifications = g_hash_table_new_full (g_str_hash, g_str_equal,
98 g_free, (GDestroyNotify) g_variant_unref);
99 g_hash_table_insert (server->applications, g_strdup (app_id), notifications);
100 }
101
102 g_hash_table_replace (notifications, g_strdup (notification_id), g_variant_ref (notification));
103
104 g_signal_emit_by_name (server, "notification-received", app_id, notification_id, notification);
105 }
106
107 static void
g_notification_server_notification_removed(GNotificationServer * server,const gchar * app_id,const gchar * notification_id)108 g_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 (server->applications, app_id);
115 if (notifications)
116 {
117 g_hash_table_remove (notifications, notification_id);
118 if (g_hash_table_size (notifications) == 0)
119 g_hash_table_remove (server->applications, app_id);
120 }
121
122 g_signal_emit_by_name (server, "notification-removed", app_id, notification_id);
123 }
124
125 static void
org_gtk_Notifications_method_call(GDBusConnection * connection,const gchar * sender,const gchar * object_path,const gchar * interface_name,const gchar * method_name,GVariant * parameters,GDBusMethodInvocation * invocation,gpointer user_data)126 org_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 (method_name, "AddNotification"))
138 {
139 const gchar *app_id;
140 const gchar *notification_id;
141 GVariant *notification;
142
143 g_variant_get (parameters, "(&s&s@a{sv})", &app_id, ¬ification_id, ¬ification);
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 (notification);
148 }
149 else if (g_str_equal (method_name, "RemoveNotification"))
150 {
151 const gchar *app_id;
152 const gchar *notification_id;
153
154 g_variant_get (parameters, "(&s&s)", &app_id, ¬ification_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, "UnknownMethod", "No such method");
161 }
162 }
163
164 static void
g_notification_server_dispose(GObject * object)165 g_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
177 static void
g_notification_server_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)178 g_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, server->is_running);
189 break;
190
191 default:
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
193 }
194 }
195
196 static void
g_notification_server_class_init(GNotificationServerClass * class)197 g_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 (object_class, PROP_IS_RUNNING,
205 g_param_spec_boolean ("is-running", "", "", FALSE,
206 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
207
208 g_signal_new ("notification-received", G_TYPE_NOTIFICATION_SERVER, G_SIGNAL_RUN_FIRST,
209 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 3,
210 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_VARIANT);
211
212 g_signal_new ("notification-removed", G_TYPE_NOTIFICATION_SERVER, G_SIGNAL_RUN_FIRST,
213 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
214 G_TYPE_STRING, G_TYPE_STRING);
215 }
216
217 static void
g_notification_server_bus_acquired(GDBusConnection * connection,const gchar * name,gpointer user_data)218 g_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, "/org/gtk/Notifications",
228 org_gtk_Notifications_get_interface (),
229 &vtable, 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
237 static void
g_notification_server_name_acquired(GDBusConnection * connection,const gchar * name,gpointer user_data)238 g_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), "is-running");
246 }
247
248 static void
g_notification_server_name_lost(GDBusConnection * connection,const gchar * name,gpointer user_data)249 g_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
261 static void
g_notification_server_init(GNotificationServer * server)262 g_notification_server_init (GNotificationServer *server)
263 {
264 server->applications = g_hash_table_new_full (g_str_hash, g_str_equal,
265 g_free, (GDestroyNotify) g_hash_table_unref);
266
267 server->name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
268 "org.gtk.Notifications",
269 G_BUS_NAME_OWNER_FLAGS_NONE,
270 g_notification_server_bus_acquired,
271 g_notification_server_name_acquired,
272 g_notification_server_name_lost,
273 server, NULL);
274 }
275
276 GNotificationServer *
g_notification_server_new(void)277 g_notification_server_new (void)
278 {
279 return g_object_new (G_TYPE_NOTIFICATION_SERVER, NULL);
280 }
281
282 void
g_notification_server_stop(GNotificationServer * server)283 g_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 (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 (server->connection, 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), "is-running");
303 }
304 }
305
306 gboolean
g_notification_server_get_is_running(GNotificationServer * server)307 g_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
314 gchar **
g_notification_server_list_applications(GNotificationServer * server)315 g_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 (server->applications, NULL);
320 }
321
322 gchar **
g_notification_server_list_notifications(GNotificationServer * server,const gchar * app_id)323 g_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 (server->applications, app_id);
332
333 if (notifications == NULL)
334 return NULL;
335
336 return (gchar **) g_hash_table_get_keys_as_array (notifications, NULL);
337 }
338