[gnome-clocks] utils: Use GSound rather than calling libcanberra directly



commit 2f52b227d13abcb940c8bc3fbaadcab5f1c7c5d7
Author: Tristan Brindle <t c brindle gmail com>
Date:   Fri Nov 21 17:58:55 2014 +0800

    utils: Use GSound rather than calling libcanberra directly
    
    https://bugzilla.gnome.org/show_bug.cgi?id=740484

 Makefile.am    |    2 +-
 configure.ac   |    2 +-
 src/utils.vala |   59 +++++++++++++++++++++++--------------------------------
 3 files changed, 27 insertions(+), 36 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 6adae26..50def4a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -104,7 +104,7 @@ AM_VALAFLAGS = \
        --pkg gio-2.0 \
        --pkg gtk+-3.0 \
        --pkg gweather-3.0 \
-       --pkg libcanberra \
+       --pkg gsound \
        --pkg geocode-glib-1.0 \
        --gresources  $(top_srcdir)/data/gnome-clocks.gresource.xml
 
diff --git a/configure.ac b/configure.ac
index 3a186cc..b352cfd 100644
--- a/configure.ac
+++ b/configure.ac
@@ -52,7 +52,7 @@ PKG_CHECK_MODULES(CLOCKS, [
     gio-2.0 >= 2.36
     glib-2.0 >= 2.39
     gtk+-3.0 >= 3.9.11
-    libcanberra >= 0.30
+    gsound >= 0.98
     gweather-3.0 >= 3.13.91
     gnome-desktop-3.0 >= 3.7.90
     geocode-glib-1.0 >= 0.99.4
diff --git a/src/utils.vala b/src/utils.vala
index e14700a..306bd71 100644
--- a/src/utils.vala
+++ b/src/utils.vala
@@ -285,63 +285,54 @@ public class Weekdays {
 
 public class Bell : Object {
     private GLib.Settings settings;
-    private Canberra.Context? canberra;
+    private GSound.Context? gsound;
+    private GLib.Cancellable cancellable;
     private string soundtheme;
     private string sound;
 
     public Bell (string soundid) {
         settings = new GLib.Settings("org.gnome.desktop.sound");
 
-        if (Canberra.Context.create (out canberra) < 0) {
-            warning ("Sound will not be available");
-            canberra = null;
+        try {
+            gsound = new GSound.Context();
+        } catch (GLib.Error e) {
+            warning ("Sound could not be initialized, error: %s", e.message);
         }
 
         soundtheme = settings.get_string ("theme-name");
         sound = soundid;
+        cancellable = new GLib.Cancellable();
     }
 
-    private bool keep_ringing () {
-        Canberra.Proplist pl;
-        Canberra.Proplist.create (out pl);
-        pl.sets (Canberra.PROP_EVENT_ID, sound);
-        pl.sets (Canberra.PROP_CANBERRA_XDG_THEME_NAME, soundtheme);
-        pl.sets (Canberra.PROP_MEDIA_ROLE, "alarm");
-
-        canberra.play_full (1, pl, (c, id, code) => {
-            if (code == Canberra.SUCCESS) {
-                GLib.Idle.add (keep_ringing);
-            }
-        });
-
-        return false;
-    }
+    private async void ring_real (bool repeat) {
+        if (gsound == null) {
+            return;
+        }
 
-    private void ring_real (bool once) {
-        if (canberra != null) {
-            if (once) {
-                canberra.play (1,
-                               Canberra.PROP_EVENT_ID, sound,
-                               Canberra.PROP_CANBERRA_XDG_THEME_NAME, soundtheme,
-                               Canberra.PROP_MEDIA_ROLE, "alarm");
-            } else {
-                GLib.Idle.add (keep_ringing);
-            }
+        try {
+            do {
+                yield gsound.play_full (cancellable,
+                                        GSound.Attribute.EVENT_ID, sound,
+                                        GSound.Attribute.CANBERRA_XDG_THEME_NAME, soundtheme,
+                                        GSound.Attribute.MEDIA_ROLE, "alarm");
+            } while (repeat);
+        } catch (GLib.IOError.CANCELLED e) {
+            // ignore
+        } catch (GLib.Error e) {
+            warning ("Error playing sound: %s", e.message);
         }
     }
 
     public void ring_once () {
-        ring_real (true);
+        ring_real.begin (false);
     }
 
     public void ring () {
-        ring_real (false);
+        ring_real.begin (true);
     }
 
     public void stop () {
-        if (canberra != null) {
-            canberra.cancel (1);
-        }
+        cancellable.cancel();
     }
 }
 


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