[baobab/wip/vala: 17/23] scanner: split interface from implementation



commit 0d819993d5149ac98a3d7fd1573ad2e28ebde172
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu Jan 5 16:48:45 2012 -0500

    scanner: split interface from implementation

 src/Makefile.am              |    2 +
 src/baobab-scanner.vala      |  137 +++---------------------------------------
 src/baobab-sync-scanner.vala |  125 ++++++++++++++++++++++++++++++++++++++
 src/baobab-window.vala       |    4 +-
 4 files changed, 138 insertions(+), 130 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 740b776..5b0a300 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -20,6 +20,7 @@ baobab_SOURCES = \
 	baobab-chart.c			\
 	baobab-treemap.c		\
 	baobab-ringschart.c		\
+	baobab-sync-scanner.vala	\
 	baobab-scanner.vala		\
 	baobab-cellrenderers.vala	\
 	baobab-application.vala		\
@@ -41,6 +42,7 @@ baobab_LDADD = \
 
 MAINTAINERCLEANFILES = \
 	baobab-application.c		\
+	baobab-sync-scanner.c		\
 	baobab-scanner.c		\
 	baobab-cellrenderers.c		\
 	baobab-window.c			\
diff --git a/src/baobab-scanner.vala b/src/baobab-scanner.vala
index 765880d..1e048e4 100644
--- a/src/baobab-scanner.vala
+++ b/src/baobab-scanner.vala
@@ -1,5 +1,5 @@
 namespace Baobab {
-	class Scanner : Gtk.TreeStore {
+	abstract class Scanner : Gtk.TreeStore {
 		public enum Columns {
 			DISPLAY_NAME,
 			PARSE_NAME,
@@ -18,7 +18,7 @@ namespace Baobab {
 			DONE
 		}
 
-		struct HardLink {
+		protected struct HardLink {
 			uint64 inode;
 			uint32 device;
 
@@ -28,17 +28,10 @@ namespace Baobab {
 			}
 		}
 
-		struct Results {
-			uint64 size;
-			uint64 alloc_size;
-			uint64 elements;
-			int max_depth;
-		}
-
-		Cancellable? cancellable;
-		HardLink[] hardlinks;
+		protected Cancellable? cancellable;
+		protected HardLink[] hardlinks;
 
-		static const string ATTRIBUTES =
+		protected static const string ATTRIBUTES =
 			FILE_ATTRIBUTE_STANDARD_NAME + "," +
 			FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME + "," +
 			FILE_ATTRIBUTE_STANDARD_TYPE + "," +
@@ -49,124 +42,11 @@ namespace Baobab {
 			FILE_ATTRIBUTE_UNIX_DEVICE + "," +
 			FILE_ATTRIBUTE_ACCESS_CAN_READ;
 
-		Results add_directory (File directory, FileInfo info, Gtk.TreeIter? parent_iter = null) {
-			var results = Results ();
-			Gtk.TreeIter iter;
-
-			if (Application.is_excluded_location (directory)) {
-				return results;
-			}
-
-			var display_name = info.get_display_name ();
-			var parse_name = directory.get_parse_name ();
-
-			append (out iter, parent_iter);
-			set (iter,
-			     Columns.DISPLAY_NAME, display_name,
-			     Columns.PARSE_NAME,   parse_name);
-
-			if (info.has_attribute (FILE_ATTRIBUTE_STANDARD_SIZE)) {
-				results.size = info.get_size ();
-			}
-
-			if (info.has_attribute (FILE_ATTRIBUTE_UNIX_BLOCKS)) {
-				results.alloc_size = 512 * info.get_attribute_uint64 (FILE_ATTRIBUTE_UNIX_BLOCKS);
-			}
-
-			results.elements = 1;
-
-			try {
-				var children = directory.enumerate_children (ATTRIBUTES, FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
-				FileInfo? child_info;
-				while ((child_info = children.next_file (cancellable)) != null) {
-					if (cancellable.is_cancelled ()) {
-						break;
-					}
-
-					switch (child_info.get_file_type ()) {
-						case FileType.DIRECTORY:
-							var child = directory.get_child (child_info.get_name ());
-							var child_results = add_directory (child, child_info, iter);
-
-							results.size += child_results.size;
-							results.alloc_size += child_results.size;
-							results.elements += child_results.elements;
-							results.max_depth = int.max (results.max_depth, child_results.max_depth + 1);
-							break;
-
-						case FileType.REGULAR:
-							if (child_info.has_attribute (FILE_ATTRIBUTE_UNIX_NLINK)) {
-								if (child_info.get_attribute_uint32 (FILE_ATTRIBUTE_UNIX_NLINK) > 1) {
-									var hl = HardLink (child_info);
-
-									/* check if we've already encountered this file */
-									if (hl in hardlinks) {
-										continue;
-									}
-
-									hardlinks += hl;
-								}
-							}
-
-							if (child_info.has_attribute (FILE_ATTRIBUTE_UNIX_BLOCKS)) {
-								results.alloc_size += 512 * child_info.get_attribute_uint64 (FILE_ATTRIBUTE_UNIX_BLOCKS);
-							}
-							results.size += child_info.get_size ();
-							results.elements++;
-							break;
-
-						default:
-							/* ignore other types (symlinks, sockets, devices, etc) */
-							break;
-					}
-				}
-			} catch (IOError.PERMISSION_DENIED e) {
-			} catch (Error e) {
-				warning ("couldn't iterate %s: %s", parse_name, e.message);
-			}
-
-			add_percent (results.size, iter);
-
-			if (!cancellable.is_cancelled ()) {
-				set (iter,
-				     Columns.SIZE,       results.size,
-				     Columns.ALLOC_SIZE, results.alloc_size,
-				     Columns.ELEMENTS,   results.elements,
-				     Columns.STATE,      State.NEED_PERCENT);
-			} else {
-				set (iter,
-				     Columns.STATE,      State.CANCELLED);
-			}
-
-			return results;
-		}
-
-		void add_percent (uint64 parent_size, Gtk.TreeIter? parent = null) {
-			Gtk.TreeIter iter;
-
-			if (iter_children (out iter, parent)) {
-				do {
-					uint64 size;
-					get (iter, Columns.SIZE, out size);
-					set (iter,
-					     Columns.PERCENT, 100 * ((double) size) / ((double) parent_size),
-					     Columns.STATE,   State.DONE);
-				} while (iter_next (ref iter));
-			}
-		}
-
-		void scan (File directory) {
-			try {
-				var info = directory.query_info (ATTRIBUTES, 0, cancellable);
-				var results = add_directory (directory, info);
-				add_percent (results.size);
-				max_depth = results.max_depth;
-			} catch { }
-		}
+		public abstract void scan (File directory);
 
-		public int max_depth { get; private set; }
+		public int max_depth { get; protected set; }
 
-		public Scanner (File directory) {
+		public Scanner () {
 			set_column_types (new Type[] {
 			                  typeof (string),  /* DIR_NAME */
 			                  typeof (string),  /* PARSE_NAME */
@@ -175,7 +55,6 @@ namespace Baobab {
 			                  typeof (uint64),  /* ALLOC_SIZE */
 			                  typeof (int),     /* ELEMENTS */
 			                  typeof (State)}); /* STATE */
-			scan (directory);
 		}
 	}
 }
diff --git a/src/baobab-sync-scanner.vala b/src/baobab-sync-scanner.vala
new file mode 100644
index 0000000..7208305
--- /dev/null
+++ b/src/baobab-sync-scanner.vala
@@ -0,0 +1,125 @@
+namespace Baobab {
+	class SyncScanner : Scanner {
+		struct Results {
+			uint64 size;
+			uint64 alloc_size;
+			uint64 elements;
+			int max_depth;
+		}
+
+		Results add_directory (File directory, FileInfo info, Gtk.TreeIter? parent_iter = null) {
+			var results = Results ();
+			Gtk.TreeIter iter;
+
+			if (Application.is_excluded_location (directory)) {
+				return results;
+			}
+
+			var display_name = info.get_display_name ();
+			var parse_name = directory.get_parse_name ();
+
+			append (out iter, parent_iter);
+			set (iter,
+			     Columns.DISPLAY_NAME, display_name,
+			     Columns.PARSE_NAME,   parse_name);
+
+			if (info.has_attribute (FILE_ATTRIBUTE_STANDARD_SIZE)) {
+				results.size = info.get_size ();
+			}
+
+			if (info.has_attribute (FILE_ATTRIBUTE_UNIX_BLOCKS)) {
+				results.alloc_size = 512 * info.get_attribute_uint64 (FILE_ATTRIBUTE_UNIX_BLOCKS);
+			}
+
+			results.elements = 1;
+
+			try {
+				var children = directory.enumerate_children (ATTRIBUTES, FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
+				FileInfo? child_info;
+				while ((child_info = children.next_file (cancellable)) != null) {
+					if (cancellable.is_cancelled ()) {
+						break;
+					}
+
+					switch (child_info.get_file_type ()) {
+						case FileType.DIRECTORY:
+							var child = directory.get_child (child_info.get_name ());
+							var child_results = add_directory (child, child_info, iter);
+
+							results.size += child_results.size;
+							results.alloc_size += child_results.size;
+							results.elements += child_results.elements;
+							results.max_depth = int.max (results.max_depth, child_results.max_depth + 1);
+							break;
+
+						case FileType.REGULAR:
+							if (child_info.has_attribute (FILE_ATTRIBUTE_UNIX_NLINK)) {
+								if (child_info.get_attribute_uint32 (FILE_ATTRIBUTE_UNIX_NLINK) > 1) {
+									var hl = HardLink (child_info);
+
+									/* check if we've already encountered this file */
+									if (hl in hardlinks) {
+										continue;
+									}
+
+									hardlinks += hl;
+								}
+							}
+
+							if (child_info.has_attribute (FILE_ATTRIBUTE_UNIX_BLOCKS)) {
+								results.alloc_size += 512 * child_info.get_attribute_uint64 (FILE_ATTRIBUTE_UNIX_BLOCKS);
+							}
+							results.size += child_info.get_size ();
+							results.elements++;
+							break;
+
+						default:
+							/* ignore other types (symlinks, sockets, devices, etc) */
+							break;
+					}
+				}
+			} catch (IOError.PERMISSION_DENIED e) {
+			} catch (Error e) {
+				warning ("couldn't iterate %s: %s", parse_name, e.message);
+			}
+
+			add_percent (results.size, iter);
+
+			if (!cancellable.is_cancelled ()) {
+				set (iter,
+				     Columns.SIZE,       results.size,
+				     Columns.ALLOC_SIZE, results.alloc_size,
+				     Columns.ELEMENTS,   results.elements,
+				     Columns.STATE,      State.NEED_PERCENT);
+			} else {
+				set (iter,
+				     Columns.STATE,      State.CANCELLED);
+			}
+
+			return results;
+		}
+
+		void add_percent (uint64 parent_size, Gtk.TreeIter? parent = null) {
+			Gtk.TreeIter iter;
+
+			if (iter_children (out iter, parent)) {
+				do {
+					uint64 size;
+					get (iter, Columns.SIZE, out size);
+					set (iter,
+					     Columns.PERCENT, 100 * ((double) size) / ((double) parent_size),
+					     Columns.STATE,   State.DONE);
+				} while (iter_next (ref iter));
+			}
+		}
+
+		protected override void scan (File directory) {
+			try {
+				var info = directory.query_info (ATTRIBUTES, 0, cancellable);
+				var results = add_directory (directory, info);
+				add_percent (results.size);
+				max_depth = results.max_depth;
+			} catch { }
+		}
+	}
+}
diff --git a/src/baobab-window.vala b/src/baobab-window.vala
index 74ab655..475df0f 100644
--- a/src/baobab-window.vala
+++ b/src/baobab-window.vala
@@ -67,7 +67,9 @@ namespace Baobab {
 				return;
 			}
 
-			model = new Scanner (directory);
+			var scanner = new SyncScanner ();
+			scanner.scan (directory);
+			model = scanner;
 			var rings_chart = builder.get_object ("rings-chart") as Chart;
 			var treemap = builder.get_object ("treemap") as Chart;
 			model.bind_property ("max-depth", rings_chart, "max-depth", BindingFlags.SYNC_CREATE);



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