Source list view: add optional filter
authorPhilipp Zabel <philipp.zabel@gmail.com>
Tue, 8 Dec 2009 22:59:55 +0000 (23:59 +0100)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Tue, 5 Jan 2010 18:38:28 +0000 (19:38 +0100)
src/plugins/catalog-plugin.vala
src/source-dialog.vala
src/source-list-view.vala
src/source-list-window.vala

index fb1cff1..f42a2bc 100644 (file)
@@ -72,7 +72,7 @@ class CatalogPlugin : Plugin {
                        }
                }
 
-               var source_list = new SourceListView (available_sources);
+               var source_list = new SourceListView (available_sources, true);
 
                var content = (VBox) dialog.get_content_area ();
                content.pack_start (source_list, true, true, 0);
index 4fcd85a..35876c7 100644 (file)
@@ -36,7 +36,7 @@ class SourceDialog : Gtk.Dialog {
                        }
                }
 
-               var source_list = new SourceListView (sources);
+               var source_list = new SourceListView (sources, true);
                content.pack_start (source_list, true, true, 0);
 
                source_list.source_activated.connect (on_source_activated);
index 03fc33e..881582b 100644 (file)
@@ -22,16 +22,28 @@ using Hildon;
 public class SourceListView : PannableArea {
        TreeView tree;
        ListStore store;
+       TreeModelFilter filtered_store;
+       bool filtered;
 
        public signal void source_activated (MovieSource source);
 
-       public SourceListView (List<MovieSource> list) {
+       public SourceListView (List<MovieSource> list, bool _filtered) {
+               filtered = _filtered;
+
                // Source list
                store = new ListStore (3, typeof (string), typeof (string), typeof (MovieSource));
                add_sources (list);
 
-               // Tree View
-               tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, store);
+               if (filtered) {
+                       // Add filter wrapper
+                       filtered_store = new TreeModelFilter (store, null);
+                       filtered_store.set_visible_func (filter_visible_func);
+
+                       // Tree View
+                       tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, filtered_store);
+               } else {
+                       tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, store);
+               }
                tree.set_headers_visible (false);
 
                add (tree);
@@ -89,11 +101,31 @@ public class SourceListView : PannableArea {
                tree.row_activated.connect (on_row_activated);
        }
 
+       bool filter_visible_func (TreeModel model, TreeIter iter) {
+               MovieSource source = null;
+
+               store.get (iter, 2, out source);
+               
+               return source.active;
+       }
+
        public void add_sources (List<MovieSource> list) {
                foreach (MovieSource source in list) {
                        TreeIter iter;
-                       store.append (out iter);
-                       store.set (iter, 0, source.get_name (), 1, source.get_description (), 2, source);
+
+                       store.insert_with_values (out iter, -1, 0, source.get_name (), 1, source.get_description (), 2, source);
+
+                       source.notify["active"].connect (this.on_source_changed);
+               }
+       }
+
+       private void on_source_changed (GLib.Object _source, GLib.ParamSpec spec) {
+               var source = (MovieSource) _source;
+               TreeIter iter;
+
+               if (get_iter (source, out iter)) {
+                       TreePath path = store.get_path (iter);
+                       store.row_changed (path, iter);
                }
        }
 
index 0a44248..a7611f0 100644 (file)
@@ -33,7 +33,7 @@ public class SourceListWindow : StackableWindow {
                set_main_menu (menu);
 
                var sources = new List<MovieSource> ();
-               source_list = new SourceListView (sources);
+               source_list = new SourceListView (sources, true);
 
                var hbox = new HBox (true, 0);
                hbox.pack_start (source_list, true, true, MARGIN_DOUBLE);