Remove debug statements from KCal backends.
[beagle.git] / beagled / LabyrinthQueryable / LabyrinthQueryable.cs
blob85c39dd7a097f4d3107b754c8076719ea8086fb3
1 //
2 // LabyrithQueryable.cs
3 //
4 // Copyright (C) 2006 Kevin Kubasik <kevin@kubasik.net>
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.Threading;
31 using System.Text;
33 using Beagle.Daemon;
34 using Beagle.Util;
36 namespace Beagle.Daemon.LabyrinthQueryable {
38 [QueryableFlavor (Name="Labyrinth", Domain=QueryDomain.Local, RequireInotify=false)]
39 public class LabyrinthQueryable : LuceneFileQueryable, IIndexableGenerator {
41 private static Logger log = Logger.Get ("LabyrinthQueryable");
44 string lab_dir;
46 public LabyrinthQueryable () : base ("LabyrinthIndex")
48 lab_dir = Path.Combine (PathFinder.HomeDir, ".gnome2");
49 lab_dir = Path.Combine (lab_dir, "labyrinth");
52 /////////////////////////////////////////////////
54 private void StartWorker()
56 if (! Directory.Exists (lab_dir)) {
57 GLib.Timeout.Add (60000, new GLib.TimeoutHandler (CheckForExistence));
58 return;
61 log.Info ("Starting Labyrinth backend");
63 Stopwatch stopwatch = new Stopwatch ();
64 stopwatch.Start ();
68 if (Inotify.Enabled)
69 Inotify.Subscribe (lab_dir, OnInotifyNewNote, Inotify.EventType.CloseWrite | Inotify.EventType.Modify);
71 Scheduler.Task task;
72 task = NewAddTask (this);
73 task.Tag = "Crawling Labyrinth Notes";
74 task.Source = this;
76 ThisScheduler.Add (task);
78 stopwatch.Stop ();
80 log.Info ("labyrinth backend worker thread done in {0}", stopwatch);
83 public override void Start ()
85 base.Start ();
87 ExceptionHandlingThread.Start (new ThreadStart (StartWorker));
90 /////////////////////////////////////////////////
93 public string StatusName {
94 get { return "LabyrinthQueryable"; }
97 private IEnumerator map_files = null;
99 public void PostFlushHook () { }
101 public bool HasNextIndexable ()
103 if (map_files == null)
104 map_files = DirectoryWalker.GetFileInfos (lab_dir).GetEnumerator ();
106 return map_files.MoveNext ();
109 public Indexable GetNextIndexable ()
111 FileInfo file = (FileInfo) map_files.Current;
113 if (! file.Exists)
114 return null;
116 if (IsUpToDate (file.FullName))
117 return null;
119 Indexable indexable = NoteToIndexable (file);
121 return indexable;
123 private bool CheckForExistence ()
125 if (!Directory.Exists (lab_dir))
126 return true;
128 this.Start ();
130 return false;
132 /////////////////////////////////////////////////
134 private static Indexable NoteToIndexable (FileInfo file)
136 Indexable indexable = new Indexable (UriFu.PathToFileUri(file.FullName));
137 indexable.Timestamp = file.LastWriteTimeUtc;
138 indexable.HitType = "Note";
139 indexable.MimeType = "x-beagle/x-labyrinth-note";
140 indexable.AddProperty (Property.NewUnsearched ("fixme:application","labyrinth"));
142 return indexable;
145 private void IndexNote (FileInfo file, Scheduler.Priority priority)
148 if (! File.Exists (file.FullName))
149 return;
151 if (IsUpToDate (file.FullName))
152 return;
154 Indexable indexable = NoteToIndexable (file);
155 Scheduler.Task task = NewAddTask (indexable);
156 task.Priority = priority;
157 task.SubPriority = 0;
158 ThisScheduler.Add (task);
163 /////////////////////////////////////////////////
166 private void OnInotifyNewNote(Inotify.Watch watch,
167 string path, string subitem, string srcpath,
168 Inotify.EventType type)
170 if (subitem.Length == 0 || (type & Inotify.EventType.IsDirectory) != 0)
171 return;
173 if ( subitem.EndsWith("map"))
174 IndexNote (new FileInfo(Path.Combine (path, subitem)), Scheduler.Priority.Immediate);
177 /////////////////////////////////////////////////