2 // FileSystemWatcherBackend.cs
4 // Copyright (C) 2005 Novell, Inc.
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.
28 using System
.Collections
;
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
))
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;
72 public bool ForgetWatch (object watch_object
)
75 FileSystemWatcher fsw
= (FileSystemWatcher
) watch_object
;
76 fsw
.EnableRaisingEvents
= false;
78 } catch (Exception ex
) {
79 Logger
.Log
.Error ("Caught exception while doing ForgetWatch");
80 Logger
.Log
.Error (ex
);
86 public void Start (FileSystemQueryable queryable
)
88 this.queryable
= queryable
;
91 public void OnChangedEvent (object source
, FileSystemEventArgs args
)
94 Logger
.Log
.Debug ("FileSystemWatcher: OnChangedEvent {0}", args
.FullPath
);
99 // If a directory changed, mark it as dirty so it will get rescanned
101 FileSystemModel
.Directory dir
;
102 dir
= queryable
.Model
.GetDirectoryByPath (args
.FullPath
);
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
);
115 public void OnCreatedEvent (object source
, FileSystemEventArgs args
)
118 Logger
.Log
.Debug ("FileSystemWatcher: OnCreatedEvent {0}", args
.FullPath
);
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
);
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
);
146 public void OnDeletedEvent (object source
, FileSystemEventArgs args
)
149 Logger
.Log
.Debug ("FileSystemWatcher: OnDeletedEvent {0}", args
.FullPath
);
154 FileSystemModel
.Directory dir
;
155 dir
= queryable
.Model
.GetDirectoryByPath (args
.FullPath
);
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
);
167 public void OnRenamedEvent (object source
, RenamedEventArgs args
)
170 Logger
.Log
.Debug ("FileSystemWatcher: OnRenamedEvent {0} {1}",
171 args
.OldFullPath
, args
.FullPath
);
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
);
184 public void OnErrorEvent (object source
, ErrorEventArgs args
)
186 // FIXME: handle the error event