[gnome-clocks: 3/3] Port to the new GLib notification API



commit 24cebe6ddf18a8abaa4b9dc0432221c66849eccc
Author: Florian Müllner <fmuellner gnome org>
Date:   Sat Oct 26 11:48:05 2013 +0200

    Port to the new GLib notification API
    
    https://bugzilla.gnome.org/show_bug.cgi?id=710913

 Makefile.am          |    1 -
 configure.ac         |    3 +--
 src/alarm.vala       |   41 ++++++++++++++++++++++++++++++++++-------
 src/application.vala |    2 ++
 src/timer.vala       |    7 ++++++-
 src/utils.vala       |   29 +----------------------------
 6 files changed, 44 insertions(+), 39 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index d9dde0d..b64979a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -100,7 +100,6 @@ AM_VALAFLAGS = \
        --pkg gtk+-3.0 \
        --pkg gweather-3.0 \
        --pkg libcanberra \
-       --pkg libnotify \
        --pkg geocode-glib-1.0 \
        --gresources  $(top_srcdir)/data/gnome-clocks.gresource.xml
 
diff --git a/configure.ac b/configure.ac
index 9a97dff..2b73b32 100644
--- a/configure.ac
+++ b/configure.ac
@@ -50,12 +50,11 @@ PKG_PROG_PKG_CONFIG([0.22])
 
 PKG_CHECK_MODULES(CLOCKS, [
     gio-2.0 >= 2.36
-    glib-2.0 >= 2.36
+    glib-2.0 >= 2.39
     gtk+-3.0 >= 3.9.11
     libcanberra >= 0.30
     gweather-3.0 >= 3.9.91
     gnome-desktop-3.0 >= 3.7.90
-    libnotify >= 0.7.0
     geocode-glib-1.0 >= 0.99.4
     geoclue-2.0 >= 1.99.3
 ])
