summaryrefslogtreecommitdiff
path: root/libnotify/notification.c
blob: 32f8212a1ed633d5abf772108256320cca865d2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/**
 * @file libnotify/notifynotification.c Notification object
 *
 * @Copyright (C) 2006 Christian Hammond
 * @Copyright (C) 2006 John Palmieri
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 */
#include "config.h"
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>

#include <libnotify/notify.h>
#include <libnotify/notify-marshal.h>
#include <libnotify/internal.h>

static void notify_notification_class_init(NotifyNotificationClass *klass);
static void notify_notification_init(NotifyNotification *sp);
static void notify_notification_finalize(GObject *object);
static void _close_signal_handler(DBusGProxy *proxy, guint32 id,
								  NotifyNotification *notification);

static void _action_signal_handler(DBusGProxy *proxy, guint32 id,
								   gchar *action,
								   NotifyNotification *notification);

typedef struct
{
	NotifyActionCallback cb;
	GFreeFunc free_func;
	gpointer user_data;

} CallbackPair;

struct _NotifyNotificationPrivate
{
	guint32 id;
	gchar *summary;
	gchar *body;

	/* NULL to use icon data. Anything else to have server lookup icon */
	gchar *icon_name;

	/*
	 * -1   = use server default
	 *  0   = never timeout
	 *  > 0 = Number of milliseconds before we timeout
    */
	gint timeout;

	GSList *actions;
	GHashTable *action_map;
	GHashTable *hints;

	GtkWidget *attached_widget;
	gint widget_old_x;
	gint widget_old_y;

	gboolean updates_pending;

	DBusGProxy *proxy;
};

enum
{
	SIGNAL_CLOSED,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };
static GObjectClass *parent_class = NULL;

G_DEFINE_TYPE(NotifyNotification, notify_notification, G_TYPE_OBJECT);

