Blame gio/gnotification.c

Packit ae235b
/*
Packit ae235b
 * Copyright © 2013 Lars Uebernickel
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Authors: Lars Uebernickel <lars@uebernic.de>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gnotification-private.h"
Packit ae235b
#include "gdbusutils.h"
Packit ae235b
#include "gicon.h"
Packit ae235b
#include "gaction.h"
Packit ae235b
#include "gioenumtypes.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gnotification
Packit ae235b
 * @short_description: User Notifications (pop up messages)
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * #GNotification is a mechanism for creating a notification to be shown
Packit ae235b
 * to the user -- typically as a pop-up notification presented by the
Packit ae235b
 * desktop environment shell.
Packit ae235b
 *
Packit ae235b
 * The key difference between #GNotification and other similar APIs is
Packit ae235b
 * that, if supported by the desktop environment, notifications sent
Packit ae235b
 * with #GNotification will persist after the application has exited,
Packit ae235b
 * and even across system reboots.
Packit ae235b
 *
Packit ae235b
 * Since the user may click on a notification while the application is
Packit ae235b
 * not running, applications using #GNotification should be able to be
Packit ae235b
 * started as a D-Bus service, using #GApplication.
Packit ae235b
 *
Packit ae235b
 * User interaction with a notification (either the default action, or
Packit ae235b
 * buttons) must be associated with actions on the application (ie:
Packit ae235b
 * "app." actions).  It is not possible to route user interaction
Packit ae235b
 * through the notification itself, because the object will not exist if
Packit ae235b
 * the application is autostarted as a result of a notification being
Packit ae235b
 * clicked.
Packit ae235b
 *
Packit ae235b
 * A notification can be sent with g_application_send_notification().
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 **/
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GNotification:
Packit ae235b
 *
Packit ae235b
 * This structure type is private and should only be accessed using the
Packit ae235b
 * public APIs.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 **/
Packit ae235b
Packit ae235b
typedef GObjectClass GNotificationClass;
Packit ae235b
Packit ae235b
struct _GNotification
Packit ae235b
{
Packit ae235b
  GObject parent;
Packit ae235b
Packit ae235b
  gchar *title;
Packit ae235b
  gchar *body;
Packit ae235b
  GIcon *icon;
Packit ae235b
  GNotificationPriority priority;
Packit ae235b
  GPtrArray *buttons;
Packit ae235b
  gchar *default_action;
Packit ae235b
  GVariant *default_action_target;
Packit ae235b
};
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  gchar *label;
Packit ae235b
  gchar *action_name;
Packit ae235b
  GVariant *target;
