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"
29 typedef GObjectClass GNotificationClass;
40 gchar *default_action;
41 GVariant *default_action_target;
51 G_DEFINE_TYPE (GNotification, g_notification, G_TYPE_OBJECT);
54 button_free (gpointer data)
56 Button *button = data;
58 g_free (button->label);
59 g_free (button->action_name);
61 g_variant_unref (button->target);
63 g_slice_free (Button, button);
67 g_notification_dispose (GObject *object)
69 GNotification *notification = G_NOTIFICATION (object);
71 g_clear_object (¬ification->image);
73 G_OBJECT_CLASS (g_notification_parent_class)->dispose (object);
77 g_notification_finalize (GObject *object)
79 GNotification *notification = G_NOTIFICATION (object);
81 g_free (notification->title);
82 g_free (notification->body);
83 g_free (notification->default_action);
84 if (notification->default_action_target)
85 g_variant_unref (notification->default_action_target);
86 g_ptr_array_free (notification->buttons, TRUE);
88 G_OBJECT_CLASS (g_notification_parent_class)->finalize (object);
92 g_notification_class_init (GNotificationClass *klass)
94 GObjectClass *object_class = G_OBJECT_CLASS (klass);
96 object_class->dispose = g_notification_dispose;
97 object_class->finalize = g_notification_finalize;
101 g_notification_init (GNotification *notification)
103 notification->buttons = g_ptr_array_new_full (2, button_free);
107 * g_notification_new:
108 * @title: the title of the notification
110 * Creates a new #GNotification with @title as its title.
112 * After populating @notification with more details, it can be sent to
113 * the desktop shell with g_application_send_notification(). Changing
114 * any properties after this call will not have any effect until
115 * resending @notification.
117 * Returns: a new #GNotification instance
122 g_notification_new (const gchar *title)
124 GNotification *notification;
126 g_return_val_if_fail (title != NULL, NULL);
128 notification = g_object_new (G_TYPE_NOTIFICATION, NULL);
129 notification->title = g_strdup (title);
135 * g_notification_get_title:
136 * @notification: a #GNotification
138 * Gets the title of @notification.
140 * Returns: the title of @notification
145 g_notification_get_title (GNotification *notification)
147 g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL);
149 return notification->title;
153 * g_notification_set_title:
154 * @notification: a #GNotification
155 * title: the new title for @notification
157 * Sets the title of @notification to @title.
162 g_notification_set_title (GNotification *notification,
165 g_return_if_fail (G_IS_NOTIFICATION (notification));
166 g_return_if_fail (title != NULL);
168 g_free (notification->title);
170 notification->title = g_strdup (title);
174 * g_notification_get_body:
175 * @notification: a #GNotification
177 * Gets the current body of @notification.
179 * Returns: (allow-none): the body of @notification
184 g_notification_get_body (GNotification *notification)
186 g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL);
188 return notification->body;
192 * g_notification_set_body:
193 * @notification: a #GNotification
194 * @body: (allow-none): the new body for @notification, or %NULL
196 * Sets the body of @notification to @body.
201 g_notification_set_body (GNotification *notification,
204 g_return_if_fail (G_IS_NOTIFICATION (notification));
205 g_return_if_fail (body != NULL);
207 g_free (notification->body);
209 notification->body = g_strdup (body);
213 * g_notification_get_image:
214 * @notification: a #GNotification
216 * Gets the image currently set on @notification.
218 * Returns: (transfer none): the image associated with @notification
223 g_notification_get_image (GNotification *notification)
225 g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL);
227 return notification->image;
231 * g_notification_set_image:
232 * @notification: a #GNotification
233 * @image: the image to be shown in @notification, as a #GIcon
235 * Sets the image of @notification to @image.
240 g_notification_set_image (GNotification *notification,
243 g_return_if_fail (G_IS_NOTIFICATION (notification));
245 if (notification->image)
246 g_object_unref (notification->image);
248 notification->image = g_object_ref (image);
252 * g_notification_get_urgent:
253 * @notification: a #GNotification
255 * Returns %TRUE if @notification is marked as urgent.
260 g_notification_get_urgent (GNotification *notification)
262 g_return_val_if_fail (G_IS_NOTIFICATION (notification), FALSE);
264 return notification->urgent;
268 * g_notification_set_urgent:
269 * @notification: a #GNotification
270 * @urgent: %TRUE if @notification is urgent
272 * Sets or unsets whether @notification is marked as urgent.
277 g_notification_set_urgent (GNotification *notification,
280 g_return_if_fail (G_IS_NOTIFICATION (notification));
282 notification->urgent = urgent;
286 * g_notification_add_button:
287 * @notification: a #GNotification
288 * @label: label of the button
289 * @detailed_action: a detailed action name
291 * Adds a button to @notification that activates the action in
292 * @detailed_action when clicked. That action must be an
293 * application-wide action (starting with "app."). If @detailed_action
294 * contains a target, the action will be activated with that target as
297 * See g_action_parse_detailed_name() for a description of the format
298 * for @detailed_action.
303 g_notification_add_button (GNotification *notification,
305 const gchar *detailed_action)
309 GError *error = NULL;
311 g_return_if_fail (detailed_action != NULL);
313 if (!g_action_parse_detailed_name (detailed_action, &action, &target, &error))
315 g_warning ("%s: %s", G_STRFUNC, error->message);
316 g_error_free (error);
320 g_notification_add_button_with_target_value (notification, label, action, target);
324 g_variant_unref (target);
328 * g_notification_add_button_with_target: (skip)
329 * @notification: a #GNotification
330 * @label: label of the button
331 * @action: an action name
332 * @target_format: (allow-none): a GVariant format string, or %NULL
333 * @...: positional parameters, as determined by @format_string
335 * Adds a button to @notification that activates @action when clicked.
336 * @action must be an application-wide action (it must start with "app.").
338 * If @target_format is given, it is used to collect remaining
339 * positional parameters into a GVariant instance, similar to
340 * g_variant_new(). @action will be activated with that GVariant as its
346 g_notification_add_button_with_target (GNotification *notification,
349 const gchar *target_format,
353 GVariant *target = NULL;
357 va_start (args, target_format);
358 target = g_variant_new_va (target_format, NULL, &args);
362 g_notification_add_button_with_target_value (notification, label, action, target);
366 * g_notification_add_button_with_target_value: (rename-to g_notification_add_button_with_target)
367 * @notification: a #GNotification
368 * @label: label of the button
369 * @action: an action name
370 * @target: (allow-none): a GVariant to use as @action's parameter, or %NULL
372 * Adds a button to @notification that activates @action when clicked.
373 * @action must be an application-wide action (it must start with "app.").
375 * If @target is non-%NULL, @action will be activated with @target as
381 g_notification_add_button_with_target_value (GNotification *notification,
388 g_return_if_fail (G_IS_NOTIFICATION (notification));
389 g_return_if_fail (label != NULL);
390 g_return_if_fail (action != NULL && g_action_name_is_valid (action));
392 if (!g_str_has_prefix (action, "app."))
394 g_warning ("%s: action '%s' does not start with 'app.'."
395 "This is unlikely to work properly.", G_STRFUNC, action);
398 button = g_slice_new0 (Button);
399 button->label = g_strdup (label);
400 button->action_name = g_strdup (action);
403 button->target = g_variant_ref_sink (target);
405 g_ptr_array_add (notification->buttons, button);
409 * g_notification_get_n_buttons:
410 * @notification: a #GNotification
412 * Returns: the amount of buttons added to @notification.
415 g_notification_get_n_buttons (GNotification *notification)
417 return notification->buttons->len;
421 * g_notification_get_button:
422 * @notification: a #GNotification
423 * @index: index of the button
424 * @label: (): return location for the button's label
425 * @action: (): return location for the button's associated action
426 * @target: (): return location for the target @action should be
429 * Returns a description of a button that was added to @notification
430 * with g_notification_add_button().
432 * @index must be smaller than the value returned by
433 * g_notification_get_n_buttons().
436 g_notification_get_button (GNotification *notification,
444 button = g_ptr_array_index (notification->buttons, index);
447 *label = g_strdup (button->label);
450 *action = g_strdup (button->action_name);
453 *target = button->target ? g_variant_ref (button->target) : NULL;
457 * g_notification_get_button_with_action:
458 * @notification: a #GNotification
459 * @action: an action name
461 * Returns the index of the button in @notification that is associated
462 * with @action, or -1 if no such button exists.
465 g_notification_get_button_with_action (GNotification *notification,
470 for (i = 0; i < notification->buttons->len; i++)
474 button = g_ptr_array_index (notification->buttons, i);
475 if (g_str_equal (action, button->action_name))
484 * g_notification_get_default_action:
485 * @notification: a #GNotification
486 * @action: (allow-none): return location for the default action
487 * @target: (allow-none): return location for the target of the default action
489 * Gets the action and target for the default action of @notification.
491 * Returns: %TRUE if @notification has a default action
494 g_notification_get_default_action (GNotification *notification,
498 if (notification->default_action == NULL)
502 *action = g_strdup (notification->default_action);
506 if (notification->default_action_target)
507 *target = g_variant_ref (notification->default_action_target);
516 * g_notification_set_default_action:
517 * @notification: a #GNotification
518 * @detailed_action: a detailed action name
520 * Sets the default action of @notification to @detailed_action. This
521 * action is activated when the notification is clicked on.
523 * The action in @detailed_action must be an application-wide action (it
524 * must start with "app."). If @detailed_action contains a target, the
525 * given action will be activated with that target as its parameter.
526 * See g_action_parse_detailed_name() for a description of the format
527 * for @detailed_action.
529 * When no default action is set, the application that the notification
530 * was sent on is activated.
535 g_notification_set_default_action (GNotification *notification,
536 const gchar *detailed_action)
540 GError *error = NULL;
542 if (!g_action_parse_detailed_name (detailed_action, &action, &target, &error))
544 g_warning ("%s: %s", G_STRFUNC, error->message);
545 g_error_free (error);
549 g_notification_set_default_action_and_target_value (notification, action, target);
553 g_variant_unref (target);
557 * g_notification_set_default_action_and_target: (skip)
558 * @notification: a #GNotification
559 * @action: an action name
560 * @target_format: (allow-none): a GVariant format string, or %NULL
561 * @...: positional parameters, as determined by @format_string
563 * Sets the default action of @notification to @action. This action is
564 * activated when the notification is clicked on. It must be an
565 * application-wide action (it must start with "app.").
567 * If @target_format is given, it is used to collect remaining
568 * positional parameters into a GVariant instance, similar to
569 * g_variant_new(). @action will be activated with that GVariant as its
572 * When no default action is set, the application that the notification
573 * was sent on is activated.
578 g_notification_set_default_action_and_target (GNotification *notification,
580 const gchar *target_format,
584 GVariant *target = NULL;
588 va_start (args, target_format);
589 target = g_variant_new_va (target_format, NULL, &args);
593 g_notification_set_default_action_and_target_value (notification, action, target);
597 * g_notification_set_default_action_and_target_value: (rename-to g_notification_set_default_action_and_target)
598 * @notification: a #GNotification
599 * @action: an action name
600 * @target: (allow-none): a GVariant to use as @action's parameter, or %NULL
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 (start with "app.").
606 * If @target_format is given, it is used to collect remaining
607 * positional parameters into a GVariant instance, similar to
610 * If @target is non-%NULL, @action will be activated with @target as
613 * When no default action is set, the application that the notification
614 * was sent on is activated.
619 g_notification_set_default_action_and_target_value (GNotification *notification,
623 g_return_if_fail (G_IS_NOTIFICATION (notification));
624 g_return_if_fail (action != NULL && g_action_name_is_valid (action));
626 if (!g_str_has_prefix (action, "app."))
628 g_warning ("%s: action '%s' does not start with 'app.'."
629 "This is unlikely to work properly.", G_STRFUNC, action);
632 g_free (notification->default_action);
633 g_clear_pointer (¬ification->default_action_target, g_variant_unref);
635 notification->default_action = g_strdup (action);
638 notification->default_action_target = g_variant_ref_sink (target);