4 // Copyright (C) 2004 Novell, Inc.
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
28 using System
.Collections
;
30 using System
.Reflection
;
38 private string templateName
= null;
39 private string html
= null;
41 public Tile (string templateName
, Hit hit
)
43 this.templateName
= templateName
;
53 html
= BuildHtml (hit
);
58 virtual protected string BuildHtml (Hit hit
)
60 StringBuilder htmlBuilder
= new StringBuilder ("");
61 StreamReader sr
= DataBarn
.GetText (templateName
);
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;
93 return String
.Format ("{0} bytes", len
);
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 @@
107 string lowerKey
= key
.ToLower ();
109 if (lowerKey
.StartsWith ("file:") && ! hit
.IsFile
)
114 return FormatDate (hit
.Timestamp
);
124 return hit
.Score
.ToString ();
127 case "file:filename":
129 case "file:directoryname":
130 return hit
.DirectoryName
;
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
];
144 private void TransformLineOfHtml (string html
, StringBuilder target
)
146 StringBuilder newHtml
= new StringBuilder ("");
148 while (i
< html
.Length
) {
149 int j
= html
.IndexOf ('@', i
);
152 int k
= html
.IndexOf ('@', j
+1);
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)
163 newHtml
.Append (expansion
);
169 newHtml
.Append (html
.Substring (i
));
171 target
.Append (newHtml
.ToString ());