4 // Copyright (C) 2004 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
.Threading
;
34 using BU
= Beagle
.Util
;
39 static Query query
= null;
40 static DateTime queryStartTime
;
41 static DateTime lastQueryTime
= DateTime
.Now
;
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
) {
58 Console
.WriteLine (" Uri: {0}", hit
.Uri
);
60 Console
.WriteLine (hit
.Uri
);
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
]);
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
);
94 static void OnFinished (QueryProxy query
)
97 Console
.WriteLine ("Elapsed time: {0:0.000}s",
98 (DateTime
.Now
- queryStartTime
).TotalSeconds
);
99 Gtk
.Application
.Quit ();
102 public static void PrintUsageAndExit ()
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";
109 "Usage: beagle-query [OPTIONS] <query string>\n\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 ();
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);
137 Console
.WriteLine ("The query failed with error:\n\n" + e
);
138 System
.Environment
.Exit (-1);
142 query
.HitsAddedEvent
+= OnHitsAdded
;
143 query
.HitsSubtractedEvent
+= OnHitsSubtracted
;
147 while (i
< args
.Length
) {
151 if (++i
>= args
.Length
) PrintUsageAndExit ();
152 query
.AddMimeType (args
[i
]);
155 if (++i
>= args
.Length
) PrintUsageAndExit ();
156 query
.AddSource (args
[i
]);
167 PrintUsageAndExit ();
171 query
.AddTextRaw (args
[i
]);
179 query
.FinishedEvent
+= OnFinished
;
183 queryStartTime
= DateTime
.Now
;
185 Gtk
.Application
.Run ();