[gnome-clocks] Port to GListModel



commit d5baf62f165229a61f110e2bdc6aaa4b491836ad
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Feb 7 14:46:56 2016 +0100

    Port to GListModel
    
    For now this is just refactoring, but it is step in the right
    direction if we want to replace the icon view with ListBox and FlowBox

 src/alarm.vala   |   59 ++++++++++++++++++++++++++++++++----------------------
 src/widgets.vala |    3 ++
 src/world.vala   |   46 ++++++++++++++++++++++++++++-------------
 3 files changed, 69 insertions(+), 39 deletions(-)
---
diff --git a/src/alarm.vala b/src/alarm.vala
index 056b707..c039dc1 100644
--- a/src/alarm.vala
+++ b/src/alarm.vala
@@ -35,6 +35,8 @@ private class Item : Object, ContentItem {
 
     public bool selectable { get; set; default = true; }
 
+    public bool selected { get; set; default = false; }
+
     public string id { get; construct set; }
 
     public string name {
@@ -184,10 +186,12 @@ private class Item : Object, ContentItem {
         return (this.alarm_time.compare (i.alarm_time) == 0 && this.active && i.active);
     }
 
-    public bool check_duplicate_alarm (List<Item> alarms_list) {
+    public bool check_duplicate_alarm (ListModel alarms) {
         update_alarm_time ();
-        foreach (Item i in alarms_list) {
-            if (compare_with_item (i)) {
+
+        var n = alarms.get_n_items ();
+        for (int i = 0; i < n; i++) {
+            if (compare_with_item (alarms.get_object (i) as Item)) {
                 return true;
             }
         }
@@ -303,17 +307,12 @@ private class SetupDialog : Gtk.Dialog {
     private Gtk.Stack am_pm_stack;
     [GtkChild]
     private Gtk.Revealer label_revealer;
-    private List<Item> alarms_list;
+    private ListModel alarms;
 
-    public SetupDialog (Gtk.Window parent, Item? alarm, List<Item> alarms) {
+    public SetupDialog (Gtk.Window parent, Item? alarm, ListModel all_alarms) {
         Object (transient_for: parent, title: alarm != null ? _("Edit Alarm") : _("New Alarm"), 
use_header_bar: 1);
 
-        alarms_list = new List<Item> ();
-        foreach (unowned Item i in alarms) {
-            if (i != alarm) {
-                alarms_list.prepend (i);
-            }
-        }
+        alarms = all_alarms;
 
         // Force LTR since we do not want to reverse [hh] : [mm]
         time_grid.set_direction (Gtk.TextDirection.LTR);
@@ -439,7 +438,7 @@ private class SetupDialog : Gtk.Dialog {
         var alarm = new Item ();
         apply_to_alarm (alarm);
 
-        if (alarm.check_duplicate_alarm (alarms_list)) {
+        if (alarm.check_duplicate_alarm (alarms)) {
             this.set_response_sensitive (1, false);
             label_revealer.set_reveal_child (true);
         } else {
@@ -524,7 +523,7 @@ public class Face : Gtk.Stack, Clocks.Clock {
     public HeaderBar header_bar { get; construct set; }
     public PanelId panel_id { get; construct set; }
 
-    private List<Item> alarms;
+    private ListStore alarms;
     private GLib.Settings settings;
     private Gtk.Button new_button;
     [GtkChild]
@@ -539,7 +538,7 @@ public class Face : Gtk.Stack, Clocks.Clock {
                 header_bar: header_bar,
                 panel_id: PanelId.ALARM);
 
-        alarms = new List<Item> ();
+        alarms = new ListStore (typeof (Item));
         settings = new GLib.Settings ("org.gnome.clocks");
 
         var app = GLib.Application.get_default();
@@ -574,7 +573,9 @@ public class Face : Gtk.Stack, Clocks.Clock {
 
         // Start ticking...
         Utils.WallClock.get_default ().tick.connect (() => {
-            foreach (var a in alarms) {
+            var n = alarms.get_n_items ();
+            for (int i = 0; i < n; i++) {
+                var a = alarms.get_object (i) as Item;
                 // a.tick() returns true if the state changed
                 if (a.tick()) {
                     if (a.state == Item.State.RINGING) {
@@ -602,9 +603,16 @@ public class Face : Gtk.Stack, Clocks.Clock {
 
     [GtkCallback]
     private void delete_selected () {
-        foreach (var i in content_view.get_selected_items ()) {
-            alarms.remove ((Item) i);
+        Object[] not_selected = {};
+        var n = alarms.get_n_items ();
+        for (int i = 0; i < n; i++) {
+            var o = alarms.get_object (i);
+            if (!((Item)o).selected) {
+                not_selected += o;
+            }
         }
+        // remove everything and readd the ones not selected
+        alarms.splice(0, n, not_selected);
         save ();
     }
 
@@ -628,9 +636,11 @@ public class Face : Gtk.Stack, Clocks.Clock {
     }
 
     private Item? find_item (string id) {
-        foreach (var i in alarms) {
-            if (i.id == id) {
-                return i;
+        var n = alarms.get_n_items ();
+        for (int i = 0; i < n; i++) {
+            var item = alarms.get_object (i) as Item;
+            if (item.id == id) {
+                return item;
             }
         }
         return null;
@@ -640,17 +650,18 @@ public class Face : Gtk.Stack, Clocks.Clock {
         foreach (var a in settings.get_value ("alarms")) {
             Item? alarm = Item.deserialize (a);
             if (alarm != null) {
-                alarms.prepend (alarm);
+                alarms.append (alarm);
                 content_view.add_item (alarm);
             }
         }
-        alarms.reverse ();
     }
 
     private void save () {
         var builder = new GLib.VariantBuilder (new VariantType ("aa{sv}"));
-        foreach (Item i in alarms) {
-            i.serialize (builder);
+        var n = alarms.get_n_items ();
+        for (int i = 0; i < n; i++) {
+            var a = alarms.get_object (i) as Item;
+            a.serialize (builder);
         }
         settings.set_value ("alarms", builder.end ());
     }
diff --git a/src/widgets.vala b/src/widgets.vala
index 8e31d10..1072251 100644
--- a/src/widgets.vala
+++ b/src/widgets.vala
@@ -258,6 +258,7 @@ public interface ContentItem : GLib.Object {
     public abstract string name { get; set; }
     public abstract string title_icon { get; set; default = null; }
     public abstract bool selectable { get; set; default = true; }
+    public abstract bool selected { get; set; default = false; }
 
     public abstract void get_thumb_properties (out string text,
                                                out string subtext,
@@ -371,6 +372,7 @@ private class IconView : Gtk.IconView {
                     ContentItem item;
                     store.get (i, Column.SELECTED, out selected, Column.ITEM, out item);
                     if (item.selectable) {
+                        item.selected = true;
                         store.set (i, Column.SELECTED, !selected);
                         selection_changed ();
                     }
@@ -419,6 +421,7 @@ private class IconView : Gtk.IconView {
             ContentItem? item;
             ((Gtk.ListStore) model).get (iter, Column.ITEM, out item);
             if (item != null && item.selectable) {
+                item.selected = true;
                 ((Gtk.ListStore) model).set (iter, Column.SELECTED, true);
             }
             return false;
diff --git a/src/world.vala b/src/world.vala
index 9c7ba27..3bfb7ef 100644
--- a/src/world.vala
+++ b/src/world.vala
@@ -31,6 +31,8 @@ public class Item : Object, ContentItem {
 
     public bool selectable { get; set; default = true; }
 
+    public bool selected { get; set; default = false; }
+
     public string name {
         get {
             // We store it in a _name member even if we overwrite it every time
@@ -228,7 +230,7 @@ public class Face : Gtk.Stack, Clocks.Clock {
     public HeaderBar header_bar { get; construct set; }
     public PanelId panel_id { get; construct set; }
 
-    private List<Item> locations;
+    private ListStore locations;
     private GLib.Settings settings;
     private Gtk.Button new_button;
     private Gtk.Button back_button;
@@ -256,7 +258,7 @@ public class Face : Gtk.Stack, Clocks.Clock {
                 panel_id: PanelId.WORLD,
                 transition_type: Gtk.StackTransitionType.CROSSFADE);
 
-        locations = new List<Item> ();
+        locations = new ListStore (typeof (Item));
         settings = new GLib.Settings ("org.gnome.clocks");
 
         day_pixbuf = Utils.load_image ("day.png");
@@ -303,7 +305,9 @@ public class Face : Gtk.Stack, Clocks.Clock {
 
         // Start ticking...
         Utils.WallClock.get_default ().tick.connect (() => {
-            foreach (var l in locations) {
+            var n = locations.get_n_items ();
+            for (int i = 0; i < n; i++) {
+                var l = locations.get_object (i) as Item;
                 l.tick();
             }
             content_view.queue_draw ();
@@ -318,9 +322,16 @@ public class Face : Gtk.Stack, Clocks.Clock {
 
     [GtkCallback]
     private void delete_selected () {
-        foreach (var i in content_view.get_selected_items ()) {
-            locations.remove ((Item) i);
+        Object[] not_selected = {};
+        var n = locations.get_n_items ();
+        for (int i = 0; i < n; i++) {
+            var o = locations.get_object (i);
+            if (!((Item)o).selected) {
+                not_selected += o;
+            }
         }
+        // remove everything and readd the ones not selected
+        locations.splice(0, n, not_selected);
         save ();
     }
 
@@ -357,18 +368,19 @@ public class Face : Gtk.Stack, Clocks.Clock {
         foreach (var l in settings.get_value ("world-clocks")) {
             Item? location = Item.deserialize (l);
             if (location != null) {
-                locations.prepend (location);
+                locations.append (location);
                 content_view.add_item (location);
             }
         }
-        locations.reverse ();
     }
 
     private void save () {
         var builder = new GLib.VariantBuilder (new VariantType ("aa{sv}"));
-        foreach (Item i in locations) {
-            if (!i.automatic) {
-                i.serialize (builder);
+        var n = locations.get_n_items ();
+        for (int i = 0; i < n; i++) {
+            var l = locations.get_object (i) as Item;
+            if (!l.automatic) {
+                l.serialize (builder);
             }
         }
         settings.set_value ("world-clocks", builder.end ());
@@ -378,8 +390,10 @@ public class Face : Gtk.Stack, Clocks.Clock {
         Geo.Info geo_info = new Geo.Info ();
 
         geo_info.location_changed.connect ((found_location) => {
-            foreach (Item i in locations) {
-                if (geo_info.is_location_similar (i.location)) {
+            var n = locations.get_n_items ();
+            for (int i = 0; i < n; i++) {
+                var l = locations.get_object (i) as Item;
+                if (geo_info.is_location_similar (l.location)) {
                     return;
                 }
             }
@@ -404,13 +418,15 @@ public class Face : Gtk.Stack, Clocks.Clock {
 
     public bool location_exists (GWeather.Location location) {
         var exists = false;
-
-        foreach (Item i in locations) {
-            if (i.location.equal(location)) {
+        var n = locations.get_n_items ();
+        for (int i = 0; i < n; i++) {
+            var l = locations.get_object (i) as Item;
+            if (l.location.equal (location)) {
                 exists = true;
                 break;
             }
         }
+
         return exists;
     }
 


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