static void
notify_notification_class_init(NotifyNotificationClass * klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS(klass);

	parent_class = g_type_class_peek_parent(klass);

	object_class->finalize = notify_notification_finalize;

	/* Create signals here: */
	signals[SIGNAL_CLOSED] =
		g_signal_new("closed",
					 G_TYPE_FROM_CLASS(object_class),
					 G_SIGNAL_RUN_FIRST,
					 G_STRUCT_OFFSET(NotifyNotificationClass, closed),
					 NULL, NULL,
					 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

	dbus_g_object_register_marshaller(notify_marshal_VOID__UINT_STRING,
									  G_TYPE_NONE,
									  G_TYPE_UINT,
									  G_TYPE_STRING, G_TYPE_INVALID);
}

static void
_g_value_free(GValue *value)
{
	g_value_unset(value);
	g_free(value);
}

static void
destroy_pair(CallbackPair *pair)
{
	if (pair->user_data != NULL && pair->free_func != NULL)
		pair->free_func(pair->user_data);

	g_free(pair);
}

static void
notify_notification_init(NotifyNotification *obj)
{
	obj->priv = g_new0(NotifyNotificationPrivate, 1);

	obj->priv->id = 0;
	obj->priv->summary = NULL;
	obj->priv->body = NULL;
	obj->priv->icon_name = NULL;
	obj->priv->timeout = NOTIFY_EXPIRES_DEFAULT;
	obj->priv->actions = NULL;
	obj->priv->hints = g_hash_table_new_full(g_str_hash, g_str_equal,
											 g_free,
											 (GFreeFunc)_g_value_free);

	obj->priv->action_map = g_hash_table_new_full(g_str_hash, g_str_equal,
												  g_free,
												  (GFreeFunc)destroy_pair);

	obj->priv->attached_widget = NULL;

	obj->priv->updates_pending = FALSE;

	obj->priv->widget_old_x = 0;
	obj->priv->widget_old_y = 0;

	obj->priv->proxy = NULL;
}

static void
notify_notification_finalize(GObject *object)
{
	NotifyNotification *obj = NOTIFY_NOTIFICATION(object);
	NotifyNotificationPrivate *priv = obj->priv;

	g_free(priv->summary);
	g_free(priv->body);
	g_free(priv->icon_name);

	if (priv->actions != NULL)
	{
		g_slist_foreach(priv->actions, (GFunc)g_free, NULL);
		g_slist_free(priv->actions);
	}

	if (priv->action_map != NULL)
		g_hash_table_destroy(priv->action_map);

	if (priv->hints != NULL)
		g_hash_table_destroy(priv->hints);

	if (priv->attached_widget != NULL)
		g_object_unref(G_OBJECT(priv->attached_widget));

	dbus_g_proxy_disconnect_signal(priv->proxy, "NotificationClosed",
								   G_CALLBACK(_close_signal_handler),
								   object);

	dbus_g_proxy_disconnect_signal(priv->proxy, "ActionInvoked",
								   G_CALLBACK(_action_signal_handler),
								   object);

	g_free(obj->priv);

	G_OBJECT_CLASS(parent_class)->finalize(object);
}

static gboolean
_notify_notification_update_applet_hints(NotifyNotification *n)
{
	NotifyNotificationPrivate *priv = n->priv;
	gboolean update_pending = FALSE;

	if (priv->attached_widget != NULL)
	{
		gint x, y, h, w;
		GtkWidget *widget = priv->attached_widget;
		GtkRequisition requisition;

		gtk_widget_size_request(widget, &requisition);
		w = requisition.width;
		h = requisition.height;

		gdk_window_get_origin(widget->window, &x, &y);

		if (GTK_WIDGET_NO_WINDOW(widget))
		{
			x += widget->allocation.x;
			y += widget->allocation.y;
		}

		x += widget->allocation.width / 2;
		y += widget->allocation.height / 2;

		if (x != priv->widget_old_x)
		{
			notify_notification_set_hint_int32(n, "x", x);
			priv->widget_old_x = x;

			update_pending = TRUE;
		}

		if (y != priv->widget_old_y)
		{
			notify_notification_set_hint_int32(n, "y", y);
			priv->widget_old_y = y;

			update_pending = TRUE;
		}
	}

	return update_pending;
}

#if 0

/* This is left here just incase we revisit autoupdating
   One thought would be to check for updates every time the icon
   is redrawn but we still have to deal with the race conditions
   that could occure between the server and the client so we will
   leave this alone for now.
*/
static gboolean
_idle_check_updates(void *user_data)
{
	NotifyNotification *n = NOTIFY_NOTIFICATION(user_data);
	NotifyNotificationPrivate *priv = n->priv;

	if (priv->is_visible)
	{
		priv->updates_pending = _notify_notification_update_applet_hints(n);

		if (priv->updates_pending)
		{
			/* Try again if we fail on next idle */
			priv->updates_pending = !notify_notification_show(n, NULL);
		}
	}
	else
	{
		priv->updates_pending = FALSE;
	}

	return TRUE;
}
#endif

GdkFilterReturn
_catch(GdkXEvent *xevent, GdkEvent *event, gpointer data)
{
	static int i = 1;

	printf("here, %i\n", i);
	i++;
	return GDK_FILTER_CONTINUE;
}

NotifyNotification *
notify_notification_new(const gchar *summary, const gchar *body,
						const gchar *icon, GtkWidget *attach)
{
	NotifyNotification *obj;

	g_return_val_if_fail(summary != NULL && *summary != '\0',     NULL);
	g_return_val_if_fail(attach == NULL || GTK_IS_WIDGET(attach), NULL);

	obj = NOTIFY_NOTIFICATION(g_object_new(NOTIFY_TYPE_NOTIFICATION, NULL));

	obj->priv->summary = g_strdup(summary);

	if (body != NULL && *body != '\0')
		obj->priv->body = g_strdup(body);

	if (icon != NULL && *icon != '\0')
		obj->priv->icon_name = g_strdup(icon);

	if (attach != NULL)
	{
		g_object_ref(G_OBJECT(attach));
		obj->priv->attached_widget = attach;
	}

	return obj;
}

gboolean
notify_notification_update(NotifyNotification *notification,
						   const gchar *summary, const gchar *body,
						   const gchar *icon)
{
	g_return_val_if_fail(notification != NULL,                 FALSE);
	g_return_val_if_fail(NOTIFY_IS_NOTIFICATION(notification), FALSE);
	g_return_val_if_fail(summary != NULL && *summary != '\0',  FALSE);

	g_free(notification->priv->summary);
	g_free(notification->priv->body);
	g_free(notification->priv->icon_name);

	notification->priv->summary = g_strdup(summary);

	if (body != NULL && *body != '\0')
		notification->priv->body = g_strdup(body);

	if (icon != NULL && *icon != '\0')
		notification->priv->icon_name = g_strdup(icon);

	notification->priv->updates_pending = TRUE;

	return TRUE;
}

void
notify_notification_attach_to_widget(NotifyNotification *notification,
									 GtkWidget *attach)
{
	g_return_if_fail(notification != NULL);
	g_return_if_fail(NOTIFY_IS_NOTIFICATION(notification));

	if (notification->priv->attached_widget != NULL)
		g_object_unref(notification->priv->attached_widget);

	notification->priv->attached_widget =
		(attach != NULL ? g_object_ref(attach) : NULL);
}

static void
_close_signal_handler(DBusGProxy *proxy, guint32 id,
					  NotifyNotification *notification)
{
	if (id == notification->priv->id)
		g_signal_emit(notification, signals[SIGNAL_CLOSED], 0);
}

static void
_action_signal_handler(DBusGProxy *proxy, guint32 id, gchar *action,
					   NotifyNotification *notification)
{
	CallbackPair *pair;

	g_return_if_fail(notification != NULL);
	g_return_if_fail(NOTIFY_IS_NOTIFICATION(notification));

	if (id != notification->priv->id)
		return;

	pair = (CallbackPair *)g_hash_table_lookup(
		notification->priv->action_map, action);

	if (pair == NULL)
		g_warning("Recieved unknown action %s", action);
	else
		pair->cb(notification, action, pair->user_data);
}

static gchar **
_gslist_to_string_array(GSList *list)
{
	GSList *l;
	GArray *a = g_array_sized_new(TRUE, FALSE, sizeof(gchar *),
								  g_slist_length(list));

	for (l = list; l != NULL; l = l->next)
		g_array_append_val(a, l->data);

	return (gchar **)g_array_free(a, FALSE);
}

gboolean
notify_notification_show(NotifyNotification *notification, GError **error)
{
	NotifyNotificationPrivate *priv;
	GError *tmp_error = NULL;
	gchar **action_array;

	g_return_val_if_fail(notification != NULL, FALSE);
	g_return_val_if_fail(NOTIFY_IS_NOTIFICATION(notification), FALSE);
	g_return_val_if_fail(error == NULL || *error == NULL, FALSE);

	priv = notification->priv;

	if (priv->proxy == NULL)
	{
		DBusGConnection *bus = dbus_g_bus_get(DBUS_BUS_SESSION, &tmp_error);

		if (tmp_error != NULL)
		{
			g_propagate_error(error, tmp_error);
			return FALSE;
		}

		priv->proxy = dbus_g_proxy_new_for_name(bus,
												NOTIFY_DBUS_NAME,
												NOTIFY_DBUS_CORE_OBJECT,
												NOTIFY_DBUS_CORE_INTERFACE);

		dbus_g_proxy_add_signal(priv->proxy, "NotificationClosed",
								G_TYPE_UINT, G_TYPE_INVALID);
		dbus_g_proxy_connect_signal(priv->proxy, "NotificationClosed",
									G_CALLBACK(_close_signal_handler),
									notification, NULL);

		dbus_g_proxy_add_signal(priv->proxy, "ActionInvoked",
								G_TYPE_UINT, G_TYPE_STRING,
								G_TYPE_INVALID);
		dbus_g_proxy_connect_signal(priv->proxy, "ActionInvoked",
									G_CALLBACK(_action_signal_handler),
									notification, NULL);

		dbus_g_connection_unref(bus);
	}

	/* If attached to a widget, modify x and y in hints */
	_notify_notification_update_applet_hints(notification);

	action_array = _gslist_to_string_array(priv->actions);

	/* TODO: make this nonblocking */
	dbus_g_proxy_call(
		priv->proxy, "Notify", &tmp_error,
		G_TYPE_STRING, notify_get_app_name(),
		G_TYPE_UINT, priv->id,
		G_TYPE_STRING, priv->icon_name != NULL ? priv->icon_name : "",
		G_TYPE_STRING, priv->summary,
		G_TYPE_STRING, priv->body,
		G_TYPE_STRV, action_array,
		dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
							G_TYPE_VALUE), priv->hints,
		G_TYPE_INT, priv->timeout,
		G_TYPE_INVALID,
		G_TYPE_UINT, &priv->id,
		G_TYPE_INVALID);

	/* Don't free the elements because they are owned by priv->actions */
	g_free(action_array);

	if (tmp_error != NULL)
	{
		g_propagate_error(error, tmp_error);
		return FALSE;
	}

	return TRUE;
}

