Remove debug statements from KCal backends.
[beagle.git] / beagled / FilterFactory.cs
blobe74ea16e47616ffd0ccf1f7e53f61f8f7a92cfe9
1 //
2 // FilterFactory.cs
3 //
4 // Copyright (C) 2004 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;
30 using System.Reflection;
32 using System.Xml;
33 using System.Xml.Serialization;
35 using Beagle.Util;
37 namespace Beagle.Daemon {
39 public class FilterFactory {
41 static private bool Debug = false;
43 static FilterFactory ()
45 ReflectionFu.ScanEnvironmentForAssemblies ("BEAGLE_FILTER_PATH", PathFinder.FilterDir,
46 delegate (Assembly a) {
47 int n = ScanAssemblyForFilters (a);
48 Logger.Log.Debug ("Loaded {0} filter{1} from {2}",
49 n, n == 1 ? "" : "s", a.Location);
50 });
53 /////////////////////////////////////////////////////////////////////////
56 static private ICollection CreateFilters (Uri uri, string extension, string mime_type)
58 Hashtable matched_filters_by_flavor = FilterFlavor.NewHashtable ();
60 foreach (FilterFlavor flavor in filter_types_by_flavor.Keys) {
61 if (flavor.IsMatch (uri, extension, mime_type)) {
62 Filter matched_filter = null;
64 try {
65 matched_filter = (Filter) Activator.CreateInstance ((Type) filter_types_by_flavor [flavor]);
67 if (flavor.MimeType != null)
68 matched_filter.MimeType = flavor.MimeType;
69 if (flavor.Extension != null)
70 matched_filter.Extension = flavor.Extension;
72 } catch (Exception e) {
73 continue;
75 matched_filters_by_flavor [flavor] = matched_filter;
79 if (Debug) {
80 foreach (DictionaryEntry entry in matched_filters_by_flavor) {
81 FilterFlavor flav = (FilterFlavor) entry.Key;
82 Filter filter = (Filter) entry.Value;
84 Logger.Log.Debug ("Found matching filter: {0}, Weight: {1}", filter, flav.Weight);
88 return matched_filters_by_flavor.Values;
91 static public int GetFilterVersion (string filter_name)
93 if (filter_versions_by_name.Contains (filter_name)) {
94 return (int) filter_versions_by_name [filter_name];
95 } else {
96 return -1;
100 /////////////////////////////////////////////////////////////////////////
102 static public ICollection CreateFiltersFromMimeType (string mime_type)
104 return CreateFilters (null, null, mime_type);
107 static public ICollection CreateFilterFromExtension (string extension)
109 return CreateFilters (null, extension, null);
112 static public ICollection CreateFiltersFromPath (string path)
114 string guessed_mime_type = XdgMime.GetMimeType (path);
115 string extension = Path.GetExtension (path);
116 return CreateFilters (UriFu.PathToFileUri (path), extension, guessed_mime_type);
119 static public ICollection CreateFiltersFromUri (Uri uri)
121 if (uri.IsFile)
122 return CreateFiltersFromPath (uri.LocalPath);
123 else
124 return CreateFilters (uri, null, null);
127 static public ICollection CreateFiltersFromIndexable (Indexable indexable)
129 string path = indexable.ContentUri.LocalPath;
130 string extension = Path.GetExtension (path);
131 string mime_type = indexable.MimeType;
132 return CreateFilters (UriFu.PathToFileUri (path), extension, mime_type);
135 /////////////////////////////////////////////////////////////////////////
137 static private bool ShouldWeFilterThis (Indexable indexable)
139 if (indexable.Filtering == IndexableFiltering.Never
140 || indexable.NoContent)
141 return false;
143 if (indexable.Filtering == IndexableFiltering.Always)
144 return true;
146 // Our default behavior is to try to filter non-transient file
147 // indexable and indexables with a specific mime type attached.
148 if (indexable.IsNonTransient || indexable.MimeType != null)
149 return true;
151 return false;
154 static public bool FilterIndexable (Indexable indexable, TextCache text_cache, out Filter filter)
156 filter = null;
157 ICollection filters = null;
159 if (indexable.Filtering == IndexableFiltering.AlreadyFiltered)
160 return false;
162 if (! ShouldWeFilterThis (indexable)) {
163 indexable.NoContent = true;
164 return false;
167 string path = null;
169 // First, figure out which filter we should use to deal with
170 // the indexable.
172 // If a specific mime type is specified, try to index as that type.
173 if (indexable.MimeType != null)
174 filters = CreateFiltersFromMimeType (indexable.MimeType);
176 if (indexable.ContentUri.IsFile) {
177 path = indexable.ContentUri.LocalPath;
179 // Otherwise sniff the mime-type from the file
180 if (indexable.MimeType == null)
181 indexable.MimeType = XdgMime.GetMimeType (path);
183 if (filters == null || filters.Count == 0) {
184 filters = CreateFiltersFromIndexable (indexable);
187 if (Directory.Exists (path)) {
188 indexable.MimeType = "inode/directory";
189 indexable.NoContent = true;
190 if (! indexable.ValidTimestamp)
191 indexable.Timestamp = Directory.GetLastWriteTimeUtc (path);
192 } else if (File.Exists (path)) {
193 // Set the timestamp to the best possible estimate (if no timestamp was set by the backend)
194 if (! indexable.ValidTimestamp)
195 indexable.Timestamp = File.GetLastWriteTimeUtc (path);
196 } else {
197 Logger.Log.Warn ("No such file: {0}", path);
198 return false;
202 // We don't know how to filter this, so there is nothing else to do.
203 if (filters.Count == 0) {
204 if (! indexable.NoContent) {
205 indexable.NoContent = true;
207 Logger.Log.Debug ("No filter for {0} ({1}) [{2}]", indexable.Uri, path, indexable.MimeType);
208 return false;
211 return true;
214 foreach (Filter candidate_filter in filters) {
215 if (Debug)
216 Logger.Log.Debug ("Testing filter: {0}", candidate_filter);
218 // Hook up the snippet writer.
219 if (candidate_filter.SnippetMode && text_cache != null) {
220 if (candidate_filter.OriginalIsText && indexable.IsNonTransient) {
221 text_cache.MarkAsSelfCached (indexable.Uri);
222 } else if (indexable.CacheContent) {
223 TextWriter writer = text_cache.GetWriter (indexable.Uri);
224 candidate_filter.AttachSnippetWriter (writer);
228 if (indexable.Crawled)
229 candidate_filter.EnableCrawlMode ();
231 // Set the filter's URI
232 candidate_filter.Uri = indexable.Uri;
234 // allow the filter access to the indexable's properties
235 candidate_filter.IndexableProperties = indexable.Properties;
237 // Open the filter, copy the file's properties to the indexable,
238 // and hook up the TextReaders.
240 bool succesful_open = false;
241 TextReader text_reader;
242 Stream binary_stream;
244 if (path != null)
245 succesful_open = candidate_filter.Open (path);
246 else if ((text_reader = indexable.GetTextReader ()) != null)
247 succesful_open = candidate_filter.Open (text_reader);
248 else if ((binary_stream = indexable.GetBinaryStream ()) != null)
249 succesful_open = candidate_filter.Open (binary_stream);
251 if (succesful_open) {
252 if (candidate_filter.Timestamp != DateTime.MinValue)
253 indexable.Timestamp = candidate_filter.Timestamp;
254 foreach (Property prop in candidate_filter.Properties)
255 indexable.AddProperty (prop);
256 indexable.SetTextReader (candidate_filter.GetTextReader ());
257 indexable.SetHotTextReader (candidate_filter.GetHotTextReader ());
259 if (Debug)
260 Logger.Log.Debug ("Successfully filtered {0} with {1}", path, candidate_filter);
262 filter = candidate_filter;
263 return true;
264 } else if (Debug) {
265 Logger.Log.Debug ("Unsuccessfully filtered {0} with {1}, falling back", path, candidate_filter);
266 candidate_filter.Cleanup ();
270 if (Debug)
271 Logger.Log.Debug ("None of the matching filters could process the file: {0}", path);
273 return false;
276 static public bool FilterIndexable (Indexable indexable, out Filter filter)
278 return FilterIndexable (indexable, null, out filter);
281 static public bool FilterIndexable (Indexable indexable)
283 Filter filter = null;
285 return FilterIndexable (indexable, null, out filter);
288 /////////////////////////////////////////////////////////////////////////
290 private static Hashtable filter_types_by_flavor = new Hashtable ();
291 private static Hashtable filter_versions_by_name = new Hashtable ();
293 static private int ScanAssemblyForFilters (Assembly assembly)
295 int count = 0;
297 foreach (Type t in ReflectionFu.GetTypesFromAssemblyAttribute (assembly, typeof (FilterTypesAttribute))) {
298 Filter filter = null;
300 try {
301 filter = (Filter) Activator.CreateInstance (t);
302 } catch (Exception ex) {
303 Logger.Log.Error (ex, "Caught exception while instantiating {0}", t);
306 if (filter == null)
307 continue;
309 filter_versions_by_name [t.ToString ()] = filter.Version;
311 foreach (FilterFlavor flavor in filter.SupportedFlavors) {
312 filter_types_by_flavor [flavor] = t;
313 FilterFlavor.Flavors.Add (flavor);
316 ++count;
319 return count;
324 /////////////////////////////////////////////////////////////////////////
326 public class FilteredStatus
328 private Uri uri;
329 private string filter_name;
330 private int filter_version;
332 [XmlIgnore]
333 public Uri Uri {
334 get { return uri; }
335 set { uri = value; }
338 [XmlAttribute ("Uri")]
339 public string UriAsString {
340 get {
341 return UriFu.UriToEscapedString (uri);
344 set {
345 uri = UriFu.EscapedStringToUri (value);
349 public string FilterName {
350 get { return filter_name; }
351 set { filter_name = value; }
354 public int FilterVersion {
355 get { return filter_version; }
356 set { filter_version = value; }
359 public static FilteredStatus New (Indexable indexable, Filter filter)
361 FilteredStatus status = new FilteredStatus ();
363 status.Uri = indexable.Uri;
364 status.FilterName = filter.GetType ().ToString ();
365 status.FilterVersion = filter.Version;
367 return status;