[glib/wip/tingping/notification-sounds] gnotification: Add g_notification_set_sound



commit 12fe17dd0974f3904045e9da0f6bdd0ac39d24eb
Author: Patrick Griffis <tingping tingping se>
Date:   Sat Nov 17 18:04:23 2018 -0500

    gnotification: Add g_notification_set_sound

 gio/gfdonotificationbackend.c | 21 +++++++++++++++++++++
 gio/gioenums.h                | 17 +++++++++++++++++
 gio/gnotification.c           | 38 ++++++++++++++++++++++++++++++++++++++
 gio/gnotification.h           |  4 ++++
 4 files changed, 80 insertions(+)
---
diff --git a/gio/gfdonotificationbackend.c b/gio/gfdonotificationbackend.c
index c4fa0dc1d..e4b3ce48d 100644
--- a/gio/gfdonotificationbackend.c
+++ b/gio/gfdonotificationbackend.c
@@ -242,6 +242,8 @@ call_notify (GDBusConnection     *con,
   GVariant *parameters;
   const gchar *body;
   guchar urgency;
+  const char *sound_name = NULL;
+  GNotificationSound sound;
 
   g_variant_builder_init (&action_builder, G_VARIANT_TYPE_STRING_ARRAY);
   if (g_notification_get_default_action (notification, NULL, NULL))
@@ -305,6 +307,25 @@ call_notify (GDBusConnection     *con,
         }
     }
 
+  sound = g_notification_get_sound (notification);
+  switch (sound)
+    {
+      case G_NOTIFICATION_SOUND_NONE:
+      case G_NOTIFICATION_SOUND_DEFAULT:
+        break;
+      case G_NOTIFICATION_SOUND_NEW_MESSAGE:
+        sound_name = "message-new-instant";
+        break;
+      default:
+        g_debug ("Unknown sound set on notification %d", sound);
+    }
+
+  if (sound_name != NULL)
+    g_variant_builder_add (&hints_builder, "{sv}", "sound-name", g_variant_new_string (sound_name));
+
+  if (sound == G_NOTIFICATION_SOUND_NONE)
+    g_variant_builder_add (&hints_builder, "{sv}", "suppress-sound", g_variant_new_boolean (TRUE));
+
   body = g_notification_get_body (notification);
 
   parameters = g_variant_new ("(susssasa{sv}i)",
diff --git a/gio/gioenums.h b/gio/gioenums.h
index a83fa71f1..bb5672d8d 100644
--- a/gio/gioenums.h
+++ b/gio/gioenums.h
@@ -1886,6 +1886,23 @@ typedef enum {
   G_NOTIFICATION_PRIORITY_URGENT
 } GNotificationPriority;
 
+/**
+ * GNotificationSound:
+ * @G_NOTIFICATION_SOUND_DEFAULT: The default behavior of the platform which may or may not have a sound.
+ * @G_NOTIFICATION_SOUND_NONE: Sounds will be surpressed if possible. Useful if playing own sounds.
+ * @G_NOTIFICATION_SOUND_NEW_MESSAGE: A sound for new messages.
+ *
+ * Sounds used for #GNotifications. The actual sounds they represent
+ * are backend defined.
+ *
+ * Since: 2.60
+ */
+typedef enum {
+  G_NOTIFICATION_SOUND_DEFAULT,
+  G_NOTIFICATION_SOUND_NONE,
+  G_NOTIFICATION_SOUND_NEW_MESSAGE,
+} GNotificationSound;
+
 /**
  * GNetworkConnectivity:
  * @G_NETWORK_CONNECTIVITY_LOCAL: The host is not configured with a
diff --git a/gio/gnotification.c b/gio/gnotification.c
index 8e837ed03..75eb4208d 100644
--- a/gio/gnotification.c
+++ b/gio/gnotification.c
@@ -77,6 +77,7 @@ struct _GNotification
   GPtrArray *buttons;
   gchar *default_action;
   GVariant *default_action_target;
+  GNotificationSound sound;
 };
 
 typedef struct
@@ -265,6 +266,24 @@ g_notification_get_icon (GNotification *notification)
   return notification->icon;
 }
 
+/*< private >
+ * g_notification_get_sound:
+ * @notification: a #GNotification
+ *
+ * Gets the sound currently set on @notification.
+ *
+ * Returns: the sound associated with @notification
+ *
+ * Since: 2.60
+ */
+GNotificationSound
+g_notification_get_sound (GNotification *notification)
+{
+  g_return_val_if_fail (G_IS_NOTIFICATION (notification), G_NOTIFICATION_SOUND_DEFAULT);
+
+  return notification->sound;
+}
+
 /**
  * g_notification_set_icon:
  * @notification: a #GNotification
@@ -286,6 +305,25 @@ g_notification_set_icon (GNotification *notification,
   notification->icon = g_object_ref (icon);
 }
 
+/**
+ * g_notification_set_sound:
+ * @notification: a #GNotification
+ * @sound: a sound to be shown with the @notification
+ *
+ * Sets the sound of @notification. The result of this sound
+ * depends upon the backend and notification service used.
+ *
+ * Since: 2.60
+ */
+void
+g_notification_set_sound (GNotification      *notification,
+                          GNotificationSound  sound)
+{
+  g_return_if_fail (G_IS_NOTIFICATION (notification));
+
+  notification->sound = sound;
+}
+
 /*< private >
  * g_notification_get_priority:
  * @notification: a #GNotification
diff --git a/gio/gnotification.h b/gio/gnotification.h
index 55e683012..3b9746296 100644
--- a/gio/gnotification.h
+++ b/gio/gnotification.h
@@ -51,6 +51,10 @@ GLIB_AVAILABLE_IN_2_40
 void                    g_notification_set_icon                         (GNotification *notification,
                                                                          GIcon         *icon);
 
+GLIB_AVAILABLE_IN_2_60
+void                    g_notification_set_sound                        (GNotification      *notification,
+                                                                         GNotificationSound  sound);
+
 GLIB_DEPRECATED_IN_2_42_FOR(g_notification_set_priority)
 void                    g_notification_set_urgent                       (GNotification *notification,
                                                                          gboolean       urgent);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]