~ubuntu-branches/ubuntu/utopic/xfce4-power-manager/utopic-proposed

« back to all changes in this revision

Viewing changes to .pc/02_fix-notify-detect.patch/src/xfpm-notify.c

  • Committer: Package Import Robot
  • Author(s): Lionel Le Folgoc
  • Date: 2012-04-03 22:15:05 UTC
  • mfrom: (2.3.8)
  • Revision ID: package-import@ubuntu.com-20120403221505-2dvza7w2d97zb7qn
Tags: 1.0.11-0ubuntu1
* New upstream bugfix release.
* debian/patches:
  - 0001-Solaris-specific-code-for-determining-the-start-time.patch,
    02_fix-notify-detect.patch,
    07_fix_broken_empty_icon_battery.patch: dropped, included upstream.
  - xubuntu_fix-status-icon-other-devices.patch: refreshed to apply cleanly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * * Copyright (C) 2008-2011 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
 
    gulong              critical_id;
69
 
    gulong              notify_id;
70
 
    
71
 
    gboolean            supports_actions;
72
 
    gboolean            supports_sync; /*For x-canonical-private-synchronous */
73
 
};
74
 
 
75
 
enum
76
 
{
77
 
    PROP_0,
78
 
    PROP_ACTIONS,
79
 
    PROP_SYNC
80
 
};
81
 
 
82
 
G_DEFINE_TYPE(XfpmNotify, xfpm_notify, G_TYPE_OBJECT)
83
 
 
84
 
static void
85
 
xfpm_notify_get_server_caps (XfpmNotify *notify)
86
 
{
87
 
    GList *caps = NULL;
88
 
    notify->priv->supports_actions = FALSE;
89
 
    notify->priv->supports_sync    = FALSE;
90
 
    
91
 
    caps = notify_get_server_caps ();
92
 
    
93
 
    if (caps != NULL) 
94
 
    {
95
 
        if (g_list_find_custom (caps, "x-canonical-private-synchronous", (GCompareFunc) g_strcmp0) != NULL)
96
 
            notify->priv->supports_sync = TRUE;
97
 
    
98
 
        if (g_list_find_custom (caps, "actions", (GCompareFunc) g_strcmp0) != NULL)
99
 
            notify->priv->supports_actions = TRUE;
100
 
 
101
 
        g_list_foreach(caps, (GFunc)g_free, NULL);
102
 
        g_list_free(caps);
103
 
    }
104
 
}
105
 
 
106
 
static void
107
 
xfpm_notify_check_server (XfpmDBusMonitor *monitor, 
108
 
                          gchar *service_name, 
109
 
                          gboolean connected,
110
 
                          gboolean on_session,
111
 
                          XfpmNotify *notify)
112
 
{
113
 
    if ( !g_strcmp0 (service_name, "org.freedesktop.Notifications") && on_session && connected )
114
 
        xfpm_notify_get_server_caps (notify);
115
 
}
116
 
 
117
 
static void xfpm_notify_get_property (GObject *object,
118
 
                                      guint prop_id,
119
 
                                      GValue *value,
120
 
                                      GParamSpec *pspec)
121
 
{
122
 
    XfpmNotify *notify;
123
 
    
124
 
    notify = XFPM_NOTIFY (object);
125
 
    
126
 
    switch (prop_id)
127
 
    {
128
 
        case PROP_ACTIONS:
129
 
            g_value_set_boolean (value, notify->priv->supports_actions);
130
 
            break;
131
 
        case PROP_SYNC:
132
 
            g_value_set_boolean (value, notify->priv->supports_sync);
133
 
            break;
134
 
        default:
135
 
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
136
 
            break;
137
 
    }
138
 
}
139
 
 
140
 
static void
141
 
xfpm_notify_class_init (XfpmNotifyClass *klass)
142
 
{
143
 
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
144
 
 
145
 
    object_class->finalize = xfpm_notify_finalize;
146
 
    object_class->get_property = xfpm_notify_get_property;
147
 
 
148
 
    g_object_class_install_property (object_class,
149
 
                                     PROP_ACTIONS,
150
 
                                     g_param_spec_boolean ("actions",
151
 
                                                           NULL, NULL,
152
 
                                                           FALSE,
153
 
                                                           G_PARAM_READABLE));
154
 
 
155
 
    g_object_class_install_property (object_class,
156
 
                                     PROP_SYNC,
157
 
                                     g_param_spec_boolean ("sync",
158
 
                                                           NULL, NULL,
159
 
                                                           FALSE,
160
 
                                                           G_PARAM_READABLE));
161
 
 
162
 
    g_type_class_add_private (klass, sizeof (XfpmNotifyPrivate));
163
 
}
164
 
 
165
 
static void
166
 
xfpm_notify_init (XfpmNotify *notify)
167
 
