cvsimport
[beagle.git] / search / Tiles / Utils.cs
blob8dbaaa4aebe054db67a969748c9470b128c2e3c9
1 using System;
2 using System.Globalization;
3 using Mono.Unix;
5 namespace Search.Tiles {
7 public struct TileGroupInfo {
8 public TileGroup Group;
9 public string Name;
10 public int Rows;
12 public TileGroupInfo (TileGroup group, string name, int rows)
14 Group = group;
15 Name = name;
16 Rows = rows;
20 public static class Utils {
22 public static TileGroupInfo[] GroupInfo = new TileGroupInfo[] {
23 new TileGroupInfo (TileGroup.Application,
24 Catalog.GetString ("Applications"), 1),
25 new TileGroupInfo (TileGroup.Contact,
26 Catalog.GetString ("Contacts"), 2),
27 new TileGroupInfo (TileGroup.Calendar,
28 Catalog.GetString ("Calendar Events"), 2),
29 new TileGroupInfo (TileGroup.Folder,
30 Catalog.GetString ("Folders"), 2),
31 new TileGroupInfo (TileGroup.Image,
32 Catalog.GetString ("Images"), 2),
33 new TileGroupInfo (TileGroup.Audio,
34 Catalog.GetString ("Audio"), 2),
35 new TileGroupInfo (TileGroup.Video,
36 Catalog.GetString ("Video"), 2),
37 new TileGroupInfo (TileGroup.Documents,
38 Catalog.GetString ("Documents"), 2),
39 new TileGroupInfo (TileGroup.Conversations,
40 Catalog.GetString ("Conversations"), 5),
41 new TileGroupInfo (TileGroup.Website,
42 Catalog.GetString ("Websites"), 2),
43 new TileGroupInfo (TileGroup.Feed,
44 Catalog.GetString ("News Feeds"), 2),
45 new TileGroupInfo (TileGroup.Archive,
46 Catalog.GetString ("Archives"), 2),
49 public static ScopeType TileGroupToScopeType (TileGroup group)
51 switch (group) {
53 case TileGroup.Application:
54 return ScopeType.Applications;
56 case TileGroup.Calendar:
57 return ScopeType.Calendar;
59 case TileGroup.Contact:
60 return ScopeType.Contacts;
62 // This TileGroup exists but does not seem to be used
63 // case TileGroup.Folder:
64 case TileGroup.Documents:
65 return ScopeType.Documents;
67 case TileGroup.Conversations:
68 return ScopeType.Conversations;
70 case TileGroup.Image:
71 return ScopeType.Images;
73 case TileGroup.Audio:
74 return ScopeType.Media;
76 case TileGroup.Video:
77 return ScopeType.Media;
79 case TileGroup.Folder:
80 return ScopeType.Folders;
82 case TileGroup.Website:
83 return ScopeType.Websites;
85 case TileGroup.Feed:
86 return ScopeType.Feeds;
88 case TileGroup.Archive:
89 return ScopeType.Archives;
93 Console.WriteLine ("Error: Could not find ScopeType for Group: {0}",group);
94 return ScopeType.Nothing;
97 public static string GetFirstPropertyOfParent (Beagle.Hit hit, string prop)
99 if (hit.ParentUri == null)
100 return hit.GetFirstProperty (prop);
101 else
102 return hit.GetFirstProperty ("parent:" + prop);
105 public static DateTime ParseTimestamp (string timestamp)
107 DateTime dt;
108 dt = new DateTime (Int32.Parse (timestamp.Substring (0, 4)),
109 Int32.Parse (timestamp.Substring (4, 2)),
110 Int32.Parse (timestamp.Substring (6, 2)),
111 Int32.Parse (timestamp.Substring (8, 2)),
112 Int32.Parse (timestamp.Substring (10, 2)),
113 Int32.Parse (timestamp.Substring (12, 2)));
114 return dt;
117 private static DateTimeFormatInfo DateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
118 private static string ShortMonthDayPattern = DateTimeFormat.MonthDayPattern.Replace ("MMMM", "MMM");
119 private static string ShortYearMonthPattern = DateTimeFormat.YearMonthPattern.Replace ("MMMM", "MMM");
120 private static string MonthDayPattern = DateTimeFormat.MonthDayPattern;
121 private static string LongDatePattern = DateTimeFormat.LongDatePattern.Replace ("dddd, ", "").Replace ("dddd ", "").Replace (" dddd", "");
123 private static string NiceDatePattern (DateTime dt, string month_day_pattern, string year_month_pattern)
125 if (dt.Year <= 1970)
126 return "-";
128 dt = dt.ToLocalTime ().Date;
129 DateTime today = DateTime.Today;
130 TimeSpan one_day = new TimeSpan (TimeSpan.TicksPerDay);
132 if (dt == today - one_day)
133 return Catalog.GetString ("Yesterday");
134 else if (dt == today)
135 return Catalog.GetString ("Today");
136 else if (dt == today + one_day)
137 return Catalog.GetString ("Tomorrow");
139 TimeSpan span;
141 if (today > dt)
142 span = today - dt;
143 else
144 span = dt - today;
146 if (span.TotalDays < 7)
147 return dt.ToString ("dddd"); // "Tuesday"
148 else if (dt.Year == today.Year || span.TotalDays < 180)
149 return dt.ToString (month_day_pattern);
150 else
151 return dt.ToString (year_month_pattern);
154 public static string NiceShortDate (string timestamp)
156 DateTime dt;
158 try {
159 dt = ParseTimestamp (timestamp);
160 } catch {
161 return "";
164 return NiceShortDate (dt);
167 public static string NiceShortDate (DateTime dt)
169 // "Jul 4" and "Jan 2001", respectively.
170 return NiceDatePattern (dt, ShortMonthDayPattern, ShortYearMonthPattern);
173 public static string NiceLongDate (string timestamp)
175 DateTime dt;
177 try {
178 dt = ParseTimestamp (timestamp);
179 } catch {
180 return "";
183 return NiceLongDate (dt);
186 public static string NiceLongDate (DateTime dt)
188 // "July 4" and "January 7, 2001", respectively.
189 return NiceDatePattern (dt, MonthDayPattern, LongDatePattern);
192 public static string NiceVeryLongDate (string timestamp)
194 DateTime dt;
196 try {
197 dt = ParseTimestamp (timestamp);
198 } catch {
199 return "";
202 return NiceVeryLongDate (dt);
205 public static string NiceVeryLongDate (DateTime dt)
207 if (dt.Year <= 1970)
208 return "-";
210 dt = dt.ToLocalTime ().Date;
211 DateTime today = DateTime.Today;
212 TimeSpan one_day = new TimeSpan (TimeSpan.TicksPerDay);
214 if (dt == today - one_day)
215 return Catalog.GetString ("Yesterday");
216 else if (dt == today)
217 return Catalog.GetString ("Today");
218 else if (dt == today + one_day)
219 return Catalog.GetString ("Tomorrow");
221 TimeSpan span;
222 bool future;
224 if (today > dt) {
225 span = today - dt;
226 future = false;
227 } else {
228 span = dt - today;
229 future = true;
232 if (span.TotalDays < 7)
233 return dt.ToString ("dddd"); // "Tuesday"
234 else if (span.TotalDays < 30 && ! future)
235 return String.Format (Catalog.GetPluralString ("{0} week ago", "{0} weeks ago", span.Days / 7) + " ({1:MMMM d, yyyy})", span.Days / 7, dt);
236 else if (span.TotalDays < 30 && future)
237 return String.Format (Catalog.GetPluralString ("In {0} week", "In {0} weeks", span.Days / 7) + " {1:MMMM d, yyyy})", span.Days / 7, dt);
238 else if (span.TotalDays < 365 + 180 && ! future) // Let's say a year and a half to stop saying months
239 return String.Format (Catalog.GetPluralString ("{0} month ago", "{0} months ago", span.Days / 30) + " ({1:MMMM d, yyyy})", span.Days / 30, dt);
240 else if (span.TotalDays < 365 + 180 && future)
241 return String.Format (Catalog.GetPluralString ("In {0} month", "In {0} months", span.Days / 30) + " ({1:MMMM d, yyyy})", span.Days / 30, dt);
242 else if (! future)
243 return String.Format (Catalog.GetPluralString ("{0} year ago", "{0} years ago", span.Days / 365) + " ({1:MMMM d, yyyy})", span.Days / 365, dt);
244 else
245 return String.Format (Catalog.GetPluralString ("In {0} year", "In {0} years", span.Days / 365) + " ({1:MMMM d, yyyy})", span.Days / 365, dt);