* Filters/FilterPackage.cs, Filters/FilterRPM.cs,
[beagle.git] / tools / Query.cs
bloba90f40688c5afb17377f9857afdfc4e9a8c89231
1 //
2 // Query.cs
3 //
4 // Copyright (C) 2004-2005 Novell, Inc.
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.Collections;
29 using System.Globalization;
30 using System.Threading;
31 using System.Text;
33 using Gtk;
35 using Beagle;
36 using Beagle.Daemon;
38 class QueryTool {
40 static int count = 0;
41 static Query query = null;
42 static DateTime queryStartTime;
43 static DateTime lastQueryTime = DateTime.Now;
45 // CLI args
46 static bool keep_running = false;
47 static bool verbose = false;
48 static bool display_hits = true;
49 static bool flood = false;
50 static bool listener = false;
51 static DateTime start_date = DateTime.MinValue;
52 static DateTime end_date = DateTime.MinValue;
54 static void OnHitsAdded (HitsAddedResponse response)
56 lastQueryTime = DateTime.Now;
58 if (count == 0 && verbose) {
59 Console.WriteLine ("First hit returned in {0:0.000}s",
60 (lastQueryTime - queryStartTime).TotalSeconds);
63 if (! display_hits) {
64 count += response.Hits.Count;
65 return;
68 foreach (Hit hit in response.Hits) {
69 if (verbose)
70 Console.WriteLine (" Uri: {0}", hit.Uri);
71 else
72 Console.WriteLine (hit.Uri);
74 if (verbose) {
75 SnippetRequest sreq = new SnippetRequest (query, hit);
76 SnippetResponse sresp = (SnippetResponse) sreq.Send ();
77 Console.WriteLine ("PaUri: {0}", hit.ParentUri != null ? hit.ParentUri.ToString () : "(null)");
78 Console.WriteLine (" Snip: {0}", sresp.Snippet != null ? sresp.Snippet : "(null)");
79 Console.WriteLine (" Type: {0}", hit.Type);
80 Console.WriteLine ("MimeT: {0}", hit.MimeType == null ? "(null)" : hit.MimeType);
81 Console.WriteLine (" Src: {0}", hit.Source);
82 Console.WriteLine ("Score: {0}", hit.Score);
83 if (hit.ValidTimestamp)
84 Console.WriteLine (" Time: {0}", hit.Timestamp.ToLocalTime ());
86 foreach (Property prop in hit.Properties)
87 Console.WriteLine (" {0} = {1}", prop.Key, prop.Value);
89 Console.WriteLine ();
92 ++count;
96 static void OnHitsSubtracted (HitsSubtractedResponse response)
98 lastQueryTime = DateTime.Now;
100 if (! display_hits)
101 return;
103 foreach (Uri uri in response.Uris) {
104 Console.WriteLine ("Subtracted Uri '{0}'", uri);
105 Console.WriteLine ();
107 --count;
111 static void OnFinished (FinishedResponse response)
113 if (verbose) {
114 Console.WriteLine ("Elapsed time: {0:0.000}s",
115 (DateTime.Now - queryStartTime).TotalSeconds);
116 Console.WriteLine ("Total hits: {0}", count);
119 if (flood)
120 SendQuery ();
121 else
122 Gtk.Application.Quit ();
125 public static void PrintUsageAndExit ()
127 string usage =
128 "beagle-query: Command-line interface to the Beagle search system.\n" +
129 "Web page: http://www.gnome.org/projects/beagle\n" +
130 "Copyright (C) 2004 Novell, Inc.\n\n";
131 usage +=
132 "Usage: beagle-query [OPTIONS] <query string>\n\n" +
133 "Options:\n" +
134 " --verbose\t\t\tPrint detailed information about each hit.\n" +
135 " --mime <mime type>\t\tConstrain search results to the specified mime\n" +
136 " \t\ttype. Can be used multiply.\n" +
137 " --type <hit type>\t\tConstrain search results to the specified hit\n" +
138 " \t\ttype. Can be used multiply.\n" +
139 " --source <source>\t\tConstrain query to the specified source.\n" +
140 " \t\tSources list available from beagle-status.\n" +
141 " --start <date>\t\tConstrain query to items after specified date.\n" +
142 " \t\tDate must be in the form \"yyyyMMdd\" or \"yyyyMMddHHmmss\"\n" +
143 " --end <date>\t\t\tConstrain query to items before specified date.\n" +
144 " \t\t\tDate must be in the form \"yyyyMMdd\" or \"yyyyMMddHHmmss\"\n" +
145 " --live-query\t\t\tRun continuously, printing notifications if a\n" +
146 " \t\t\tquery changes.\n" +
147 " --stats-only\t\t\tOnly display statistics about the query, not\n" +
148 " \t\t\tthe actual results.\n" +
149 " --max-hits\t\t\tLimit number of search results per backend\n" +
150 " \t\t\t(default = 100, max = 100)\n" +
151 " --flood\t\t\tExecute the query over and over again. Don't do that.\n" +
152 " --listener\t\t\tExecute an index listener query. Don't do that either.\n" +
153 " --help\t\t\tPrint this usage message.\n";
155 Console.WriteLine (usage);
157 Console.WriteLine (
158 "'query string' can include specific keywords e.g. author:jon.\n" +
159 "Supported keywords are:");
160 IDictionaryEnumerator property_keyword_enum = PropertyKeywordFu.MappingEnumerator;
161 while (property_keyword_enum.MoveNext ()) {
162 PropertyDetail prop = property_keyword_enum.Value as PropertyDetail;
163 Console.WriteLine (" {0,-12} for {1}", property_keyword_enum.Key, prop.Description);
166 System.Environment.Exit (0);
169 static void OnClosed ()
171 if (flood)
172 SendQuery ();
173 else
174 Gtk.Application.Quit ();
177 static int query_counter = 0;
178 static void SendQuery ()
180 ++query_counter;
181 if (flood) {
182 if (query_counter > 1)
183 Console.WriteLine ();
184 Console.WriteLine ("Sending query #{0}", query_counter);
187 queryStartTime = DateTime.Now;
188 try {
189 query.SendAsync ();
190 } catch (Exception ex) {
191 Console.WriteLine ("Could not connect to the Beagle daemon. The daemon probably isn't running.");
192 Console.WriteLine (ex);
193 System.Environment.Exit (-1);
197 static void Main (string[] args)
199 Gtk.Application.InitCheck ("beagle-query", ref args);
201 if (args.Length == 0 || Array.IndexOf (args, "--help") > -1 || Array.IndexOf (args, "--usage") > -1)
202 PrintUsageAndExit ();
204 StringBuilder query_str = new StringBuilder ();
206 string[] formats = {
207 "yyyyMMdd",
208 "yyyyMMddHHmmss"
211 query = new Query ();
213 // Parse args
214 int i = 0;
215 while (i < args.Length) {
216 switch (args [i]) {
218 case "--mime":
219 if (++i >= args.Length) PrintUsageAndExit ();
220 query.AddMimeType (args [i]);
221 break;
222 case "--type":
223 if (++i >= args.Length) PrintUsageAndExit ();
224 query.AddHitType (args [i]);
225 break;
226 case "--source":
227 if (++i >= args.Length) PrintUsageAndExit ();
228 query.AddSource (args [i]);
229 break;
230 case "--live-query":
231 keep_running = true;
232 break;
233 case "--verbose":
234 verbose = true;
235 break;
236 case "--stats-only":
237 verbose = true;
238 display_hits = false;
239 break;
240 case "--max-hits":
241 if (++i >= args.Length) PrintUsageAndExit ();
242 query.MaxHits = Int32.Parse (args[i]);
243 break;
244 case "--flood":
245 flood = true;
246 break;
247 case "--listener":
248 listener = true;
249 keep_running = true;
250 break;
251 case "--start":
252 if (++i >= args.Length) PrintUsageAndExit ();
253 try {
254 start_date = DateTime.ParseExact (args[i], formats,
255 CultureInfo.InvariantCulture,
256 DateTimeStyles.None);
257 } catch (FormatException) {
258 Console.WriteLine ("Invalid start date");
259 System.Environment.Exit (-1);
261 start_date = start_date.ToUniversalTime ();
262 break;
264 case "--end":
265 if (++i >= args.Length) PrintUsageAndExit ();
266 try {
267 end_date = DateTime.ParseExact (args[i], formats,
268 CultureInfo.InvariantCulture,
269 DateTimeStyles.None);
270 } catch (FormatException) {
271 Console.WriteLine ("Invalid end date");
272 System.Environment.Exit (-1);
274 end_date = end_date.ToUniversalTime ();
275 break;
277 default:
278 if (query_str.Length > 0)
279 query_str.Append (' ');
280 query_str.Append ('"');
281 query_str.Append (args [i]);
282 query_str.Append ('"');
283 break;
286 ++i;
289 if (listener) {
291 query.IsIndexListener = true;
293 } else {
295 if (query_str.Length > 0)
296 query.AddText (query_str.ToString ());
298 if (start_date != DateTime.MinValue || end_date != DateTime.MinValue) {
299 QueryPart_DateRange part = new QueryPart_DateRange ();
301 if (start_date != DateTime.MinValue)
302 part.StartDate = start_date;
304 if (end_date != DateTime.MinValue)
305 part.EndDate = end_date;
307 query.AddPart (part);
311 query.HitsAddedEvent += OnHitsAdded;
312 query.HitsSubtractedEvent += OnHitsSubtracted;
315 if (! keep_running)
316 query.FinishedEvent += OnFinished;
317 else
318 query.ClosedEvent += OnClosed;
320 SendQuery ();
322 Gtk.Application.Run ();