| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| typedef GObjectClass GNotificationClass; |
| |
| struct _GNotification |
| { |
| GObject parent; |
| |
| gchar *title; |
| gchar *body; |
| GIcon *icon; |
| GNotificationPriority priority; |
| GPtrArray *buttons; |
| gchar *default_action; |
| GVariant *default_action_target; |
| }; |
| |
| typedef struct |
| { |
| gchar *label; |
| gchar *action_name; |
| GVariant *target; |
| } Button; |
| |
| G_DEFINE_TYPE (GNotification, g_notification, G_TYPE_OBJECT) |
| |
| static void |
| button_free (gpointer data) |
| { |
| Button *button = data; |
| |
| g_free (button->label); |
| g_free (button->action_name); |
| if (button->target) |
| g_variant_unref (button->target); |
| |
| g_slice_free (Button, button); |
| } |
| |
| static void |
| g_notification_dispose (GObject *object) |
| { |
| GNotification *notification = G_NOTIFICATION (object); |
| |
| g_clear_object (¬ification->icon); |
| |
| G_OBJECT_CLASS (g_notification_parent_class)->dispose (object); |
| } |
| |
| static void |
| g_notification_finalize (GObject *object) |
| { |
| GNotification *notification = G_NOTIFICATION (object); |
| |
| g_free (notification->title); |
| g_free (notification->body); |
| g_free (notification->default_action); |
| if (notification->default_action_target) |
| g_variant_unref (notification->default_action_target); |
| g_ptr_array_free (notification->buttons, TRUE); |
| |
| G_OBJECT_CLASS (g_notification_parent_class)->finalize (object); |
| } |
| |
| static void |
| g_notification_class_init (GNotificationClass *klass) |
| { |
| GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| |
| object_class->dispose = g_notification_dispose; |
| object_class->finalize = g_notification_finalize; |
| } |
| |
| static void |
| g_notification_init (GNotification *notification) |
| { |
| notification->buttons = g_ptr_array_new_full (2, button_free); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| GNotification * |
| g_notification_new (const gchar *title) |
| { |
| GNotification *notification; |
| |
| g_return_val_if_fail (title != NULL, NULL); |
| |
| notification = g_object_new (G_TYPE_NOTIFICATION, NULL); |
| notification->title = g_strdup (title); |
| |
| return notification; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const gchar * |
| g_notification_get_title (GNotification *notification) |
| { |
| g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL); |
| |
| return notification->title; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| g_notification_set_title (GNotification *notification, |
| const gchar *title) |
| { |
| g_return_if_fail (G_IS_NOTIFICATION (notification)); |
| g_return_if_fail (title != NULL); |
| |
| g_free (notification->title); |
| |
| notification->title = g_strdup (title); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const gchar * |
| g_notification_get_body (GNotification *notification) |
| { |
| g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL); |
| |
| return notification->body; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| g_notification_set_body (GNotification *notification, |
| const gchar *body) |
| { |
| g_return_if_fail (G_IS_NOTIFICATION (notification)); |
| g_return_if_fail (body != NULL); |
| |
| g_free (notification->body); |
| |
| notification->body = g_strdup (body); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| GIcon * |
| g_notification_get_icon (GNotification *notification) |
| { |
| g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL); |
| |
| return notification->icon; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| g_notification_set_icon (GNotification *notification, |
| GIcon *icon) |
| { |
| g_return_if_fail (G_IS_NOTIFICATION (notification)); |
| |
| if (notification->icon) |
| g_object_unref (notification->icon); |
| |
| notification->icon = g_object_ref (icon); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| GNotificationPriority |
| g_notification_get_priority (GNotification *notification) |
| { |
| g_return_val_if_fail (G_IS_NOTIFICATION (notification), G_NOTIFICATION_PRIORITY_NORMAL); |
| |
| return notification->priority; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| g_notification_set_urgent (GNotification *notification, |
| gboolean urgent) |
| { |
| g_return_if_fail (G_IS_NOTIFICATION (notification)); |
| |
| notification->priority = urgent ? |
| G_NOTIFICATION_PRIORITY_URGENT : |
| G_NOTIFICATION_PRIORITY_NORMAL; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| g_notification_set_priority (GNotification *notification, |
| GNotificationPriority priority) |
| { |
| g_return_if_fail (G_IS_NOTIFICATION (notification)); |
| |
| notification->priority = priority; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| g_notification_add_button (GNotification *notification, |
| const gchar *label, |
| const gchar *detailed_action) |
| { |
| gchar *action; |
| GVariant *target; |
| GError *error = NULL; |
| |
| g_return_if_fail (detailed_action != NULL); |
| |
| if (!g_action_parse_detailed_name (detailed_action, &action, &target, &error)) |
| { |
| g_warning ("%s: %s", G_STRFUNC, error->message); |
| g_error_free (error); |
| return; |
| } |
| |
| g_notification_add_button_with_target_value (notification, label, action, target); |
| |
| g_free (action); |
| if (target) |
| g_variant_unref (target); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| g_notification_add_button_with_target (GNotification *notification, |
| const gchar *label, |
| const gchar *action, |
| const gchar *target_format, |
| ...) |
| { |
| va_list args; |
| GVariant *target = NULL; |
| |
| if (target_format) |
| { |
| va_start (args, target_format); |
| target = g_variant_new_va (target_format, NULL, &args); |
| va_end (args); |
| } |
| |
| g_notification_add_button_with_target_value (notification, label, action, target); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| g_notification_add_button_with_target_value (GNotification *notification, |
| const gchar *label, |
| const gchar *action, |
| GVariant *target) |
| { |
| Button *button; |
| |
| g_return_if_fail (G_IS_NOTIFICATION (notification)); |
| g_return_if_fail (label != NULL); |
| g_return_if_fail (action != NULL && g_action_name_is_valid (action)); |
| |
| if (!g_str_has_prefix (action, "app.")) |
| { |
| g_warning ("%s: action '%s' does not start with 'app.'." |
| "This is unlikely to work properly.", G_STRFUNC, action); |
| } |
| |
| button = g_slice_new0 (Button); |
| button->label = g_strdup (label); |
| button->action_name = g_strdup (action); |
| |
| if (target) |
| button->target = g_variant_ref_sink (target); |
| |
| g_ptr_array_add (notification->buttons, button); |
| } |
| |
| |
| |
| |
| |
| |
| |
| guint |
| g_notification_get_n_buttons (GNotification *notification) |
| { |
| return notification->buttons->len; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| g_notification_get_button (GNotification *notification, |
| gint index, |
| gchar **label, |
| gchar **action, |
| GVariant **target) |
| { |
| Button *button; |
| |
| button = g_ptr_array_index (notification->buttons, index); |
| |
| if (label) |
| *label = g_strdup (button->label); |
| |
| if (action) |
| *action = g_strdup (button->action_name); |
| |
| if (target) |
| *target = button->target ? g_variant_ref (button->target) : NULL; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| gint |
| g_notification_get_button_with_action (GNotification *notification, |
| const gchar *action) |
| { |
| guint i; |
| |
| for (i = 0; i < notification->buttons->len; i++) |
| { |
| Button *button; |
| |
| button = g_ptr_array_index (notification->buttons, i); |
| if (g_str_equal (action, button->action_name)) |
| return i; |
| } |
| |
| return -1; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| gboolean |
| g_notification_get_default_action (GNotification *notification, |
| gchar **action, |
| GVariant **target) |
| { |
| if (notification->default_action == NULL) |
| return FALSE; |
| |
| if (action) |
| *action = g_strdup (notification->default_action); |
| |
| if (target) |
| { |
| if (notification->default_action_target) |
| *target = g_variant_ref (notification->default_action_target); |
| else |
| *target = NULL; |
| } |
| |
| return TRUE; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| g_notification_set_default_action (GNotification *notification, |
| const gchar *detailed_action) |
| { |
| gchar *action; |
| GVariant *target; |
| GError *error = NULL; |
| |
| if (!g_action_parse_detailed_name (detailed_action, &action, &target, &error)) |
| { |
| g_warning ("%s: %s", G_STRFUNC, error->message); |
| g_error_free (error); |
| return; |
| } |
| |
| g_notification_set_default_action_and_target_value (notification, action, target); |
| |
| g_free (action); |
| if (target) |
| g_variant_unref (target); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| g_notification_set_default_action_and_target (GNotification *notification, |
| const gchar *action, |
| const gchar *target_format, |
| ...) |
| { |
| va_list args; |
| GVariant *target = NULL; |
| |
| if (target_format) |
| { |
| va_start (args, target_format); |
| target = g_variant_new_va (target_format, NULL, &args); |
| va_end (args); |
| } |
| |
| g_notification_set_default_action_and_target_value (notification, action, target); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void |
| g_notification_set_default_action_and_target_value (GNotification *notification, |
| const gchar *action, |
| GVariant *target) |
| { |
| g_return_if_fail (G_IS_NOTIFICATION (notification)); |
| g_return_if_fail (action != NULL && g_action_name_is_valid (action)); |
| |
| if (!g_str_has_prefix (action, "app.")) |
| { |
| g_warning ("%s: action '%s' does not start with 'app.'." |
| "This is unlikely to work properly.", G_STRFUNC, action); |
| } |
| |
| g_free (notification->default_action); |
| g_clear_pointer (¬ification->default_action_target, g_variant_unref); |
| |
| notification->default_action = g_strdup (action); |
| |
| if (target) |
| notification->default_action_target = g_variant_ref_sink (target); |
| } |
| |
| static GVariant * |
| g_notification_serialize_button (Button *button) |
| { |
| GVariantBuilder builder; |
| |
| g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}")); |
| |
| g_variant_builder_add (&builder, "{sv}", "label", g_variant_new_string (button->label)); |
| g_variant_builder_add (&builder, "{sv}", "action", g_variant_new_string (button->action_name)); |
| |
| if (button->target) |
| g_variant_builder_add (&builder, "{sv}", "target", button->target); |
| |
| return g_variant_builder_end (&builder); |
| } |
| |
| static GVariant * |
| g_notification_get_priority_nick (GNotification *notification) |
| { |
| GEnumClass *enum_class; |
| GEnumValue *value; |
| GVariant *nick; |
| |
| enum_class = g_type_class_ref (G_TYPE_NOTIFICATION_PRIORITY); |
| value = g_enum_get_value (enum_class, g_notification_get_priority (notification)); |
| g_assert (value != NULL); |
| nick = g_variant_new_string (value->value_nick); |
| g_type_class_unref (enum_class); |
| |
| return nick; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| GVariant * |
| g_notification_serialize (GNotification *notification) |
| { |
| GVariantBuilder builder; |
| |
| g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}")); |
| |
| if (notification->title) |
| g_variant_builder_add (&builder, "{sv}", "title", g_variant_new_string (notification->title)); |
| |
| if (notification->body) |
| g_variant_builder_add (&builder, "{sv}", "body", g_variant_new_string (notification->body)); |
| |
| if (notification->icon) |
| { |
| GVariant *serialized_icon; |
| |
| if ((serialized_icon = g_icon_serialize (notification->icon))) |
| { |
| g_variant_builder_add (&builder, "{sv}", "icon", serialized_icon); |
| g_variant_unref (serialized_icon); |
| } |
| } |
| |
| g_variant_builder_add (&builder, "{sv}", "priority", g_notification_get_priority_nick (notification)); |
| |
| if (notification->default_action) |
| { |
| g_variant_builder_add (&builder, "{sv}", "default-action", |
| g_variant_new_string (notification->default_action)); |
| |
| if (notification->default_action_target) |
| g_variant_builder_add (&builder, "{sv}", "default-action-target", |
| notification->default_action_target); |
| } |
| |
| if (notification->buttons->len > 0) |
| { |
| GVariantBuilder actions_builder; |
| guint i; |
| |
| g_variant_builder_init (&actions_builder, G_VARIANT_TYPE ("aa{sv}")); |
| |
| for (i = 0; i < notification->buttons->len; i++) |
| { |
| Button *button = g_ptr_array_index (notification->buttons, i); |
| g_variant_builder_add (&actions_builder, "@a{sv}", g_notification_serialize_button (button)); |
| } |
| |
| g_variant_builder_add (&builder, "{sv}", "buttons", g_variant_builder_end (&actions_builder)); |
| } |
| |
| return g_variant_builder_end (&builder); |
| } |