Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / FileSystemQueryable / TreeCrawlTask.cs
blob9c4b1e7ff8c6c0c332c31d2aaad8c3edc200f75a
1 //
2 // TreeCrawlTask.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.IO;
31 using Beagle.Daemon;
32 using Beagle.Util;
34 namespace Beagle.Daemon.FileSystemQueryable {
36 public class TreeCrawlTask : Scheduler.Task {
38 public delegate void Handler (DirectoryModel parent, string name);
40 private object big_lock = new object ();
41 private bool is_active = false;
42 private Handler handler;
43 private Queue to_be_crawled = new Queue ();
45 public TreeCrawlTask (Handler handler)
47 this.handler = handler;
48 this.Tag = "Tree Crawler";
49 this.Priority = Scheduler.Priority.Delayed;
52 public bool IsActive {
53 get { lock (big_lock) return is_active; }
56 // Returns 'true' if the queue was empty before adding
57 // this item.
58 public bool Add (DirectoryModel dir)
60 lock (big_lock) {
61 bool was_empty;
62 was_empty = (to_be_crawled.Count == 0);
64 if (!was_empty && to_be_crawled.Contains (dir))
65 return false;
67 to_be_crawled.Enqueue (dir);
68 Description = String.Format ("Pending directories: {0}", to_be_crawled.Count);
69 return was_empty;
73 override protected void DoTaskReal ()
75 DirectoryModel dir;
77 lock (big_lock) {
78 if (to_be_crawled.Count == 0) {
79 is_active = false;
80 return;
82 dir = to_be_crawled.Dequeue () as DirectoryModel;
83 is_active = true;
86 if (dir.IsAttached) {
87 if (FileSystemQueryable.Debug)
88 Logger.Log.Debug ("Scanning '{0}' for subdirectories", dir.FullName);
90 try {
91 foreach (string name in DirectoryWalker.GetDirectoryNames (dir.FullName))
92 handler (dir, name);
93 } catch (DirectoryNotFoundException ex) {
94 Logger.Log.Debug ("Couldn't scan '{0}' for subdirectories", dir.FullName);
98 lock (big_lock) {
99 if (to_be_crawled.Count != 0)
100 Reschedule = true;
101 else
102 is_active = false;