[gnome-boxes/wip/feborges/flowbox: 14/24] collection: Decorate a Gtk.ListStore to manage items



commit bb2ec677d34f3c17d44a90195cd6a7baaf859cd6
Author: Felipe Borges <felipeborges gnome org>
Date:   Tue Dec 19 15:38:51 2017 +0100

    collection: Decorate a Gtk.ListStore to manage items
    
    Using a Gtk.ListStore we can let Gtk.ListBox and Gtk.FlowBox
    bind to the same model, and apply both filtering and sorting
    at the same place.
    
    These changes require project-wide tweaks in the way we iterate
    over CollectionItems. For this, a Collection.foreach was introduced.
    We use the classic C for loop for the other cases where loop control
    instructions are necessary.
    
    A step back in this implementation in comparison to the previous
    one is that now the removal of items is done in linear time.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=791839

 src/app-window.vala                 |    2 +-
 src/app.vala                        |   16 +++++++++++-----
 src/collection-filter-switcher.vala |    2 +-
 src/collection-toolbar.vala         |    4 ++--
 src/collection.vala                 |   35 ++++++++++++++++++++++++++++-------
 src/empty-boxes.vala                |    2 +-
 src/libvirt-broker.vala             |    4 +++-
 src/selection-toolbar.vala          |    2 +-
 src/selectionbar.vala               |    8 ++++----
 9 files changed, 52 insertions(+), 23 deletions(-)
