Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / Tiles / Template.cs
blobadbca1d0924ead359d2cc4430a35e3ef768f89b2
1 //
2 // Template.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all 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
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System.IO;
28 using System;
29 using System.Collections;
30 using System.Reflection;
31 using BU = Beagle.Util;
33 namespace Beagle.Tile {
35 public class Template {
37 private ArrayList lines = new ArrayList ();
39 private bool dirty = true;
40 private string string_html;
42 private Hashtable values = new Hashtable ();
44 private void Setup (Stream stream)
46 StreamReader sr = new StreamReader (stream);
47 string line;
48 while ((line = sr.ReadLine ()) != null)
49 lines.Add (line);
50 dirty = true;
53 public Template (string template_resource)
55 // We look for the resource in the assembly that contains
56 // the type's definition.
57 Assembly assembly = Assembly.GetAssembly (this.GetType ());
58 Stream stream = assembly.GetManifestResourceStream (template_resource);
60 if (stream == null)
61 throw new Exception (String.Format ("No such resource: {0}", template_resource));
62 Setup (stream);
65 public Template (Stream stream)
67 Setup (stream);
70 public void AddProperties (ICollection properties)
72 foreach (Property prop in properties)
73 this.values [prop.Key] = prop.Value;
74 dirty = true;
77 public void AddHit (Hit hit)
79 string uri = hit.Uri.ToString ();
81 values["Uri"] = uri;
83 if (uri.Length > 40)
84 uri = uri.Substring (0, 40) + "...";
85 values["DisplayUri"] = uri;
87 values["MimeType"] = hit.MimeType;
88 values["Source"] = hit.Source;
89 values["Score"] = hit.Score.ToString ();
90 values["ScoreRaw"] = hit.ScoreRaw.ToString ();
91 values["ScoreMultiplier"] = hit.ScoreMultiplier.ToString ();
92 values["Timestamp"] = BU.StringFu.DateTimeToString (hit.Timestamp);
93 values["Path"] = hit.Path;
94 values["FileName"] = hit.FileName;
95 values["DirectoryName"] = hit.DirectoryName;
96 if (hit.FileInfo != null)
97 values["FolderName"] = hit.FileInfo.Directory.Name;
98 else if (hit.DirectoryInfo != null)
99 values["FolderName"] = hit.DirectoryInfo.Parent.Name;
100 AddProperties (hit.Properties);
103 public IDictionary Values {
104 get { return values; }
107 public string this [string key] {
108 get { return (string) values[key]; }
109 set {
110 if (value == null) {
111 if (values.Contains (key))
112 values.Remove (key);
113 return;
115 values[key] = value as string;
116 dirty = true;
120 private string GetValue (string key)
122 int pos;
123 string formatter;
124 string subkey;
126 pos = key.IndexOf ('%');
128 if (pos >= 0) {
129 formatter = key.Substring (0, pos);
130 subkey = key.Substring (pos + 1);
131 } else {
132 formatter = null;
133 subkey = key;
136 switch (formatter) {
137 case "" :
138 if (values.Contains (subkey))
139 return (string)values[subkey];
140 else
141 return null;
142 case "image" :
143 return Images.GetHtmlSource (subkey, "image/png"); // FIXME: other mime types
144 case "stock" :
145 string stockid;
146 int size;
147 pos = subkey.IndexOf ('|');
148 if (pos >= 0) {
149 size = int.Parse (subkey.Substring (0, pos));
150 stockid = subkey.Substring (pos + 1);
151 } else {
152 size = 48;
153 stockid = subkey;
156 return Images.GetHtmlSourceForStock (stockid,
157 size);
158 case "date" :
159 try {
160 return BU.StringFu.DateTimeToPrettyString (BU.StringFu.StringToDateTime ((string)values [subkey]));
161 } catch {
162 return (string)values[subkey];
164 case "text" :
165 return Mono.Posix.Catalog.GetString (subkey); // I18N
166 default :
167 if (values.Contains (key))
168 return BU.StringFu.EscapeStringForHtml ((string)values[subkey], false);
169 else
170 return null;
174 private string Build ()
176 StringWriter writer = new StringWriter ();
177 foreach (string line in lines) {
178 string new_line = "";
179 if (line.Length < 1) {
180 writer.WriteLine ("");
181 continue;
184 string [] fragments = line.Split ('@');
185 for (int i = 0; i < fragments.Length; i++) {
186 if (i % 2 == 1) {
187 string value = GetValue (fragments[i]);
188 if (value == null) {
189 new_line = "";
190 break;
192 new_line = new_line + value;
193 } else {
194 new_line = new_line + fragments[i];
197 if (new_line != "")
198 writer.Write (new_line + "\n");
201 return writer.ToString ();
204 public override string ToString ()
206 if (dirty) {
207 string_html = Build ();
208 dirty = false;
211 return string_html;