diff --git a/src/alarm.vala b/src/alarm.vala
index 0216533..22786fc 100644
--- a/src/alarm.vala
+++ b/src/alarm.vala
@@ -95,6 +95,7 @@ private class Item : Object, ContentItem {
     private GLib.DateTime snooze_time;
     private GLib.DateTime ring_end_time;
     private Utils.Bell bell;
+    private GLib.Notification notification;
 
     public Item () {
         id = GLib.DBus.generate_guid ();
@@ -110,13 +111,11 @@ private class Item : Object, ContentItem {
     }
 
     private void setup_bell () {
-        bell = new Utils.Bell ("alarm-clock-elapsed", _("Alarm"), name);
-        bell.add_action ("stop", _("Stop"), () => {
-            stop ();
-        });
-        bell.add_action ("snooze", _("Snooze"), () => {
-            snooze ();
-        });
+        bell = new Utils.Bell ("alarm-clock-elapsed");
+        notification = new GLib.Notification (_("Alarm"));
+        notification.set_body (name);
+        notification.add_button (_("Stop"), "app.stop-alarm::".concat(id));
+        notification.add_button (_("Snooze"), "app.snooze-alarm::".concat(id));
     }
 
     public void reset () {
@@ -158,6 +157,8 @@ private class Item : Object, ContentItem {
     }
 
     public virtual signal void ring () {
+        var app = GLib.Application.get_default ();
+        app.send_notification (null, notification);
         bell.ring ();
     }
 
@@ -491,6 +492,23 @@ public class MainPanel : Gtk.Stack, Clocks.Clock {
         alarms = new List<Item> ();
         settings = new GLib.Settings ("org.gnome.clocks");
 
+        var app = GLib.Application.get_default();
+        var action = app.lookup_action ("stop-alarm");
+        ((GLib.SimpleAction)action).activate.connect ((action, param) => {
+            var item = find_item (param.get_string());
+            if (item != null) {
+                item.stop();
+            }
+        });
+
+        action = app.lookup_action ("snooze-alarm");
+        ((GLib.SimpleAction)action).activate.connect ((action, param) => {
+            var item = find_item (param.get_string());
+            if (item != null) {
+                item.snooze();
+            }
+        });
+
         // Translators: "New" refers to an alarm
         new_button = new Gtk.Button.with_label (_("New"));
         new_button.valign = Gtk.Align.CENTER;
@@ -557,6 +575,15 @@ public class MainPanel : Gtk.Stack, Clocks.Clock {
 
     public signal void ring ();
 
+    private Item? find_item (string id) {
+        foreach (var i in alarms) {
+            if (i.id == id) {
+                return i;
+            }
+        }
+        return null;
+    }
+
     private void load () {
         foreach (var a in settings.get_value ("alarms")) {
             Item? alarm = Item.deserialize (a);
diff --git a/src/application.vala b/src/application.vala
index 711e8cf..c2792a3 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -26,6 +26,8 @@ public class Application : Gtk.Application {
     };
 
     const GLib.ActionEntry[] action_entries = {
+        { "stop-alarm", null, "s" },
+        { "snooze-alarm", null, "s" },
         { "quit", on_quit_activate }
     };
 
diff --git a/src/timer.vala b/src/timer.vala
index 77d5186..f7f88cc 100644
--- a/src/timer.vala
+++ b/src/timer.vala
@@ -82,6 +82,7 @@ public class MainPanel : Gtk.Stack, Clocks.Clock {
     private double span;
     private GLib.Timer timer;
     private Utils.Bell bell;
+    private GLib.Notification notification;
     [GtkChild]
     private AnalogFrame setup_frame;
     [GtkChild]
@@ -110,7 +111,9 @@ public class MainPanel : Gtk.Stack, Clocks.Clock {
         span = 0;
         timer = new GLib.Timer ();
 
-        bell = new Utils.Bell ("complete", _("Time is up!"), _("Timer countdown finished"));
+        bell = new Utils.Bell ("complete");
+        notification = new GLib.Notification (_("Time is up!"));
+        notification.set_body (_("Timer countdown finished"));
 
         // Force LTR since we do not want to reverse [hh] : [mm] : [ss]
         grid_spinbuttons.set_direction (Gtk.TextDirection.LTR);
@@ -119,6 +122,8 @@ public class MainPanel : Gtk.Stack, Clocks.Clock {
     }
 
     public virtual signal void ring () {
+        var app = GLib.Application.get_default ();
+        app.send_notification (null, notification);
         bell.ring_once ();
     }
 
diff --git a/src/utils.vala b/src/utils.vala
index 30d28cf..f344e91 100644
--- a/src/utils.vala
+++ b/src/utils.vala
@@ -298,11 +298,8 @@ public class Bell : Object {
     private Canberra.Context? canberra;
     private string soundtheme;
     private string sound;
-    private Notify.Notification? notification;
 
-    public delegate void ActionCallback ();
-
-    public Bell (string soundid, string title, string msg) {
+    public Bell (string soundid) {
         settings = new GLib.Settings("org.gnome.desktop.sound");
 
         if (Canberra.Context.create (out canberra) < 0) {
@@ -312,14 +309,6 @@ public class Bell : Object {
 
         soundtheme = settings.get_string ("theme-name");
         sound = soundid;
-
-        notification = null;
-        if (Notify.is_initted() || Notify.init ("GNOME Clocks")) {
-            notification = new Notify.Notification (title, msg, "gnome-clocks");
-            notification.set_hint_string ("desktop-entry", "gnome-clocks");
-        } else {
-            warning ("Could not initialize notification");
-        }
     }
 
     private bool keep_ringing () {
@@ -349,14 +338,6 @@ public class Bell : Object {
                 GLib.Idle.add (keep_ringing);
             }
         }
-
-        if (notification != null) {
-            try {
-                notification.show ();
-            } catch (GLib.Error error) {
-                warning (error.message);
-            }
-        }
     }
 
     public void ring_once () {
@@ -372,14 +353,6 @@ public class Bell : Object {
             canberra.cancel (1);
         }
     }
-
-    public void add_action (string action, string label, owned ActionCallback callback) {
-        if (notification != null) {
-            notification.add_action (action, label, (n, a) => {
-                callback ();
-            });
-        }
-    }
 }
 
 } // namespace Utils


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