void
notify_notification_set_timeout(NotifyNotification *notification,
								gint timeout)
{
	g_return_if_fail(notification != NULL);
	g_return_if_fail(NOTIFY_IS_NOTIFICATION(notification));

	notification->priv->timeout = timeout;
}

void
notify_notification_set_category(NotifyNotification *notification,
								 const char *category)
{
	g_return_if_fail(notification != NULL);
	g_return_if_fail(NOTIFY_IS_NOTIFICATION(notification));

	notify_notification_set_hint_string(notification, "category", category);
}

void
notify_notification_set_urgency(NotifyNotification *notification,
								NotifyUrgency l)
{
	g_return_if_fail(notification != NULL);
	g_return_if_fail(NOTIFY_IS_NOTIFICATION(notification));

	notify_notification_set_hint_byte(notification, "urgency", (guchar)l);
}

static gboolean
_gvalue_array_append_int(GValueArray *array, gint i)
{
	GValue *value = g_new0(GValue, 1);

	if (value == NULL)
		return FALSE;

	g_value_init(value, G_TYPE_INT);
	g_value_set_int(value, i);
	g_value_array_append(array, value);

	return TRUE;
}

static gboolean
_gvalue_array_append_bool(GValueArray *array, gboolean b)
{
	GValue *value = g_new0(GValue, 1);

	if (value == NULL)
		return FALSE;

	g_value_init(value, G_TYPE_BOOLEAN);
	g_value_set_boolean(value, b);
	g_value_array_append(array, value);

	return TRUE;
}

