RTF filter complies to MS RTF 1.5 specification. (works well with 1.8 as well).
[beagle.git] / Best / BestWindow.cs
blob8d00f686657f8566a22ad446424a8276d9b0ff20
1 //
2 // BestWindow.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.
28 using System;
29 using System.Collections;
31 using Gtk;
32 using GtkSharp;
34 using Beagle;
35 using Beagle.Tile;
37 namespace Best {
39 public class BestWindow : Gtk.Window {
41 public BestWindow (string query) : base (WindowType.Toplevel)
43 CreateWindow (query);
46 public BestWindow () : base (WindowType.Toplevel)
48 CreateWindow (null);
51 public void FocusEntry () {
52 entry.SelectRegion (0, -1);
53 entry.GrabFocus ();
56 void CreateWindow (string query)
58 Title = "Bleeding-Edge Search Tool";
60 DeleteEvent += new DeleteEventHandler (this.DoDelete);
62 Icon = Images.GetPixbuf ("smalldog.png");
64 Widget content = CreateContents ();
66 VBox main = new VBox (false, 3);
67 main.PackStart (content, true, true, 3);
68 content.Show ();
69 Add (main);
70 main.Show ();
71 main.Realize ();
72 canvas.Realize ();
74 root = new SimpleRootTile ();
75 canvas.Root = root;
77 DefaultWidth = 600;
78 DefaultHeight = 700;
80 accel_group = new Gtk.AccelGroup ();
81 this.AddAccelGroup (accel_group);
82 global_keys = new GlobalKeybinder (accel_group);
84 // Close window (Ctrl-W)
85 global_keys.AddAccelerator (new EventHandler (this.HideWindowHandler),
86 (uint) Gdk.Key.w,
87 Gdk.ModifierType.ControlMask,
88 Gtk.AccelFlags.Visible);
90 // Close window (Escape)
91 global_keys.AddAccelerator (new EventHandler (this.HideWindowHandler),
92 (uint) Gdk.Key.Escape,
94 Gtk.AccelFlags.Visible);
96 DBusisms.BeagleDown += OnBeagleDown;
98 UpdatePage ();
100 if (query != null)
101 Search (query);
105 //////////////////////////
107 Query query = null;
108 Gtk.AccelGroup accel_group;
109 GlobalKeybinder global_keys;
111 private void SetBusy (bool busy)
113 if (busy) {
114 this.GdkWindow.Cursor = new Gdk.Cursor (Gdk.CursorType.Watch);
115 } else {
116 this.GdkWindow.Cursor = null;
121 private void OnBeagleDown ()
123 SetBusy (false);
124 DetachQuery ();
127 private void DoDelete (object o, DeleteEventArgs args)
129 Hide ();
132 private void HideWindowHandler (object o, EventArgs args)
134 Hide ();
137 //////////////////////////
139 private void DoForwardClicked (object o, EventArgs args)
141 root.HitCollection.PageForward ();
142 UpdatePage ();
145 private void DoBackClicked (object o, EventArgs args)
147 root.HitCollection.PageBack ();
148 UpdatePage ();
151 //////////////////////////
153 private Gtk.Entry entry;
155 private Gtk.ScrolledWindow swin;
156 private TileCanvas canvas;
157 private SimpleRootTile root;
158 private Gtk.Label page_label;
159 private Gtk.Button back_button;
160 private Gtk.Button forward_button;
162 private Gtk.Button StockButton (string stockid, string label)
164 Gtk.HBox button_contents = new HBox (false, 0);
165 button_contents.Show ();
166 Gtk.Widget button_image = new Gtk.Image (stockid, Gtk.IconSize.Button);
167 button_image.Show ();
168 button_contents.PackStart (button_image, false, false, 1);
169 Gtk.Label button_label = new Gtk.Label (label);
170 button_label.Show ();
171 button_contents.PackStart (button_label, false, false, 1);
173 Gtk.Button button = new Gtk.Button ();
174 button.Add (button_contents);
176 return button;
179 private Gtk.Widget CreateContents ()
181 Gtk.HBox entryLine = new HBox (false, 3);
183 Gtk.Label words = new Gtk.Label ("Search terms:");
184 entryLine.PackStart (words, false, false, 3);
186 entry = new Gtk.Entry ();
187 entry.Activated += new EventHandler (this.DoSearch);
188 entryLine.PackStart (entry, true, true, 3);
190 Gtk.HBox buttonContents = new HBox (false, 0);
191 Gtk.Widget buttonImg = Beagle.Images.GetWidget ("icon-search.png");
192 buttonContents.PackStart (buttonImg, false, false, 1);
193 Gtk.Label buttonLabel = new Gtk.Label ("Find");
194 buttonContents.PackStart (buttonLabel, false, false, 1);
196 Gtk.Button button = new Gtk.Button ();
197 button.Add (buttonContents);
198 button.Clicked += new EventHandler (this.DoSearch);
199 entryLine.PackStart (button, false, false, 3);
201 canvas = new TileCanvas ();
202 canvas.Show ();
204 HBox pager = new HBox ();
205 page_label = new Label ();
206 page_label.Show ();
207 pager.PackStart (page_label, false, false, 3);
209 forward_button = StockButton ("gtk-go-forward",
210 "Show More Results");
211 forward_button.Show ();
212 forward_button.Clicked += new EventHandler (DoForwardClicked);
213 pager.PackEnd (forward_button, false, false, 3);
215 back_button = StockButton ("gtk-go-back",
216 "Show Previous Results");
217 back_button.Show ();
219 back_button.Clicked += new EventHandler (DoBackClicked);
220 pager.PackEnd (back_button, false, false, 3);
222 pager.Show ();
224 VBox contents = new VBox (false, 3);
225 contents.PackStart (entryLine, false, true, 3);
226 contents.PackStart (canvas, true, true, 3);
227 contents.PackStart (pager, false, false, 3);
229 entryLine.ShowAll ();
230 canvas.ShowAll ();
232 return contents;
235 private void UpdatePage ()
237 back_button.Sensitive = root.HitCollection.CanPageBack;
238 forward_button.Sensitive = root.HitCollection.CanPageForward;
240 string label;
241 if (root.HitCollection.NumResults == 0)
242 label = "No results.";
243 else if (root.HitCollection.FirstDisplayed == 0)
244 label = String.Format ("Best <b>{0} results of {1}</b> are shown.",
245 root.HitCollection.LastDisplayed + 1,
246 root.HitCollection.NumResults);
247 else
248 label = String.Format ("Results <b>{0} through {1} of {2}</b> are shown.",
249 root.HitCollection.FirstDisplayed + 1,
250 root.HitCollection.LastDisplayed + 1,
251 root.HitCollection.NumResults);
252 page_label.Markup = label;
255 private string delayedQuery = null;
257 private bool RunDelayedQuery ()
259 if (delayedQuery != null) {
260 string tmp = delayedQuery;
261 delayedQuery = null;
262 System.Console.WriteLine ("Delayed query fired");
263 Search (tmp);
266 return false;
269 private void QueueDelayedQuery ()
271 GLib.Timeout.Add (1000, new GLib.TimeoutHandler (RunDelayedQuery));
274 private void DoSearch (object o, EventArgs args)
276 try {
277 Search (entry.Text);
279 catch (Exception e)
281 delayedQuery = entry.Text;
282 DBusisms.BeagleUpAgain += QueueDelayedQuery;
284 if (e.ToString ().IndexOf ("'com.novell.Beagle'") != -1) {
285 root.Error ("The query for <i>" + entry.Text + "</i> failed." +
286 "<br>The likely cause is that the beagle daemon isn't running.");
287 root.OfferDaemonRestart = true;
288 } else if (e.ToString().IndexOf ("Unable to determine the address") != -1) {
289 root.Error ("The query for <i>" + entry.Text + "</i> failed.<br>" +
290 "The session bus isn't running. See http://beaglewiki.org/index.php/Installing%20Beagle for information on setting up a session bus.");
291 } else
292 root.Error ("The query for <i>" + entry.Text + "</i> failed with error:<br><br>" + e);
294 UpdatePage ();
297 //////////////////////////
299 private void Close ()
301 Destroy ();
304 //////////////////////////
306 private void OnHitsAdded (Query source, ICollection hits)
308 root.Add (hits);
309 UpdatePage ();
312 private void OnHitsSubtracted (Query source, ICollection uris)
314 root.Subtract (uris);
315 UpdatePage ();
318 private void OnFinished (QueryProxy source)
320 SetBusy (false);
323 private void OnCancelled (QueryProxy source)
325 SetBusy (false);
328 private void AttachQuery ()
330 query.HitsAddedEvent += OnHitsAdded;
331 query.HitsSubtractedEvent += OnHitsSubtracted;
332 query.FinishedEvent += OnFinished;
333 query.CancelledEvent += OnCancelled;
336 private void DetachQuery ()
338 if (query != null) {
339 query.HitsAddedEvent -= OnHitsAdded;
340 query.HitsSubtractedEvent -= OnHitsSubtracted;
341 query.FinishedEvent -= OnFinished;
342 query.CancelledEvent -= OnCancelled;
343 query.Dispose ();
344 query = null;
348 private void Search (String searchString)
350 entry.Text = searchString;
351 if (query != null) {
352 query.Cancel ();
353 DetachQuery ();
356 query = Factory.NewQuery ();
358 query.AddDomain (QueryDomain.Neighborhood);
360 // Disable non-local searching for now.
361 //query.AddDomain (QueryDomain.Global);
363 query.AddText (searchString);
365 AttachQuery ();
367 root.Query = query;
368 root.Start ();
370 SetBusy (true);
371 query.Start ();