From 1a9ebb6794aaf3a9ae4c30fba0b16872e3e383c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20No=C3=ABl?= Date: Sat, 10 Feb 2018 00:55:03 +0000 Subject: gnotification: add sound theme support and implement the backends --- gio/gcocoanotificationbackend.c | 9 +++++++- gio/gfdonotificationbackend.c | 8 +++++++ gio/gnotification-private.h | 2 ++ gio/gnotification.c | 51 +++++++++++++++++++++++++++++++++++++++++ gio/gnotification.h | 4 ++++ 5 files changed, 73 insertions(+), 1 deletion(-) diff --git a/gio/gcocoanotificationbackend.c b/gio/gcocoanotificationbackend.c index ae4ad8833..84abc00b6 100644 --- a/gio/gcocoanotificationbackend.c +++ b/gio/gcocoanotificationbackend.c @@ -202,7 +202,7 @@ g_cocoa_notification_backend_send_notification (GNotificationBackend *backend, const gchar *cstr_id, GNotification *notification) { - NSString *str_title = nil, *str_text = nil, *str_id = nil; + NSString *str_title = nil, *str_text = nil, *str_id = nil, *str_sound_name = nil; NSImage *content = nil; const char *cstr; GIcon *icon; @@ -215,6 +215,8 @@ g_cocoa_notification_backend_send_notification (GNotificationBackend *backend, str_text = nsstring_from_cstr (cstr); if (cstr_id != NULL) str_id = nsstring_from_cstr (cstr_id); + if ((cstr = g_notification_get_sound_name (notification))) + str_sound_name = nsstring_from_cstr (cstr); if ((icon = g_notification_get_icon (notification))) content = nsimage_from_gicon (icon); /* NOTE: There is no priority */ @@ -224,6 +226,11 @@ g_cocoa_notification_backend_send_notification (GNotificationBackend *backend, userNotification.informativeText = str_text; userNotification.identifier = str_id; userNotification.contentImage = content; + if (!str_sound_name) + userNotification.soundName = str_sound_name; + else + userNotification.soundName = NSUserNotificationDefaultSoundName; + /* NOTE: Buttons only show up if your bundle has NSUserNotificationAlertStyle set to "alerts" */ add_actions_to_notification (userNotification, notification); diff --git a/gio/gfdonotificationbackend.c b/gio/gfdonotificationbackend.c index a0d481433..b9bce190a 100644 --- a/gio/gfdonotificationbackend.c +++ b/gio/gfdonotificationbackend.c @@ -223,6 +223,7 @@ call_notify (GDBusConnection *con, GIcon *icon; GVariant *parameters; const gchar *body; + const gchar *sound_name; guchar urgency; g_variant_builder_init (&action_builder, G_VARIANT_TYPE_STRING_ARRAY); @@ -267,6 +268,13 @@ call_notify (GDBusConnection *con, g_variant_new_string (g_application_get_application_id (app))); urgency = urgency_from_priority (g_notification_get_priority (notification)); g_variant_builder_add (&hints_builder, "{sv}", "urgency", g_variant_new_byte (urgency)); + sound_name = g_notification_get_sound_name (app); + if (sound_name != NULL) + { + g_variant_builder_add (&hints_builder, "{sv}", "sound-name", + g_variant_new_string (sound_name)); + } + icon = g_notification_get_icon (notification); if (icon != NULL) { diff --git a/gio/gnotification-private.h b/gio/gnotification-private.h index e3e4a7818..cd2be58ff 100644 --- a/gio/gnotification-private.h +++ b/gio/gnotification-private.h @@ -49,4 +49,6 @@ gboolean g_notification_get_default_action (GNotifi GVariant * g_notification_serialize (GNotification *notification); +const gchar * g_notification_get_sound_name (GNotification *notification); + #endif diff --git a/gio/gnotification.c b/gio/gnotification.c index 8e837ed03..113c73948 100644 --- a/gio/gnotification.c +++ b/gio/gnotification.c @@ -77,6 +77,7 @@ struct _GNotification GPtrArray *buttons; gchar *default_action; GVariant *default_action_target; + gchar *sound_name; }; typedef struct @@ -122,6 +123,7 @@ g_notification_finalize (GObject *object) if (notification->default_action_target) g_variant_unref (notification->default_action_target); g_ptr_array_free (notification->buttons, TRUE); + g_free (notification->sound_name); G_OBJECT_CLASS (g_notification_parent_class)->finalize (object); } @@ -784,5 +786,54 @@ g_notification_serialize (GNotification *notification) g_variant_builder_add (&builder, "{sv}", "buttons", g_variant_builder_end (&actions_builder)); } + if (notification->sound_name) + g_variant_builder_add (&builder, "{sv}", "sound-name", g_variant_new_string (notification->sound_name)); + return g_variant_builder_end (&builder); } + +/*< private > + * g_notification_get_sound_name: + * @notification: a #GNotification + * + * Gets the title of @notification. + * + * Returns: (nullable): the sound requested to be played when showing the @notification + * + * Since: 2.56 + */ +const gchar * +g_notification_get_sound_name (GNotification *notification) +{ + g_return_val_if_fail (G_IS_NOTIFICATION (notification), NULL); + + return notification->sound_name; +} + +/** + * g_notification_set_sound_name: + * @notification: a #GNotification + * @sound_name: (nullable): the name of the sound requested to be played when + * showing the @notification, or %NULL to play the default sound + * + * Sets the name of the sound played when showing of @notification to + * @sound_name. The name of the sound should follow the freedesktop sound theme + * specification (See https://freedesktop.org/wiki/Specifications/sound-theme-spec/ + * for further information). + * + * If @sound_name is NULL, the default sound will be played. + * + * Since: 2.56 + */ +void +g_notification_set_sound_name (GNotification *notification, + const gchar *sound_name) +{ + g_return_if_fail (G_IS_NOTIFICATION (notification)); + + g_free (notification->sound_name); + if (sound_name) + { + notification->sound_name = g_strdup (sound_name); + } +} diff --git a/gio/gnotification.h b/gio/gnotification.h index 55e683012..a47e69ec3 100644 --- a/gio/gnotification.h +++ b/gio/gnotification.h @@ -92,6 +92,10 @@ void g_notification_set_default_action_and_target_value (GNotifi const gchar *action, GVariant *target); +GLIB_AVAILABLE_IN_2_56 +void g_notification_set_sound_name (GNotification *notification, + const gchar *sound_name); + G_END_DECLS #endif -- cgit v1.2.1