[baobab/wip/new-design] Put one class per file



commit 25bad2478b595f91c36b0bfc55abe1c2a695f78c
Author: Stefano Facchini <stefano facchini gmail com>
Date:   Wed Apr 4 12:04:40 2012 +0200

    Put one class per file

 src/Makefile.am                  |    4 +-
 src/baobab-location-monitor.vala |   83 ++++++++++++
 src/baobab-location-widget.vala  |   72 +++++++++++
 src/baobab-location.vala         |  107 ++++++++++++++++
 src/baobab-volume-list.vala      |  256 --------------------------------------
 5 files changed, 265 insertions(+), 257 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 1a75195..f2bcb78 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -36,7 +36,9 @@ baobab_SOURCES = \
 	baobab-application.vala		\
 	baobab-window.vala		\
 	baobab-connect-server.vala	\
-	baobab-volume-list.vala		\
+	baobab-location.vala		\
+	baobab-location-monitor.vala	\
+	baobab-location-widget.vala	\
 	main.vala			\
 	$(BUILT_SOURCES)
 
diff --git a/src/baobab-location-monitor.vala b/src/baobab-location-monitor.vala
new file mode 100644
index 0000000..2e64d98
--- /dev/null
+++ b/src/baobab-location-monitor.vala
@@ -0,0 +1,83 @@
+namespace Baobab {
+	public class LocationMonitor {
+		private VolumeMonitor monitor;
+
+		private List<Location> locations = null;
+
+		public signal void changed ();
+
+		public LocationMonitor () {
+			monitor = VolumeMonitor.get ();
+			monitor.mount_changed.connect (mount_changed);
+			monitor.mount_removed.connect (mount_removed);
+			monitor.mount_added.connect (mount_added);
+			monitor.volume_changed.connect (volume_changed);
+			monitor.volume_removed.connect (volume_removed);
+			monitor.volume_added.connect (volume_added);
+
+			initial_fill ();
+		}
+
+		public unowned List<Location> get_locations () {
+			return locations;
+		}
+
+		void volume_changed (Volume volume) {
+			changed ();
+		}
+
+		void volume_removed (Volume volume) {
+			print ("Vol removed: %s\n", volume.get_name ());
+		}
+
+		void volume_added (Volume volume) {
+			print ("Vol added: %s\n", volume.get_name ());
+			locations.append (new Location.from_volume (volume));
+
+			changed ();
+		}
+
+		void mount_changed (Mount mount) {
+			print ("Changed: %s\n", mount.get_root ().get_path ());
+		}
+
+		void mount_removed (Mount mount) {
+			print ("Removed: %s\n", mount.get_root ().get_path ());
+			foreach (var location in locations) {
+				if (location.mount == mount) {
+					locations.remove (location);
+					break;
+				}
+			}
+
+			changed ();
+		}
+
+		void mount_added (Mount mount) {
+			print ("Added: %s\n", mount.get_root ().get_path ());
+			if (mount.get_volume () == null) {
+				locations.append (new Location.from_mount (mount));
+			}
+
+			changed ();
+		}
+
+		void initial_fill () {
+			locations.append (new Location.for_main_volume ());
+
+			foreach (var volume in monitor.get_volumes ()) {
+				locations.append (new Location.from_volume (volume));
+			}
+
+			foreach (var mount in monitor.get_mounts ()) {
+				if (mount.get_volume () == null) {
+					locations.append (new Location.from_mount (mount));
+				} else {
+					// Already added as volume
+				}
+			}
+
+			changed ();
+		}
+	}
+}
diff --git a/src/baobab-location-widget.vala b/src/baobab-location-widget.vala
new file mode 100644
index 0000000..0288781
--- /dev/null
+++ b/src/baobab-location-widget.vala
@@ -0,0 +1,72 @@
+namespace Baobab {
+	public class LocationWidget : Gtk.Grid {
+		private static Gtk.SizeGroup name_size_group = null;
+		private static Gtk.SizeGroup mount_point_size_group = null;
+		private static Gtk.SizeGroup size_size_group = null;
+		private static Gtk.SizeGroup used_size_group = null;
+		private static Gtk.SizeGroup button_size_group = null;
+
+		public delegate void ActionOnClick (Location location);
+
+		void ensure_size_groups () {
+			if (name_size_group != null)
+				return;
+
+			name_size_group = new Gtk.SizeGroup (Gtk.SizeGroupMode.HORIZONTAL);
+			mount_point_size_group = new Gtk.SizeGroup (Gtk.SizeGroupMode.HORIZONTAL);
+			size_size_group = new Gtk.SizeGroup (Gtk.SizeGroupMode.HORIZONTAL);
+			used_size_group = new Gtk.SizeGroup (Gtk.SizeGroupMode.HORIZONTAL);
+			button_size_group = new Gtk.SizeGroup (Gtk.SizeGroupMode.HORIZONTAL);
+		}
+
+		public LocationWidget (Location location, ActionOnClick action) {
+			orientation = Gtk.Orientation.HORIZONTAL;
+			column_spacing = 10;
+			margin = 6;
+
+			ensure_size_groups ();
+
+			var image = new Gtk.Image.from_gicon (location.icon, Gtk.IconSize.DIALOG);
+			add (image);
+
+			var label = new Gtk.Label (location.name);
+			label.xalign = 0;
+			name_size_group.add_widget (label);
+			add (label);
+
+			label = new Gtk.Label (location.mount_point != null ? location.mount_point : "");
+			label.hexpand = true;
+			label.halign = Gtk.Align.CENTER;
+			label.xalign = 0;
+			label.get_style_context ().add_class ("dim-label");
+			mount_point_size_group.add_widget (label);
+			add (label);
+
+			label = new Gtk.Label (location.size != null ? format_size (location.size) : "");
+			size_size_group.add_widget (label);
+			add (label);
+
+			if (location.used != null) {
+				var progress = new Gtk.ProgressBar ();
+				progress.valign = Gtk.Align.CENTER;
+				progress.set_fraction ((double) location.used / location.size);
+				used_size_group.add_widget (progress);
+				add (progress);
+			} else {
+				label = new Gtk.Label (_("Usage unknown"));
+				used_size_group.add_widget (label);
+				add (label);
+			}
+
+			string button_label = location.mount_point != null ? _("Scan") : _("Mount and scan");
+			var button = new Gtk.Button.with_label (button_label);
+			button.valign = Gtk.Align.CENTER;
+			button_size_group.add_widget (button);
+			add (button);
+
+			button.clicked.connect(() => { action (location); });
+
+			show_all ();
+		}
+	}
+}
\ No newline at end of file
diff --git a/src/baobab-location.vala b/src/baobab-location.vala
new file mode 100644
index 0000000..17f2c7a
--- /dev/null
+++ b/src/baobab-location.vala
@@ -0,0 +1,107 @@
+namespace Baobab {
+	public class Location {
+		public string name { get; private set; }
+		public string? mount_point { get; private set; }
+
+		public uint64? size { get; private set; }
+		public uint64? used { get; private set; }
+		public Icon? icon { get; private set; }
+
+		public Volume? volume { get; private set; }
+		public Mount? mount { get; private set; }
+
+		protected static const string FS_ATTRIBUTES =
+			FileAttribute.FILESYSTEM_SIZE + "," +
+			FileAttribute.FILESYSTEM_USED;
+
+		public Location.from_volume (Volume volume_) {
+			volume = volume_;
+			volume.changed.connect((vol) => {
+				print ("Vol changed: %s\n", volume.get_name ());
+				update_volume_info ();
+			});
+			update_volume_info ();
+		}
+
+		public Location.from_mount (Mount mount_) {
+			mount = mount_;
+			fill_from_mount ();
+		}
+
+		public Location.for_main_volume () {
+			name = _("Main volume");
+			mount_point = "/";
+			icon = new ThemedIcon ("drive-harddisk-system");
+
+			uint64? size_ = null;
+			uint64? used_ = null;
+			get_fs_size (File.new_for_path ("/"), out size_, out used_);
+
+			size = size_;
+			used = used_;
+		}
+
+		public Location (string name_,
+				 string? mount_point_,
+				 uint64? size_,
+				 uint64? used_,
+				 Icon? icon_) {
+			name = name_;
+			mount_point = mount_point_;
+			size = size_;
+			used = used_;
+			icon = icon_;
+		}
+
+		void update_volume_info () {
+			mount = volume.get_mount ();
+
+			if (mount != null) {
+				fill_from_mount ();
+			} else {
+				name = volume.get_name ();
+				icon = volume.get_icon ();
+				mount_point = null;
+				size = null;
+				used = null;
+			}
+		}
+
+		void fill_from_mount () {
+			name = mount.get_name ();
+
+			var file = mount.get_root ();
+
+			uint64? size_ = null;
+			uint64? used_ = null;
+			get_fs_size (file, out size_, out used_);
+
+			size = size_;
+			used = used_;
+			mount_point = file.get_path ();
+			icon = mount.get_icon ();
+		}
+
+		static void get_fs_size (File file, out uint64? size, out uint64? used) {
+			try {
+				var info = file.query_filesystem_info (FS_ATTRIBUTES, null);
+				if (info.has_attribute (FileAttribute.FILESYSTEM_SIZE))
+					size = info.get_attribute_uint64 (FileAttribute.FILESYSTEM_SIZE);
+				if (info.has_attribute (FileAttribute.FILESYSTEM_USED))
+					used = info.get_attribute_uint64 (FileAttribute.FILESYSTEM_USED);
+			} catch (Error e) {
+			}
+		}
+
+		public async void mount_volume () throws Error {
+			if (mount != null || volume == null)
+				return;
+
+			var mount_op = new Gtk.MountOperation (null);
+			yield volume.mount (MountMountFlags.NONE, mount_op, null);
+
+			print("Mount finished on: %s\n", volume.get_mount ().get_root ().get_path ());
+			update_volume_info ();
+		}
+	}
+}



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