[baobab] Fix problems when cancelling a scan



commit 89836199f1cbf62e411f6ff799365feb0414af81
Author: Paolo Borelli <pborelli gnome org>
Date:   Thu Aug 2 20:48:16 2012 +0200

    Fix problems when cancelling a scan
    
    Now that we reuse the scanner, we need to be more careful when
    cancelling: we need to join the thread, drain the async queue and reset
    the cancellable.

 src/baobab-scanner.vala |   21 ++++++++++++++++++---
 src/baobab-window.vala  |    6 +++++-
 src/fixes.vapi          |    1 +
 3 files changed, 24 insertions(+), 4 deletions(-)
---
diff --git a/src/baobab-scanner.vala b/src/baobab-scanner.vala
index 1168a76..f621941 100644
--- a/src/baobab-scanner.vala
+++ b/src/baobab-scanner.vala
@@ -78,6 +78,8 @@ namespace Baobab {
             }
         }
 
+        GLib2.Thread? thread;
+
         HardLink[] hardlinks;
         HashTable<File, unowned File> excluded_locations;
 
@@ -238,7 +240,7 @@ namespace Baobab {
                 results.percent = 100.0;
                 array.results += (owned) results;
                 results_queue.push ((owned) array);
-            } catch {
+            } catch (Error e) {
             }
 
             // drop the thread's reference on the Scanner object
@@ -316,14 +318,27 @@ namespace Baobab {
             return this.self != null;
         }
 
+        public void cancel_scan () {
+            if (thread != null) {
+                thread.join ();
+                thread = null;
+            }
+            var tmp = results_queue.try_pop ();
+            while (tmp != null) {
+                tmp = results_queue.try_pop ();
+            }
+            base.clear ();
+            cancellable.reset ();
+        }
+
         public void scan (bool force) {
             if (force) {
+                cancel_scan ();
                 successful = false;
-                clear ();
             }
 
             if (!successful) {
-                new GLib2.Thread ("scanner", scan_in_thread);
+                thread = new GLib2.Thread ("scanner", scan_in_thread);
                 Timeout.add (100, process_results);
             } else {
                 completed ();
diff --git a/src/baobab-window.vala b/src/baobab-window.vala
index 6c21178..5980e47 100644
--- a/src/baobab-window.vala
+++ b/src/baobab-window.vala
@@ -529,7 +529,11 @@ namespace Baobab {
                     scanner.finish();
                 } catch (IOError.CANCELLED e) {
                     // Handle cancellation silently
-                    scanner.clear ();
+                    if (scan_completed_handler > 0) {
+                        scanner.disconnect (scan_completed_handler);
+                        scan_completed_handler = 0;
+                    }
+                    scanner.cancel_scan ();
                     return;
                 } catch (Error e) {
                     var primary = _("Could not scan folder \"%s\" or some of the folders it contains.").printf (scanner.directory.get_parse_name ());
diff --git a/src/fixes.vapi b/src/fixes.vapi
index facf725..66c0bba 100644
--- a/src/fixes.vapi
+++ b/src/fixes.vapi
@@ -3,5 +3,6 @@ namespace GLib2 {
 	public class Thread {
 		public delegate void* ThreadFunc ();
 		public Thread (string thread_name, ThreadFunc func);
+		public void* join ();
 	}
 }



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