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
{
43 public LabyrinthQueryable () : base ("LabyrinthIndex")
45 lab_dir
= Path
.Combine (PathFinder
.HomeDir
, ".gnome2");
46 lab_dir
= Path
.Combine (lab_dir
, "labyrinth");
49 /////////////////////////////////////////////////
51 private void StartWorker()
53 if (! Directory
.Exists (lab_dir
)) {
54 GLib
.Timeout
.Add (60000, new GLib
.TimeoutHandler (CheckForExistence
));
58 Log
.Info ("Starting Labyrinth backend");
60 Stopwatch stopwatch
= new Stopwatch ();
66 Inotify
.Subscribe (lab_dir
, OnInotifyNewNote
, Inotify
.EventType
.CloseWrite
| Inotify
.EventType
.Modify
);
69 task
= NewAddTask (this);
70 task
.Tag
= "Crawling Labyrinth Notes";
73 ThisScheduler
.Add (task
);
77 Log
.Info ("labyrinth backend worker thread done in {0}", stopwatch
);
80 public override void Start ()
84 ExceptionHandlingThread
.Start (new ThreadStart (StartWorker
));
87 /////////////////////////////////////////////////
90 public string StatusName
{
91 get { return "LabyrinthQueryable"; }
94 private IEnumerator map_files
= null;
96 public void PostFlushHook () { }
98 public bool HasNextIndexable ()
100 if (map_files
== null)
101 map_files
= DirectoryWalker
.GetFileInfos (lab_dir
).GetEnumerator ();
103 return map_files
.MoveNext ();
106 public Indexable
GetNextIndexable ()
108 FileInfo file
= (FileInfo
) map_files
.Current
;
113 if (IsUpToDate (file
.FullName
))
116 Indexable indexable
= NoteToIndexable (file
);
120 private bool CheckForExistence ()
122 if (!Directory
.Exists (lab_dir
))
129 /////////////////////////////////////////////////
131 private static Indexable
NoteToIndexable (FileInfo file
)
133 Indexable indexable
= new Indexable (UriFu
.PathToFileUri(file
.FullName
));
134 indexable
.Timestamp
= file
.LastWriteTimeUtc
;
135 indexable
.HitType
= "Note";
136 indexable
.MimeType
= "x-beagle/x-labyrinth-note";
137 indexable
.AddProperty (Property
.NewUnsearched ("fixme:application","labyrinth"));
142 private void IndexNote (FileInfo file
, Scheduler
.Priority priority
)
145 if (! File
.Exists (file
.FullName
))
148 if (IsUpToDate (file
.FullName
))
151 Indexable indexable
= NoteToIndexable (file
);
152 Scheduler
.Task task
= NewAddTask (indexable
);
153 task
.Priority
= priority
;
154 task
.SubPriority
= 0;
155 ThisScheduler
.Add (task
);
160 /////////////////////////////////////////////////
163 private void OnInotifyNewNote(Inotify
.Watch watch
,
164 string path
, string subitem
, string srcpath
,
165 Inotify
.EventType type
)
167 if (subitem
.Length
== 0 || (type
& Inotify
.EventType
.IsDirectory
) != 0)
170 if ( subitem
.EndsWith("map"))
171 IndexNote (new FileInfo(Path
.Combine (path
, subitem
)), Scheduler
.Priority
.Immediate
);
174 /////////////////////////////////////////////////