2 * Copyright © 2013 Lars Uebernickel
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Authors: Lars Uebernickel <lars@uebernic.de>
24 #include "gnotification-private.h"
25 #include "gdbusutils.h"
30 * SECTION:gnotification
31 * @short_description: User Notifications (pop up messages)
34 * #GNotification is a mechanism for creating a notification to be shown
35 * to the user -- typically as a pop-up notification presented by the
36 * desktop environment shell.
38 * The key difference between #GNotification and other similar APIs is
39 * that, if supported by the desktop environment, notifications sent
40 * with #GNotification will persist after the application has exited,
41 * and even across system reboots.
43 * Since the user may click on a notification while the application is
44 * not running, applications using #GNotification should be able to be
45 * started as a D-Bus service, using #GApplication.
47 * User interaction with a notification (either the default action, or
48 * buttons) must be associated with actions on the application (ie:
49 * "app." actions). It is not possible to route user interaction
50 * through the notification itself, because the object will not exist if
51 * the application is autostarted as a result of a notification being
54 * A notification can be sent with g_application_send_notification().
62 * This structure type is private and should only be accessed using the
68 typedef GObjectClass GNotificationClass;
79 gchar *default_action;
80 GVariant *default_action_target;
90 G_DEFINE_TYPE (GNotification, g_notification, G_TYPE_OBJECT);
93 button_free (gpointer data)
95 Button *button = data;
97 g_free (button->label);
98 g_free (button->action_name);
100 g_variant_unref (button->target);
102 g_slice_free (Button, button);
106 g_notification_dispose (GObject *object)
108 GNotification *notification = G_NOTIFICATION (object);
110 g_clear_object (¬ification->icon);
112 G_OBJECT_CLASS (g_notification_parent_class)->dispose (object);
116 g_notification_finalize (GObject *object)
118 GNotification *notification = G_NOTIFICATION (object);
120 g_free (notification->title);
121 g_free (notification->body);
122 g_free (notification->default_action);
123 if (notification->default_action_target)
124 g_variant_unref (notification->default_action_target);
125 g_ptr_array_free (notification->buttons, TRUE);
127 G_OBJECT_CLASS (g_notification_parent_class)->finalize (object);
131 g_notification_class_init (GNotificationClass *klass)
133 GObjectClass *object_class = G_OBJECT_CLASS (klass);
135 object_class->dispose = g_notification_dispose;
136 object_class->finalize = g_notification_finalize;
140 g_notification_init (GNotification *notification)
142 notification->buttons = g_ptr_array_new_full (2, button_free);
146 * g_notification_new:
147 * @title: the title of the notification
149 * Creates a new #GNotification with @title as its title.
151 * After populating @notification with more details, it can be sent to
152 * the desktop shell with g_application_send_notification(). Changing
153 * any properties after this call will not have any effect until
154 * resending @notification.
156 * Returns: a new #GNotification instance
161 g_notification_new (const gchar *title)
163 GNotification *notification;
165 g_return_val_if_fail (title != NULL, NULL);
167 notification = g_object_new (G_TYPE_NOTIFICATION, NULL);
168 notification->title = g_strdup (title);
174 * g_notification_get_title:
175 * @notification: a #GNotification
177 * Gets the title of @notification.
179 * Returns: the title of @notification
184 g_notification_get_title (GNotification *notification)
186 g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL);
188 return notification->title;
192 * g_notification_set_title:
193 * @notification: a #GNotification
194 * @title: the new title for @notification
196 * Sets the title of @notification to @title.
201 g_notification_set_title (GNotification *notification,
204 g_return_if_fail (G_IS_NOTIFICATION (notification));
205 g_return_if_fail (title != NULL);
207 g_free (notification->title);
209 notification->title = g_strdup (title);
213 * g_notification_get_body:
214 * @notification: a #GNotification
216 * Gets the current body of @notification.
218 * Returns: (allow-none): the body of @notification
223 g_notification_get_body (GNotification *notification)
225 g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL);
227 return notification->body;
231 * g_notification_set_body:
232 * @notification: a #GNotification
233 * @body: (allow-none): the new body for @notification, or %NULL
235 * Sets the body of @notification to @body.
240 g_notification_set_body (GNotification *notification,
243 g_return_if_fail (G_IS_NOTIFICATION (notification));
244 g_return_if_fail (body != NULL);
246 g_free (notification->body);
248 notification->body = g_strdup (body);
252 * g_notification_get_icon:
253 * @notification: a #GNotification
255 * Gets the icon currently set on @notification.
257 * Returns: (transfer none): the icon associated with @notification
262 g_notification_get_icon (GNotification *notification)
264 g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL);
266 return notification->icon;
270 * g_notification_set_icon:
271 * @notification: a #GNotification
272 * @icon: the icon to be shown in @notification, as a #GIcon
274 * Sets the icon of @notification to @icon.
279 g_notification_set_icon (GNotification *notification,
282 g_return_if_fail (G_IS_NOTIFICATION (notification));
284 if (notification->icon)
285 g_object_unref (notification->icon);
287 notification->icon = g_object_ref (icon);
291 * g_notification_get_urgent:
292 * @notification: a #GNotification
294 * Returns %TRUE if @notification is marked as urgent.
299 g_notification_get_urgent (GNotification *notification)
301 g_return_val_if_fail (G_IS_NOTIFICATION (notification), FALSE);
303 return notification->urgent;
307 * g_notification_set_urgent:
308 * @notification: a #GNotification
309 * @urgent: %TRUE if @notification is urgent
311 * Sets or unsets whether @notification is marked as urgent.
316 g_notification_set_urgent (GNotification *notification,
319 g_return_if_fail (G_IS_NOTIFICATION (notification));
321 notification->urgent = urgent;
325 * g_notification_add_button:
326 * @notification: a #GNotification
327 * @label: label of the button
328 * @detailed_action: a detailed action name
330 * Adds a button to @notification that activates the action in
331 * @detailed_action when clicked. That action must be an
332 * application-wide action (starting with "app."). If @detailed_action
333 * contains a target, the action will be activated with that target as
336 * See g_action_parse_detailed_name() for a description of the format
337 * for @detailed_action.
342 g_notification_add_button (GNotification *notification,
344 const gchar *detailed_action)
348 GError *error = NULL;
350 g_return_if_fail (detailed_action != NULL);
352 if (!g_action_parse_detailed_name (detailed_action, &action, &target, &error))
354 g_warning ("%s: %s", G_STRFUNC, error->message);
355 g_error_free (error);
359 g_notification_add_button_with_target_value (notification, label, action, target);
363 g_variant_unref (target);
367 * g_notification_add_button_with_target: (skip)
368 * @notification: a #GNotification
369 * @label: label of the button
370 * @action: an action name
371 * @target_format: (allow-none): a GVariant format string, or %NULL
372 * @...: positional parameters, as determined by @format_string
374 * Adds a button to @notification that activates @action when clicked.
375 * @action must be an application-wide action (it must start with "app.").
377 * If @target_format is given, it is used to collect remaining
378 * positional parameters into a GVariant instance, similar to
379 * g_variant_new(). @action will be activated with that GVariant as its
385 g_notification_add_button_with_target (GNotification *notification,
388 const gchar *target_format,
392 GVariant *target = NULL;
396 va_start (args, target_format);
397 target = g_variant_new_va (target_format, NULL, &args);
401 g_notification_add_button_with_target_value (notification, label, action, target);
405 * g_notification_add_button_with_target_value: (rename-to g_notification_add_button_with_target)
406 * @notification: a #GNotification
407 * @label: label of the button
408 * @action: an action name
409 * @target: (allow-none): a GVariant to use as @action's parameter, or %NULL
411 * Adds a button to @notification that activates @action when clicked.
412 * @action must be an application-wide action (it must start with "app.").
414 * If @target is non-%NULL, @action will be activated with @target as
420 g_notification_add_button_with_target_value (GNotification *notification,
427 g_return_if_fail (G_IS_NOTIFICATION (notification));
428 g_return_if_fail (label != NULL);
429 g_return_if_fail (action != NULL && g_action_name_is_valid (action));
431 if (!g_str_has_prefix (action, "app."))
433 g_warning ("%s: action '%s' does not start with 'app.'."
434 "This is unlikely to work properly.", G_STRFUNC, action);
437 button = g_slice_new0 (Button);
438 button->label = g_strdup (label);
439 button->action_name = g_strdup (action);
442 button->target = g_variant_ref_sink (target);
444 g_ptr_array_add (notification->buttons, button);
448 * g_notification_get_n_buttons:
449 * @notification: a #GNotification
451 * Returns: the amount of buttons added to @notification.
454 g_notification_get_n_buttons (GNotification *notification)
456 return notification->buttons->len;
460 * g_notification_get_button:
461 * @notification: a #GNotification
462 * @index: index of the button
463 * @label: (): return location for the button's label
464 * @action: (): return location for the button's associated action
465 * @target: (): return location for the target @action should be
468 * Returns a description of a button that was added to @notification
469 * with g_notification_add_button().
471 * @index must be smaller than the value returned by
472 * g_notification_get_n_buttons().
475 g_notification_get_button (GNotification *notification,
483 button = g_ptr_array_index (notification->buttons, index);
486 *label = g_strdup (button->label);
489 *action = g_strdup (button->action_name);
492 *target = button->target ? g_variant_ref (button->target) : NULL;
496 * g_notification_get_button_with_action:
497 * @notification: a #GNotification
498 * @action: an action name
500 * Returns the index of the button in @notification that is associated
501 * with @action, or -1 if no such button exists.
504 g_notification_get_button_with_action (GNotification *notification,
509 for (i = 0; i < notification->buttons->len; i++)
513 button = g_ptr_array_index (notification->buttons, i);
514 if (g_str_equal (action, button->action_name))
523 * g_notification_get_default_action:
524 * @notification: a #GNotification
525 * @action: (allow-none): return location for the default action
526 * @target: (allow-none): return location for the target of the default action
528 * Gets the action and target for the default action of @notification.
530 * Returns: %TRUE if @notification has a default action
533 g_notification_get_default_action (GNotification *notification,
537 if (notification->default_action == NULL)
541 *action = g_strdup (notification->default_action);
545 if (notification->default_action_target)
546 *target = g_variant_ref (notification->default_action_target);
555 * g_notification_set_default_action:
556 * @notification: a #GNotification
557 * @detailed_action: a detailed action name
559 * Sets the default action of @notification to @detailed_action. This
560 * action is activated when the notification is clicked on.
562 * The action in @detailed_action must be an application-wide action (it
563 * must start with "app."). If @detailed_action contains a target, the
564 * given action will be activated with that target as its parameter.
565 * See g_action_parse_detailed_name() for a description of the format
566 * for @detailed_action.
568 * When no default action is set, the application that the notification
569 * was sent on is activated.
574 g_notification_set_default_action (GNotification *notification,
575 const gchar *detailed_action)
579 GError *error = NULL;
581 if (!g_action_parse_detailed_name (detailed_action, &action, &target, &error))
583 g_warning ("%s: %s", G_STRFUNC, error->message);
584 g_error_free (error);
588 g_notification_set_default_action_and_target_value (notification, action, target);
592 g_variant_unref (target);
596 * g_notification_set_default_action_and_target: (skip)
597 * @notification: a #GNotification
598 * @action: an action name
599 * @target_format: (allow-none): a GVariant format string, or %NULL
600 * @...: positional parameters, as determined by @format_string
602 * Sets the default action of @notification to @action. This action is
603 * activated when the notification is clicked on. It must be an
604 * application-wide action (it must start with "app.").
606 * If @target_format is given, it is used to collect remaining
607 * positional parameters into a GVariant instance, similar to
608 * g_variant_new(). @action will be activated with that GVariant as its
611 * When no default action is set, the application that the notification
612 * was sent on is activated.
617 g_notification_set_default_action_and_target (GNotification *notification,
619 const gchar *target_format,
623 GVariant *target = NULL;
627 va_start (args, target_format);
628 target = g_variant_new_va (target_format, NULL, &args);
632 g_notification_set_default_action_and_target_value (notification, action, target);
636 * g_notification_set_default_action_and_target_value: (rename-to g_notification_set_default_action_and_target)
637 * @notification: a #GNotification
638 * @action: an action name
639 * @target: (allow-none): a GVariant to use as @action's parameter, or %NULL
641 * Sets the default action of @notification to @action. This action is
642 * activated when the notification is clicked on. It must be an
643 * application-wide action (start with "app.").
645 * If @target_format is given, it is used to collect remaining
646 * positional parameters into a GVariant instance, similar to
649 * If @target is non-%NULL, @action will be activated with @target as
652 * When no default action is set, the application that the notification
653 * was sent on is activated.
658 g_notification_set_default_action_and_target_value (GNotification *notification,
662 g_return_if_fail (G_IS_NOTIFICATION (notification));
663 g_return_if_fail (action != NULL && g_action_name_is_valid (action));
665 if (!g_str_has_prefix (action, "app."))
667 g_warning ("%s: action '%s' does not start with 'app.'."
668 "This is unlikely to work properly.", G_STRFUNC, action);
671 g_free (notification->default_action);
672 g_clear_pointer (¬ification->default_action_target, g_variant_unref);
674 notification->default_action = g_strdup (action);
677 notification->default_action_target = g_variant_ref_sink (target);
681 g_notification_serialize_button (Button *button)
683 GVariantBuilder builder;
685 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
687 g_variant_builder_add (&builder, "{sv}", "label", g_variant_new_string (button->label));
688 g_variant_builder_add (&builder, "{sv}", "action", g_variant_new_string (button->action_name));
691 g_variant_builder_add (&builder, "{sv}", "target", button->target);
693 return g_variant_builder_end (&builder);
697 * g_notification_serialize:
699 * Serializes @notification into an floating variant of type a{sv}.
701 * Returns: the serialized @notification as a floating variant.
704 g_notification_serialize (GNotification *notification)
706 GVariantBuilder builder;
708 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
710 if (notification->title)
711 g_variant_builder_add (&builder, "{sv}", "title", g_variant_new_string (notification->title));
713 if (notification->body)
714 g_variant_builder_add (&builder, "{sv}", "body", g_variant_new_string (notification->body));
716 if (notification->icon)
718 GVariant *serialized_icon;
720 if ((serialized_icon = g_icon_serialize (notification->icon)))
722 g_variant_builder_add (&builder, "{sv}", "icon", serialized_icon);
723 g_variant_unref (serialized_icon);
727 g_variant_builder_add (&builder, "{sv}", "urgent", g_variant_new_boolean (notification->urgent));
729 if (notification->default_action)
731 g_variant_builder_add (&builder, "{sv}", "default-action",
732 g_variant_new_string (notification->default_action));
734 if (notification->default_action_target)
735 g_variant_builder_add (&builder, "{sv}", "default-action-target",
736 notification->default_action_target);
739 if (notification->buttons->len > 0)
741 GVariantBuilder actions_builder;
744 g_variant_builder_init (&actions_builder, G_VARIANT_TYPE ("aa{sv}"));
746 for (i = 0; i < notification->buttons->len; i++)
748 Button *button = g_ptr_array_index (notification->buttons, i);
749 g_variant_builder_add (&actions_builder, "@a{sv}", g_notification_serialize_button (button));
752 g_variant_builder_add (&builder, "{sv}", "buttons", g_variant_builder_end (&actions_builder));
755 return g_variant_builder_end (&builder);