---
diff --git a/src/app-window.vala b/src/app-window.vala
index e3b7144..2b2e900 100644
--- a/src/app-window.vala
+++ b/src/app-window.vala
@@ -231,7 +231,7 @@ private class Boxes.AppWindow: Gtk.ApplicationWindow, Boxes.UI {
 
         switch (ui_state) {
         case UIState.COLLECTION:
-            if (App.app.collection.items.length != 0)
+            if (App.app.collection.length != 0)
                 below_bin.visible_child = view;
             else
                 below_bin.visible_child = empty_boxes;
diff --git a/src/app.vala b/src/app.vala
index 9c21615..fa4bd6f 100644
--- a/src/app.vala
+++ b/src/app.vala
@@ -4,7 +4,9 @@ private abstract class Boxes.Broker : GLib.Object {
     // Overriding subclass should chain-up at the end of its implementation
     public virtual async void add_source (CollectionSource source) throws GLib.Error {
         var used_configs = new GLib.List<BoxConfig> ();
-        foreach (var item in App.app.collection.items.data) {
+        for (int i = 0; i < App.app.collection.length; i++) {
+            var item = App.app.collection.get_item (i);
+
             if (!(item is Machine))
                 continue;
 
@@ -294,7 +296,9 @@ private class Boxes.App: Gtk.Application {
         main_window.set_state (UIState.COLLECTION);
 
         // after "ready" all items should be listed
-        foreach (var item in collection.items.data) {
+        for (int i = 0 ; i < collection.length ; i++) {
+            var item = collection.get_item (i);
+
             if (item.name == name) {
                 main_window.select_item (item);
 
@@ -307,7 +311,9 @@ private class Boxes.App: Gtk.Application {
         main_window.set_state (UIState.COLLECTION);
 
         // after "ready" all items should be listed
-        foreach (var item in collection.items.data) {
+        for (int i = 0 ; i < collection.length ; i++) {
+            var item = collection.get_item (i);
+
             if (!(item is Boxes.Machine))
                 continue;
             var machine = item as Boxes.Machine;
@@ -491,7 +497,7 @@ private class Boxes.App: Gtk.Application {
         var context = new MainContext ();
 
         context.push_thread_default ();
-        foreach (var item in collection.items.data) {
+        collection.foreach_item ((item) => {
             if (item is LibvirtMachine) {
                 var machine = item as LibvirtMachine;
 
@@ -503,7 +509,7 @@ private class Boxes.App: Gtk.Application {
                     });
                 }
             }
-        }
+        });
         context.pop_thread_default ();
 
         // wait for async methods to complete
diff --git a/src/collection-filter-switcher.vala b/src/collection-filter-switcher.vala
index 47c7867..c6bb55f 100644
--- a/src/collection-filter-switcher.vala
+++ b/src/collection-filter-switcher.vala
@@ -50,7 +50,7 @@ private class Boxes.CollectionFilterSwitcher: Gtk.ButtonBox {
     }
 
     private void update_sensitivity () {
-        sensitive = (App.app.collection.items.length != 0);
+        sensitive = (App.app.collection.length != 0);
     }
 
     [GtkCallback]
diff --git a/src/collection-toolbar.vala b/src/collection-toolbar.vala
index f62c802..cc476be 100644
--- a/src/collection-toolbar.vala
+++ b/src/collection-toolbar.vala
@@ -80,11 +80,11 @@ private class Boxes.CollectionToolbar: HeaderBar {
     }
 
     private void update_search_btn () {
-        search_btn.sensitive = App.app.collection.items.length != 0;
+        search_btn.sensitive = App.app.collection.length != 0;
     }
 
     private void update_select_btn () {
-        select_btn.sensitive = App.app.collection.items.length != 0;
+        select_btn.sensitive = App.app.collection.length != 0;
     }
 
     private void update_view_type (AppWindow.ViewType view_type) {
diff --git a/src/collection.vala b/src/collection.vala
index 6e82b5b..91cb74c 100644
--- a/src/collection.vala
+++ b/src/collection.vala
@@ -30,28 +30,49 @@ private class Boxes.Collection: GLib.Object {
     public signal void item_added (CollectionItem item);
     public signal void item_removed (CollectionItem item);
 
-    public GenericArray<CollectionItem> items;
+    public GLib.ListStore items;
+
+    public uint length {
+        get { return items.get_n_items (); }
+    }
+
+    public delegate void CollectionForeachFunc (CollectionItem item);
 
     construct {
-        items = new GenericArray<CollectionItem> ();
+        items = new GLib.ListStore (typeof (CollectionItem));
     }
 
     public Collection () {
     }
 
+    public CollectionItem get_item (int position) {
+        return items.get_item (position) as CollectionItem;
+    }
+
     public void add_item (CollectionItem item) {
-        items.add (item);
+        items.append (item);
         item_added (item);
     }
 
     public void remove_item (CollectionItem item) {
-        items.remove (item);
-        item_removed (item);
+        for (int i = 0 ; i < length ; i++) {
+           if (get_item (i) == item) {
+                items.remove (i);
+                item_removed (item);
+
+                break;
+            }
+        }
     }
 
     public void populate (ICollectionView view) {
-        for (uint i = 0 ; i < items.length ; i++)
-            view.add_item (items[i]);
+        for (int i = 0 ; i < length ; i++)
+            view.add_item (get_item (i));
+    }
+
+    public void foreach_item (CollectionForeachFunc foreach_func) {
+        for (int i = 0 ; i < length ; i++)
+            foreach_func (get_item (i));
     }
 }
 
diff --git a/src/empty-boxes.vala b/src/empty-boxes.vala
index 5c75a0c..3f03d50 100644
--- a/src/empty-boxes.vala
+++ b/src/empty-boxes.vala
@@ -26,7 +26,7 @@ private class Boxes.EmptyBoxes : Gtk.Stack, Boxes.UI {
     }
 
     private void update_visibility () {
-        var visible = App.app.collection.items.length == 0;
+        var visible = App.app.collection.length == 0;
         if (visible && visible_child != grid_box)
             visible_child = grid_box;
 
diff --git a/src/libvirt-broker.vala b/src/libvirt-broker.vala
index 04ba131..831dcde 100644
--- a/src/libvirt-broker.vala
+++ b/src/libvirt-broker.vala
@@ -140,7 +140,9 @@ private class Boxes.LibvirtBroker : Boxes.Broker {
             var disk_path = (clone.vm_creator as VMImporter).source_media.device_file;
             LibvirtMachine? cloned = null;
 
-            foreach (var item in App.app.collection.items.data) {
+            for (int i = 0 ; i < App.app.collection.length ; i++) {
+                var item = App.app.collection.get_item (i);
+
                 if (!(item is LibvirtMachine))
                     continue;
 
diff --git a/src/selection-toolbar.vala b/src/selection-toolbar.vala
index 261991f..074e9be 100644
--- a/src/selection-toolbar.vala
+++ b/src/selection-toolbar.vala
@@ -50,6 +50,6 @@ private class Boxes.SelectionToolbar: HeaderBar {
     }
 
     private void update_search_btn () {
-        search_btn.sensitive = App.app.collection.items.length != 0;
+        search_btn.sensitive = App.app.collection.length != 0;
     }
 }
diff --git a/src/selectionbar.vala b/src/selectionbar.vala
index 38364f7..272abcf 100644
--- a/src/selectionbar.vala
+++ b/src/selectionbar.vala
@@ -118,13 +118,13 @@ private class Boxes.Selectionbar: Gtk.Revealer {
     }
 
     private void update_delete_btn () {
-        foreach (var item in App.app.collection.items.data) {
+        App.app.collection.foreach_item ((item) => {
             var can_delete_id = item.get_data<ulong> ("can_delete_id");
             if (can_delete_id > 0) {
                     item.disconnect (can_delete_id);
                     item.set_data<ulong> ("can_delete_id", 0);
             }
-        }
+        });
 
         var sensitive = App.app.selected_items.length () > 0;
         foreach (var item in App.app.selected_items) {
@@ -144,13 +144,13 @@ private class Boxes.Selectionbar: Gtk.Revealer {
     }
 
     private void update_open_btn () {
-        foreach (var item in App.app.collection.items.data) {
+        App.app.collection.foreach_item ((item) => {
             var importing_id = item.get_data<ulong> ("importing_id");
             if (importing_id > 0) {
                 item.disconnect (importing_id);
                 item.set_data<ulong> ("importing_id", 0);
             }
-        }
+        });
 
         var items = App.app.selected_items.length ();
         var sensitive = items > 0;


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