[baobab/wip/new-design: 48/59] Use a .ui file for the volume chooser dialog and fixup coding style



commit 4ea1b334cfcb04af84631dda6b6eb576a2c18458
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Mar 11 21:24:03 2012 +0100

    Use a .ui file for the volume chooser dialog and fixup coding style

 src/Makefile.am                       |    5 +-
 src/baobab-volume-chooser-dialog.ui   |  141 +++++++++++++++
 src/baobab-volume-chooser-dialog.vala |   79 ---------
 src/baobab-volume-list.vala           |  307 ++++++++++++++++-----------------
 src/baobab-window.vala                |   54 +++++-
 src/baobab.gresource.xml              |    1 +
 6 files changed, 341 insertions(+), 246 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 11beb6c..b7fd35c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -36,7 +36,6 @@ baobab_SOURCES = \
 	baobab-application.vala		\
 	baobab-window.vala		\
 	baobab-connect-server.vala	\
-	baobab-volume-chooser-dialog.vala	\
 	baobab-volume-list.vala		\
 	main.vala			\
 	$(BUILT_SOURCES)
@@ -73,7 +72,7 @@ MAINTAINERCLEANFILES = \
 EXTRA_DIST = \
 	baobab.gresource.xml \
 	baobab-main-window.ui \
-	baobab-preferences-dialog.ui \
-	baobab-menu.ui
+	baobab-menu.ui \
+	baobab-volume-chooser-dialog.vala
 
 -include $(top_srcdir)/git.mk
