4 // WebLinkMatchRenderer.cs: Knows how to render WebLink matches.
7 // Nat Friedman <nat@nat.org>
8 // Kevin Godby <godbyk@yahoo.com>
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
36 using System
.Collections
;
37 using System
.Reflection
;
39 //[assembly:Dashboard.MatchRendererFactory ("Dashboard.WebLinkMatchRenderer")]
43 public class WebLinkHitRenderer
: HitRendererHtml
{
45 public WebLinkHitRenderer ()
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)
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
== "")
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 (" ");
127 xw
.WriteEndElement (); // td
129 xw
.WriteStartElement ("td");
130 xw
.WriteAttributeString ("valign", "top");
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
);
164 Console
.WriteLine ("Can't find resource '{0}'", name
);
168 private string CompositeEmblemIcon (String emblem
)
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"));
182 new Pixbuf (GetImage ("white.png"));
184 white
.Composite (bookmark
,
186 48, 20, // height,width
189 Gdk
.InterpType
.Bilinear
,
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
,
199 48, 18, // height,width
202 Gdk
.InterpType
.Bilinear
,
205 emblem
= System
.IO
.Path
.GetFileName (emblem
);
206 emblem
= PathFinder
.AppDataFileName ("transient:WebLinkHitRenderer",
208 bookmark
.Savev (emblem
, "png", null, null);
212 public enum SortDirection
{
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
;
230 // These try..catch blocks are here in case we receive
231 // a match without a Score property. (Assume Score = 0)
233 ScoreX
= Single
.Parse (Convert
.ToString (MatchX
["Score"]));
239 ScoreY
= Single
.Parse (Convert
.ToString (MatchY
["Score"]));
244 if (MatchX
== null && MatchY
== null) {
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;
251 return (this.m_direction
== SortDirection
.Ascending
) ?
252 ScoreX
.CompareTo (ScoreY
) :
253 ScoreY
.CompareTo (ScoreX
);
255 } // end IComparer.Compare
256 } // end public class WebLinkScoreComparer