* KNotesQueryable.cs: Dont re-index all the notes when the notes file changes. Since...
[beagle.git] / beagled / ThunderbirdQueryable / ThunderbirdQueryable.cs
blob5e937687037a582403b16afcd613a94c533a583a
1 //
2 // ThunderbirdQueryable.cs: This is where all the magic begins!
3 //
4 // Copyright (C) 2006 Pierre Östlund
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // 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 FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.IO;
29 using System.Threading;
30 using System.Collections;
31 using System.Text.RegularExpressions;
33 using Beagle;
34 using Beagle.Util;
36 namespace Beagle.Daemon.ThunderbirdQueryable {
38 [QueryableFlavor (Name = "Thunderbird", Domain = QueryDomain.Local, RequireInotify = false)]
39 public class ThunderbirdQueryable : LuceneQueryable {
40 private static DateTime indexing_start;
41 private ThunderbirdIndexer indexer;
43 public ThunderbirdQueryable () :
44 base ("ThunderbirdIndex")
46 // Remove one second from the start time to make sure we don't run into any troubles
47 indexing_start = DateTime.UtcNow.Subtract (new TimeSpan (0, 0, 1));
48 indexer = null;
50 GMime.Global.Init ();
52 if (Environment.GetEnvironmentVariable ("BEAGLE_THUNDERBIRD_DEBUG") != null) {
53 Thunderbird.Debug = true;
54 Logger.Log.Debug ("Running Thunderbird backend in debug mode");
58 public override void Start ()
60 base.Start ();
61 ExceptionHandlingThread.Start (new ThreadStart (StartWorker));
64 private void StartWorker ()
66 Logger.Log.Info ("Starting Thunderbird backend");
67 Stopwatch watch = new Stopwatch ();
68 watch.Start ();
70 string root_path = Thunderbird.GetRootPath ();
71 if (!Directory.Exists (root_path)) {
72 GLib.Timeout.Add (60000, new GLib.TimeoutHandler (IndexDataCheck));
73 Logger.Log.Info ("No data available for indexing in {0}", root_path);
74 return;
77 State = QueryableState.Crawling;
78 indexer = new ThunderbirdIndexer (this, Thunderbird.GetProfilePaths (root_path));
79 indexer.Crawl ();
80 State = QueryableState.Idle;
82 watch.Stop ();
83 Logger.Log.Info ("Thunderbird backend done in {0}s", watch.ElapsedTime);
86 private bool IndexDataCheck ()
88 if (!Directory.Exists (Thunderbird.GetRootPath ()))
89 return true;
91 StartWorker ();
92 return false;
95 // We need this in order to perform custom queries to the lucene database
96 override protected LuceneQueryingDriver BuildLuceneQueryingDriver (string index_name,
97 int minor_version,
98 bool read_only_mode)
100 return new LuceneAccess (index_name, minor_version, read_only_mode);
103 /////////////////////////////////////////////////////////////////////////////////////
105 public Scheduler.Task NewRemoveTaskByDate (DateTime end_date)
107 return NewAddTask (new DateIndexableGenerator (Driver, Lucene, end_date));
110 // The purpose of this IndexableGenerator is to remove mails older than the
111 // specified date (when beagle began to index Thunderbird mails)
112 private class DateIndexableGenerator : IIndexableGenerator {
113 private LuceneQueryingDriver driver;
114 private LuceneAccess lucene;
115 private DateTime end_date;
117 private Uri[] stored_uris;
118 private IEnumerator enumerator;
120 public DateIndexableGenerator (LuceneQueryingDriver driver, LuceneAccess lucene, DateTime end_date)
122 this.driver = driver;
123 this.lucene = lucene;
124 this.end_date = end_date;
125 this.stored_uris = null;
128 public Indexable GetNextIndexable ()
130 return new Indexable (IndexableType.Remove, (Uri) enumerator.Current);
133 public bool HasNextIndexable ()
135 if (stored_uris == null) {
136 stored_uris = driver.PropertyQuery (Property.NewKeyword ("fixme:client", "thunderbird"));
137 enumerator = stored_uris.GetEnumerator ();
140 do {
141 while (enumerator == null || !enumerator.MoveNext ())
142 return false;
143 } while (MatchesDate ((enumerator.Current as Uri)));
145 return true;
148 private bool MatchesDate (Uri uri)
150 LuceneAccess.StoredInfo info = lucene.GetStoredInfo (uri);
152 try {
153 if (!info.Equals (end_date) && info.LastIndex.CompareTo (end_date) < 0)
154 return false;
155 } catch {}
157 return true;
160 public string StatusName {
161 get {
162 return String.Format ("Removing Thunderbird mails past {0}", end_date.ToString ());
166 public void PostFlushHook () { }
169 /////////////////////////////////////////////////////////////////////////////////////
171 public LuceneAccess Lucene {
172 get { return (LuceneAccess) Driver; }
175 public static DateTime IndexingStart {
176 get { return indexing_start; }