* Filters/FilterPackage.cs, Filters/FilterRPM.cs,
[beagle.git] / Renderers / FileHitRenderer.cs
blobfc04264274e5a813491b1fb092909775773d62cf
1 //
2 // GNOME Dashboard
3 //
4 // FileMatchRenderer.cs: Knows how to render File matches.
5 //
6 // Author:
7 // Kevin Godby <godbyk@yahoo.com>
8 //
11 // Copyright (C) 2003, 2004 Kevin Godby
12 // Copyright (C) 2004 Novell, Inc.
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to deal
16 // in the Software without restriction, including without limitation the rights
17 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 // copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
21 // The above copyright notice and this permission notice shall be included in all
22 // copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 // SOFTWARE.
34 using System;
35 using System.Collections;
36 using System.Xml;
37 using System.IO;
39 using BU = Beagle.Util;
41 //[assembly:Dashboard.MatchRendererFactory ("Dashboard.FileMatchRenderer")]
43 namespace Beagle {
45 public class FileHitRenderer : HitRendererHtml {
47 public FileHitRenderer ()
49 type = "File";
52 protected override string HitsToHtml (ArrayList hits)
54 StringWriter sw = new StringWriter ();
55 XmlWriter xw = new XmlTextWriter (sw);
57 xw.WriteStartElement ("div"); // Start the File results block
59 xw.WriteStartElement ("table");
60 xw.WriteAttributeString ("border", "0");
61 xw.WriteAttributeString ("width", "100%");
62 xw.WriteStartElement ("tr");
63 xw.WriteStartElement ("td");
64 xw.WriteAttributeString ("bgcolor", "#fffa6e");
65 xw.WriteAttributeString ("nowrap", "1");
66 xw.WriteStartElement ("font");
67 xw.WriteAttributeString ("size", "+2");
68 xw.WriteString ("Your Files");
69 xw.WriteEndElement (); // font
70 xw.WriteEndElement (); // td
71 xw.WriteEndElement (); // tr
72 xw.WriteEndElement (); // table
74 xw.WriteStartElement ("table"); // Start the results table
75 xw.WriteAttributeString ("border", "0");
76 xw.WriteAttributeString ("width", "100%");
77 xw.WriteAttributeString ("cellpadding", "0");
78 xw.WriteAttributeString ("cellspacing", "0");
80 // Sort results by score
81 IComparer filescorecomparer = new FileScoreComparer ();
82 hits.Sort (filescorecomparer);
84 bool color_band = true;
85 foreach (Hit hit in hits) {
86 HTMLRenderSingleFile (hit, color_band, xw);
87 color_band = !color_band;
90 xw.WriteEndElement (); // End results table
91 xw.WriteEndElement (); // End File results block
93 xw.Close ();
95 //Console.WriteLine ("-- File Renderer ----------------------------------------------------");
96 //Console.WriteLine (sw.ToString ());
97 //Console.WriteLine ("---------------------------------------------------------------------\n\n");
99 return sw.ToString ();
102 private void HTMLRenderSingleFile (Hit hit, bool color_band, XmlWriter xw)
104 if (! hit.IsFile)
105 return;
107 string Text = hit.FileName;
108 string Icon = BU.GnomeIconLookup.LookupMimeIcon (hit.MimeType,
109 (Gtk.IconSize) 48);
110 string Score = Convert.ToString (hit.Score);
111 if (Score == null || Score == "")
112 Score = "n/a";
114 // DEBUG
115 //Console.WriteLine ("File name: {0}", file.FullName);
116 //Console.WriteLine ("Creation time: {0}", file.CreationTime);
117 //Console.WriteLine ("Last Access time: {0}", file.LastAccessTime);
118 //Console.WriteLine ("Last Write Time: {0}", file.LastWriteTime);
119 //Console.WriteLine ("Size: {0}", file.Length);
121 FileInfo info = hit.FileInfo;
122 string LastModifiedDate = info.LastWriteTime.ToLongDateString ();
123 string LastAccessedDate = info.LastAccessTime.ToLongDateString ();
125 xw.WriteStartElement ("tr");
126 if (color_band) // highlight every other row
127 xw.WriteAttributeString ("bgcolor", "#eeeeee"); //originally #f6f2f6
128 xw.WriteStartElement ("td");
129 xw.WriteAttributeString ("valign", "top");
130 xw.WriteAttributeString ("align", "left");
132 // Show the file's icon (and make it a link)
133 xw.WriteStartElement ("a"); // link
135 String href = hit.Uri;
136 if (hit.MimeType != null)
137 href += " " + hit.MimeType;
138 xw.WriteAttributeString ("href", href);
140 xw.WriteStartElement ("img"); // icon
141 xw.WriteAttributeString ("src", Icon);
142 xw.WriteAttributeString ("border", "0");
143 xw.WriteEndElement (); // img
144 xw.WriteEndElement (); // a
145 xw.WriteEndElement (); // td
147 xw.WriteStartElement ("td");
148 xw.WriteAttributeString ("valign", "top");
149 xw.WriteAttributeString ("align", "left");
150 xw.WriteAttributeString ("width", "100%");
151 xw.WriteStartElement ("font"); // Make the font smaller to fit window width
153 // Print the filename (w/ hyperlink)
154 xw.WriteStartElement ("a");
155 xw.WriteAttributeString ("href", href);
156 xw.WriteStartElement ("font");
157 xw.WriteAttributeString ("size", "+1");
158 xw.WriteString (Text);
159 xw.WriteEndElement (); // font
160 xw.WriteEndElement (); // a href
161 xw.WriteStartElement ("font");
162 xw.WriteAttributeString ("color", "#666666");
163 xw.WriteString (" (" + Score + ")");
164 xw.WriteEndElement (); // font
166 xw.WriteStartElement ("br");
167 xw.WriteEndElement (); // br
169 // Print 'Last modified: date'
170 xw.WriteStartElement ("font");
171 xw.WriteAttributeString ("color", "#666666");
172 xw.WriteString ("Last modified " + LastModifiedDate);
173 xw.WriteEndElement (); // font
175 xw.WriteStartElement ("br");
176 xw.WriteEndElement (); // br
178 // FIXME:
179 // Last accessed isn't very useful, since it will just
180 // tell us the last time the file crawler ran, right?
181 // We really want to know the last time the *user* accessed
182 // the file.
184 // Print 'Last accessed: date'
185 xw.WriteStartElement ("font");
186 xw.WriteAttributeString ("color", "#666666");
187 xw.WriteString ("Last accessed " + LastAccessedDate);
188 xw.WriteEndElement (); // font
190 xw.WriteEndElement (); // font
191 xw.WriteEndElement (); // td
192 xw.WriteEndElement (); // tr
196 public enum SortDirection {
197 Ascending,
198 Descending
201 public class FileScoreComparer : IComparer {
203 // Reverse sort -- highest score first
204 private SortDirection m_direction = SortDirection.Descending;
206 int IComparer.Compare (Object x, Object y) {
208 Hit MatchX = (Hit) x;
209 Hit MatchY = (Hit) y;
211 float ScoreX;
212 float ScoreY;
214 // These try..catch blocks are here in case we receive
215 // a match without a Score property. (Assume Score = 0)
216 try {
217 ScoreX = Single.Parse (Convert.ToString (MatchX ["Score"]));
218 } catch {
219 ScoreX = 0;
222 try {
223 ScoreY = Single.Parse (Convert.ToString (MatchY ["Score"]));
224 } catch {
225 ScoreY = 0;
228 if (MatchX == null && MatchY == null) {
229 return 0;
230 } else if (MatchX == null && MatchY != null) {
231 return (this.m_direction == SortDirection.Ascending) ? -1 : 1;
232 } else if (MatchX != null && MatchY == null) {
233 return (this.m_direction == SortDirection.Ascending) ? 1 : -1;
234 } else {
235 return (this.m_direction == SortDirection.Ascending) ?
236 ScoreX.CompareTo (ScoreY) :
237 ScoreY.CompareTo (ScoreX);
238 } // end if
240 } // end IComparer.Compare
242 } // end class FileScoreComparer