diff --git a/src/baobab-volume-chooser-dialog.ui b/src/baobab-volume-chooser-dialog.ui
new file mode 100644
index 0000000..cd94590
--- /dev/null
+++ b/src/baobab-volume-chooser-dialog.ui
@@ -0,0 +1,141 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <!-- interface-requires gtk+ 3.0 -->
+  <object class="GtkDialog" id="volume-dialog">
+    <property name="can_focus">False</property>
+    <property name="border_width">5</property>
+    <property name="title" translatable="yes">Select Volume</property>
+    <property name="modal">True</property>
+    <property name="type_hint">dialog</property>
+    <child internal-child="vbox">
+      <object class="GtkBox" id="dialog-vbox1">
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">2</property>
+        <child internal-child="action_area">
+          <object class="GtkButtonBox" id="dialog-action_area1">
+            <property name="can_focus">False</property>
+            <property name="layout_style">end</property>
+            <child>
+              <object class="GtkButton" id="cancel-button">
+                <property name="label">gtk-cancel</property>
+                <property name="use_action_appearance">False</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="use_action_appearance">False</property>
+                <property name="use_stock">True</property>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkButton" id="scan-button">
+                <property name="label" translatable="yes">_Scan</property>
+                <property name="use_action_appearance">False</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="use_action_appearance">False</property>
+                <property name="use_underline">True</property>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+                <property name="position">1</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="pack_type">end</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkTreeView" id="volume-treeview">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <child internal-child="selection">
+              <object class="GtkTreeSelection" id="treeview-selection1"/>
+            </child>
+            <child>
+              <object class="GtkTreeViewColumn" id="volume-column">
+                <property name="sizing">autosize</property>
+                <property name="title" translatable="yes">Volume</property>
+                <property name="expand">True</property>
+                <property name="clickable">True</property>
+                <property name="reorderable">True</property>
+                <property name="sort_column_id">0</property>
+                <child>
+                  <object class="GtkCellRendererPixbuf" id="icon-renderer"/>
+                  <attributes>
+                    <attribute name="gicon">5</attribute>
+                  </attributes>
+                </child>
+                <child>
+                  <object class="GtkCellRendererText" id="volume-renderer"/>
+                  <attributes>
+                    <attribute name="text">0</attribute>
+                  </attributes>
+                </child>
+              </object>
+            </child>
+            <child>
+              <object class="GtkTreeViewColumn" id="mount-column">
+                <property name="sizing">autosize</property>
+                <property name="title" translatable="yes">Mounted</property>
+                <property name="expand">True</property>
+                <property name="clickable">True</property>
+                <property name="reorderable">True</property>
+                <property name="sort_column_id">0</property>
+                <child>
+                  <object class="GtkCellRendererToggle" id="mount-renderer"/>
+                </child>
+              </object>
+            </child>
+            <child>
+              <object class="GtkTreeViewColumn" id="size-column">
+                <property name="title" translatable="yes">Size</property>
+                <child>
+                  <object class="GtkCellRendererText" id="size-renderer"/>
+                  <attributes>
+                    <attribute name="text">2</attribute>
+                  </attributes>
+                </child>
+              </object>
+            </child>
+            <child>
+              <object class="GtkTreeViewColumn" id="usage-column">
+                <property name="title" translatable="yes">Usage</property>
+                <child>
+                  <object class="GtkCellRendererProgress" id="usage-renderer">
+                    <property name="width">140</property>
+                    <property name="ypad">8</property>
+                  </object>
+                  <attributes>
+                    <attribute name="text">4</attribute>
+                    <attribute name="value">3</attribute>
+                  </attributes>
+                </child>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">True</property>
+            <property name="fill">True</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+    <action-widgets>
+      <action-widget response="0">cancel-button</action-widget>
+      <action-widget response="1">scan-button</action-widget>
+    </action-widgets>
+  </object>
+</interface>
diff --git a/src/baobab-volume-list.vala b/src/baobab-volume-list.vala
index 3c6ae8c..77eeb2e 100644
--- a/src/baobab-volume-list.vala
+++ b/src/baobab-volume-list.vala
@@ -1,155 +1,154 @@
 namespace Baobab {
-        public class VolumeList : Gtk.ListStore {
-                public enum Columns {
-                        NAME,
-                        MOUNT_POINT,
-                        SIZE_LABEL,
-                        USAGE_PERCENT,
-                        USAGE_LABEL,
-                        ICON,
-                        UDISKS_OBJECT,
-                        COLUMNS
-                }
-
-                private UDisks.Client client = null;
-                private VolumeMonitor monitor;
-
-                public VolumeList () {
-                        set_column_types (new Type[] {
-                                          typeof (string),   // NAME
-                                          typeof (string),   // MOUNT_POINT
-                                          typeof (string),   // SIZE_LABEL
-                                          typeof (int),      // USAGE_PERCENT
-                                          typeof (string),   // USAGE_LABEL
-                                          typeof (Icon),     // ICON
-                                          typeof (Object)}); // UDISKS_OBJECT
-
-                        try {
-                                client = new UDisks.Client.sync (null);
-                                client.changed.connect(() => { update (); });
-                        } catch (Error e) {
-                        }
-
-                        monitor = VolumeMonitor.get ();
-                        monitor.mount_changed.connect ((mount) => { update (); });
-                        monitor.mount_removed.connect ((mount) => { update (); });
-                        monitor.mount_added.connect ((mount) => { update (); });
-
-                        update ();
-                }
-
-                private void compute_usage (File file, ref uint64? size, ref int percent) {
-                        try {
-                                var info = file.query_filesystem_info (FileAttribute.FILESYSTEM_SIZE + "," +
-                                                                       FileAttribute.FILESYSTEM_USED,
-                                                                       null);
-                                if (size == null && info.has_attribute (FileAttribute.FILESYSTEM_SIZE))
-                                        size = info.get_attribute_uint64 (FileAttribute.FILESYSTEM_SIZE);
-                                if (size != null && size > 0 && info.has_attribute (FileAttribute.FILESYSTEM_USED)) {
-                                        var used = info.get_attribute_uint64 (FileAttribute.FILESYSTEM_USED);
-                                        percent = (int) ((double) used / size * 100);
-                                }
-                        } catch (Error e) {
-                        }
-                }
-
-                private void update () {
-                        clear ();
-
-                        var pretty_names = new HashTable<string, string> (str_hash, str_equal);
-                        var icons = new HashTable<string, Icon> (str_hash, str_equal);
-
-                        foreach (unowned UnixMountEntry mount in UnixMountEntry.get (null)) {
-                                var device = mount.get_device_path ();
-                                pretty_names.insert (device, mount.guess_name ());
-                                icons.insert (device, mount.guess_icon ());
-                        }
-
-                        foreach (var volume in monitor.get_volumes ()) {
-                                var device = volume.get_identifier (VolumeIdentifier.UNIX_DEVICE);
-                                if (device != null) {
-                                        pretty_names.insert (device, volume.get_name ());
-                                        icons.insert (device, volume.get_icon ());
-                                }
-                        }
-
-                        Gtk.TreeIter iter;
-
-                        var dbus_objects = client.get_object_manager ().get_objects ();
-                        foreach (var dbus_object in dbus_objects) {
-                                UDisks.Object volume = (UDisks.Object) dbus_object;
-
-                                var block = volume.get_block ();
-                                if (block == null ||
-                                    block.hint_ignore ||
-                                    volume.get_filesystem () == null)
-                                        continue;
-
-                                append (out iter);
-
-                                uint64? size = block.size;
-
-                                var mount_point = volume.get_filesystem ().mount_points[0];
-                                int percent = -1;
-                                if (mount_point != null) {
-                                        var file = File.new_for_path (mount_point);
-                                        compute_usage (file, ref size, ref percent);
-                                }
-
-                                if (mount_point != null && percent != -1)
-                                        set (iter, Columns.USAGE_PERCENT, percent);
-                                else
-                                        set (iter, Columns.USAGE_LABEL,  _("Unknown"));
-
-
-                                var name = block.device;
-                                var icon = icons.lookup (name);
-                                name = pretty_names.lookup (name);
-                                if (mount_point == "/") {
-                                        name = _("Main volume");
-                                        icon = new ThemedIcon ("drive-harddisk-system");
-                                }
-
-                                set (iter,
-                                     Columns.NAME,          name,
-                                     Columns.SIZE_LABEL,    format_size (size),
-                                     Columns.MOUNT_POINT,   mount_point,
-                                     Columns.ICON,          icon,
-                                     Columns.UDISKS_OBJECT, volume);
-                        }
-
-                        foreach (var mount in monitor.get_mounts ()) {
-                                bool found = false;
-                                var mount_point = mount.get_root ().get_path ();
-
-                                get_iter_first (out iter);
-                                do {
-                                        string other_mount;
-                                        get (iter, Columns.MOUNT_POINT, out other_mount);
-                                        if (mount_point == other_mount) {
-                                                found = true;
-                                                break;
-                                        }
-                                } while (iter_next (ref iter));
-
-                                if (!found) {
-                                        append (out iter);
-                                        set (iter,
-                                             Columns.NAME,        mount.get_name (),
-                                             Columns.MOUNT_POINT, mount_point,
-                                             Columns.ICON,        mount.get_icon ());
-
-                                        int percent = -1;
-                                        uint64? size = null;
-                                        compute_usage (mount.get_root (), ref size, ref percent);
-                                        if (size != null)
-                                                set (iter, Columns.SIZE_LABEL, format_size (size));
-                                        if (percent != -1)
-                                                set (iter, Columns.USAGE_PERCENT, percent);
-                                        else
-                                                set (iter, Columns.USAGE_LABEL, _("Unknown"));
-                                }
-                        }
-                }
-        }
-}
\ No newline at end of file
+	public class VolumeList : Gtk.ListStore {
+		public enum Columns {
+			NAME,
+			MOUNT_POINT,
+			SIZE_LABEL,
+			USAGE_PERCENT,
+			USAGE_LABEL,
+			ICON,
+			UDISKS_OBJECT,
+			COLUMNS
+		}
+
+		private UDisks.Client client = null;
+		private VolumeMonitor monitor;
+
+		public VolumeList () {
+			set_column_types (new Type[] {
+					  typeof (string),   // NAME
+					  typeof (string),   // MOUNT_POINT
+					  typeof (string),   // SIZE_LABEL
+					  typeof (int),      // USAGE_PERCENT
+					  typeof (string),   // USAGE_LABEL
+					  typeof (Icon),     // ICON
+					  typeof (Object)}); // UDISKS_OBJECT
+
+			try {
+				client = new UDisks.Client.sync (null);
+				client.changed.connect(() => { update (); });
+			} catch (Error e) {
+			}
+
+			monitor = VolumeMonitor.get ();
+			monitor.mount_changed.connect ((mount) => { update (); });
+			monitor.mount_removed.connect ((mount) => { update (); });
+			monitor.mount_added.connect ((mount) => { update (); });
+
+			update ();
+		}
+
+		protected static const string FS_ATTRIBUTES =
+			FileAttribute.FILESYSTEM_SIZE + "," +
+			FileAttribute.FILESYSTEM_USED;
+
+		private void compute_usage (File file, ref uint64? size, ref int percent) {
+			try {
+				var info = file.query_filesystem_info (FS_ATTRIBUTES, null);
+				if (size == null && info.has_attribute (FileAttribute.FILESYSTEM_SIZE))
+					size = info.get_attribute_uint64 (FileAttribute.FILESYSTEM_SIZE);
+				if (size != null && size > 0 && info.has_attribute (FileAttribute.FILESYSTEM_USED)) {
+					var used = info.get_attribute_uint64 (FileAttribute.FILESYSTEM_USED);
+					percent = (int) ((double) used / size * 100);
+				}
+			} catch (Error e) {
+			}
+		}
+
+		private void update () {
+			clear ();
+
+			var pretty_names = new HashTable<string, string> (str_hash, str_equal);
+			var icons = new HashTable<string, Icon> (str_hash, str_equal);
+
+			foreach (unowned UnixMountEntry mount in UnixMountEntry.get (null)) {
+				var device = mount.get_device_path ();
+				pretty_names.insert (device, mount.guess_name ());
+				icons.insert (device, mount.guess_icon ());
+			}
+
+			foreach (var volume in monitor.get_volumes ()) {
+				var device = volume.get_identifier (VolumeIdentifier.UNIX_DEVICE);
+				if (device != null) {
+					pretty_names.insert (device, volume.get_name ());
+					icons.insert (device, volume.get_icon ());
+				}
+			}
+
+			Gtk.TreeIter iter;
+
+			var dbus_objects = client.get_object_manager ().get_objects ();
+			foreach (var dbus_object in dbus_objects) {
+				UDisks.Object volume = (UDisks.Object) dbus_object;
+
+				var block = volume.get_block ();
+				if (block == null || block.hint_ignore || volume.get_filesystem () == null)
+					continue;
+
+				append (out iter);
+
+				uint64? size = block.size;
+
+				var mount_point = volume.get_filesystem ().mount_points[0];
+				int percent = -1;
+				if (mount_point != null) {
+					var file = File.new_for_path (mount_point);
+					compute_usage (file, ref size, ref percent);
+				}
+
+				if (mount_point != null && percent != -1)
+					set (iter, Columns.USAGE_PERCENT, percent);
+				else
+					set (iter, Columns.USAGE_LABEL, _("Unknown"));
+
+				var name = block.device;
+				var icon = icons.lookup (name);
+				name = pretty_names.lookup (name);
+				if (mount_point == "/") {
+					name = _("Main volume");
+					icon = new ThemedIcon ("drive-harddisk-system");
+				}
+
+				set (iter,
+				     Columns.NAME, name,
+				     Columns.SIZE_LABEL, format_size (size),
+				     Columns.MOUNT_POINT, mount_point,
+				     Columns.ICON, icon,
+				     Columns.UDISKS_OBJECT, volume);
+			}
+
+			foreach (var mount in monitor.get_mounts ()) {
+				bool found = false;
+				var mount_point = mount.get_root ().get_path ();
+
+				get_iter_first (out iter);
+				do {
+					string other_mount;
+					get (iter, Columns.MOUNT_POINT, out other_mount);
+					if (mount_point == other_mount) {
+						found = true;
+						break;
+					}
+				} while (iter_next (ref iter));
+
+				if (!found) {
+					append (out iter);
+					set (iter,
+					     Columns.NAME, mount.get_name (),
+					     Columns.MOUNT_POINT, mount_point,
+					     Columns.ICON, mount.get_icon ());
+
+					int percent = -1;
+					uint64? size = null;
+					compute_usage (mount.get_root (), ref size, ref percent);
+					if (size != null)
+						set (iter, Columns.SIZE_LABEL, format_size (size));
+					if (percent != -1)
+						set (iter, Columns.USAGE_PERCENT, percent);
+					else
+						set (iter, Columns.USAGE_LABEL, _("Unknown"));
+				}
+			}
+		}
+	}
+}
diff --git a/src/baobab-window.vala b/src/baobab-window.vala
index 50d67e0..edafb86 100644
--- a/src/baobab-window.vala
+++ b/src/baobab-window.vala
@@ -132,21 +132,55 @@ namespace Baobab {
 		}
 
 		void on_scan_volume_activate () {
-			var volume_chooser = new VolumeChooserDialog (this);
+			var builder = new Gtk.Builder ();
+			try {
+				builder.add_from_resource ("/org/gnome/baobab/ui/baobab-volume-chooser-dialog.ui");
+			} catch (Error e) {
+				error ("loading volume chooser builder file: %s", e.message);
+			}
 
-			volume_chooser.response.connect ((response) => {
-				if (response == Gtk.ResponseType.ACCEPT) {
+			var dialog = builder.get_object ("volume-dialog") as Gtk.Dialog;
+			var volume_treeview = builder.get_object ("volume-treeview") as Gtk.TreeView;
+			var mount_column = builder.get_object ("mount-column") as Gtk.TreeViewColumn;
+			var mount_renderer = builder.get_object ("mount-renderer") as Gtk.CellRendererToggle;
+
+			VolumeList volumes = new VolumeList ();
+			volume_treeview.model = volumes;
+
+			mount_column.set_cell_data_func (mount_renderer, (layout, cell, model, iter) => {
+				string? mount_point = null;
+				model.get (iter, VolumeList.Columns.MOUNT_POINT, out mount_point);
+				((Gtk.CellRendererToggle) cell).active = mount_point != null;
+			});
+
+			dialog.response.connect ((response) => {
+				if (response == 1) {
 					try {
-                                                var dir = volume_chooser.get_file ();
-                                                scan_directory (dir, true);
-                                        } catch (Error e) {
-                                                message (_("Could not analyze volume."), e.message, Gtk.MessageType.ERROR);
-                                        }
+						Gtk.TreePath path = volume_treeview.get_selection ().get_selected_rows (null).data;
+						Gtk.TreeIter iter;
+
+						volumes.get_iter (out iter, path);
+
+						string? mount_point = null;
+						volumes.get (iter, VolumeList.Columns.MOUNT_POINT, out mount_point);
+
+						if (mount_point == null) {
+							UDisks.Object volume;
+							volumes.get (iter, VolumeList.Columns.UDISKS_OBJECT, out volume);
+							var fs = volume.get_filesystem ();
+							fs.call_mount_sync (new GLib.Variant ("a{sv}", null), out mount_point, null);
+						}
+
+						scan_directory (File.new_for_path (mount_point), true);
+					} catch (Error e) {
+						message (_("Could not analyze volume."), e.message, Gtk.MessageType.ERROR);
+					}
 				}
-				volume_chooser.destroy ();
+				dialog.destroy ();
 			});
 
-			volume_chooser.show ();
+			dialog.set_transient_for (this);
+			dialog.show ();
 		}
 
 		void on_scan_folder_activate () {
diff --git a/src/baobab.gresource.xml b/src/baobab.gresource.xml
index d0789b8..c9930d9 100644
--- a/src/baobab.gresource.xml
+++ b/src/baobab.gresource.xml
@@ -3,5 +3,6 @@
   <gresource prefix="/org/gnome/baobab/ui">
     <file preprocess="xml-stripblanks">baobab-main-window.ui</file>
     <file preprocess="xml-stripblanks">baobab-menu.ui</file>
+    <file preprocess="xml-stripblanks">baobab-volume-chooser-dialog.ui</file>
   </gresource>
 </gresources>



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