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
29 using System
.Collections
;
39 public class BestWindow
: Gtk
.Window
{
41 public BestWindow (string query
) : base (WindowType
.Toplevel
)
46 public BestWindow () : base (WindowType
.Toplevel
)
51 public void FocusEntry () {
52 entry
.SelectRegion (0, -1);
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);
74 root
= new SimpleRootTile ();
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
),
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
;
105 //////////////////////////
108 Gtk
.AccelGroup accel_group
;
109 GlobalKeybinder global_keys
;
111 private void SetBusy (bool busy
)
114 this.GdkWindow
.Cursor
= new Gdk
.Cursor (Gdk
.CursorType
.Watch
);
116 this.GdkWindow
.Cursor
= null;
121 private void OnBeagleDown ()
127 private void DoDelete (object o
, DeleteEventArgs args
)
132 private void HideWindowHandler (object o
, EventArgs args
)
137 //////////////////////////
139 private void DoForwardClicked (object o
, EventArgs args
)
141 root
.HitCollection
.PageForward ();
145 private void DoBackClicked (object o
, EventArgs args
)
147 root
.HitCollection
.PageBack ();
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
);
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 ();
204 HBox pager
= new HBox ();
205 page_label
= new Label ();
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");
219 back_button
.Clicked
+= new EventHandler (DoBackClicked
);
220 pager
.PackEnd (back_button
, false, false, 3);
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 ();
235 private void UpdatePage ()
237 back_button
.Sensitive
= root
.HitCollection
.CanPageBack
;
238 forward_button
.Sensitive
= root
.HitCollection
.CanPageForward
;
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
);
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
;
262 System
.Console
.WriteLine ("Delayed query fired");
269 private void QueueDelayedQuery ()
271 GLib
.Timeout
.Add (1000, new GLib
.TimeoutHandler (RunDelayedQuery
));
274 private void DoSearch (object o
, EventArgs args
)
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.");
292 root
.Error ("The query for <i>" + entry
.Text
+ "</i> failed with error:<br><br>" + e
);
297 //////////////////////////
299 private void Close ()
304 //////////////////////////
306 private void OnHitsAdded (Query source
, ICollection hits
)
312 private void OnHitsSubtracted (Query source
, ICollection uris
)
314 root
.Subtract (uris
);
318 private void OnFinished (QueryProxy source
)
323 private void OnCancelled (QueryProxy source
)
328 private void AttachQuery ()
330 query
.HitsAddedEvent
+= OnHitsAdded
;
331 query
.HitsSubtractedEvent
+= OnHitsSubtracted
;
332 query
.FinishedEvent
+= OnFinished
;
333 query
.CancelledEvent
+= OnCancelled
;
336 private void DetachQuery ()
339 query
.HitsAddedEvent
-= OnHitsAdded
;
340 query
.HitsSubtractedEvent
-= OnHitsSubtracted
;
341 query
.FinishedEvent
-= OnFinished
;
342 query
.CancelledEvent
-= OnCancelled
;
348 private void Search (String searchString
)
350 entry
.Text
= searchString
;
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
);