root/trunk/libnotify/libnotify/notify.c

Revision 3005 (checked in by chipx86, 10 months ago)

Add support for sending the closed reason in the signal handler. Closes ticket #139.

Line 
1 /**
2  * @file libnotify/notify.c Notifications library
3  *
4  * @Copyright (C) 2004-2006 Christian Hammond <chipx86@chipx86.com>
5  * @Copyright (C) 2004-2006 Mike Hearn <mike@navi.cx>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA  02111-1307, USA.
21  */
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <unistd.h>
26 #include <libnotify/notify.h>
27 #include <libnotify/internal.h>
28 #include <libnotify/notify-marshal.h>
29
30 static gboolean _initted = FALSE;
31 static gchar *_app_name = NULL;
32 static DBusGProxy *_proxy = NULL;
33 static DBusGConnection *_dbus_gconn = NULL;
34 static GList *_active_notifications = NULL;
35
36 /**
37  * notify_init:
38  * @app_name: The name of the application initializing libnotify.
39  *
40  * Initialized libnotify. This must be called before any other functions.
41  *
42  * Returns: %TRUE if successful, or %FALSE on error.
43  */
44 gboolean
45 notify_init(const char *app_name)
46 {
47     GError *error = NULL;
48     DBusGConnection *bus = NULL;
49
50     g_return_val_if_fail(app_name != NULL, FALSE);
51     g_return_val_if_fail(*app_name != '\0', FALSE);
52
53     if (_initted)
54         return TRUE;
55
56     _app_name = g_strdup(app_name);
57
58     g_type_init();
59
60     bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
61
62     if (error != NULL)
63     {
64         g_message("Unable to get session bus: %s", error->message);
65         g_error_free(error);
66         return FALSE;
67     }
68
69     _proxy = dbus_g_proxy_new_for_name(bus,
70                                        NOTIFY_DBUS_NAME,
71                                        NOTIFY_DBUS_CORE_OBJECT,
72                                        NOTIFY_DBUS_CORE_INTERFACE);
73     dbus_g_connection_unref(bus);
74
75     dbus_g_object_register_marshaller(notify_marshal_VOID__UINT_UINT,
76                                       G_TYPE_NONE,
77                                       G_TYPE_UINT,
78                                       G_TYPE_UINT, G_TYPE_INVALID);
79
80     dbus_g_object_register_marshaller(notify_marshal_VOID__UINT_STRING,
81                                       G_TYPE_NONE,
82                                       G_TYPE_UINT,
83                                       G_TYPE_STRING, G_TYPE_INVALID);
84
85     dbus_g_proxy_add_signal(_proxy, "NotificationClosed",
86                             G_TYPE_UINT, G_TYPE_UINT,
87                             G_TYPE_INVALID);
88     dbus_g_proxy_add_signal(_proxy, "ActionInvoked",
89                             G_TYPE_UINT, G_TYPE_STRING,
90                             G_TYPE_INVALID);
91
92     _initted = TRUE;
93
94     return TRUE;
95 }
96
97 /**
98  * notify_get_app_name:
99  *
100  * Gets the application name registered.
101  *
102  * Returns: The registered application name, passed to notify_init().
103  */
104 const gchar *
105 notify_get_app_name(void)
106 {
107     return _app_name;
108 }
109
110 /**
111  * notify_uninit:
112  *
113  * Uninitialized libnotify.
114  *
115  * This should be called when the program no longer needs libnotify for
116  * the rest of its lifecycle, typically just before exitting.
117  */
118 void
119 notify_uninit(void)
120 {
121     GList *l;
122
123     if (!_initted)
124         return;
125
126     if (_app_name != NULL)
127     {
128         g_free(_app_name);
129         _app_name = NULL;
130     }
131
132     for (l = _active_notifications; l != NULL; l = l->next)
133     {
134         NotifyNotification *n = NOTIFY_NOTIFICATION(l->data);
135
136         if (_notify_notification_get_timeout(n) == 0 ||
137             _notify_notification_has_nondefault_actions(n))
138         {
139             notify_notification_close(n, NULL);
140         }
141     }
142
143     g_object_unref(_proxy);
144
145     _initted = FALSE;
146 }
147
148 /**
149  * notify_is_initted:
150  *
151  * Gets whether or not libnotify is initialized.
152  *
153  * Returns: %TRUE if libnotify is initialized, or %FALSE otherwise.
154  */
155 gboolean
156 notify_is_initted(void)
157 {
158     return _initted;
159 }
160
161 DBusGConnection *
162 _notify_get_dbus_g_conn(void)
163 {
164     return _dbus_gconn;
165 }
166
167 DBusGProxy *
168 _notify_get_g_proxy(void)
169 {
170     return _proxy;
171 }
172
173 /**
174  * notify_get_server_caps:
175  *
176  * Queries the server for its capabilities and returns them in a #GList.
177  *
178  * Returns: A #GList of server capability strings.
179  */
180 GList *
181 notify_get_server_caps(void)
182 {
183     GError *error = NULL;
184     char **caps = NULL, **cap;
185     GList *result = NULL;
186     DBusGProxy *proxy = _notify_get_g_proxy();
187
188     g_return_val_if_fail(proxy != NULL, NULL);
189
190     if (!dbus_g_proxy_call(proxy, "GetCapabilities", &error,
191                            G_TYPE_INVALID,
192                            G_TYPE_STRV, &caps, G_TYPE_INVALID))
193     {
194         g_message("GetCapabilities call failed: %s", error->message);
195         g_error_free(error);
196         return NULL;
197     }
198
199     for (cap = caps; *cap != NULL; cap++)
200     {
201         result = g_list_append(result, g_strdup(*cap));
202     }
203
204     g_strfreev(caps);
205
206     return result;
207 }
208
209 /**
210  * notify_get_server_info:
211  * @ret_name: The resulting server name.
212  * @ret_vendor: The resulting server vendor.
213  * @ret_version: The resulting server version.
214  * @ret_spec_version: The resulting version of the specification the server is
215  *                    compliant with.
216  *
217  * Queries the server for its information, specifically, the name, vendor,
218  * server version, and the version of the notifications specification that it
219  * is compliant with.
220  *
221  * Returns: %TRUE if successful, and the variables passed will be set. %FALSE
222  *          on failure.
223  */
224 gboolean
225 notify_get_server_info(char **ret_name, char **ret_vendor,
226                        char **ret_version, char **ret_spec_version)
227 {
228     GError *error = NULL;
229     DBusGProxy *proxy = _notify_get_g_proxy();
230     char *name, *vendor, *version, *spec_version;
231
232     g_return_val_if_fail(proxy != NULL, FALSE);
233
234     if (!dbus_g_proxy_call(proxy, "GetServerInformation", &error,
235                            G_TYPE_INVALID,
236                            G_TYPE_STRING, &name,
237                            G_TYPE_STRING, &vendor,
238                            G_TYPE_STRING, &version,
239                            G_TYPE_STRING, &spec_version,
240                            G_TYPE_INVALID))
241     {
242         g_message("GetServerInformation call failed: %s", error->message);
243         return FALSE;
244     }
245
246     if (ret_name != NULL)
247         *ret_name = name;
248
249     if (ret_vendor != NULL)
250         *ret_vendor = vendor;
251
252     if (ret_version != NULL)
253         *ret_version = version;
254
255     if (spec_version != NULL)
256         *ret_spec_version = spec_version;
257
258     return TRUE;
259 }
260
261 void
262 _notify_cache_add_notification(NotifyNotification *n)
263 {
264     _active_notifications = g_list_prepend(_active_notifications, n);
265 }
266
267 void
268 _notify_cache_remove_notification(NotifyNotification *n)
269 {
270     _active_notifications = g_list_remove(_active_notifications, n);
271 }
Note: See TracBrowser for help on using the browser.