[baobab/wip/vala: 35/41] rework code for excluded locations



commit d570d8efb5d83a3a44024c781b7532aae5001974
Author: Ryan Lortie <desrt desrt ca>
Date:   Fri Jan 6 09:39:47 2012 -0500

    rework code for excluded locations
    
    Instead of keeping a list attached to the Baobab.Application and having
    a method there for checking it, attach a list to the scanner object and
    create it at the start of the scan.
    
    This prevents the scanner from calling back into the application class
    from the worker thread.
    
    Also, switch to using a hashtable.  We have at least 4 items in it (plus
    the ones that the user added) so this should already be slightly faster
    than a linear scan.

 src/baobab-application.vala      |   42 +++++++++++++++++--------------------
 src/baobab-scanner.vala          |    2 +
 src/baobab-sync-scanner.vala     |    2 +-
 src/baobab-threaded-scanner.vala |    3 +-
 src/baobab-window.vala           |    8 +++---
 5 files changed, 28 insertions(+), 29 deletions(-)
---
diff --git a/src/baobab-application.vala b/src/baobab-application.vala
index 0a78217..9ac6631 100644
--- a/src/baobab-application.vala
+++ b/src/baobab-application.vala
@@ -4,8 +4,6 @@ namespace Baobab {
 		Settings prefs_settings;
 		Settings ui_settings;
 
-		File[] excluded_locations;
-
 		static Application baobab;
 
 		protected override void activate () {
@@ -19,18 +17,31 @@ namespace Baobab {
 			}
 		}
 
-		void excluded_uris_changed (Settings settings, string key) {
-			var uris = settings.get_strv (key);
+		static void add_excluded_location (HashTable<File, unowned File> table, File file) {
+			table.insert (file, file);
+		}
+
+		public static HashTable<File, unowned File> get_excluded_locations () {
+			var app = baobab;
 
-			excluded_locations = new File[0];
+			var excluded_locations = new HashTable<File, unowned File> (file_hash, file_equal);
+			add_excluded_location (excluded_locations, File.new_for_path ("/proc"));
+			add_excluded_location (excluded_locations, File.new_for_path ("/sys"));
+			add_excluded_location (excluded_locations, File.new_for_path ("/selinux"));
+			add_excluded_location (excluded_locations, File.new_for_path ("/selinux"));
+
+			var home = File.new_for_path (Environment.get_home_dir ());
+			add_excluded_location (excluded_locations, home.get_child (".gvfs"));
 
 			var root = File.new_for_path ("/");
-			foreach (var uri in uris) {
-				var file = File.new_for_uri (uri);
+			foreach (var uri in app.prefs_settings.get_value ("excluded-uris")) {
+				var file = File.new_for_uri ((string) uri);
 				if (!file.equal (root)) {
-					excluded_locations += file;
+					excluded_locations.insert (file, file);
 				}
 			}
+
+			return excluded_locations;
 		}
 
 		protected override void startup () {
@@ -41,9 +52,6 @@ namespace Baobab {
 			ui_settings = new Settings ("org.gnome.baobab.ui");
 			prefs_settings = new Settings ("org.gnome.baobab.preferences");
 			desktop_settings = new Settings ("org.gnome.desktop.interface");
-
-			excluded_uris_changed (prefs_settings, "excluded-uris");
-			prefs_settings.changed["excluded-uris"] += excluded_uris_changed;
 		}
 
 		protected override bool local_command_line ([CCode (array_length = false, array_null_terminated = true)] ref unowned string[] arguments, out int exit_status) {
@@ -60,18 +68,6 @@ namespace Baobab {
 			Object (application_id: "org.gnome.baobab", flags: ApplicationFlags.HANDLES_OPEN);
 		}
 
-		public static bool is_excluded_location (File file) {
-			var app = baobab;
-
-			foreach (var location in app.excluded_locations) {
-				if (file.equal (location)) {
-					return true;
-				}
-			}
-
-			return false;
-		}
-
 		public static Settings get_desktop_settings () {
 			var app = baobab;
 			return app.desktop_settings;
diff --git a/src/baobab-scanner.vala b/src/baobab-scanner.vala
index affee14..b27d5c2 100644
--- a/src/baobab-scanner.vala
+++ b/src/baobab-scanner.vala
@@ -28,6 +28,7 @@ namespace Baobab {
 			}
 		}
 
+		protected HashTable<File, unowned File> excluded_locations;
 		protected Cancellable? cancellable;
 		protected HardLink[] hardlinks;
 
@@ -62,6 +63,7 @@ namespace Baobab {
 			                  typeof (int),     /* ELEMENTS */
 			                  typeof (State)}); /* STATE */
 			set_sort_column_id (Columns.SIZE, Gtk.SortType.DESCENDING);
+			excluded_locations = Application.get_excluded_locations ();
 		}
 	}
 }
diff --git a/src/baobab-sync-scanner.vala b/src/baobab-sync-scanner.vala
index a8d8317..a1f0ee3 100644
--- a/src/baobab-sync-scanner.vala
+++ b/src/baobab-sync-scanner.vala
@@ -11,7 +11,7 @@ namespace Baobab {
 			var results = Results ();
 			Gtk.TreeIter iter;
 
-			if (Application.is_excluded_location (directory)) {
+			if (excluded_locations.lookup (directory) != null) {
 				return results;
 			}
 
diff --git a/src/baobab-threaded-scanner.vala b/src/baobab-threaded-scanner.vala
index 072191b..076c772 100644
--- a/src/baobab-threaded-scanner.vala
+++ b/src/baobab-threaded-scanner.vala
@@ -67,7 +67,7 @@ namespace Baobab {
 		Results? add_directory (File directory, FileInfo info, Results? parent = null) {
 			var results_array = new ResultsArray ();
 
-			if (Application.is_excluded_location (directory)) {
+			if (excluded_locations.lookup (directory) != null) {
 				return null;
 			}
 
@@ -220,6 +220,7 @@ namespace Baobab {
 
 		protected override void scan (File directory) {
 			this.directory = directory;
+			this.excluded_locations = Application.get_excluded_locations ();
 
 			// the thread owns a reference on the Scanner object
 			this.self = this;
diff --git a/src/baobab-window.vala b/src/baobab-window.vala
index 3bd8c5d..ccc458d 100644
--- a/src/baobab-window.vala
+++ b/src/baobab-window.vala
@@ -160,10 +160,10 @@ namespace Baobab {
 		}
 
 		public bool check_dir (File directory) {
-			if (Application.is_excluded_location (directory)) {
-				message("", _("Cannot check an excluded folder!"), Gtk.MessageType.INFO);
-				return false;
-			}
+			//if (Application.is_excluded_location (directory)) {
+			//	message("", _("Cannot check an excluded folder!"), Gtk.MessageType.INFO);
+			//	return false;
+			//}
 
 			try {
 				var info = directory.query_info (FILE_ATTRIBUTE_STANDARD_TYPE, FileQueryInfoFlags.NONE, null);



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