5 // Copyright (C) 2004 Novell, Inc.
9 // Permission is hereby granted, free of charge, to any person obtaining a
10 // copy of this software and associated documentation files (the "Software"),
11 // to deal in the Software without restriction, including without limitation
12 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 // and/or sell copies of the Software, and to permit persons to whom the
14 // Software is furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 // DEALINGS IN THE SOFTWARE.
29 using System
.Collections
;
35 namespace Beagle
.Daemon
.EvolutionMailDriver
{
38 public delegate void ItemAddedHandler (FileInfo file
);
40 private static bool Debug
= false;
42 ArrayList roots
= new ArrayList ();
44 Hashtable last_write_time_cache
= new Hashtable ();
46 public ItemAddedHandler MboxAddedEvent
;
47 public ItemAddedHandler SummaryAddedEvent
;
49 public MailCrawler (params string[] paths
)
51 foreach (string p
in paths
) {
52 if (Directory
.Exists (p
))
57 private bool FileIsInteresting (FileInfo file
)
59 DateTime cached_time
= new DateTime ();
60 if (last_write_time_cache
.Contains (file
.FullName
))
61 cached_time
= (DateTime
) last_write_time_cache
[file
.FullName
];
63 last_write_time_cache
[file
.FullName
] = file
.LastWriteTimeUtc
;
65 return cached_time
< file
.LastWriteTimeUtc
;
68 private void OnInotifyEvent (Inotify
.Watch watch
,
72 Inotify
.EventType type
)
77 string full_path
= Path
.Combine (path
, subitem
);
79 if ((type
& Inotify
.EventType
.Create
) != 0 && (type
& Inotify
.EventType
.IsDirectory
) != 0) {
84 if ((type
& Inotify
.EventType
.Delete
) != 0 && (type
& Inotify
.EventType
.IsDirectory
) != 0) {
89 if ((type
& Inotify
.EventType
.MovedTo
) != 0) {
90 if (subitem
== "summary") {
92 Logger
.Log
.Info ("Reindexing updated IMAP summary: {0}", full_path
);
93 if (SummaryAddedEvent
!= null)
94 SummaryAddedEvent (new FileInfo (full_path
));
95 } else if (Path
.GetExtension (full_path
) == ".ev-summary") {
97 string mbox_file
= Path
.ChangeExtension (full_path
, null);
98 Logger
.Log
.Info ("Reindexing updated mbox: {0}", mbox_file
);
99 if (MboxAddedEvent
!= null)
100 MboxAddedEvent (new FileInfo (mbox_file
));
105 private void Watch (string root
)
107 Queue pending
= new Queue ();
109 pending
.Enqueue (root
);
111 while (pending
.Count
> 0) {
113 string dir
= (string) pending
.Dequeue ();
115 foreach (string subdir
in DirectoryWalker
.GetDirectories (dir
)) {
116 if (Shutdown
.ShutdownRequested
)
119 if (Inotify
.Enabled
) {
120 Inotify
.Subscribe (dir
, OnInotifyEvent
,
121 Inotify
.EventType
.Create
122 | Inotify
.EventType
.Delete
123 | Inotify
.EventType
.MovedTo
);
126 pending
.Enqueue (subdir
);
129 Stopwatch watch
= new Stopwatch ();
131 Logger
.Log
.Debug ("Starting watch on {0}", dir
);
133 foreach (string path
in DirectoryWalker
.GetItems (dir
, new DirectoryWalker
.FileFilter (IsSummary
))) {
134 if (Shutdown
.ShutdownRequested
)
137 FileInfo file
= new FileInfo (path
);
139 if (file
.Name
== "summary") {
140 if (SummaryAddedEvent
!= null && FileIsInteresting (file
))
141 SummaryAddedEvent (file
);
142 } else if (file
.Extension
== ".ev-summary") {
143 string mbox_name
= Path
.Combine (file
.DirectoryName
,
144 Path
.GetFileNameWithoutExtension (file
.Name
));
145 FileInfo mbox_file
= new FileInfo (mbox_name
);
146 if (MboxAddedEvent
!= null && FileIsInteresting (mbox_file
))
147 MboxAddedEvent (mbox_file
);
152 Logger
.Log
.Debug ("Crawled {0} in {1}", dir
, watch
);
156 private bool IsSummary (string path
, string name
)
158 if (name
== "summary")
161 if (Path
.GetExtension (name
) == ".ev-summary")
169 foreach (string root
in roots
) {
170 if (Shutdown
.ShutdownRequested
)