{
168
 
    notify->priv = XFPM_NOTIFY_GET_PRIVATE (notify);
169
 
    
170
 
    notify->priv->notification = NULL;
171
 
    notify->priv->critical = NULL;
172
 
    
173
 
    notify->priv->critical_id = 0;
174
 
    notify->priv->notify_id   = 0;
175
 
    
176
 
    notify->priv->monitor = xfpm_dbus_monitor_new ();
177
 
    xfpm_dbus_monitor_add_service (notify->priv->monitor, DBUS_BUS_SESSION, "org.freedesktop.Notifications");
178
 
    g_signal_connect (notify->priv->monitor, "service-connection-changed",
179
 
                      G_CALLBACK (xfpm_notify_check_server), notify);
180
 
    
181
 
    xfpm_notify_get_server_caps (notify);
182
 
}
183
 
 
184
 
static void
185
 
xfpm_notify_finalize (GObject *object)
186
 
{
187
 
    XfpmNotify *notify;
188
 
 
189
 
    notify = XFPM_NOTIFY (object);
190
 
    
191
 
    xfpm_notify_close_normal (notify);
192
 
    xfpm_notify_close_critical (notify);
193
 
    
194
 
    G_OBJECT_CLASS (xfpm_notify_parent_class)->finalize(object);
195
 
}
196
 
 
197
 
static void
198
 
xfpm_notify_set_notification_icon (NotifyNotification *n, const gchar *icon_name )
199
 
{
200
 
    GdkPixbuf *pix = xfpm_icon_load (icon_name, 48);
201
 
    
202
 
    if ( pix )
203
 
    {
204
 
        notify_notification_set_icon_from_pixbuf (n,
205
 
                                                  pix);
206
 
        g_object_unref ( G_OBJECT(pix));
207
 
    }
208
 
    
209
 
}
210
 
 
211
 
static NotifyNotification *
212
 
xfpm_notify_new_notification_internal (const gchar *title, const gchar *message,
213
 
                                       const gchar *icon_name, guint timeout,
214
 
                                       XfpmNotifyUrgency urgency, GtkStatusIcon *icon)
215
 
{
216
 
    NotifyNotification *n;
217
 
    
218
 
#ifdef NOTIFY_CHECK_VERSION
219
 
#if NOTIFY_CHECK_VERSION (0, 7, 0) 
220
 
    n = notify_notification_new (title, message, NULL);
221
 
#else
222
 
    n = notify_notification_new (title, message, NULL, NULL);
223
 
#endif
224
 
#else
225
 
    n = notify_notification_new (title, message, NULL, NULL);
226
 
#endif
227
 
 
228
 
    
229
 
    if ( icon_name )
230
 
        xfpm_notify_set_notification_icon (n, icon_name);
231
 
 
232
 
#ifdef NOTIFY_CHECK_VERSION
233
 
#if !NOTIFY_CHECK_VERSION (0, 7, 0) 
234
 
    if ( icon )
235
 
        notify_notification_attach_to_status_icon (n, icon);
236
 
#else
237
 
    if ( icon )
238
 
        notify_notification_attach_to_status_icon (n, icon);
239
 
#endif
240
 
#endif
241
 
        
242
 
    notify_notification_set_urgency (n, (NotifyUrgency)urgency);
243
 
    
244
 
    if ( timeout != 0)
245
 
        notify_notification_set_timeout (n, timeout);
246
 
    
247
 
    return n;
248
 
}
249
 
 
250
 
static void
251
 
xfpm_notify_closed_cb (NotifyNotification *n, XfpmNotify *notify)
252
 
{
253
 
    notify->priv->notification = NULL;
254
 
    g_object_unref (G_OBJECT (n));
255
 
}
256
 
 
257
 
static void
258
 
xfpm_notify_close_critical_cb (NotifyNotification *n, XfpmNotify *notify)
259
 
{
260
 
    notify->priv->critical = NULL;
261
 
    g_object_unref (G_OBJECT (n));
262
 
}
263
 
 
264
 
static gboolean
265
 
xfpm_notify_show (NotifyNotification *n)
266
 
{
267
 
    notify_notification_show (n, NULL);
268
 
    return FALSE;
269
 
}
270
 
 
271
 
static void
272
 
xfpm_notify_close_notification (XfpmNotify *notify )
273
 
{
274
 
    if (notify->priv->notify_id != 0)
275
 
    {
276
 
        g_source_remove (notify->priv->notify_id);
277
 
        notify->priv->notify_id = 0;
278
 
    }
279
 
    
280
 
    if ( notify->priv->notification )
281
 
    {
282
 
        if (!notify_notification_close (notify->priv->notification, NULL))
283
 
            g_warning ("Failed to close notification\n");
284
 
        
285
 
        g_object_unref (G_OBJECT(notify->priv->notification) );
286
 
        notify->priv->notification  = NULL;
287
 
    }
288
 
}
289
 
 
290
 
XfpmNotify *
291
 
xfpm_notify_new (void)
292
 