static gboolean
_gvalue_array_append_byte_array(GValueArray *array, guchar *bytes, gsize len)
{
	GArray *byte_array;
	GValue *value;

	byte_array = g_array_sized_new(FALSE, FALSE, sizeof(guchar), len);

	if (byte_array == NULL)
		return FALSE;

	byte_array = g_array_append_vals(byte_array, bytes, len);

	if ((value = g_new0(GValue, 1)) == NULL)
	{
		g_array_free(byte_array, TRUE);
		return FALSE;
	}

	g_value_init(value, dbus_g_type_get_collection("GArray", G_TYPE_UCHAR));
	g_value_set_boxed_take_ownership(value, byte_array);
	g_value_array_append(array, value);

	return TRUE;
}

void
notify_notification_set_icon_from_pixbuf(NotifyNotification *notification,
										 GdkPixbuf *icon)
{
	gint width;
	gint height;
	gint rowstride;
	gint bits_per_sample;
	gint n_channels;
	guchar *image;
	gsize image_len;
	GValueArray *image_struct;
	GValue *value;

	g_return_if_fail(notification != NULL);
	g_return_if_fail(NOTIFY_IS_NOTIFICATION(notification));

	width           = gdk_pixbuf_get_width(icon);
	height          = gdk_pixbuf_get_height(icon);
	rowstride       = gdk_pixbuf_get_rowstride(icon);
	n_channels      = gdk_pixbuf_get_n_channels(icon);
	bits_per_sample = gdk_pixbuf_get_bits_per_sample(icon);
	image_len       = (height - 1) * rowstride + width *
	                  ((n_channels * bits_per_sample + 7) / 8);

	image = gdk_pixbuf_get_pixels(icon);

	image_struct = g_value_array_new(8);

	if (image_struct == NULL)
		goto fail;

	_gvalue_array_append_int(image_struct, width);
	_gvalue_array_append_int(image_struct, height);
	_gvalue_array_append_int(image_struct, rowstride);
	_gvalue_array_append_bool(image_struct, gdk_pixbuf_get_has_alpha(icon));
	_gvalue_array_append_int(image_struct, bits_per_sample);
	_gvalue_array_append_int(image_struct, n_channels);
	_gvalue_array_append_byte_array(image_struct, image, image_len);

	value = g_new0(GValue, 1);

	if (value == NULL)
		goto fail;

	g_value_init(value, G_TYPE_VALUE_ARRAY);
	g_value_set_boxed(value, image_struct);

	g_hash_table_insert(notification->priv->hints,
						g_strdup("icon_data"), value);

	return;

fail:
	if (image_struct != NULL)
		g_value_array_free(image_struct);
}

