[baobab/wip/new-design: 3/59] scanner: accumulate sizes and calculate percent



commit e114356fafc12e938db44251f2638f593d6dc201
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu Jan 5 11:06:07 2012 -0500

    scanner: accumulate sizes and calculate percent

 src/baobab-scanner.vala |  109 ++++++++++++++++++++++++++++++++---------------
 1 files changed, 75 insertions(+), 34 deletions(-)
---
diff --git a/src/baobab-scanner.vala b/src/baobab-scanner.vala
index 14ab984..d7e94ab 100644
--- a/src/baobab-scanner.vala
+++ b/src/baobab-scanner.vala
@@ -7,9 +7,17 @@ namespace Baobab {
 			SIZE,
 			ALLOC_SIZE,
 			ELEMENTS,
+			STATE,
 			COLUMNS
 		}
 
+		public enum State {
+			SCANNING,
+			CANCELLED,
+			NEED_PERCENT,
+			DONE
+		}
+
 		struct HardLink {
 			uint64 inode;
 			uint32 device;
@@ -20,6 +28,13 @@ namespace Baobab {
 			}
 		}
 
+		struct Results {
+			uint64 size;
+			uint64 alloc_size;
+			uint64 elements;
+			int max_depth;
+		}
+
 		Cancellable? cancellable;
 		HardLink[] hardlinks;
 
@@ -34,54 +49,49 @@ namespace Baobab {
 			FILE_ATTRIBUTE_UNIX_DEVICE + "," +
 			FILE_ATTRIBUTE_ACCESS_CAN_READ;
 
-		void add_directory (File directory, FileInfo info, Gtk.TreeIter? parent_iter = null) {
+		Results add_directory (File directory, FileInfo info, Gtk.TreeIter? parent_iter = null) {
+			var results = Results ();
 			Gtk.TreeIter iter;
-			uint64 alloc_size;
-			uint64 size;
-			int elements;
-
-			elements = 0;
 
 			if (Application.is_excluded_location (directory)) {
-				return;
+				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)) {
-				size = info.get_size ();
-			} else {
-				size = 0;
+				results.size = info.get_size ();
 			}
 
 			if (info.has_attribute (FILE_ATTRIBUTE_UNIX_BLOCKS)) {
-				alloc_size = 512 * info.get_attribute_uint64 (FILE_ATTRIBUTE_UNIX_BLOCKS);
-			} else {
-				alloc_size = 0;
+				results.alloc_size = 512 * info.get_attribute_uint64 (FILE_ATTRIBUTE_UNIX_BLOCKS);
 			}
 
-			append (out iter, parent_iter);
-			set (iter, 0, display_name);
-			set (iter, 1, parse_name);
-			set (iter, 2, 0.0);
-			set (iter, 3, size);
-			set (iter, 4, alloc_size);
-			set (iter, 5, elements);
+			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 ()) {
-						return;
+						break;
 					}
 
 					switch (child_info.get_file_type ()) {
 						case FileType.DIRECTORY:
 							var child = directory.get_child (child_info.get_name ());
-							add_directory (child, child_info, iter);
-							elements++;
+							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:
@@ -99,10 +109,10 @@ namespace Baobab {
 							}
 
 							if (child_info.has_attribute (FILE_ATTRIBUTE_UNIX_BLOCKS)) {
-								alloc_size += 512 * child_info.get_attribute_uint64 (FILE_ATTRIBUTE_UNIX_BLOCKS);
+								results.alloc_size += 512 * child_info.get_attribute_uint64 (FILE_ATTRIBUTE_UNIX_BLOCKS);
 							}
-							size += child_info.get_size ();
-							elements++;
+							results.size += child_info.get_size ();
+							results.elements++;
 							break;
 
 						default:
@@ -115,21 +125,52 @@ namespace Baobab {
 				warning ("couldn't iterate %s: %s", parse_name, e.message);
 			}
 
+			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);
+					add_percent (size, iter);
+				} while (iter_next (ref iter));
+			}
 		}
 
 		void scan (File directory) {
-			try {var info = directory.query_info (ATTRIBUTES, 0, cancellable);
-			add_directory (directory, info);} catch { }
+			try {
+				var info = directory.query_info (ATTRIBUTES, 0, cancellable);
+				var results = add_directory (directory, info);
+				add_percent (results.size);
+			} catch { }
 		}
 
 		public Scanner (File directory) {
 			set_column_types (new Type[] {
-			                  typeof (string), /* DIR_NAME */
-			                  typeof (string), /* PARSE_NAME */
-			                  typeof (double), /* PERCENT */
-			                  typeof (uint64), /* SIZE */
-			                  typeof (uint64), /* ALLOC_SIZE */
-			                  typeof (int)});  /* ELEMENTS */
+			                  typeof (string),  /* DIR_NAME */
+			                  typeof (string),  /* PARSE_NAME */
+			                  typeof (double),  /* PERCENT */
+			                  typeof (uint64),  /* SIZE */
+			                  typeof (uint64),  /* ALLOC_SIZE */
+			                  typeof (int),     /* ELEMENTS */
+			                  typeof (State)}); /* STATE */
 			scan (directory);
 		}
 	}



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