~noskcaj/ubuntu/saucy/xfce4-power-manager/1.2.0-2ubuntu1

« back to all changes in this revision

Viewing changes to src/xfpm-notify.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2010-12-09 18:28:34 UTC
  • mfrom: (2.3.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20101209182834-efb7dinmf9ssp3es
Tags: 1.0.1-0ubuntu1
Upload to natty (pkg-xfce svn r4665), no Ubuntu changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * * Copyright (C) 2008-2009 Ali <aliov@xfce.org>
 
3
 *
 
4
 * Licensed under the GNU General Public License Version 2
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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
 
19
 */
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#include <config.h>
 
23
#endif
 
24
 
 
25
#include <stdio.h>
 
26
 
 
27
#ifdef HAVE_STDLIB_H
 
28
#include <stdlib.h>
 
29
#endif
 
30
 
 
31
#ifdef HAVE_STRING_H
 
32
#include <string.h>
 
33
#endif
 
34
 
 
35
#ifdef HAVE_ERRNO_H
 
36
#include <errno.h>
 
37
#endif
 
38
 
 
39
#include <gtk/gtk.h>
 
40
 
 
41
#include <libxfce4util/libxfce4util.h>
 
42
 
 
43
#include <libnotify/notify.h>
 
44
 
 
45
#include "xfpm-common.h"
 
46
#include "xfpm-notify.h"
 
47
#include "xfpm-dbus-monitor.h"
 
48
 
 
49
static void xfpm_notify_finalize   (GObject *object);
 
50
 
 
51
static NotifyNotification * xfpm_notify_new_notification_internal (const gchar *title, 
 
52
                                                                   const gchar *message, 
 
53
                                                                   const gchar *icon_name, 
 
54
                                                                   guint timeout, 
 
55
                                                                   XfpmNotifyUrgency urgency, 
 
56
                                                                   GtkStatusIcon *icon) G_GNUC_MALLOC;
 
57
 
 
58
#define XFPM_NOTIFY_GET_PRIVATE(o) \
 
59
(G_TYPE_INSTANCE_GET_PRIVATE((o), XFPM_TYPE_NOTIFY, XfpmNotifyPrivate))
 
60
 
 
61
struct XfpmNotifyPrivate
 
62
{
 
63
    XfpmDBusMonitor    *monitor;
 
64
    
 
65
    NotifyNotification *notification;
 
66
    NotifyNotification *critical;
 
67
    
 
68
    gboolean            supports_actions;
 
69
    gboolean            supports_sync; /*For x-canonical-private-synchronous */
 
70
};
 
71
 
 
72
enum
 
73
{
 
74
    PROP_0,
 
75
    PROP_ACTIONS,
 
76
    PROP_SYNC
 
77
};
 
78
 
 
79
G_DEFINE_TYPE(XfpmNotify, xfpm_notify, G_TYPE_OBJECT)
 
80
 
 
81
static void
 
82
xfpm_notify_get_server_caps (XfpmNotify *notify)
 
83
{
 
84
    GList *caps = NULL;
 
85
    notify->priv->supports_actions = FALSE;
 
86
    notify->priv->supports_sync    = FALSE;
 
87
    
 
88
    caps = notify_get_server_caps ();
 
89
    
 
90
    if (caps != NULL) 
 
91
    {
 
92
        if (g_list_find_custom (caps, "x-canonical-private-synchronous", (GCompareFunc) g_strcmp0) != NULL)
 
93
            notify->priv->supports_sync = TRUE;
 
94
    
 
95
        if (g_list_find_custom (caps, "actions", (GCompareFunc) g_strcmp0) != NULL)
 
96
            notify->priv->supports_actions = TRUE;
 
97
 
 
98
        g_list_foreach(caps, (GFunc)g_free, NULL);
 
99
        g_list_free(caps);
 
100
    }
 
101
}
 
102
 
 
103
static void
 
104
xfpm_notify_check_server (XfpmDBusMonitor *monitor, 
 
105
                          gchar *service_name, 
 
106
                          gboolean connected,
 
107
                          gboolean on_session,
 
108
                          XfpmNotify *notify)
 
109
{
 
110
    if ( !g_strcmp0 (service_name, "org.freedesktop.Notifications") && on_session && connected )
 
111
        xfpm_notify_get_server_caps (notify);
 
112
}
 
113
 
 
114
static void xfpm_notify_get_property (GObject *object,
 
115
                                      guint prop_id,
 
116
                                      GValue *value,
 
117
                                      GParamSpec *pspec)
 
118
{
 
119
    XfpmNotify *notify;
 
120
    
 
121
    notify = XFPM_NOTIFY (object);
 
122
    
 
123
    switch (prop_id)
 
124
    {
 
125
        case PROP_ACTIONS:
 
126
            g_value_set_boolean (value, notify->priv->supports_actions);
 
127
            break;
 
128
        case PROP_SYNC:
 
129
            g_value_set_boolean (value, notify->priv->supports_sync);
 
130
            break;
 
131
        default:
 
132
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
133
            break;
 
134
    }
 
135
}
 
136
 
 
137
static void
 
138
xfpm_notify_class_init (XfpmNotifyClass *klass)
 
139
{
 
140
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
141
 
 
142
    object_class->finalize = xfpm_notify_finalize;
 
143
    object_class->get_property = xfpm_notify_get_property;
 
144
 
 
145
    g_object_class_install_property (object_class,
 
146
                                     PROP_ACTIONS,
 
147
                                     g_param_spec_boolean ("actions",
 
148
                                                           NULL, NULL,
 
149
                                                           FALSE,
 
150
                                                           G_PARAM_READABLE));
 
151
 
 
152
    g_object_class_install_property (object_class,
 
153
                                     PROP_SYNC,
 
154
                                     g_param_spec_boolean ("sync",
 
155
                                                           NULL, NULL,
 
156
                                                           FALSE,
 
157
                                                           G_PARAM_READABLE));
 
158
 
 
159
    g_type_class_add_private (klass, sizeof (XfpmNotifyPrivate));
 
160
}
 
161
 
 
162
static void
 
163
xfpm_notify_init (XfpmNotify *notify)
 
164
{
 
165
    notify->priv = XFPM_NOTIFY_GET_PRIVATE (notify);
 
166
    
 
167
    notify->priv->notification = NULL;
 
168
    notify->priv->critical = NULL;
 
169
    
 
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);
 
174
    
 
175
    xfpm_notify_get_server_caps (notify);
 
176
}
 
