~cszikszoy/do-plugins/pastebin

« back to all changes in this revision

Viewing changes to GCalendar/src/GCal.cs

  • Committer: Christopher Halse Rogers
  • Date: 2008-08-24 08:44:24 UTC
  • mfrom: (244.1.2 do-plugins)
  • Revision ID: raof@ubuntu.com-20080824084424-8gp5ff6v9nku9z21
Merge in the tip of 0.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* DoGCal.cs
 
2
 *
 
3
 * GNOME Do is the legal property of its developers. Please refer to the
 
4
 * COPYRIGHT file distributed with this
 
5
 * source distribution.
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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/>.
 
19
 */
 
20
 
 
21
using System;
 
22
using System.IO;
 
23
using System.Net;
 
24
using System.Threading;
 
25
using System.Collections.Generic;
 
26
 
 
27
using Google.GData.Client;
 
28
using Google.GData.Calendar;
 
29
using Google.GData.Extensions;
 
30
 
 
31
using Gtk;
 
32
using Do.Universe;
 
33
 
 
34
namespace GCalendar
 
35
{
 
36
        public class GCal
 
37
        {
 
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
 
46
                        
 
47
                static GCal ()
 
48
                {       
 
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));
 
57
                        try {
 
58
                                Connect (username, password);
 
59
                        } catch (Exception e) {
 
60
                                Console.Error.WriteLine (e.Message);
 
61
                        }
 
62
                }
 
63
                
 
64
                public static string GAppName {
 
65
                        get { return gAppName; }
 
66
                }
 
67
                
 
68
                public static List<IItem> Calendars {
 
69
                        get { return calendars; }
 
70
                }
 
71
                
 
72
                public static List<IItem> Events {
 
73
                        get { return events; }
 
74
                }
 
75
 
 
76
                private static void Connect (string username, string password) 
 
77
                {
 
78
                        try {
 
79
                                service = new CalendarService (gAppName);
 
80
                                service.setUserCredentials (username, password);
 
81
                        } catch (Exception e) {
 
82
                                Console.Error.WriteLine (e.Message);
 
83
                        }
 
84
                }
 
85
                
 
86
                public static void UpdateCalendars ()
 
87
                {               
 
88
                        if (!Monitor.TryEnter (calendars)) return;
 
89
                        if (calendars.Count > 0) {
 
90
                                //Console.Error.WriteLine("GCal: Cache not cleared, returning");
 
91
                                Monitor.Exit (calendars);
 
92
                                return;
 
93
                        }
 
94
                        AtomFeed cal_list = QueryCalendars ();
 
95
                        if (cal_list == null) return;
 
96
                        
 
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"));
 
101
                        }
 
102
                        Monitor.Exit (calendars);
 
103
                        //Console.Error.WriteLine("GCal: Calendar list set");
 
104
                }
 
105
                
 
106
                public static AtomFeed QueryCalendars ()
 
107
                {                       
 
108
                        FeedQuery query = new FeedQuery ();
 
109
                        query.Uri = new Uri (FeedUri);
 
110
                        try {
 
111
                                return service.Query (query);
 
112
                        } catch (Exception e) {
 
113
                                Console.Error.WriteLine (e.Message);
 
114
                        }
 
115
                        return null;
 
116
                }
 
117
                                
 
118
                public static EventFeed GetEvents (string calUrl)
 
119
                {
 
120
                        return GetEvents (calUrl, DateTime.Now, DateTime.Now.AddMonths(1));
 
121
                }
 
122
                
 
123
                public static EventFeed GetEvents (string calUrl, DateTime startTime,
 
124
                                            DateTime endTime)
 
125
        {       
 
126
            EventQuery query = new EventQuery (calUrl);
 
127
            query.StartTime = startTime;
 
128
            query.EndTime = endTime;
 
129
                        query.SortOrder = CalendarSortOrder.ascending;
 
130
                        EventFeed events;
 
131
                        try {
 
132
                                events = service.Query (query) as EventFeed;
 
133
                        } catch (WebException e) {
 
134
                                events = null;
 
135
                                Console.Error.WriteLine (e.Message);
 
136
                        }
 
137
                        return events;
 
138
        }
 
139
        
 
140
        public static void UpdateEvents ()
 
141
        {
 
142
                EventFeed eventsFeed;
 
143
                string eventUrl, eventDesc, start;
 
144
                
 
145
                lock (calendars) {
 
146
                        lock (events) {
 
147
                                foreach (GCalendarItem cal in calendars) {
 
148
                                                try {
 
149
                                                        eventsFeed = GCal.GetEvents (cal.URL);
 
150
                                                } catch (Exception e) {
 
151
                                                        Console.Error.WriteLine (e.Message);
 
152
                                                        return;
 
153
                                                }
 
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;
 
161
                                                        try {
 
162
                                                        SetReminder (entry.Title.Text, eventDesc,
 
163
                                                                entry.Times [0].StartTime, entry.EventId);
 
164
                                                        Console.Error.WriteLine ("{0} Has notification", entry.EventId);
 
165
                                                } catch {
 
166
                                                        Console.Error.WriteLine (entry.Title.Text + "has no notifications");
 
167
                                                }
 
168
                                        }
 
169
                                                    events.Add (new GCalendarEventItem (entry.Title.Text, eventUrl,
 
170
                                                            eventDesc));
 
171
                                    }
 
172
                                        }
 
173
                                }
 