void
notify_notification_set_hint_int32(NotifyNotification *notification,
								   const gchar *key, gint value)
{
	GValue *hint_value;

	g_return_if_fail(notification != NULL);
	g_return_if_fail(NOTIFY_IS_NOTIFICATION(notification));
	g_return_if_fail(key != NULL && *key != '\0');

	hint_value = g_new0(GValue, 1);
	g_value_init(hint_value, G_TYPE_INT);
	g_value_set_int(hint_value, value);
	g_hash_table_insert(notification->priv->hints,
						g_strdup(key), hint_value);
}

void
notify_notification_set_hint_double(NotifyNotification *notification,
									const gchar *key, gdouble value)
{
	GValue *hint_value;

	g_return_if_fail(notification != NULL);
	g_return_if_fail(NOTIFY_IS_NOTIFICATION(notification));
	g_return_if_fail(key != NULL && *key != '\0');

	hint_value = g_new0(GValue, 1);
	g_value_init(hint_value, G_TYPE_FLOAT);
	g_value_set_float(hint_value, value);
	g_hash_table_insert(notification->priv->hints,
						g_strdup(key), hint_value);
}

void
notify_notification_set_hint_byte(NotifyNotification *notification,
								  const gchar *key, guchar value)
{
	GValue *hint_value;

	g_return_if_fail(notification != NULL);
	g_return_if_fail(NOTIFY_IS_NOTIFICATION(notification));
	g_return_if_fail(key != NULL && *key != '\0');

	hint_value = g_new0(GValue, 1);
	g_value_init(hint_value, G_TYPE_UCHAR);
	g_value_set_uchar(hint_value, value);

	g_hash_table_insert(notification->priv->hints, g_strdup(key), hint_value);
}

void
notify_notification_set_hint_byte_array(NotifyNotification *notification,
										const gchar *key,
										const guchar *value, gsize len)
{
	GValue *hint_value;
	GArray *byte_array;

	g_return_if_fail(notification != NULL);
	g_return_if_fail(NOTIFY_IS_NOTIFICATION(notification));
	g_return_if_fail(key != NULL && *key != '\0');
	g_return_if_fail(value != NULL);
	g_return_if_fail(len > 0);

	byte_array = g_array_sized_new(FALSE, FALSE, sizeof(guchar), len);
	byte_array = g_array_append_vals(byte_array, value, len);

	hint_value = g_new0(GValue, 1);
	g_value_init(hint_value, dbus_g_type_get_collection("GArray",
														G_TYPE_UCHAR));
	g_value_set_boxed_take_ownership(hint_value, byte_array);

	g_hash_table_insert(notification->priv->hints,
						g_strdup(key), hint_value);
}