177
 
 
178
static void
 
179
xfpm_notify_finalize (GObject *object)
 
180
{
 
181
    XfpmNotify *notify;
 
182
 
 
183
    notify = XFPM_NOTIFY (object);
 
184
    
 
185
    xfpm_notify_close_normal (notify);
 
186
    xfpm_notify_close_critical (notify);
 
187
    
 
188
    G_OBJECT_CLASS (xfpm_notify_parent_class)->finalize(object);
 
189
}
 
190
 
 
191
static void
 
192
xfpm_notify_set_notification_icon (NotifyNotification *n, const gchar *icon_name )
 
193
{
 
194
    GdkPixbuf *pix = xfpm_icon_load (icon_name, 48);
 
195
    
 
196
    if ( pix )
 
197
    {
 
198
        notify_notification_set_icon_from_pixbuf (n,
 
199
                                                  pix);
 
200
        g_object_unref ( G_OBJECT(pix));
 
201
    }
 
202
    
 
203
}
 
204
 
 
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)
 
209
{
 
210
    NotifyNotification *n;
 
211
    
 
212
    n = notify_notification_new (title, message, NULL, NULL);
 
213
    
 
214
    if ( icon_name )
 
215
        xfpm_notify_set_notification_icon (n, icon_name);
 
216
        
 
217
    if ( icon )
 
218
        notify_notification_attach_to_status_icon (n, icon);
 
219
        
 
220
    notify_notification_set_urgency (n, (NotifyUrgency)urgency);
 
221
    
 
222
    if ( timeout != 0)
 
223
        notify_notification_set_timeout (n, timeout);
 
224
    
 
225
    return n;
 
226
}
 
227
 
 
228
static void
 
229
xfpm_notify_closed_cb (NotifyNotification *n, XfpmNotify *notify)
 
230
{
 
231
    notify->priv->notification = NULL;
 
232
    g_object_unref (G_OBJECT (n));
 
233
}
 
234
 
 
235
static void
 
236
xfpm_notify_close_critical_cb (NotifyNotification *n, XfpmNotify *notify)
 
237
{
 
238
    notify->priv->critical = NULL;
 
239
    g_object_unref (G_OBJECT (n));
 
240
}
 
241
 
 
242
static gboolean
 
243
xfpm_notify_show (NotifyNotification *n)
 
244
{
 
245
    notify_notification_show (n, NULL);
 
246
    return FALSE;
 
247
}
 
248
 
 
249
static void
 
250
xfpm_notify_close_notification (XfpmNotify *notify )
 
