[gnome-clocks/wip/vala] Try to implement weekdays



commit 4b0f208b6d1158eab9199f272056d962ab86a6dd
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Feb 17 19:15:30 2013 +0100

    Try to implement weekdays

 src/alarm.vala |  102 ++++++++-------------------------
 src/utils.vala |  175 ++++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 165 insertions(+), 112 deletions(-)
---
diff --git a/src/alarm.vala b/src/alarm.vala
index b98849a..5ec98d9 100644
--- a/src/alarm.vala
+++ b/src/alarm.vala
@@ -29,7 +29,6 @@ private class Item : Object {
         SNOOZING
     }
 
-    private string _name;
     public string name {
         get {
             return _name;
@@ -37,42 +36,17 @@ private class Item : Object {
 
         set {
             _name = value;
-            bell = new Utils.Bell ("alarm-clock-elapsed", _("Alarm"), value);
+            bell = new Utils.Bell ("alarm-clock-elapsed", _("Alarm"), _name);
         }
     }
+
     public int hour { get; set; }
     public int minute { get; set; }
-
-    // days[0] = Monday, ..., days[6] = Sunday
-    public int[] days { get; set; }
+    public Utils.Weekdays days { get; construct set; }
 
     public string repeat_label {
         owned get {
-            string r = null;
-            var n_active_days = days.length;
-            int[] weekdays = {0, 1, 2, 3, 4};
-            if (n_active_days == 0) {
-                r = ""; // TODO: Should whe show something for no-repeat alarms?
-            } else if (n_active_days == 1) {
-                r = Utils.LocalizedWeekdays.plurals[_days[0]];
-            } else if (n_active_days == 7) {
-                r = _("Every Day");
-            } else if (days_equal (weekdays)) {
-                r = _("Weekdays");
-            } else {
-                string[] abbrs = {};
-                for (int i = 0; i < 7; i++) {
-                    var day_num = (Utils.LocalizedWeekdays.first_weekday + i) % 7;
-                    foreach (int d in days) {
-                        if (d == day_num) {
-                            abbrs += Utils.LocalizedWeekdays.abbrs[d];
-
-                        }
-                    }
-                }
-                r = string.joinv (", ", abbrs);
-            }
-            return r;
+            return days.get_label ();
         }
     }
 
@@ -101,15 +75,15 @@ private class Item : Object {
         }
     }
 
+    private string _name;
     private bool _active;
     private GLib.DateTime alarm_time;
     private GLib.DateTime snooze_time;
     private GLib.DateTime ring_end_time;
     private Utils.Bell bell;
 
-    public Item.with_data (string name, bool active, int hour, int minute, int[] days) {
-        Object (name: name, active: active, hour: hour, minute: minute);
-        this.days = days;
+    public Item.with_data (string name, bool active, int hour, int minute, Utils.Weekdays days) {
+        Object (name: name, active: active, hour: hour, minute: minute, days: days);
 
         bell = new Utils.Bell ("alarm-clock-elapsed", _("Alarm"), name);
 
@@ -121,17 +95,7 @@ private class Item : Object {
     }
 
     public bool equal (Item a) {
-        return a.name == name && a.hour == hour && a.minute == minute && days_equal (a.days);
-    }
-
-    private bool days_equal (int[] days) {
-        if (_days.length == days.length) {
-            if (Memory.cmp (_days, days, _days.length * sizeof (int)) == 0) {
-                return true;
-            }
-        }
-
-        return false;
+        return a.name == name && a.hour == hour && a.minute == minute && days.equal (a.days);
     }
 
     public void reset () {
@@ -159,7 +123,7 @@ private class Item : Object {
         alarm_time = dt;
     }
 
-    private void update_snooze_time(GLib.DateTime start_time) {
+    private void update_snooze_time (GLib.DateTime start_time) {
         snooze_time = start_time.add_minutes (SNOOZE_MINUTES);
     }
 
@@ -217,25 +181,16 @@ private class Item : Object {
         builder.add ("{sv}", "active", new GLib.Variant.boolean (active));
         builder.add ("{sv}", "hour", new GLib.Variant.int32 (hour));
         builder.add ("{sv}", "minute", new GLib.Variant.int32 (minute));
-        builder.add ("{sv}", "days", days_variant ());
+        builder.add ("{sv}", "days", days.serialize ());
         builder.close ();
     }
 
-    // GLib.Variant.fixed_array is broken in vala for now, so roll our own
-    private GLib.Variant days_variant () {
-        var builder = new GLib.VariantBuilder (new VariantType ("ai"));
-        foreach (var i in days) {
-            builder.add ("i", new GLib.Variant.int32 (i));
-        }
-        return builder.end ();
-    }
-
     public static Item? deserialize (GLib.Variant alarm_variant) {
         string? name = null;
         bool active = true;
         int hour = -1;
         int minute = -1;
-        int[] days = null;
+        Utils.Weekdays? days = null;
         foreach (var v in alarm_variant) {
             var key = v.get_child_value (0).get_string ();
             if (key == "name") {
@@ -247,7 +202,7 @@ private class Item : Object {
             } else if (key == "minute") {
                 minute = v.get_child_value (1).get_child_value (0).get_int32 ();
             } else if (key == "days") {
-                days = {}; //TODO
+                days = Utils.Weekdays.deserialize (v.get_child_value (1).get_child_value (0));
             }
         }
         if (name != null && hour > 0 && minute > 0) {
@@ -284,9 +239,6 @@ private class SetupDialog : Gtk.Dialog {
         m_spinbutton = builder.get_object ("m_spinbutton") as Gtk.SpinButton;
         name_entry = builder.get_object ("name_entry") as Gtk.Entry;
         active_switch = builder.get_object ("active_switch") as Gtk.Switch;
-        assert (builder != null && grid != null && time_box != null &&
-                day_buttons_box != null && h_spinbutton != null &&
-                m_spinbutton != null && name_entry != null);
 
         h_spinbutton.output.connect (show_leading_zeros);
         m_spinbutton.output.connect (show_leading_zeros);
@@ -303,15 +255,13 @@ private class SetupDialog : Gtk.Dialog {
         // day_buttons[0] referencing the button for Monday, and so on.
         day_buttons = new Gtk.ToggleButton[7];
         for (int i = 0; i < 7; i++) {
-            var abbr = Utils.LocalizedWeekdays.abbrs[i];
-            var button = new Gtk.ToggleButton.with_label (abbr);
+            var button = new Gtk.ToggleButton.with_label (Utils.Weekdays.abbreviation ((Utils.Weekdays.Day) 
i));
             day_buttons[i] = button;
         }
 
-        var first_weekday = Utils.LocalizedWeekdays.first_weekday;
-
         // Pack the buttons, starting with the first day of the week
         // depending on the locale.
+        var first_weekday = Utils.Weekdays.get_first_weekday ();
         for (int i = 0; i < 7; i++) {
             var day_number = (first_weekday + i) % 7;
             day_buttons_box.pack_start (day_buttons[day_number]);
@@ -327,14 +277,14 @@ private class SetupDialog : Gtk.Dialog {
         bool active;
         int hour;
         int minute;
-        int[] days;
+        unowned Utils.Weekdays? days;
 
-        var wc = Utils.WallClock.get_default ();
         if (alarm == null) {
+            var wc = Utils.WallClock.get_default ();
             name = _("New Alarm");
             hour = wc.date_time.get_hour();
             minute = wc.date_time.get_minute();
-            days = {};
+            days = null;
             active = true;
         } else {
             name = alarm.name;
@@ -364,8 +314,10 @@ private class SetupDialog : Gtk.Dialog {
         name_entry.set_text (name);
 
         // Set the toggle buttons for weekdays.
-        for (int i = 0; i < days.length; i++) {
-            day_buttons[days[i]].active = true;
+        if (days != null) {
+            for (int i = 0; i < 7; i++) {
+                day_buttons[i].active = days.get ((Utils.Weekdays.Day) i);
+            }
         }
 
         // Set On/Off switch.
@@ -387,18 +339,14 @@ private class SetupDialog : Gtk.Dialog {
             }
         }
 
-        int[] days = {};
-        for (int i = 0; i < 7; i++) {
-            if (day_buttons[i].active) {
-                days += i;
-            }
-        }
-
         alarm.name = name;
         alarm.active = active;
         alarm.hour = hour;
         alarm.minute = minute;
-        alarm.days = days;
+
+        for (int i = 0; i < 7; i++) {
+            alarm.days.set ((Utils.Weekdays.Day) i, day_buttons[i].active);
+        }
     }
 
     private bool show_leading_zeros (Gtk.SpinButton spin_button) {
diff --git a/src/utils.vala b/src/utils.vala
index 1ae605b..34473eb 100644
--- a/src/utils.vala
+++ b/src/utils.vala
@@ -126,6 +126,146 @@ public class WallClock : Object {
     }
 }
 
+public class Weekdays {
+    public enum Day {
+        MON,
+        TUE,
+        WED,
+        THU,
+        FRI,
+        SAT,
+        SUN
+    }
+
+    private const string[] plurals = {
+        N_("Mondays"),
+        N_("Tuesdays"),
+        N_("Wednesdays"),
+        N_("Thursdays"),
+        N_("Fridays"),
+        N_("Saturdays"),
+        N_("Sundays")
+    };
+
+    private const string[] abbreviations = {
+        // TODO: fetch localized abbreviations from glib/libc
+        "Mon",
+        "Tue",
+        "Wed",
+        "Thu",
+        "Fri",
+        "Sat",
+        "Sun"
+    };
+
+    private const bool[] weekdays = {
+        true, true, true, true, true, false, false
+    };
+
+    public static Day get_first_weekday () {
+        // TODO: Implement
+        return Day.MON;
+    }
+
+    public static string plural (Day d) {
+        assert (d >= 0 && d < 7);
+        return plurals[d].dup ();
+    }
+
+    public static string abbreviation (Day d) {
+        assert (d >= 0 && d < 7);
+        return abbreviations[d].dup ();
+    }
+
+    private bool[] days= {
+        false, false, false, false, false, false, false
+    };
+
+    public Weekdays() {
+    }
+
+    private bool days_equal (bool[] d) {
+        assert (d.length == 7);
+        return (Memory.cmp (d, days, days.length * sizeof (bool)) == 0);
+    }
+
+    public bool equal (Weekdays d) {
+        return days_equal (d.days);
+    }
+
+    public bool get (Day d) {
+        assert (d >= 0 && d < 7);
+        return days[d];
+    }
+
+    public void set (Day d, bool on) {
+        assert (d >= 0 && d < 7);
+        days[d] = on;
+    }
+
+    public string get_label () {
+        string r = null;
+        int n = 0;
+        int first = -1;
+        for (int i = 0; i < 7; i++) {
+            if (get ((Day) i)) {
+                if (first < 0) {
+                    first = i;
+                }
+                n++;
+            }
+        }
+
+        if (n == 0) {
+            r = "".dup ();
+        } else if (n == 1) {
+            r = plural ((Day) first);
+        } else if (n == 7) {
+            r = _("Every Day").dup ();
+        } else if (days_equal (weekdays)) {
+            r = _("Weekdays").dup ();
+        } else {
+            string[] abbrs = {};
+            for (int i = 0; i < 7; i++) {
+                Day d = (get_first_weekday () + i) % 7;
+                if (get (d)) {
+                    abbrs += abbreviation (d);
+                }
+            }
+            r = string.joinv (", ", abbrs);
+        }
+        return r;
+    }
+
+    // Note that we serialze days according to ISO 8601
+    // (1 is Monday, 2 is Tuesday... 7 is Sunday)
+
+    public GLib.Variant serialize () {
+        var builder = new GLib.VariantBuilder (new VariantType ("ai"));
+        int32 i = 1;
+        foreach (var d in days) {
+            if (d) {
+                builder.add ("i", new GLib.Variant.int32 (i));
+            }
+            i++;
+        }
+        return builder.end ();
+    }
+
+    public static Weekdays deserialize (GLib.Variant days_variant) {
+        Weekdays d = new Weekdays ();
+        foreach (var v in days_variant) {
+            int32 i = v.get_int32 ();
+            if (i > 1 && i <= 7) {
+                d.set ((Day) (i - 1), true);
+            } else {
+                warning ("Invalid days %d", i);
+            }
+        }
+        return d;
+    }
+}
+
 public class Bell : Object {
     private GLib.Settings settings;
     private Canberra.Context? canberra;
@@ -177,40 +317,5 @@ public class Bell : Object {
     }
 }
 
-public class LocalizedWeekdays {
-    private static const string[] _plurals = {
-        N_("Mondays"),
-        N_("Tuesdays"),
-        N_("Wednesdays"),
-        N_("Thursdays"),
-        N_("Fridays"),
-        N_("Saturdays"),
-        N_("Sundays")
-    };
-
-    public static string[] plurals {
-        get { return _plurals; }
-    }
-
-    private const string[] _abbrs = {
-        // TODO: fetch localized abbreviations from glib/libc
-        "Mon",
-        "Tue",
-        "Wed",
-        "Thu",
-        "Fri",
-        "Sat",
-        "Sun"
-    };
-
-    public static string[] abbrs {
-        get { return _abbrs; }
-    }
-
-    // TODO: Implement
-    public static int first_weekday { get; private set; default = 0; }
-
-}
-
 } // namespace Utils
 } // namespace Clocks


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