2 // LabyrithQueryable.cs
4 // Copyright (C) 2006 Kevin Kubasik <kevin@kubasik.net>
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
;
30 using System
.Threading
;
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");
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
));
61 log
.Info ("Starting Labyrinth backend");
63 Stopwatch stopwatch
= new Stopwatch ();
69 Inotify
.Subscribe (lab_dir
, OnInotifyNewNote
, Inotify
.EventType
.CloseWrite
| Inotify
.EventType
.Modify
);
72 task
= NewAddTask (this);
73 task
.Tag
= "Crawling Labyrinth Notes";
76 ThisScheduler
.Add (task
);
80 log
.Info ("labyrinth backend worker thread done in {0}", stopwatch
);
83 public override void 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
;
116 if (IsUpToDate (file
.FullName
))
119 Indexable indexable
= NoteToIndexable (file
);
123 private bool CheckForExistence ()
125 if (!Directory
.Exists (lab_dir
))
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"));
145 private void IndexNote (FileInfo file
, Scheduler
.Priority priority
)
148 if (! File
.Exists (file
.FullName
))
151 if (IsUpToDate (file
.FullName
))
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)
173 if ( subitem
.EndsWith("map"))
174 IndexNote (new FileInfo(Path
.Combine (path
, subitem
)), Scheduler
.Priority
.Immediate
);
177 /////////////////////////////////////////////////