RTF filter complies to MS RTF 1.5 specification. (works well with 1.8 as well).
[beagle.git] / tools / Query.cs
blob364898d2fd6bd007482ba5838effeee66f3b812c
1 //
2 // Query.cs
3 //
4 // Copyright (C) 2004 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.Threading;
31 using Gtk;
33 using Beagle;
34 using BU = Beagle.Util;
36 class QueryTool {
38 static int count = 0;
39 static Query query = null;
40 static DateTime queryStartTime;
41 static DateTime lastQueryTime = DateTime.Now;
43 // CLI args
44 static bool keepRunning = false;
45 static bool verbose = false;
47 static void OnHitsAdded (Query source, ICollection hits)
49 lastQueryTime = DateTime.Now;
51 if (count == 0 && verbose) {
52 Console.WriteLine ("First hit returned in {0:0.000}s",
53 (lastQueryTime - queryStartTime).TotalSeconds);
56 foreach (Hit hit in hits) {
57 if (verbose)
58 Console.WriteLine (" Uri: {0}", hit.Uri);
59 else
60 Console.WriteLine (hit.Uri);
62 if (verbose) {
63 Console.WriteLine (" Type: {0}", hit.Type);
64 Console.WriteLine ("MimeT: {0}", hit.MimeType == null ? "(null)" : hit.MimeType);
65 Console.WriteLine (" Src: {0}", hit.Source);
66 Console.WriteLine ("Score: {0}", hit.Score);
67 if (hit.ValidTimestamp)
68 Console.WriteLine (" Time: {0}", hit.Timestamp);
69 if (hit.ValidRevision)
70 Console.WriteLine (" Rev: {0}", hit.Revision);
72 foreach (String key in hit.Keys)
73 Console.WriteLine (" {0} = {1}", key, hit [key]);
75 Console.WriteLine ();
78 ++count;
82 static void OnHitsSubtracted (Query source, ICollection uris)
84 lastQueryTime = DateTime.Now;
86 foreach (Uri uri in uris) {
87 Console.WriteLine ("Subtracted Uri '{0}'", uri);
88 Console.WriteLine ();
90 --count;
94 static void OnFinished (QueryProxy query)
96 if (verbose)
97 Console.WriteLine ("Elapsed time: {0:0.000}s",
98 (DateTime.Now - queryStartTime).TotalSeconds);
99 Gtk.Application.Quit ();
102 public static void PrintUsageAndExit ()
104 string usage =
105 "beagle-query: Command-line interface to the Beagle search system.\n" +
106 "Web page: http://www.gnome.org/projects/beagle\n" +
107 "Copyright (C) 2004 Novell, Inc.\n\n";
108 usage +=
109 "Usage: beagle-query [OPTIONS] <query string>\n\n" +
110 "Options:\n" +
111 " --verbose\t\t\tPrint detailed information about each hit.\n" +
112 " --mime <mime type>\t\tConstrain search results to the specified mime\n" +
113 " \t\ttype. Can be used multiply.\n" +
114 " --source <source>\t\tConstrain query to the specified source.\n" +
115 " \t\tSources list available from beagle-status.\n" +
116 " --live-query\t\t\tRun continuously, printing notifications if a\n" +
117 " \t\t\tquery changes.\n" +
118 " --help\t\t\tPrint this usage message.\n";
120 Console.WriteLine (usage);
122 System.Environment.Exit (0);
126 static void Main (string[] args)
128 Gtk.Application.Init ();
130 try {
131 query = Beagle.Factory.NewQuery ();
132 } catch (Exception e) {
133 if (e.ToString ().IndexOf ("com.novell.Beagle") != -1) {
134 Console.WriteLine ("Could not query. The Beagle daemon is probably not running, or maybe you\ndon't have D-BUS set up properly.");
135 System.Environment.Exit (-1);
136 } else {
137 Console.WriteLine ("The query failed with error:\n\n" + e);
138 System.Environment.Exit (-1);
142 query.HitsAddedEvent += OnHitsAdded;
143 query.HitsSubtractedEvent += OnHitsSubtracted;
145 // Parse args
146 int i = 0;
147 while (i < args.Length) {
148 switch (args [i]) {
150 case "--mime":
151 if (++i >= args.Length) PrintUsageAndExit ();
152 query.AddMimeType (args [i]);
153 break;
154 case "--source":
155 if (++i >= args.Length) PrintUsageAndExit ();
156 query.AddSource (args [i]);
157 break;
158 case "--live-query":
159 keepRunning = true;
160 break;
161 case "--verbose":
162 verbose = true;
163 break;
165 case "--help":
166 case "--usage":
167 PrintUsageAndExit ();
168 return;
170 default:
171 query.AddTextRaw (args [i]);
172 break;
175 ++i;
178 if (! keepRunning)
179 query.FinishedEvent += OnFinished;
181 query.Start ();
183 queryStartTime = DateTime.Now;
185 Gtk.Application.Run ();