gnotification: add sound theme support and implement the backends

This commit is contained in:
Corentin Noël 2018-02-10 00:55:03 +00:00
parent 80ce29f44b
commit 1a9ebb6794
5 changed files with 73 additions and 1 deletions

View File

@ -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);

View File

@ -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)
{

View File

@ -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

View File

@ -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);
}
}

View File

@ -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