4 // Copyright (C) 2004-2005 Novell, Inc.
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
28 using System
.Collections
;
29 using System
.Globalization
;
30 using System
.Threading
;
41 static Query query
= null;
42 static DateTime queryStartTime
;
43 static DateTime lastQueryTime
= DateTime
.Now
;
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
);
64 count
+= response
.Hits
.Count
;
68 foreach (Hit hit
in response
.Hits
) {
70 Console
.WriteLine (" Uri: {0}", hit
.Uri
);
72 Console
.WriteLine (hit
.Uri
);
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
);
96 static void OnHitsSubtracted (HitsSubtractedResponse response
)
98 lastQueryTime
= DateTime
.Now
;
103 foreach (Uri uri
in response
.Uris
) {
104 Console
.WriteLine ("Subtracted Uri '{0}'", uri
);
105 Console
.WriteLine ();
111 static void OnFinished (FinishedResponse response
)
114 Console
.WriteLine ("Elapsed time: {0:0.000}s",
115 (DateTime
.Now
- queryStartTime
).TotalSeconds
);
116 Console
.WriteLine ("Total hits: {0}", count
);
122 Gtk
.Application
.Quit ();
125 public static void PrintUsageAndExit ()
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";
132 "Usage: beagle-query [OPTIONS] <query string>\n\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
);
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 ()
174 Gtk
.Application
.Quit ();
177 static int query_counter
= 0;
178 static void SendQuery ()
182 if (query_counter
> 1)
183 Console
.WriteLine ();
184 Console
.WriteLine ("Sending query #{0}", query_counter
);
187 queryStartTime
= DateTime
.Now
;
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 ();
211 query
= new Query ();
215 while (i
< args
.Length
) {
219 if (++i
>= args
.Length
) PrintUsageAndExit ();
220 query
.AddMimeType (args
[i
]);
223 if (++i
>= args
.Length
) PrintUsageAndExit ();
224 query
.AddHitType (args
[i
]);
227 if (++i
>= args
.Length
) PrintUsageAndExit ();
228 query
.AddSource (args
[i
]);
238 display_hits
= false;
241 if (++i
>= args
.Length
) PrintUsageAndExit ();
242 query
.MaxHits
= Int32
.Parse (args
[i
]);
252 if (++i
>= args
.Length
) PrintUsageAndExit ();
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 ();
265 if (++i
>= args
.Length
) PrintUsageAndExit ();
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 ();
278 if (query_str
.Length
> 0)
279 query_str
.Append (' ');
280 query_str
.Append ('"');
281 query_str
.Append (args
[i
]);
282 query_str
.Append ('"');
291 query
.IsIndexListener
= true;
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
;
316 query
.FinishedEvent
+= OnFinished
;
318 query
.ClosedEvent
+= OnClosed
;
322 Gtk
.Application
.Run ();