{
293
 
    static gpointer xfpm_notify_object = NULL;
294
 
    
295
 
    if ( xfpm_notify_object != NULL )
296
 
    {
297
 
        g_object_ref (xfpm_notify_object);
298
 
    }
299
 
    else
300
 
    {
301
 
        xfpm_notify_object = g_object_new (XFPM_TYPE_NOTIFY, NULL);
302
 
        g_object_add_weak_pointer (xfpm_notify_object, &xfpm_notify_object);
303
 
    }
304
 
    return XFPM_NOTIFY (xfpm_notify_object);
305
 
}
306
 
 
307
 
void xfpm_notify_show_notification (XfpmNotify *notify, const gchar *title,
308
 
                                    const gchar *text,  const gchar *icon_name,
309
 
                                    gint timeout, gboolean simple,
310
 
                                    XfpmNotifyUrgency urgency, GtkStatusIcon *icon)
311
 
{
312
 
    NotifyNotification *n;
313
 
    
314
 
    if ( !simple )
315
 
        xfpm_notify_close_notification (notify);
316
 
    
317
 
    n = xfpm_notify_new_notification_internal (title, 
318
 
                                               text, icon_name, 
319
 
                                               timeout, urgency, 
320
 
                                               icon);
321
 
                                               
322
 
    xfpm_notify_present_notification (notify, n, simple);
323
 
}
324
 
 
325
 
NotifyNotification *xfpm_notify_new_notification (XfpmNotify *notify,
326
 
                                                  const gchar *title,
327
 
                                                  const gchar *text,
328
 
                                                  const gchar *icon_name,
329
 
                                                  guint timeout,
330
 
                                                  XfpmNotifyUrgency urgency,
331
 
                                                  GtkStatusIcon *icon)
332
 
{
333
 
    NotifyNotification *n = xfpm_notify_new_notification_internal (title, 
334
 
                                                                   text, icon_name, 
335
 
                                                                   timeout, urgency, 
336
 
                                                                   icon);
337
 
    return n;
338
 
}
339
 
 
340
 
void xfpm_notify_add_action_to_notification (XfpmNotify *notify, NotifyNotification *n,
341
 
                                            const gchar *id, const gchar *action_label,
342
 
                                            NotifyActionCallback callback, gpointer data)
343
 
{
344
 
    g_return_if_fail (XFPM_IS_NOTIFY(notify));
345
 
    
346
 
    notify_notification_add_action (n, id, action_label,
347
 
                                   (NotifyActionCallback)callback,
348
 
                                    data, NULL);
349
 
    
350
 
}
351
 
 
352
 
void xfpm_notify_present_notification (XfpmNotify *notify, NotifyNotification *n, gboolean simple)
353
 
{
354
 
    g_return_if_fail (XFPM_IS_NOTIFY(notify));
355
 
    
356
 
    if ( !simple )
357
 
        xfpm_notify_close_notification (notify);
358
 
    
359
 
    if ( !simple )
360
 
    {
361
 
        g_signal_connect (G_OBJECT(n),"closed",
362
 
                        G_CALLBACK(xfpm_notify_closed_cb), notify);
363
 
        notify->priv->notification = n;
364
 
    }
365
 
    
366
 
    notify->priv->notify_id = g_idle_add ((GSourceFunc) xfpm_notify_show, n);
367
 
}
368
 
 
369
 
void xfpm_notify_critical (XfpmNotify *notify, NotifyNotification *n)
370
 
{
371
 
    g_return_if_fail (XFPM_IS_NOTIFY (notify));
372
 
 
373
 
    xfpm_notify_close_critical (notify);
374
 
    
375
 
    notify->priv->critical = n;
376
 
    
377
 
    g_signal_connect (G_OBJECT (n), "closed", 
378
 
                      G_CALLBACK (xfpm_notify_close_critical_cb), notify);
379
 
                      
380
 
    notify->priv->critical_id = g_idle_add ((GSourceFunc) xfpm_notify_show, n);
381
 
}
382
 
 
383
 
void xfpm_notify_close_critical (XfpmNotify *notify)
384
 
{
385
 
    g_return_if_fail (XFPM_IS_NOTIFY (notify));
386
 
    
387
 
    
388
 
    if (notify->priv->critical_id != 0)
389
 
    {
390
 
        g_source_remove (notify->priv->critical_id);
391
 
        notify->priv->critical_id = 0;
392
 
    }
393
 
    
394
 
    if ( notify->priv->critical )
395
 
    {
396
 
        if (!notify_notification_close (notify->priv->critical, NULL))
397
 
            g_warning ("Failed to close notification\n");
398
 
        
399
 
        g_object_unref (G_OBJECT(notify->priv->critical) );
400
 
        notify->priv->critical  = NULL;
401
 
    }
402
 
}
403
 
 
404
 
void xfpm_notify_close_normal  (XfpmNotify *notify)
405
 
{
406
 
    g_return_if_fail (XFPM_IS_NOTIFY (notify));
407
 
    
408
 
    xfpm_notify_close_notification (notify);
409
 
}