void
notify_notification_set_hint_string(NotifyNotification *notification,
									const gchar *key, const gchar *value)
{
	GValue *hint_value;

	g_return_if_fail(notification != NULL);
	g_return_if_fail(NOTIFY_IS_NOTIFICATION(notification));
	g_return_if_fail(key != NULL && *key != '\0');

	hint_value = g_new0(GValue, 1);
	g_value_init(hint_value, G_TYPE_STRING);
	g_value_set_string(hint_value, value);
	g_hash_table_insert(notification->priv->hints,
						g_strdup(key), hint_value);
}

static gboolean
_remove_all(void)
{
	return TRUE;
}

void
notify_notification_clear_hints(NotifyNotification *notification)
{
	g_return_if_fail(notification != NULL);
	g_return_if_fail(NOTIFY_IS_NOTIFICATION(notification));

	g_hash_table_foreach_remove(notification->priv->hints,
								(GHRFunc)_remove_all, NULL);
}

void
notify_notification_clear_actions(NotifyNotification *notification)
{
	g_return_if_fail(notification != NULL);
	g_return_if_fail(NOTIFY_IS_NOTIFICATION(notification));

	g_hash_table_foreach_remove(notification->priv->action_map,
								(GHRFunc)_remove_all, NULL);

	if (notification->priv->actions != NULL)
	{
		g_slist_foreach(notification->priv->actions, (GFunc)g_free, NULL);
		g_slist_free(notification->priv->actions);
	}

	notification->priv->actions = NULL;
}

void
notify_notification_add_action(NotifyNotification *notification,
							   const char *action,
							   const char *label,
							   NotifyActionCallback callback,
							   gpointer user_data, GFreeFunc free_func)
{
	NotifyNotificationPrivate *priv;
	CallbackPair *pair;

	g_return_if_fail(notification != NULL);
	g_return_if_fail(NOTIFY_IS_NOTIFICATION(notification));
	g_return_if_fail(action != NULL && *action != '\0');
	g_return_if_fail(label != NULL && *label != '\0');
	g_return_if_fail(callback != NULL);

	priv = notification->priv;

	priv->actions = g_slist_append(priv->actions, g_strdup(action));
	priv->actions = g_slist_append(priv->actions, g_strdup(label));

	pair = g_new0(CallbackPair, 1);
	pair->cb = callback;
	pair->user_data = user_data;
	g_hash_table_insert(priv->action_map, g_strdup(action), pair);
}

gboolean
notify_notification_close(NotifyNotification *notification,
						  GError **error)
{
	NotifyNotificationPrivate *priv;
	GError *tmp_error = NULL;

	g_return_val_if_fail(notification != NULL, FALSE);
	g_return_val_if_fail(NOTIFY_IS_NOTIFICATION(notification), FALSE);
	g_return_val_if_fail(error == NULL || *error == NULL, FALSE);

	priv = notification->priv;

	if (priv->proxy == NULL)
	{
		DBusGConnection *bus = dbus_g_bus_get(DBUS_BUS_SESSION, &tmp_error);

		if (tmp_error != NULL)
		{
			g_propagate_error(error, tmp_error);
			return FALSE;
		}

		priv->proxy = dbus_g_proxy_new_for_name(bus,
												NOTIFY_DBUS_NAME,
												NOTIFY_DBUS_CORE_OBJECT,
												NOTIFY_DBUS_CORE_INTERFACE);
		dbus_g_connection_unref(bus);
	}

	dbus_g_proxy_call(priv->proxy, "CloseNotification", &tmp_error,
					  G_TYPE_UINT, priv->id, G_TYPE_INVALID,
					  G_TYPE_INVALID);

	if (tmp_error != NULL)
	{
		g_propagate_error(error, tmp_error);
		return FALSE;
	}

	return TRUE;
}