251
{
 
252
    if ( notify->priv->notification )
 
253
    {
 
254
        if (!notify_notification_close (notify->priv->notification, NULL))
 
255
            g_warning ("Failed to close notification\n");
 
256
        
 
257
        g_object_unref (G_OBJECT(notify->priv->notification) );
 
258
        notify->priv->notification  = NULL;
 
259
    }
 
260
}
 
261
 
 
262
XfpmNotify *
 
263
xfpm_notify_new (void)
 
264
{
 
265
    static gpointer xfpm_notify_object = NULL;
 
266
    
 
267
    if ( xfpm_notify_object != NULL )
 
268
    {
 
269
        g_object_ref (xfpm_notify_object);
 
270
    }
 
271
    else
 
272
    {
 
273
        xfpm_notify_object = g_object_new (XFPM_TYPE_NOTIFY, NULL);
 
274
        g_object_add_weak_pointer (xfpm_notify_object, &xfpm_notify_object);
 
275
    }
 
276
    return XFPM_NOTIFY (xfpm_notify_object);
 
277
}
 
278
 
 
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)
 
283
{
 
284
    NotifyNotification *n;
 
285
    
 
286
    if ( !simple )
 
287
        xfpm_notify_close_notification (notify);
 
288
    
 
289
    n = xfpm_notify_new_notification_internal (title, 
 
290
                                               text, icon_name, 
 
291
                                               timeout, urgency, 
 
292
                                               icon);
 
293
                                               
 
294
    xfpm_notify_present_notification (notify, n, simple);
 
295
}
 
296
 
 
297
NotifyNotification *xfpm_notify_new_notification (XfpmNotify *notify,
 
298
                                                  const gchar *title,
 
299
                                                  const gchar *text,
 
300
                                                  const gchar *icon_name,
 
301
                                                  guint timeout,
 
302
                                                  XfpmNotifyUrgency urgency,
 
303
                                                  GtkStatusIcon *icon)
 
304
{
 
305
    NotifyNotification *n = xfpm_notify_new_notification_internal (title, 
 
306
                                                                   text, icon_name, 
 
307
                                                                   timeout, urgency, 
 
308
                                                                   icon);
 
309
    return n;
 
310
}
 
311
 
 
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)
 
315
{
 
316
    g_return_if_fail (XFPM_IS_NOTIFY(notify));
 
317
    
 
318
    notify_notification_add_action (n, id, action_label,
 
319
                                   (NotifyActionCallback)callback,
 
320
                                    data, NULL);
 
321
    
 
322
}
 
323
 
 
324
void xfpm_notify_present_notification (XfpmNotify *notify, NotifyNotification *n, gboolean simple)
 
325
{
 
326
    g_return_if_fail (XFPM_IS_NOTIFY(notify));
 
327
    
 
328
    if ( !simple )
 
329
        xfpm_notify_close_notification (notify);
 
330
    
 
331
    if ( !simple )
 
332
    {
 
333
        g_signal_connect (G_OBJECT(n),"closed",
 
334
                        G_CALLBACK(xfpm_notify_closed_cb), notify);
 
335
        notify->priv->notification = n;
 
336
    }
 
337
    
 
338
    g_idle_add ((GSourceFunc) xfpm_notify_show, n);
 
339
}
 
340
 
 
341
void xfpm_notify_critical (XfpmNotify *notify, NotifyNotification *n)
 
342
{
 
343
    g_return_if_fail (XFPM_IS_NOTIFY (notify));
 
344
 
 
345
    xfpm_notify_close_critical (notify);
 
346
    
 
347
    notify->priv->critical = n;
 
348
    
 
349
    g_signal_connect (G_OBJECT (n), "closed", 
 
350
                      G_CALLBACK (xfpm_notify_close_critical_cb), notify);
 
351
                      
 
352
    g_idle_add ((GSourceFunc) xfpm_notify_show, n);
 
353
}
 
354
 
 
355
void xfpm_notify_close_critical (XfpmNotify *notify)
 
356
{
 
357
    g_return_if_fail (XFPM_IS_NOTIFY (notify));
 
358
    
 
359
    if ( notify->priv->critical )
 
360
    {
 
361
        if (!notify_notification_close (notify->priv->critical, NULL))
 
362
            g_warning ("Failed to close notification\n");
 
363
        
 
364
        g_object_unref (G_OBJECT(notify->priv->critical) );
 
365
        notify->priv->critical  = NULL;
 
366
    }
 
367
}
 
368
 
 
369
void xfpm_notify_close_normal  (XfpmNotify *notify)
 
370
{
 
371
    g_return_if_fail (XFPM_IS_NOTIFY (notify));
 
372
    
 
373
    xfpm_notify_close_notification (notify);
 
374
}