Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / FileSystemQueryable / FileSystemWatcherBackend.cs
blob7a5664796d4b31639c4d98b1802b169d95c5e0bd
1 //
2 // FileSystemWatcherBackend.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all 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
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.IO;
31 using Beagle.Util;
32 using Beagle.Daemon;
34 namespace Beagle.Daemon.FileSystemQueryable {
36 public class FileSystemWatcherBackend : IFileEventBackend {
38 static public bool Debug = true;
40 Hashtable to_be_watched = new Hashtable ();
41 FileSystemQueryable queryable;
43 public FileSystemWatcherBackend ()
45 // FIXME: This list shouldn't be hard-wired
46 to_be_watched [PathFinder.HomeDir] = true;
47 to_be_watched [Path.Combine (PathFinder.HomeDir, "Desktop")] = true;
48 to_be_watched [Path.Combine (PathFinder.HomeDir, "Documents")] = true;
51 public object CreateWatch (string path)
53 if (! to_be_watched.Contains (path))
54 return null;
56 if (Debug)
57 Logger.Log.Debug ("FileSystemWatcher watching {0}", path);
59 FileSystemWatcher fsw = new FileSystemWatcher (path);
61 fsw.Changed += OnChangedEvent;
62 fsw.Created += OnCreatedEvent;
63 fsw.Deleted += OnDeletedEvent;
64 fsw.Renamed += OnRenamedEvent;
65 fsw.Error += OnErrorEvent;
67 fsw.EnableRaisingEvents = true;
69 return fsw;
72 public bool ForgetWatch (object watch_object)
74 try {
75 FileSystemWatcher fsw = (FileSystemWatcher) watch_object;
76 fsw.EnableRaisingEvents = false;
77 fsw.Dispose ();
78 } catch (Exception ex) {
79 Logger.Log.Error ("Caught exception while doing ForgetWatch");
80 Logger.Log.Error (ex);
81 return false;
83 return true;
86 public void Start (FileSystemQueryable queryable)
88 this.queryable = queryable;
91 public void OnChangedEvent (object source, FileSystemEventArgs args)
93 if (Debug)
94 Logger.Log.Debug ("FileSystemWatcher: OnChangedEvent {0}", args.FullPath);
96 #if false
97 try {
99 // If a directory changed, mark it as dirty so it will get rescanned
100 // more quickly.
101 FileSystemModel.Directory dir;
102 dir = queryable.Model.GetDirectoryByPath (args.FullPath);
103 if (dir != null)
104 queryable.Model.ReportChanges (dir);
106 queryable.Add (args.FullPath);
108 } catch (Exception ex) {
109 Logger.Log.Warn ("Brain-damage in FileSystemWatcher.OnChangedEvent '{0}'", args.FullPath);
110 Logger.Log.Warn (ex);
112 #endif
115 public void OnCreatedEvent (object source, FileSystemEventArgs args)
117 if (Debug)
118 Logger.Log.Debug ("FileSystemWatcher: OnCreatedEvent {0}", args.FullPath);
120 #if false
121 try {
123 // When a new directory is created, add it to our model
124 // FIXME: Just in case, don't we need to check the directories unique ID here?
126 if (Directory.Exists (args.FullPath)
127 && ! queryable.Model.Ignore (args.FullPath)) {
128 string parent = Path.GetDirectoryName (args.FullPath);
129 FileSystemModel.Directory dir;
130 dir = queryable.Model.GetDirectoryByPath (parent);
131 if (dir == null) {
132 Logger.Log.Debug ("Couldn't find parent to create {0}", args.FullPath);
133 } else if (! dir.HasChildWithName (args.Name)) {
134 queryable.Model.AddChild (dir, Path.GetFileName (args.FullPath));
137 queryable.Add (args.FullPath);
139 } catch (Exception ex) {
140 Logger.Log.Warn ("Brain-damage in FileSystemWatcher.OnCreatedEvent '{0}'", args.FullPath);
141 Logger.Log.Warn (ex);
143 #endif
146 public void OnDeletedEvent (object source, FileSystemEventArgs args)
148 if (Debug)
149 Logger.Log.Debug ("FileSystemWatcher: OnDeletedEvent {0}", args.FullPath);
151 #if false
152 try {
154 FileSystemModel.Directory dir;
155 dir = queryable.Model.GetDirectoryByPath (args.FullPath);
156 if (dir != null)
157 queryable.Model.Delete (dir);
158 queryable.Remove (args.FullPath);
160 } catch (Exception ex) {
161 Logger.Log.Warn ("Brain-damage in FileSystemWatcher.OnDeletedEvent '{0}'", args.FullPath);
162 Logger.Log.Warn (ex);
164 #endif
167 public void OnRenamedEvent (object source, RenamedEventArgs args)
169 if (Debug)
170 Logger.Log.Debug ("FileSystemWatcher: OnRenamedEvent {0} {1}",
171 args.OldFullPath, args.FullPath);
172 #if false
173 try {
175 queryable.Rename (args.OldFullPath, args.FullPath);
177 } catch (Exception ex) {
178 Logger.Log.Warn ("Brain-damage in FileSystemWatcher.OnRenamedEvent '{0}'", args.FullPath);
179 Logger.Log.Warn (ex);
181 #endif
184 public void OnErrorEvent (object source, ErrorEventArgs args)
186 // FIXME: handle the error event