* Filters/FilterPackage.cs, Filters/FilterRPM.cs,
[beagle.git] / Renderers / Tile.cs
blob04abbb1a456fe6a36d366404a7ad43a27d1ea093
1 //
2 // Tile.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.
27 using System;
28 using System.Collections;
29 using System.IO;
30 using System.Reflection;
31 using System.Text;
33 namespace Beagle {
35 public class Tile {
37 private Hit hit;
38 private string templateName = null;
39 private string html = null;
41 public Tile (string templateName, Hit hit)
43 this.templateName = templateName;
44 this.hit = hit;
47 public Hit Hit {
48 get { return hit; }
50 public string Html {
51 get {
52 if (html == null)
53 html = BuildHtml (hit);
54 return html;
58 virtual protected string BuildHtml (Hit hit)
60 StringBuilder htmlBuilder = new StringBuilder ("");
61 StreamReader sr = DataBarn.GetText (templateName);
62 string line;
64 while ((line = sr.ReadLine ()) != null)
65 TransformLineOfHtml (line, htmlBuilder);
67 return htmlBuilder.ToString ();
71 // The ugly details of building the Html
74 private string FormatDate (DateTime dt)
76 TimeSpan age = DateTime.Now - dt;
77 // FIXME: Saner date formatting
78 if (age.TotalHours < 18)
79 return dt.ToShortTimeString ();
80 if (age.TotalDays < 180)
81 return dt.ToString ("MMM d, h:mm tt");
82 return dt.ToString ("MMM d, yyyy");
85 private string FormatFileLength (long len)
87 const long oneMb = 1024*1024;
89 if (len < 0)
90 return null;
92 if (len < 1024)
93 return String.Format ("{0} bytes", len);
95 if (len < oneMb)
96 return String.Format ("{0:0.0} kb", len/(double)1024);
98 return String.Format ("{0:0.0} Mb", len/(double)oneMb);
101 private string ExpandKey (string key)
103 // This allows you to get a @ via @@
104 if (key == "")
105 return "@";
107 string lowerKey = key.ToLower ();
109 if (lowerKey.StartsWith ("file:") && ! hit.IsFile)
110 return null;
112 switch (lowerKey) {
113 case "timestamp":
114 return FormatDate (hit.Timestamp);
115 case "uri":
116 return hit.Uri;
117 case "type":
118 return hit.Type;
119 case "mimetype":
120 return hit.MimeType;
121 case "source":
122 return hit.Source;
123 case "score":
124 return hit.Score.ToString ();
125 case "file:path":
126 return hit.Path;
127 case "file:filename":
128 return hit.FileName;
129 case "file:directoryname":
130 return hit.DirectoryName;
131 case "file:length":
132 return FormatFileLength (hit.FileInfo.Length);
133 case "file:creationtime":
134 return FormatDate (hit.FileInfo.CreationTime);
135 case "file:lastwritetime":
136 return FormatDate (hit.FileInfo.LastWriteTime);
139 string val = hit [key];
141 return val;
144 private void TransformLineOfHtml (string html, StringBuilder target)
146 StringBuilder newHtml = new StringBuilder ("");
147 int i = 0;
148 while (i < html.Length) {
149 int j = html.IndexOf ('@', i);
150 if (j == -1)
151 break;
152 int k = html.IndexOf ('@', j+1);
153 if (k == -1)
154 break;
156 newHtml.Append (html.Substring (i, j-i));
158 string key = html.Substring (j+1, k-j-1);
159 string expansion = ExpandKey (key);
160 // Drop lines w/ a failed expansion
161 if (expansion == null)
162 return;
163 newHtml.Append (expansion);
165 i = k+1;
168 if (i < html.Length)
169 newHtml.Append (html.Substring (i));
171 target.Append (newHtml.ToString ());