174
                        }
 
175
        }
 
176
        
 
177
        private static void SetReminder (string title, string description, 
 
178
                DateTime when, string eid)
 
179
        {
 
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);
 
184
                
 
185
                if (!(when.Millisecond - DateTime.Now.Millisecond < 36000000)) {
 
186
                        Console.Error.WriteLine ("statement is false");
 
187
                        return;
 
188
                }
 
189
                        
 
190
                lock (notified) {
 
191
                        notified.Add (eid);
 
192
                }
 
193
                
 
194
                Console.Error.WriteLine ("A message will be sent");
 
195
                GLib.Timeout.Add ((uint) (when.Millisecond - DateTime.Now.Millisecond),
 
196
                        delegate {
 
197
                                Console.Error.WriteLine ("Sending message");
 
198
                                Do.Addins.NotificationBridge.ShowMessage (title, description); 
 
199
                                return false; }
 
200
                );
 
201
        }
 
202
        
 
203
        public static EventFeed SearchEvents (string calUrl, string needle) 
 
204
        {
 
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);
 
211
                        EventFeed events;
 
212
                        try {
 
213
                                events = service.Query(query) as EventFeed;
 
214
                        } catch (WebException e) {
 
215
                                events = null;
 
216
                                Console.Error.WriteLine (e.Message);
 
217
                        }
 
218
            return events; 
 
219
        }
 
220
        
 
221
        public static EventEntry NewEvent (string calUrl, string data) 
 
222
        {
 
223
            EventEntry entry = new EventEntry ();
 
224
            entry.QuickAdd = true;
 
225
            entry.Content.Content = data;
 
226
            Uri post_uri = new Uri(calUrl);
 
227
                        EventEntry nevent;
 
228
                        try {
 
229
                                nevent = service.Insert(post_uri, entry) as EventEntry;
 
230
                        } catch (WebException e) {
 
231
                                nevent = null;
 
232
                                Console.Error.WriteLine (e.Message);
 
233
                        }
 
234
            return nevent;
 
235
        }
 
236
                
 
237
                private static void ClearList (List<IItem> list)
 
238
                {
 
239
                        lock (list) {
 
240
                                list.Clear ();
 
241
                        }
 
242
                }
 
243
                
 
244
                private static DateTime [] ParseEventDates (string needle, string [] keywords)
 
245
                {
 
246
                        needle = needle.ToLower ();
 
247
                        
 
248
                        int keydex;
 
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);
 
255
                                        break;
 
256
                                }
 
257
                        }
 
258
                        
 
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);
 
266
                                        
 
267
                        else if (needle.Contains ("from "))
 
268
                                return ParseDateRange (needle);
 
269
                        else 
 
270
                                return new DateTime [] {DateTime.Now, DateTime.Now.AddYears(1)};
 
271
                }
 
272
                
 
273
                private static DateTime [] ParseSingleDate (string needle)
 
274
                {
 
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);
 
280
                        }
 
281
                        else if (needle.Contains ("before") || needle.Contains ("until ")) {
 
282
                                needle = StripDatePrefix (needle);
 
283
                                dates[0] = DateTime.Now;
 
284
                                dates[1] = DateTime.Parse (needle);
 
285
                        }
 
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);
 
290
                        }
 
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);
 
295
                        }
 
296
                        return dates;
 
297
                }
 
298
                
 
299
                private static DateTime [] ParseDateRange (string needle)
 
300
                {
 
301
                        DateTime [] dates = new DateTime [2];
 
302
                        needle = needle.ToLower ();
 
303
                        needle = needle.Substring (needle.IndexOf ("from "), 
 
304
                                needle.Length - needle.IndexOf ("from "));
 
305
                        try {
 
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);
 
312
                                        return dates;
 
313
                                }
 
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 ());
 
318
                                
 
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);
 
325
                        }
 
326
                        return dates;
 
327
                }
 
328
                
 
329
                private static string StripDatePrefix (string needle)
 
330
                {
 
331
                        needle = needle.Trim ().ToLower ();
 
332
                        needle = needle.Substring (needle.IndexOf (" "));
 
333
                        return needle;
 
334
                }
 
335
                
 
336
                private static string ParseSearchString (string needle, string[] keywords)
 
337
                {
 
338
                        needle = needle.ToLower ();
 
339
                        foreach (string keyword in keywords) {
 
340
                        if (needle.Contains (keyword))
 
341
                                needle = needle.Substring(0,needle.IndexOf (keyword)).Trim ();
 
342
                        }
 
343
                        
 
344
                        return needle;                                                  
 
345
                }
 
346
                
 
347
                public static bool TryConnect (string username, string password)
 
348
                {
 
349
                        CalendarService test;
 
350
                        FeedQuery query;
 
351
                        
 
352
                        test = new CalendarService (gAppName);
 
353
                        test.setUserCredentials (username, password);
 
354
                        query = new FeedQuery ();
 
355
                        query.Uri = new Uri (FeedUri);
 
356
                        try {
 
357
                                test.Query (query);
 
358
                                Connect (username, password);
 
359
                        } catch (Exception e) {
 
360
                                Console.Error.WriteLine (e.Message);
 
361
                                return false;
 
362
                        }
 
363
                        return true;
 
364
                }
 
365
        }
 
366
}
 
 
b'\\ No newline at end of file'