2
* * Copyright (C) 2008-2009 Ali <aliov@xfce.org>
4
* Licensed under the GNU General Public License Version 2
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
41
#include <libxfce4util/libxfce4util.h>
43
#include <libnotify/notify.h>
45
#include "xfpm-common.h"
46
#include "xfpm-notify.h"
47
#include "xfpm-dbus-monitor.h"
49
static void xfpm_notify_finalize (GObject *object);
51
static NotifyNotification * xfpm_notify_new_notification_internal (const gchar *title,
53
const gchar *icon_name,
55
XfpmNotifyUrgency urgency,
56
GtkStatusIcon *icon) G_GNUC_MALLOC;
58
#define XFPM_NOTIFY_GET_PRIVATE(o) \
59
(G_TYPE_INSTANCE_GET_PRIVATE((o), XFPM_TYPE_NOTIFY, XfpmNotifyPrivate))
61
struct XfpmNotifyPrivate
63
XfpmDBusMonitor *monitor;
65
NotifyNotification *notification;
66
NotifyNotification *critical;
68
gboolean supports_actions;
69
gboolean supports_sync; /*For x-canonical-private-synchronous */
79
G_DEFINE_TYPE(XfpmNotify, xfpm_notify, G_TYPE_OBJECT)
82
xfpm_notify_get_server_caps (XfpmNotify *notify)
85
notify->priv->supports_actions = FALSE;
86
notify->priv->supports_sync = FALSE;
88
caps = notify_get_server_caps ();
92
if (g_list_find_custom (caps, "x-canonical-private-synchronous", (GCompareFunc) g_strcmp0) != NULL)
93
notify->priv->supports_sync = TRUE;
95
if (g_list_find_custom (caps, "actions", (GCompareFunc) g_strcmp0) != NULL)
96
notify->priv->supports_actions = TRUE;
98
g_list_foreach(caps, (GFunc)g_free, NULL);
104
xfpm_notify_check_server (XfpmDBusMonitor *monitor,
110
if ( !g_strcmp0 (service_name, "org.freedesktop.Notifications") && on_session && connected )
111
xfpm_notify_get_server_caps (notify);
114
static void xfpm_notify_get_property (GObject *object,
121
notify = XFPM_NOTIFY (object);
126
g_value_set_boolean (value, notify->priv->supports_actions);
129
g_value_set_boolean (value, notify->priv->supports_sync);
132
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138
xfpm_notify_class_init (XfpmNotifyClass *klass)
140
GObjectClass *object_class = G_OBJECT_CLASS (klass);
142
object_class->finalize = xfpm_notify_finalize;
143
object_class->get_property = xfpm_notify_get_property;
145
g_object_class_install_property (object_class,
147
g_param_spec_boolean ("actions",
152
g_object_class_install_property (object_class,
154
g_param_spec_boolean ("sync",
159
g_type_class_add_private (klass, sizeof (XfpmNotifyPrivate));
163
xfpm_notify_init (XfpmNotify *notify)
165
notify->priv = XFPM_NOTIFY_GET_PRIVATE (notify);
167
notify->priv->notification = NULL;
168
notify->priv->critical = NULL;
170
notify->priv->monitor = xfpm_dbus_monitor_new ();
171
xfpm_dbus_monitor_add_service (notify->priv->monitor, DBUS_BUS_SESSION, "org.freedesktop.Notifications");
172
g_signal_connect (notify->priv->monitor, "service-connection-changed",
173
G_CALLBACK (xfpm_notify_check_server), notify);
175
xfpm_notify_get_server_caps (notify);
179
xfpm_notify_finalize (GObject *object)
183
notify = XFPM_NOTIFY (object);
185
xfpm_notify_close_normal (notify);
186
xfpm_notify_close_critical (notify);
188
G_OBJECT_CLASS (xfpm_notify_parent_class)->finalize(object);
192
xfpm_notify_set_notification_icon (NotifyNotification *n, const gchar *icon_name )
194
GdkPixbuf *pix = xfpm_icon_load (icon_name, 48);
198
notify_notification_set_icon_from_pixbuf (n,
200
g_object_unref ( G_OBJECT(pix));
205
static NotifyNotification *
206
xfpm_notify_new_notification_internal (const gchar *title, const gchar *message,
207
const gchar *icon_name, guint timeout,
208
XfpmNotifyUrgency urgency, GtkStatusIcon *icon)
210
NotifyNotification *n;
212
n = notify_notification_new (title, message, NULL, NULL);
215
xfpm_notify_set_notification_icon (n, icon_name);
218
notify_notification_attach_to_status_icon (n, icon);
220
notify_notification_set_urgency (n, (NotifyUrgency)urgency);
223
notify_notification_set_timeout (n, timeout);
229
xfpm_notify_closed_cb (NotifyNotification *n, XfpmNotify *notify)
231
notify->priv->notification = NULL;
232
g_object_unref (G_OBJECT (n));
236
xfpm_notify_close_critical_cb (NotifyNotification *n, XfpmNotify *notify)
238
notify->priv->critical = NULL;
239
g_object_unref (G_OBJECT (n));
243
xfpm_notify_show (NotifyNotification *n)
245
notify_notification_show (n, NULL);
250
xfpm_notify_close_notification (XfpmNotify *notify )
252
if ( notify->priv->notification )
254
if (!notify_notification_close (notify->priv->notification, NULL))
255
g_warning ("Failed to close notification\n");
257
g_object_unref (G_OBJECT(notify->priv->notification) );
258
notify->priv->notification = NULL;
263
xfpm_notify_new (void)
265
static gpointer xfpm_notify_object = NULL;
267
if ( xfpm_notify_object != NULL )
269
g_object_ref (xfpm_notify_object);
273
xfpm_notify_object = g_object_new (XFPM_TYPE_NOTIFY, NULL);
274
g_object_add_weak_pointer (xfpm_notify_object, &xfpm_notify_object);
276
return XFPM_NOTIFY (xfpm_notify_object);
279
void xfpm_notify_show_notification (XfpmNotify *notify, const gchar *title,
280
const gchar *text, const gchar *icon_name,
281
gint timeout, gboolean simple,
282
XfpmNotifyUrgency urgency, GtkStatusIcon *icon)
284
NotifyNotification *n;
287
xfpm_notify_close_notification (notify);
289
n = xfpm_notify_new_notification_internal (title,
294
xfpm_notify_present_notification (notify, n, simple);
297
NotifyNotification *xfpm_notify_new_notification (XfpmNotify *notify,
300
const gchar *icon_name,
302
XfpmNotifyUrgency urgency,
305
NotifyNotification *n = xfpm_notify_new_notification_internal (title,
312
void xfpm_notify_add_action_to_notification (XfpmNotify *notify, NotifyNotification *n,
313
const gchar *id, const gchar *action_label,
314
NotifyActionCallback callback, gpointer data)
316
g_return_if_fail (XFPM_IS_NOTIFY(notify));
318
notify_notification_add_action (n, id, action_label,
319
(NotifyActionCallback)callback,
324
void xfpm_notify_present_notification (XfpmNotify *notify, NotifyNotification *n, gboolean simple)
326
g_return_if_fail (XFPM_IS_NOTIFY(notify));
329
xfpm_notify_close_notification (notify);
333
g_signal_connect (G_OBJECT(n),"closed",
334
G_CALLBACK(xfpm_notify_closed_cb), notify);
335
notify->priv->notification = n;
338
g_idle_add ((GSourceFunc) xfpm_notify_show, n);
341
void xfpm_notify_critical (XfpmNotify *notify, NotifyNotification *n)
343
g_return_if_fail (XFPM_IS_NOTIFY (notify));
345
xfpm_notify_close_critical (notify);
347
notify->priv->critical = n;
349
g_signal_connect (G_OBJECT (n), "closed",
350
G_CALLBACK (xfpm_notify_close_critical_cb), notify);
352
g_idle_add ((GSourceFunc) xfpm_notify_show, n);
355
void xfpm_notify_close_critical (XfpmNotify *notify)
357
g_return_if_fail (XFPM_IS_NOTIFY (notify));
359
if ( notify->priv->critical )
361
if (!notify_notification_close (notify->priv->critical, NULL))
362
g_warning ("Failed to close notification\n");
364
g_object_unref (G_OBJECT(notify->priv->critical) );
365
notify->priv->critical = NULL;
369
void xfpm_notify_close_normal (XfpmNotify *notify)
371
g_return_if_fail (XFPM_IS_NOTIFY (notify));
373
xfpm_notify_close_notification (notify);