Show total number of results for google driver too. Helps to know if my query resulte...
[beagle.git] / search / Tiles / Utils.cs
blob51aa6f7bfc4756383e2592d5982d2f9c1a9dc89d
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;
98 public static DateTime ParseTimestamp (string timestamp)
100 DateTime dt;
101 dt = new DateTime (Int32.Parse (timestamp.Substring (0, 4)),
102 Int32.Parse (timestamp.Substring (4, 2)),
103 Int32.Parse (timestamp.Substring (6, 2)),
104 Int32.Parse (timestamp.Substring (8, 2)),
105 Int32.Parse (timestamp.Substring (10, 2)),
106 Int32.Parse (timestamp.Substring (12, 2)));
107 return dt;
110 private static DateTimeFormatInfo DateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
111 private static string ShortMonthDayPattern = DateTimeFormat.MonthDayPattern.Replace ("MMMM", "MMM");
112 private static string ShortYearMonthPattern = DateTimeFormat.YearMonthPattern.Replace ("MMMM", "MMM");
113 private static string MonthDayPattern = DateTimeFormat.MonthDayPattern;
114 private static string LongDatePattern = DateTimeFormat.LongDatePattern.Replace ("dddd, ", "").Replace ("dddd ", "").Replace (" dddd", "");
116 private static string NiceDatePattern (DateTime dt, string month_day_pattern, string year_month_pattern)
118 if (dt.Year <= 1970)
119 return "-";
121 dt = dt.ToLocalTime ().Date;
122 DateTime today = DateTime.Today;
123 TimeSpan one_day = new TimeSpan (TimeSpan.TicksPerDay);
125 if (dt == today - one_day)
126 return Catalog.GetString ("Yesterday");
127 else if (dt == today)
128 return Catalog.GetString ("Today");
129 else if (dt == today + one_day)
130 return Catalog.GetString ("Tomorrow");
132 TimeSpan span;
134 if (today > dt)
135 span = today - dt;
136 else
137 span = dt - today;
139 if (span.TotalDays < 7)
140 return dt.ToString ("dddd"); // "Tuesday"
141 else if (dt.Year == today.Year || span.TotalDays < 180)
142 return dt.ToString (month_day_pattern);
143 else
144 return dt.ToString (year_month_pattern);
147 public static string NiceShortDate (string timestamp)
149 DateTime dt;
151 try {
152 dt = ParseTimestamp (timestamp);
153 } catch {
154 return "";
157 return NiceShortDate (dt);
160 public static string NiceShortDate (DateTime dt)
162 // "Jul 4" and "Jan 2001", respectively.
163 return NiceDatePattern (dt, ShortMonthDayPattern, ShortYearMonthPattern);
166 public static string NiceLongDate (string timestamp)
168 DateTime dt;
170 try {
171 dt = ParseTimestamp (timestamp);
172 } catch {
173 return "";
176 return NiceLongDate (dt);
179 public static string NiceLongDate (DateTime dt)
181 // "July 4" and "January 7, 2001", respectively.
182 return NiceDatePattern (dt, MonthDayPattern, LongDatePattern);
185 public static string NiceVeryLongDate (string timestamp)
187 DateTime dt;
189 try {
190 dt = ParseTimestamp (timestamp);
191 } catch {
192 return "";
195 return NiceVeryLongDate (dt);
198 public static string NiceVeryLongDate (DateTime dt)
200 if (dt.Year <= 1970)
201 return "-";
203 dt = dt.ToLocalTime ().Date;
204 DateTime today = DateTime.Today;
205 TimeSpan one_day = new TimeSpan (TimeSpan.TicksPerDay);
207 if (dt == today - one_day)
208 return Catalog.GetString ("Yesterday");
209 else if (dt == today)
210 return Catalog.GetString ("Today");
211 else if (dt == today + one_day)
212 return Catalog.GetString ("Tomorrow");
214 TimeSpan span;
215 bool future;
217 if (today > dt) {
218 span = today - dt;
219 future = false;
220 } else {
221 span = dt - today;
222 future = true;
225 if (span.TotalDays < 7)
226 return dt.ToString ("dddd"); // "Tuesday"
227 else if (span.TotalDays < 30 && ! future)
228 return String.Format (Catalog.GetPluralString ("{0} week ago", "{0} weeks ago", span.Days / 7) + " ({1:MMMM d, yyyy})", span.Days / 7, dt);
229 else if (span.TotalDays < 30 && future)
230 return String.Format (Catalog.GetPluralString ("In {0} week", "In {0} weeks", span.Days / 7) + " {1:MMMM d, yyyy})", span.Days / 7, dt);
231 else if (span.TotalDays < 365 + 180 && ! future) // Let's say a year and a half to stop saying months
232 return String.Format (Catalog.GetPluralString ("{0} month ago", "{0} months ago", span.Days / 30) + " ({1:MMMM d, yyyy})", span.Days / 30, dt);
233 else if (span.TotalDays < 365 + 180 && future)
234 return String.Format (Catalog.GetPluralString ("In {0} month", "In {0} months", span.Days / 30) + " ({1:MMMM d, yyyy})", span.Days / 30, dt);
235 else if (! future)
236 return String.Format (Catalog.GetPluralString ("{0} year ago", "{0} years ago", span.Days / 365) + " ({1:MMMM d, yyyy})", span.Days / 365, dt);
237 else
238 return String.Format (Catalog.GetPluralString ("In {0} year", "In {0} years", span.Days / 365) + " ({1:MMMM d, yyyy})", span.Days / 365, dt);