Dont index style nodes.
[beagle.git] / beagled / EvolutionMailDriver / MailCrawler.cs
blob81b4bb2d7d357edfd59c84f03de1c5d5cdb2579c
2 //
3 // MailCrawler.cs
4 //
5 // Copyright (C) 2004 Novell, Inc.
6 //
7 //
8 //
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.
28 using System;
29 using System.Collections;
30 using System.IO;
32 using Beagle.Util;
33 using Beagle.Daemon;
35 namespace Beagle.Daemon.EvolutionMailDriver {
37 class MailCrawler {
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))
53 roots.Add (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,
69 string path,
70 string subitem,
71 string srcpath,
72 Inotify.EventType type)
74 if (subitem == "")
75 return;
77 string full_path = Path.Combine (path, subitem);
79 if ((type & Inotify.EventType.Create) != 0 && (type & Inotify.EventType.IsDirectory) != 0) {
80 Watch (full_path);
81 return;
84 if ((type & Inotify.EventType.Delete) != 0 && (type & Inotify.EventType.IsDirectory) != 0) {
85 watch.Unsubscribe ();
86 return;
89 if ((type & Inotify.EventType.MovedTo) != 0) {
90 if (subitem == "summary") {
91 // IMAP 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") {
96 // mbox 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)
117 return;
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 ();
130 if (Debug)
131 Logger.Log.Debug ("Starting watch on {0}", dir);
132 watch.Start ();
133 foreach (string path in DirectoryWalker.GetItems (dir, new DirectoryWalker.FileFilter (IsSummary))) {
134 if (Shutdown.ShutdownRequested)
135 return;
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);
150 watch.Stop ();
151 if (Debug)
152 Logger.Log.Debug ("Crawled {0} in {1}", dir, watch);
156 private bool IsSummary (string path, string name)
158 if (name == "summary")
159 return true;
161 if (Path.GetExtension (name) == ".ev-summary")
162 return true;
164 return false;
167 public void Crawl ()
169 foreach (string root in roots) {
170 if (Shutdown.ShutdownRequested)
171 return;
173 Watch (root);