3
* GNOME Do is the legal property of its developers. Please refer to the
4
* COPYRIGHT file distributed with this
7
* This program is free software: you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation, either version 3 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24
using System.Threading;
25
using System.Collections.Generic;
27
using Google.GData.Client;
28
using Google.GData.Calendar;
29
using Google.GData.Extensions;
38
const string FeedUri = "http://www.google.com/calendar/feeds/default";
39
private static string gAppName = "alexLauni-gnomeDoGCalPlugin-1.2";
40
private static string username, password;
41
private static CalendarService service;
42
private static List<IItem> calendars;
43
private static List<IItem> events;
44
private static List<string> notified;
45
const int CacheSeconds = 500; //cache contacts
49
System.Net.ServicePointManager.CertificatePolicy = new CertHandler ();
50
calendars = new List<IItem> ();
51
events = new List<IItem> ();
52
notified = new List<string> ();
53
GLib.Timeout.Add (CacheSeconds * 1000, delegate { ClearList (calendars); return true; });
54
GLib.Timeout.Add (CacheSeconds * 1000, delegate { ClearList (events); return true; });
55
Configuration.GetAccountData (out username, out password,
56
typeof (Configuration));
58
Connect (username, password);
59
} catch (Exception e) {
60
Console.Error.WriteLine (e.Message);
64
public static string GAppName {
65
get { return gAppName; }
68
public static List<IItem> Calendars {
69
get { return calendars; }
72
public static List<IItem> Events {
73
get { return events; }
76
private static void Connect (string username, string password)
79
service = new CalendarService (gAppName);
80
service.setUserCredentials (username, password);
81
} catch (Exception e) {
82
Console.Error.WriteLine (e.Message);
86
public static void UpdateCalendars ()
88
if (!Monitor.TryEnter (calendars)) return;
89
if (calendars.Count > 0) {
90
//Console.Error.WriteLine("GCal: Cache not cleared, returning");
91
Monitor.Exit (calendars);
94
AtomFeed cal_list = QueryCalendars ();
95
if (cal_list == null) return;
97
for (int i = 0; i < cal_list.Entries.Count; i++) {
98
string cal_url = cal_list.Entries[i].Id.Uri.ToString ();
99
calendars.Add (new GCalendarItem (cal_list.Entries[i].Title.Text,
100
cal_url.Replace ("/default","") + "/private/full"));
102
Monitor.Exit (calendars);
103
//Console.Error.WriteLine("GCal: Calendar list set");
106
public static AtomFeed QueryCalendars ()
108
FeedQuery query = new FeedQuery ();
109
query.Uri = new Uri (FeedUri);
111
return service.Query (query);
112
} catch (Exception e) {
113
Console.Error.WriteLine (e.Message);
118
public static EventFeed GetEvents (string calUrl)
120
return GetEvents (calUrl, DateTime.Now, DateTime.Now.AddMonths(1));
123
public static EventFeed GetEvents (string calUrl, DateTime startTime,
126
EventQuery query = new EventQuery (calUrl);
127
query.StartTime = startTime;
128
query.EndTime = endTime;
129
query.SortOrder = CalendarSortOrder.ascending;
132
events = service.Query (query) as EventFeed;
133
} catch (WebException e) {
135
Console.Error.WriteLine (e.Message);
140
public static void UpdateEvents ()
142
EventFeed eventsFeed;
143
string eventUrl, eventDesc, start;
147
foreach (GCalendarItem cal in calendars) {
149
eventsFeed = GCal.GetEvents (cal.URL);
150
} catch (Exception e) {
151
Console.Error.WriteLine (e.Message);
154
foreach (EventEntry entry in eventsFeed.Entries) {
155
eventUrl = entry.AlternateUri.Content;
156
eventDesc = entry.Content.Content;
157
if (entry.Times.Count > 0) {
158
start = entry.Times [0].StartTime.ToString ();
159
start = start.Substring (0,start.IndexOf (' '));
160
eventDesc = start + " - " + eventDesc;
162
SetReminder (entry.Title.Text, eventDesc,
163
entry.Times [0].StartTime, entry.EventId);
164
Console.Error.WriteLine ("{0} Has notification", entry.EventId);
166
Console.Error.WriteLine (entry.Title.Text + "has no notifications");
169
events.Add (new GCalendarEventItem (entry.Title.Text, eventUrl,
177
private static void SetReminder (string title, string description,
178
DateTime when, string eid)
180
Console.Error.WriteLine (when);
181
Console.Error.Write (DateTime.Now);
182
Console.Error.WriteLine ("SetReminder called");
183
Console.Error.WriteLine (when.Millisecond - DateTime.Now.Millisecond);
185
if (!(when.Millisecond - DateTime.Now.Millisecond < 36000000)) {
186
Console.Error.WriteLine ("statement is false");
194
Console.Error.WriteLine ("A message will be sent");
195
GLib.Timeout.Add ((uint) (when.Millisecond - DateTime.Now.Millisecond),
197
Console.Error.WriteLine ("Sending message");
198
Do.Addins.NotificationBridge.ShowMessage (title, description);
203
public static EventFeed SearchEvents (string calUrl, string needle)
205
string [] keywords = {"from ","until ","in ", "after ", "before ", "on "};
206
EventQuery query = new EventQuery(calUrl);
207
DateTime [] dates = ParseEventDates (needle,keywords);
208
query.StartTime = dates[0];
209
query.EndTime = dates[1];
210
query.Query = ParseSearchString (needle,keywords);
213
events = service.Query(query) as EventFeed;
214
} catch (WebException e) {
216
Console.Error.WriteLine (e.Message);
221
public static EventEntry NewEvent (string calUrl, string data)
223
EventEntry entry = new EventEntry ();
224
entry.QuickAdd = true;
225
entry.Content.Content = data;
226
Uri post_uri = new Uri(calUrl);
229
nevent = service.Insert(post_uri, entry) as EventEntry;
230
} catch (WebException e) {
232
Console.Error.WriteLine (e.Message);
237
private static void ClearList (List<IItem> list)
244
private static DateTime [] ParseEventDates (string needle, string [] keywords)
246
needle = needle.ToLower ();
249
// String string to just dates if search term + date range found
250
foreach (string keyword in keywords) {
251
if (needle.Contains(keyword)) {
252
keydex = needle.IndexOf (keyword);
253
needle = needle.Substring (keydex, needle.Length - keydex);
254
//Console.Error.WriteLine ("Needle stripped to {0}",needle);
259
// Get date ranges for single date keywords
260
if ((needle.StartsWith ("in ") || needle.Contains (" in ")) ||
261
needle.Contains ("before ") || needle.Contains ("after ") ||
262
(needle.StartsWith ("on ") || needle.Contains (" on ")) ||
263
needle.Contains ("until ") || (needle.Contains ("from ") &&
264
!( needle.Contains (" to ") || needle.Contains("-"))))
265
return ParseSingleDate (needle);
267
else if (needle.Contains ("from "))
268
return ParseDateRange (needle);
270
return new DateTime [] {DateTime.Now, DateTime.Now.AddYears(1)};
273
private static DateTime [] ParseSingleDate (string needle)
275
DateTime [] dates = new DateTime [2];
276
if (needle.StartsWith ("in") || needle.Contains (" in ")) {
277
needle = StripDatePrefix (needle);
278
dates[0] = DateTime.Parse (needle);
279
dates[1] = dates[0].AddMonths (1);
281
else if (needle.Contains ("before") || needle.Contains ("until ")) {
282
needle = StripDatePrefix (needle);
283
dates[0] = DateTime.Now;
284
dates[1] = DateTime.Parse (needle);
286
else if (needle.Contains ("after ") || needle.Contains ("from ")) {
287
needle = StripDatePrefix (needle);
288
dates[0] = DateTime.Parse (needle);
289
dates[1] = dates[0].AddYears (5);
291
else if (needle.StartsWith ("on ") || needle.Contains (" on ")) {
292
needle = StripDatePrefix (needle);
293
dates[0] = DateTime.Parse (needle);
294
dates[1] = dates[0].AddDays(1);
299
private static DateTime [] ParseDateRange (string needle)
301
DateTime [] dates = new DateTime [2];
302
needle = needle.ToLower ();
303
needle = needle.Substring (needle.IndexOf ("from "),
304
needle.Length - needle.IndexOf ("from "));
306
int seperatorIndex = needle.IndexOf ("-");
307
if (seperatorIndex == -1 )
308
seperatorIndex = needle.IndexOf (" to ");
309
if (seperatorIndex == -1 ) {
310
dates[0] = DateTime.Now;
311
dates[1] = new DateTime (2012,12,27);
314
string start = needle.Substring (0, seperatorIndex);
315
if (start.Substring(0,4).Equals ("from"))
316
start = start.Substring (4).Trim ();
317
dates[0] = DateTime.Parse (start.Trim ());
319
string end = needle.Substring (seperatorIndex + 1);
320
if (end.Contains ("to "))
321
end = end.Substring (3);
322
dates[1] = DateTime.Parse (end.Trim ());
323
} catch (FormatException e) {
324
Console.Error.WriteLine (e.Message);
329
private static string StripDatePrefix (string needle)
331
needle = needle.Trim ().ToLower ();
332
needle = needle.Substring (needle.IndexOf (" "));
336
private static string ParseSearchString (string needle, string[] keywords)
338
needle = needle.ToLower ();
339
foreach (string keyword in keywords) {
340
if (needle.Contains (keyword))
341
needle = needle.Substring(0,needle.IndexOf (keyword)).Trim ();
347
public static bool TryConnect (string username, string password)
349
CalendarService test;
352
test = new CalendarService (gAppName);
353
test.setUserCredentials (username, password);
354
query = new FeedQuery ();
355
query.Uri = new Uri (FeedUri);
358
Connect (username, password);
359
} catch (Exception e) {
360
Console.Error.WriteLine (e.Message);
b'\\ No newline at end of file'