Packit ae235b
} Button;
Packit ae235b
Packit ae235b
G_DEFINE_TYPE (GNotification, g_notification, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
static void
Packit ae235b
button_free (gpointer data)
Packit ae235b
{
Packit ae235b
  Button *button = data;
Packit ae235b
Packit ae235b
  g_free (button->label);
Packit ae235b
  g_free (button->action_name);
Packit ae235b
  if (button->target)
Packit ae235b
    g_variant_unref (button->target);
Packit ae235b
Packit ae235b
  g_slice_free (Button, button);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_notification_dispose (GObject *object)
Packit ae235b
{
Packit ae235b
  GNotification *notification = G_NOTIFICATION (object);
Packit ae235b
Packit ae235b
  g_clear_object (&notification->icon);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_notification_parent_class)->dispose (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_notification_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GNotification *notification = G_NOTIFICATION (object);
Packit ae235b
Packit ae235b
  g_free (notification->title);
Packit ae235b
  g_free (notification->body);
Packit ae235b
  g_free (notification->default_action);
Packit ae235b
  if (notification->default_action_target)
Packit ae235b
    g_variant_unref (notification->default_action_target);
Packit ae235b
  g_ptr_array_free (notification->buttons, TRUE);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_notification_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_notification_class_init (GNotificationClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  object_class->dispose = g_notification_dispose;
Packit ae235b
  object_class->finalize = g_notification_finalize;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_notification_init (GNotification *notification)
Packit ae235b
{
Packit ae235b
  notification->buttons = g_ptr_array_new_full (2, button_free);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_notification_new:
Packit ae235b
 * @title: the title of the notification
Packit ae235b
 *
Packit ae235b
 * Creates a new #GNotification with @title as its title.
Packit ae235b
 *
Packit ae235b
 * After populating @notification with more details, it can be sent to
Packit ae235b
 * the desktop shell with g_application_send_notification(). Changing
Packit ae235b
 * any properties after this call will not have any effect until
Packit ae235b
 * resending @notification.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GNotification instance
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
GNotification *
Packit ae235b
g_notification_new (const gchar *title)
Packit ae235b
{
Packit ae235b
  GNotification *notification;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (title != NULL, NULL);
Packit ae235b
Packit ae235b
  notification = g_object_new (G_TYPE_NOTIFICATION, NULL);
Packit ae235b
  notification->title = g_strdup (title);
Packit ae235b
Packit ae235b
  return notification;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< private >
Packit ae235b
 * g_notification_get_title:
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 *
Packit ae235b
 * Gets the title of @notification.
Packit ae235b
 *
Packit ae235b
 * Returns: the title of @notification
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_notification_get_title (GNotification *notification)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL);
Packit ae235b
Packit ae235b
  return notification->title;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_notification_set_title:
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 * @title: the new title for @notification
Packit ae235b
 *
Packit ae235b
 * Sets the title of @notification to @title.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_notification_set_title (GNotification *notification,
Packit ae235b
                          const gchar   *title)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_NOTIFICATION (notification));
Packit ae235b
  g_return_if_fail (title != NULL);
Packit ae235b
Packit ae235b
  g_free (notification->title);
Packit ae235b
Packit ae235b
  notification->title = g_strdup (title);
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< private >
Packit ae235b
 * g_notification_get_body:
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 *
Packit ae235b
 * Gets the current body of @notification.
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable): the body of @notification
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_notification_get_body (GNotification *notification)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL);
Packit ae235b
Packit ae235b
  return notification->body;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_notification_set_body:
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 * @body: (nullable): the new body for @notification, or %NULL
Packit ae235b
 *
Packit ae235b
 * Sets the body of @notification to @body.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_notification_set_body (GNotification *notification,
Packit ae235b
                         const gchar   *body)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_NOTIFICATION (notification));
Packit ae235b
  g_return_if_fail (body != NULL);
Packit ae235b
Packit ae235b
  g_free (notification->body);
Packit ae235b
Packit ae235b
  notification->body = g_strdup (body);
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< private >
Packit ae235b
 * g_notification_get_icon:
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 *
Packit ae235b
 * Gets the icon currently set on @notification.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the icon associated with @notification
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
GIcon *
Packit ae235b
g_notification_get_icon (GNotification *notification)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL);
Packit ae235b
Packit ae235b
  return notification->icon;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_notification_set_icon:
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 * @icon: the icon to be shown in @notification, as a #GIcon
Packit ae235b
 *
Packit ae235b
 * Sets the icon of @notification to @icon.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_notification_set_icon (GNotification *notification,
Packit ae235b
                         GIcon         *icon)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_NOTIFICATION (notification));
Packit ae235b
Packit ae235b
  if (notification->icon)
Packit ae235b
    g_object_unref (notification->icon);
Packit ae235b
Packit ae235b
  notification->icon = g_object_ref (icon);
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< private >
Packit ae235b
 * g_notification_get_priority:
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 *
Packit ae235b
 * Returns the priority of @notification
Packit ae235b
 *
Packit ae235b
 * Since: 2.42
Packit ae235b
 */
Packit ae235b
GNotificationPriority
Packit ae235b
g_notification_get_priority (GNotification *notification)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_NOTIFICATION (notification), G_NOTIFICATION_PRIORITY_NORMAL);
Packit ae235b
Packit ae235b
  return notification->priority;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_notification_set_urgent:
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 * @urgent: %TRUE if @notification is urgent
Packit ae235b
 *
Packit ae235b
 * Deprecated in favor of g_notification_set_priority().
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 * Deprecated: 2.42: Since 2.42, this has been deprecated in favour of
Packit ae235b
 *    g_notification_set_priority().
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_notification_set_urgent (GNotification *notification,
Packit ae235b
                           gboolean       urgent)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_NOTIFICATION (notification));
Packit ae235b
Packit ae235b
  notification->priority = urgent ?
Packit ae235b
      G_NOTIFICATION_PRIORITY_URGENT :
