[baobab] Factor out the location list widget



commit 61c53d9ea6046efda1f84df4e80424eec0f8d2f4
Author: Paolo Borelli <pborelli gnome org>
Date:   Tue Jul 24 13:03:00 2012 +0200

    Factor out the location list widget

 src/Makefile.am                 |    1 +
 src/baobab-location-list.vala   |   56 +++++++++++++++++++++++++++++
 src/baobab-location-widget.vala |    4 +-
 src/baobab-main-window.ui       |   15 +++++++-
 src/baobab-window.vala          |   75 +++++++++++---------------------------
 5 files changed, 95 insertions(+), 56 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 8eb7740..e597099 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -37,6 +37,7 @@ baobab_SOURCES = \
 	baobab-connect-server.vala	\
 	baobab-location-monitor.vala	\
 	baobab-location.vala		\
+	baobab-location-list.vala	\
 	baobab-location-widget.vala	\
 	baobab-scanner.vala		\
 	baobab-window.vala		\
diff --git a/src/baobab-location-list.vala b/src/baobab-location-list.vala
new file mode 100644
index 0000000..8915673
--- /dev/null
+++ b/src/baobab-location-list.vala
@@ -0,0 +1,56 @@
+/* -*- indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* Baobab - disk usage analyzer
+ *
+ * Copyright (C) 2012  Paolo Borelli <pborelli gnome org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+namespace Baobab {
+
+    public class LocationList : Egg.ListBox {
+        LocationMonitor location_monitor;
+        LocationWidget.LocationAction? location_action;
+
+        construct {
+            location_monitor = LocationMonitor.get ();
+            location_monitor.changed.connect (() => { update (); });
+
+            set_separator_funcs (update_separator);
+        }
+
+        void update_separator (ref Gtk.Widget? separator, Gtk.Widget widget, Gtk.Widget? before_widget) {
+            if (before_widget != null && separator == null) {
+                separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
+            } else {
+                separator = null;
+            }
+        }
+
+        public void set_action (owned LocationWidget.LocationAction? action) {
+            location_action = (owned)action;
+        }
+
+        public void update () {
+            this.foreach ((widget) => { widget.destroy (); });
+
+            foreach (var location in location_monitor.get_locations ()) {
+                add (new LocationWidget (location, location_action));
+            }
+
+            show_all ();
+        }
+    }
+}
diff --git a/src/baobab-location-widget.vala b/src/baobab-location-widget.vala
index 883b390..3faf02d 100644
--- a/src/baobab-location-widget.vala
+++ b/src/baobab-location-widget.vala
@@ -26,7 +26,7 @@ namespace Baobab {
         private static Gtk.SizeGroup usage_size_group = null;
         private static Gtk.SizeGroup button_size_group = null;
 
-        public delegate void ActionOnClick (Location location);
+        public delegate void LocationAction (Location location);
 
         void ensure_size_groups () {
             if (name_size_group == null) {
@@ -36,7 +36,7 @@ namespace Baobab {
             }
         }
 
-        public LocationWidget (Location location, ActionOnClick action) {
+        public LocationWidget (Location location, LocationAction action) {
             orientation = Gtk.Orientation.HORIZONTAL;
             column_spacing = 12;
             margin = 6;
diff --git a/src/baobab-main-window.ui b/src/baobab-main-window.ui
index 66b6bee..2840654 100644
--- a/src/baobab-main-window.ui
+++ b/src/baobab-main-window.ui
@@ -195,10 +195,23 @@
             <property name="visible">True</property>
             <property name="orientation">vertical</property>
             <child>
-              <object class="GtkScrolledWindow" id="volume-scrolled-window">
+              <object class="GtkScrolledWindow" id="location-scrolled-window">
                 <property name="visible">True</property>
                 <property name="vexpand">True</property>
                 <property name="hexpand">True</property>
+                <child>
+                  <object class="GtkViewport" id="location-viewport">
+                    <property name="visible">True</property>
+                    <child>
+                      <object class="BaobabLocationList" id="location-list">
+                        <property name="visible">True</property>
+                        <style>
+                          <class name="baobab-main-view"/>
+                        </style>
+                      </object>
+                    </child>
+                  </object>
+                </child>
               </object>
             </child>
            </object>
diff --git a/src/baobab-window.vala b/src/baobab-window.vala
index 8865d65..a228407 100644
--- a/src/baobab-window.vala
+++ b/src/baobab-window.vala
@@ -34,14 +34,13 @@ namespace Baobab {
         Gtk.Label infobar_primary;
         Gtk.Label infobar_secondary;
         Gtk.ScrolledWindow location_scroll;
-        Egg.ListBox location_list;
+        LocationList location_list;
         Gtk.TreeView treeview;
         Gtk.Notebook chart_notebook;
         Chart rings_chart;
         Chart treemap_chart;
         Gtk.Spinner spinner;
         Scanner? scanner;
-        LocationMonitor location_monitor;
 
         static Gdk.Cursor busy_cursor;
 
@@ -124,15 +123,18 @@ namespace Baobab {
             infobar = builder.get_object ("infobar") as Gtk.InfoBar;
             infobar_primary = builder.get_object ("infobar-primary-label") as Gtk.Label;
             infobar_secondary = builder.get_object ("infobar-secondary-label") as Gtk.Label;
-            location_scroll = builder.get_object ("volume-scrolled-window") as Gtk.ScrolledWindow;
-            location_list = builder.get_object ("location-view") as Egg.ListBox;
+            location_scroll = builder.get_object ("location-scrolled-window") as Gtk.ScrolledWindow;
+            location_list = builder.get_object ("location-list") as LocationList;
             treeview = builder.get_object ("treeview") as Gtk.TreeView;
             chart_notebook = builder.get_object ("chart-notebook") as Gtk.Notebook;
             rings_chart = builder.get_object ("rings-chart") as Chart;
             treemap_chart = builder.get_object ("treemap-chart") as Chart;
             spinner = builder.get_object ("spinner") as Gtk.Spinner;
 
-            setup_home_page ();
+            location_list.set_adjustment (location_scroll.get_vadjustment ());
+            location_list.set_action (on_scan_location_activate);
+            location_list.update ();
+
             setup_treeview (builder);
 
             var infobar_close_button = builder.get_object ("infobar-close-button") as Gtk.Button;
@@ -223,6 +225,21 @@ namespace Baobab {
             connect_server.show ();
         }
 
+        void on_scan_location_activate (Location location) {
+            if (location.is_home_location) {
+                on_scan_home_activate ();
+            } else {
+                location.mount_volume.begin ((location_, res) => {
+                    try {
+                        location.mount_volume.end (res);
+                        scan_directory (File.new_for_path (location.mount_point), ScanFlags.EXCLUDE_MOUNTS);
+                    } catch (Error e) {
+                        message (_("Could not analyze volume."), e.message, Gtk.MessageType.ERROR);
+                    }
+                });
+            }
+        }
+
         void on_stop_activate () {
             if (scanner != null) {
                 scanner.cancel ();
@@ -320,54 +337,6 @@ namespace Baobab {
             Gtk.drag_dest_unset (this);
         }
 
-        void update_locations () {
-            location_list.foreach ((widget) => { widget.destroy (); });
-
-            foreach (var location in location_monitor.get_locations ()) {
-                LocationWidget loc_widget;
-                if (location.is_home_location) {
-                    loc_widget = new LocationWidget (location, (location_) => {
-                        on_scan_home_activate ();
-                    });
-                } else {
-                    loc_widget = new LocationWidget (location, (location_) => {
-                        location_.mount_volume.begin ((location__, res) => {
-                            try {
-                                location_.mount_volume.end (res);
-                                scan_directory (File.new_for_path (location_.mount_point), ScanFlags.EXCLUDE_MOUNTS);
-                            } catch (Error e) {
-                                message (_("Could not analyze volume."), e.message, Gtk.MessageType.ERROR);
-                            }
-                        });
-                    });
-                }
-
-                location_list.add (loc_widget);
-            }
-
-            location_list.show_all ();
-        }
-
-        void update_separator (ref Gtk.Widget? separator, Gtk.Widget widget, Gtk.Widget? before_widget) {
-            if (before_widget != null && separator == null) {
-                separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
-            } else {
-                separator = null;
-            }
-        }
-
-        void setup_home_page () {
-            location_list = new Egg.ListBox ();
-            location_scroll.add_with_viewport (location_list);
-            location_list.set_adjustment (location_scroll.get_vadjustment ());
-            location_list.get_style_context ().add_class ("baobab-main-view");
-            location_list.set_separator_funcs (update_separator);
-
-            location_monitor = LocationMonitor.get ();
-            location_monitor.changed.connect (() => { update_locations (); });
-            update_locations ();
-        }
-
         bool show_treeview_popup (Gtk.Menu popup, Gdk.EventButton? event) {
             if (event != null) {
                 popup.popup (null, null, null, event.button, event.time);



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