Remove debug statements from KCal backends.
[beagle.git] / Renderers / WebLinkHitRenderer.cs
blob866ae5632c2c4d73e2f73322862c75a30d6a29e4
1 //
2 // GNOME Dashboard
3 //
4 // WebLinkMatchRenderer.cs: Knows how to render WebLink matches.
5 //
6 // Author:
7 // Nat Friedman <nat@nat.org>
8 // Kevin Godby <godbyk@yahoo.com>
9 //
12 // Permission is hereby granted, free of charge, to any person obtaining a copy
13 // of this software and associated documentation files (the "Software"), to deal
14 // in the Software without restriction, including without limitation the rights
15 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 // copies of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
19 // The above copyright notice and this permission notice shall be included in all
20 // copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 // SOFTWARE.
32 using Gdk;
33 using System;
34 using System.IO;
35 using System.Xml;
36 using System.Collections;
37 using System.Reflection;
39 //[assembly:Dashboard.MatchRendererFactory ("Dashboard.WebLinkMatchRenderer")]
41 namespace Beagle {
43 public class WebLinkHitRenderer : HitRendererHtml {
45 public WebLinkHitRenderer ()
47 type = "WebLink";
50 protected override string HitsToHtml (ArrayList hits)
52 StringWriter sw = new StringWriter ();
53 XmlWriter xw = new XmlTextWriter (sw);
55 xw.WriteStartElement ("div"); // start WebLink results block
57 xw.WriteStartElement ("table"); // WebLink header
58 xw.WriteAttributeString ("border", "0");
59 xw.WriteAttributeString ("width", "100%");
60 xw.WriteStartElement ("tr");
61 xw.WriteStartElement ("td");
62 xw.WriteAttributeString ("bgcolor", "#fffa6e");
63 xw.WriteStartElement ("font");
64 xw.WriteAttributeString ("size", "+2");
65 xw.WriteString ("Web Sites");
66 xw.WriteEndElement (); // font
67 xw.WriteEndElement (); // td
68 xw.WriteEndElement (); // tr
69 xw.WriteEndElement (); // table
71 xw.WriteStartElement ("table"); // Results table
72 xw.WriteAttributeString ("border", "0");
73 xw.WriteAttributeString ("width", "100%");
74 xw.WriteAttributeString ("cellpadding", "0");
75 xw.WriteAttributeString ("cellspacing", "0");
77 // Sort results by score
78 IComparer weblinkscorecomparer = new WebLinkScoreComparer ();
79 hits.Sort (weblinkscorecomparer);
81 bool color_band = true;
82 foreach (Hit hit in hits) {
83 HTMLRenderSingleWebLink (hit, color_band, xw);
84 color_band = !color_band;
87 xw.WriteEndElement (); // results table (table)
88 xw.WriteEndElement (); // end WebLink block (div)
90 xw.Close ();
92 //Console.WriteLine ("-- WebLink Renderer -----------------------------------------------");
93 //Console.WriteLine (sw.ToString ());
94 //Console.WriteLine ("---------------------------------------------------------------------\n\n");
96 return sw.ToString ();
99 private void HTMLRenderSingleWebLink (Hit hit, bool color_band, XmlWriter xw)
101 string icon = CompositeEmblemIcon (Favicons.GetIconPath (hit.Uri));
103 string Title = (string)hit ["Title"];
104 string Score = Convert.ToString (hit.Score);
105 if (Score == null || Score == "")
106 Score = "n/a";
108 xw.WriteStartElement ("tr");
110 xw.WriteStartElement ("td");
111 xw.WriteAttributeString ("valign", "center");
113 xw.WriteStartElement ("a");
114 xw.WriteAttributeString ("href", hit.Uri);
116 xw.WriteStartElement ("img");
117 xw.WriteAttributeString ("src", icon);
118 xw.WriteAttributeString ("border", "0");
119 xw.WriteEndElement (); // img
121 xw.WriteEndElement (); // a href
123 xw.WriteEndElement (); // td
125 xw.WriteStartElement ("td");
126 xw.WriteRaw ("&nbsp;");
127 xw.WriteEndElement (); // td
129 xw.WriteStartElement ("td");
130 xw.WriteAttributeString ("valign", "top");
132 xw.WriteRaw (Title);
134 xw.WriteStartElement ("font");
135 xw.WriteAttributeString ("size", "-2");
136 xw.WriteAttributeString ("color", "#666666");
137 xw.WriteString (" (score=" + Score + ")");
138 xw.WriteEndElement (); // font
140 xw.WriteStartElement ("br");
141 xw.WriteEndElement (); // br
143 xw.WriteStartElement ("font");
144 xw.WriteAttributeString ("size", "-1");
145 xw.WriteAttributeString ("color", "#666666");
146 xw.WriteStartElement ("a");
147 xw.WriteAttributeString ("href", hit.Uri);
148 xw.WriteAttributeString ("style", "text-decoration: none;");
149 xw.WriteString (hit.Uri);
150 xw.WriteEndElement (); // a
151 xw.WriteEndElement (); // font
153 xw.WriteEndElement (); // td
155 xw.WriteEndElement (); // tr
159 private Stream GetImage (string name)
161 Assembly assembly = System.Reflection.Assembly.GetCallingAssembly ();
162 System.IO.Stream s = assembly.GetManifestResourceStream (name);
163 if (s == null)
164 Console.WriteLine ("Can't find resource '{0}'", name);
165 return s;
168 private string CompositeEmblemIcon (String emblem)
170 if (emblem == null)
171 return "internal:bookmark.png";
173 if (! File.Exists (emblem)) {
174 return "internal:bookmark.png";
177 // Composite an icon...
178 Gdk.Pixbuf icon = new Pixbuf (emblem);
179 Gdk.Pixbuf bookmark =
180 new Pixbuf (GetImage ("bookmark.png"));
181 Gdk.Pixbuf white =
182 new Pixbuf (GetImage ("white.png"));
184 white.Composite (bookmark,
185 0, 0, // dest x,y
186 48, 20, // height,width
187 0, 0, // offset x,y
188 1, 1, // scaling x,y
189 Gdk.InterpType.Bilinear,
190 127); // Alpha
192 // I just want to make the icon be 16x16.
193 // This does it for me!
194 Gdk.Pixbuf small_icon = icon.ScaleSimple (16, 16, // x,y
195 Gdk.InterpType.Bilinear);
197 small_icon.Composite(bookmark,
198 0, 0, // dest x,y
199 48, 18, // height,width
200 31, 2, // offset x,y
201 1, 1, // scaling x,y
202 Gdk.InterpType.Bilinear,
203 255); // Alpha
205 emblem = System.IO.Path.GetFileName (emblem);
206 emblem = PathFinder.AppDataFileName ("transient:WebLinkHitRenderer",
207 "emblem-" + emblem);
208 bookmark.Savev (emblem, "png", null, null);
209 return emblem;
212 public enum SortDirection {
213 Ascending,
214 Descending
217 public class WebLinkScoreComparer : IComparer {
219 // Reverse sort -- highest score first
220 private SortDirection m_direction = SortDirection.Descending;
222 int IComparer.Compare (Object x, Object y) {
224 Hit MatchX = (Hit) x;
225 Hit MatchY = (Hit) y;
227 float ScoreX;
228 float ScoreY;
230 // These try..catch blocks are here in case we receive
231 // a match without a Score property. (Assume Score = 0)
232 try {
233 ScoreX = Single.Parse (Convert.ToString (MatchX ["Score"]));
234 } catch {
235 ScoreX = 0;
238 try {
239 ScoreY = Single.Parse (Convert.ToString (MatchY ["Score"]));
240 } catch {
241 ScoreY = 0;
244 if (MatchX == null && MatchY == null) {
245 return 0;
246 } else if (MatchX == null && MatchY != null) {
247 return (this.m_direction == SortDirection.Ascending) ? -1 : 1;
248 } else if (MatchX != null && MatchY == null) {
249 return (this.m_direction == SortDirection.Ascending) ? 1 : -1;
250 } else {
251 return (this.m_direction == SortDirection.Ascending) ?
252 ScoreX.CompareTo (ScoreY) :
253 ScoreY.CompareTo (ScoreX);
254 } // end if
255 } // end IComparer.Compare
256 } // end public class WebLinkScoreComparer