Packit ae235b
      G_NOTIFICATION_PRIORITY_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_notification_set_priority:
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 * @priority: a #GNotificationPriority
Packit ae235b
 *
Packit ae235b
 * Sets the priority of @notification to @priority. See
Packit ae235b
 * #GNotificationPriority for possible values.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_notification_set_priority (GNotification         *notification,
Packit ae235b
                             GNotificationPriority  priority)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_NOTIFICATION (notification));
Packit ae235b
Packit ae235b
  notification->priority = priority;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_notification_add_button:
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 * @label: label of the button
Packit ae235b
 * @detailed_action: a detailed action name
Packit ae235b
 *
Packit ae235b
 * Adds a button to @notification that activates the action in
Packit ae235b
 * @detailed_action when clicked. That action must be an
Packit ae235b
 * application-wide action (starting with "app."). If @detailed_action
Packit ae235b
 * contains a target, the action will be activated with that target as
Packit ae235b
 * its parameter.
Packit ae235b
 *
Packit ae235b
 * See g_action_parse_detailed_name() for a description of the format
Packit ae235b
 * for @detailed_action.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_notification_add_button (GNotification *notification,
Packit ae235b
                           const gchar   *label,
Packit ae235b
                           const gchar   *detailed_action)
Packit ae235b
{
Packit ae235b
  gchar *action;
Packit ae235b
  GVariant *target;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_return_if_fail (detailed_action != NULL);
Packit ae235b
Packit ae235b
  if (!g_action_parse_detailed_name (detailed_action, &action, &target, &error))
Packit ae235b
    {
Packit ae235b
      g_warning ("%s: %s", G_STRFUNC, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_notification_add_button_with_target_value (notification, label, action, target);
Packit ae235b
Packit ae235b
  g_free (action);
Packit ae235b
  if (target)
Packit ae235b
    g_variant_unref (target);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_notification_add_button_with_target: (skip)
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 * @label: label of the button
Packit ae235b
 * @action: an action name
Packit ae235b
 * @target_format: (nullable): a #GVariant format string, or %NULL
Packit ae235b
 * @...: positional parameters, as determined by @target_format
Packit ae235b
 *
Packit ae235b
 * Adds a button to @notification that activates @action when clicked.
Packit ae235b
 * @action must be an application-wide action (it must start with "app.").
Packit ae235b
 *
Packit ae235b
 * If @target_format is given, it is used to collect remaining
Packit ae235b
 * positional parameters into a #GVariant instance, similar to
Packit ae235b
 * g_variant_new(). @action will be activated with that #GVariant as its
Packit ae235b
 * parameter.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_notification_add_button_with_target (GNotification *notification,
Packit ae235b
                                       const gchar   *label,
Packit ae235b
                                       const gchar   *action,
Packit ae235b
                                       const gchar   *target_format,
Packit ae235b
                                       ...)
Packit ae235b
{
Packit ae235b
  va_list args;
Packit ae235b
  GVariant *target = NULL;
Packit ae235b
Packit ae235b
  if (target_format)
Packit ae235b
    {
Packit ae235b
      va_start (args, target_format);
Packit ae235b
      target = g_variant_new_va (target_format, NULL, &args);
Packit ae235b
      va_end (args);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_notification_add_button_with_target_value (notification, label, action, target);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_notification_add_button_with_target_value: (rename-to g_notification_add_button_with_target)
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 * @label: label of the button
Packit ae235b
 * @action: an action name
Packit ae235b
 * @target: (nullable): a #GVariant to use as @action's parameter, or %NULL
Packit ae235b
 *
Packit ae235b
 * Adds a button to @notification that activates @action when clicked.
Packit ae235b
 * @action must be an application-wide action (it must start with "app.").
Packit ae235b
 *
Packit ae235b
 * If @target is non-%NULL, @action will be activated with @target as
Packit ae235b
 * its parameter.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_notification_add_button_with_target_value (GNotification *notification,
Packit ae235b
                                             const gchar   *label,
Packit ae235b
                                             const gchar   *action,
Packit ae235b
                                             GVariant      *target)
Packit ae235b
{
Packit ae235b
  Button *button;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_NOTIFICATION (notification));
Packit ae235b
  g_return_if_fail (label != NULL);
Packit ae235b
  g_return_if_fail (action != NULL && g_action_name_is_valid (action));
Packit ae235b
Packit ae235b
  if (!g_str_has_prefix (action, "app."))
Packit ae235b
    {
Packit ae235b
      g_warning ("%s: action '%s' does not start with 'app.'."
Packit ae235b
                 "This is unlikely to work properly.", G_STRFUNC, action);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  button =  g_slice_new0 (Button);
Packit ae235b
  button->label = g_strdup (label);
Packit ae235b
  button->action_name = g_strdup (action);
Packit ae235b
Packit ae235b
  if (target)
Packit ae235b
    button->target = g_variant_ref_sink (target);
Packit ae235b
Packit ae235b
  g_ptr_array_add (notification->buttons, button);
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< private >
Packit ae235b
 * g_notification_get_n_buttons:
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 *
Packit ae235b
 * Returns: the amount of buttons added to @notification.
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_notification_get_n_buttons (GNotification *notification)
Packit ae235b
{
Packit ae235b
  return notification->buttons->len;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< private >
Packit ae235b
 * g_notification_get_button:
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 * @index: index of the button
Packit ae235b
 * @label: (): return location for the button's label
Packit ae235b
 * @action: (): return location for the button's associated action
Packit ae235b
 * @target: (): return location for the target @action should be
Packit ae235b
 * activated with
Packit ae235b
 *
Packit ae235b
 * Returns a description of a button that was added to @notification
Packit ae235b
 * with g_notification_add_button().
Packit ae235b
 *
Packit ae235b
 * @index must be smaller than the value returned by
Packit ae235b
 * g_notification_get_n_buttons().
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_notification_get_button (GNotification  *notification,
Packit ae235b
                           gint            index,
Packit ae235b
                           gchar         **label,
Packit ae235b
                           gchar         **action,
Packit ae235b
                           GVariant      **target)
Packit ae235b
{
Packit ae235b
  Button *button;
Packit ae235b
Packit ae235b
  button = g_ptr_array_index (notification->buttons, index);
Packit ae235b
Packit ae235b
  if (label)
Packit ae235b
    *label = g_strdup (button->label);
Packit ae235b
Packit ae235b
  if (action)
Packit ae235b
    *action = g_strdup (button->action_name);
Packit ae235b
Packit ae235b
  if (target)
Packit ae235b
    *target = button->target ? g_variant_ref (button->target) : NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< private >
Packit ae235b
 * g_notification_get_button_with_action:
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 * @action: an action name
Packit ae235b
 *
Packit ae235b
 * Returns the index of the button in @notification that is associated
Packit ae235b
 * with @action, or -1 if no such button exists.
Packit ae235b
 */
Packit ae235b
gint
Packit ae235b
g_notification_get_button_with_action (GNotification *notification,
Packit ae235b
                                       const gchar   *action)
Packit ae235b
{
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  for (i = 0; i < notification->buttons->len; i++)
Packit ae235b
    {
Packit ae235b
      Button *button;
Packit ae235b
Packit ae235b
      button = g_ptr_array_index (notification->buttons, i);
Packit ae235b
      if (g_str_equal (action, button->action_name))
Packit ae235b
        return i;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return -1;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/*< private >
Packit ae235b
 * g_notification_get_default_action:
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 * @action: (nullable): return location for the default action
Packit ae235b
 * @target: (nullable): return location for the target of the default action
Packit ae235b
 *
Packit ae235b
 * Gets the action and target for the default action of @notification.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @notification has a default action
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_notification_get_default_action (GNotification  *notification,
Packit ae235b
                                   gchar         **action,
Packit ae235b
                                   GVariant      **target)
Packit ae235b
{
Packit ae235b
  if (notification->default_action == NULL)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (action)
Packit ae235b
    *action = g_strdup (notification->default_action);
Packit ae235b
Packit ae235b
  if (target)
Packit ae235b
    {
Packit ae235b
      if (notification->default_action_target)
Packit ae235b
        *target = g_variant_ref (notification->default_action_target);
Packit ae235b
      else
Packit ae235b
        *target = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_notification_set_default_action:
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 * @detailed_action: a detailed action name
Packit ae235b
 *
Packit ae235b
 * Sets the default action of @notification to @detailed_action. This
Packit ae235b
 * action is activated when the notification is clicked on.
Packit ae235b
 *
Packit ae235b
 * The action in @detailed_action must be an application-wide action (it
Packit ae235b
 * must start with "app."). If @detailed_action contains a target, the
Packit ae235b
 * given action will be activated with that target as its parameter.
Packit ae235b
 * See g_action_parse_detailed_name() for a description of the format
Packit ae235b
 * for @detailed_action.
Packit ae235b
 *
Packit ae235b
 * When no default action is set, the application that the notification
Packit ae235b
 * was sent on is activated.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_notification_set_default_action (GNotification *notification,
Packit ae235b
                                   const gchar   *detailed_action)
Packit ae235b
{
Packit ae235b
  gchar *action;
Packit ae235b
  GVariant *target;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  if (!g_action_parse_detailed_name (detailed_action, &action, &target, &error))
Packit ae235b
    {
Packit ae235b
      g_warning ("%s: %s", G_STRFUNC, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_notification_set_default_action_and_target_value (notification, action, target);
Packit ae235b
Packit ae235b
  g_free (action);
Packit ae235b
  if (target)
Packit ae235b
    g_variant_unref (target);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_notification_set_default_action_and_target: (skip)
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 * @action: an action name
Packit ae235b
 * @target_format: (nullable): a #GVariant format string, or %NULL
Packit ae235b
 * @...: positional parameters, as determined by @target_format
Packit ae235b
 *
Packit ae235b
 * Sets the default action of @notification to @action. This action is
Packit ae235b
 * activated when the notification is clicked on. It must be an
Packit ae235b
 * application-wide action (it must start with "app.").
Packit ae235b
 *
Packit ae235b
 * If @target_format is given, it is used to collect remaining
Packit ae235b
 * positional parameters into a #GVariant instance, similar to
Packit ae235b
 * g_variant_new(). @action will be activated with that #GVariant as its
Packit ae235b
 * parameter.
Packit ae235b
 *
Packit ae235b
 * When no default action is set, the application that the notification
Packit ae235b
 * was sent on is activated.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_notification_set_default_action_and_target (GNotification *notification,
Packit ae235b
                                              const gchar   *action,
Packit ae235b
                                              const gchar   *target_format,
Packit ae235b
                                              ...)
Packit ae235b
{
Packit ae235b
  va_list args;
Packit ae235b
  GVariant *target = NULL;
Packit ae235b
Packit ae235b
  if (target_format)
Packit ae235b
    {
Packit ae235b
      va_start (args, target_format);
Packit ae235b
      target = g_variant_new_va (target_format, NULL, &args);
Packit ae235b
      va_end (args);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_notification_set_default_action_and_target_value (notification, action, target);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_notification_set_default_action_and_target_value: (rename-to g_notification_set_default_action_and_target)
Packit ae235b
 * @notification: a #GNotification
Packit ae235b
 * @action: an action name
Packit ae235b
 * @target: (nullable): a #GVariant to use as @action's parameter, or %NULL
Packit ae235b
 *
Packit ae235b
 * Sets the default action of @notification to @action. This action is
Packit ae235b
 * activated when the notification is clicked on. It must be an
Packit ae235b
 * application-wide action (start with "app.").
Packit ae235b
 *
Packit ae235b
 * If @target is non-%NULL, @action will be activated with @target as
Packit ae235b
 * its parameter.
Packit ae235b
 *
Packit ae235b
 * When no default action is set, the application that the notification
Packit ae235b
 * was sent on is activated.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_notification_set_default_action_and_target_value (GNotification *notification,
Packit ae235b
                                                    const gchar   *action,
Packit ae235b
                                                    GVariant      *target)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_NOTIFICATION (notification));
Packit ae235b
  g_return_if_fail (action != NULL && g_action_name_is_valid (action));
Packit ae235b
Packit ae235b
  if (!g_str_has_prefix (action, "app."))
Packit ae235b
    {
Packit ae235b
      g_warning ("%s: action '%s' does not start with 'app.'."
Packit ae235b
                 "This is unlikely to work properly.", G_STRFUNC, action);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (notification->default_action);
Packit ae235b
  g_clear_pointer (&notification->default_action_target, g_variant_unref);
Packit ae235b
Packit ae235b
  notification->default_action = g_strdup (action);
Packit ae235b
Packit ae235b
  if (target)
Packit ae235b
    notification->default_action_target = g_variant_ref_sink (target);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GVariant *
Packit ae235b
g_notification_serialize_button (Button *button)
Packit ae235b
{
Packit ae235b
  GVariantBuilder builder;
Packit ae235b
Packit ae235b
  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
Packit ae235b
Packit ae235b
  g_variant_builder_add (&builder, "{sv}", "label", g_variant_new_string (button->label));
Packit ae235b
  g_variant_builder_add (&builder, "{sv}", "action", g_variant_new_string (button->action_name));
Packit ae235b
Packit ae235b
  if (button->target)
Packit ae235b
    g_variant_builder_add (&builder, "{sv}", "target", button->target);
Packit ae235b
Packit ae235b
  return g_variant_builder_end (&builder);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GVariant *
Packit ae235b
g_notification_get_priority_nick (GNotification *notification)
Packit ae235b
{
Packit ae235b
  GEnumClass *enum_class;
Packit ae235b
  GEnumValue *value;
Packit ae235b
  GVariant *nick;
Packit ae235b
Packit ae235b
  enum_class = g_type_class_ref (G_TYPE_NOTIFICATION_PRIORITY);
Packit ae235b
  value = g_enum_get_value (enum_class, g_notification_get_priority (notification));
Packit ae235b
  g_assert (value != NULL);
Packit ae235b
  nick = g_variant_new_string (value->value_nick);
Packit ae235b
  g_type_class_unref (enum_class);
Packit ae235b
Packit ae235b
  return nick;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< private >
Packit ae235b
 * g_notification_serialize:
Packit ae235b
 *
Packit ae235b
 * Serializes @notification into an floating variant of type a{sv}.
Packit ae235b
 *
Packit ae235b
 * Returns: the serialized @notification as a floating variant.
Packit ae235b
 */
Packit ae235b
GVariant *
Packit ae235b
g_notification_serialize (GNotification *notification)
Packit ae235b
{
Packit ae235b
  GVariantBuilder builder;
Packit ae235b
Packit ae235b
  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
Packit ae235b
Packit ae235b
  if (notification->title)
Packit ae235b
    g_variant_builder_add (&builder, "{sv}", "title", g_variant_new_string (notification->title));
Packit ae235b
Packit ae235b
  if (notification->body)
Packit ae235b
    g_variant_builder_add (&builder, "{sv}", "body", g_variant_new_string (notification->body));
Packit ae235b
Packit ae235b
  if (notification->icon)
Packit ae235b
    {
Packit ae235b
      GVariant *serialized_icon;
Packit ae235b
Packit ae235b
      if ((serialized_icon = g_icon_serialize (notification->icon)))
Packit ae235b
        {
Packit ae235b
          g_variant_builder_add (&builder, "{sv}", "icon", serialized_icon);
Packit ae235b
          g_variant_unref (serialized_icon);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_variant_builder_add (&builder, "{sv}", "priority", g_notification_get_priority_nick (notification));
Packit ae235b
Packit ae235b
  if (notification->default_action)
Packit ae235b
    {
Packit ae235b
      g_variant_builder_add (&builder, "{sv}", "default-action",
Packit ae235b
                                               g_variant_new_string (notification->default_action));
Packit ae235b
Packit ae235b
      if (notification->default_action_target)
Packit ae235b
        g_variant_builder_add (&builder, "{sv}", "default-action-target",
Packit ae235b
                                                  notification->default_action_target);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (notification->buttons->len > 0)
Packit ae235b
    {
Packit ae235b
      GVariantBuilder actions_builder;
Packit ae235b
      guint i;
Packit ae235b
Packit ae235b
      g_variant_builder_init (&actions_builder, G_VARIANT_TYPE ("aa{sv}"));
Packit ae235b
Packit ae235b
      for (i = 0; i < notification->buttons->len; i++)
Packit ae235b
        {
Packit ae235b
          Button *button = g_ptr_array_index (notification->buttons, i);
Packit ae235b
          g_variant_builder_add (&actions_builder, "@a{sv}", g_notification_serialize_button (button));
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_variant_builder_add (&builder, "{sv}", "buttons", g_variant_builder_end (&actions_builder));
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_variant_builder_end (&builder);
Packit ae235b
}