~cszikszoy/do-plugins/MA-Config-Extension

« back to all changes in this revision

Viewing changes to GoogleDocs/src/GDocs.cs

  • Committer: Peng Deng
  • Date: 2009-06-17 20:43:58 UTC
  • mfrom: (618.2.3 gdoc-fix)
  • Revision ID: dengpeng@gmail.com-20090617204358-hob41x2dapj5kngh
Merge minor fixes/improvements to GDocs plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// GDocs.cs
2
 
//
3
 
// GNOME Do is the legal property of its developers. Please refer to the
4
 
// COPYRIGHT file distributed with this source distribution.
5
 
//
 
2
// 
 
3
// Copyright (C) 2009 GNOME Do
 
4
// 
6
5
// This program is free software: you can redistribute it and/or modify
7
6
// it under the terms of the GNU General Public License as published by
8
7
// the Free Software Foundation, either version 3 of the License, or
9
8
// (at your option) any later version.
10
 
//
 
9
// 
11
10
// This program is distributed in the hope that it will be useful,
12
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
13
// GNU General Public License for more details.
15
 
//
 
14
// 
16
15
// You should have received a copy of the GNU General Public License
17
16
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
//
19
 
 
20
17
 
21
18
using System;
22
19
using System.Net;
36
33
 
37
34
namespace GDocs
38
35
{
39
 
 
40
 
        public static class GDocs
 
36
        
 
37
        public class GDocs
41
38
        {
42
 
 
43
 
                const string FeedUri =
44
 
                        "http://docs.google.com/feeds/documents/private/full";
 
39
                
 
40
                const string FeedUri = "http://docs.google.com/feeds/documents/private/full";
45
41
                public const string GAppName = "pengDeng-gnomeDoGDocsPlugin-1.0";
46
42
                
47
43
                static List<Item> docs;
 
44
                static object docs_lock;
48
45
                static GDocsPreferences prefs;
49
46
                static DocumentsService service;
50
47
                
51
48
                static GDocs ()
52
49
                {
53
50
                        docs = new List<Item> ();
 
51
                        docs_lock = new object ();
54
52
                        prefs = new GDocsPreferences ();
55
 
 
 
53
                        
56
54
                        // Google works over SSL, we need accept the cert.
57
55
                        System.Net.ServicePointManager.CertificatePolicy = new CertHandler ();
58
 
 
59
56
                        Connect (Preferences.Username, Preferences.Password);
60
57
                }
61
58
                
65
62
                                service = new DocumentsService (GAppName);
66
63
                                service.setUserCredentials (username, password);
67
64
                        } catch (Exception e) {
68
 
                                Log.Error (e.Message);
 
65
                                Log<GDocs>.Error (e.Message);
69
66
                                return false;
70
67
                        }
71
68
                        return true;
72
69
                }
73
 
 
 
70
                
74
71
                internal static GDocsPreferences Preferences {
75
72
                        get { return prefs; }
76
73
                }
77
74
                
78
75
                public static List<Item> Docs {
79
 
                        get { return docs; }
 
76
                        get { 
 
77
                                lock (docs_lock)
 
78
                                        return docs; 
 
79
                        }
80
80
                }
81
81
                
82
82
                public static void UpdateDocs ()
83
83
                {
84
 
                        DocumentsFeed docsFeed;
85
 
                        DocumentsListQuery query = new DocumentsListQuery ();
86
 
                        query.Uri = new Uri (FeedUri);
87
 
                        
88
 
                        try {
89
 
                                docsFeed = service.Query (query);
90
 
                        } catch (Exception e) {
91
 
                                docsFeed = null;
92
 
                                Log.Error (e.Message);
93
 
                                return;
94
 
                        }
95
 
                        
96
 
                        lock (docs) {                           
97
 
                                docs.Clear ();                          
98
 
                                foreach (DocumentEntry doc in docsFeed.Entries) {                                       
99
 
                                        GDocsItem item = MaybeItemFromEntry (doc);
 
84
                        lock (docs_lock) {
 
85
                                DocumentsFeed docsFeed;
 
86
                                DocumentsListQuery query = new DocumentsListQuery ();
 
87
                                query.Uri = new Uri (FeedUri);
 
88
                                
 
89
                                try {
 
90
                                        docsFeed = service.Query (query);
 
91
                                } catch (Exception e) {
 
92
                                        docsFeed = null;
 
93
                                        Log<GDocs>.Error (e.Message);
 
94
                                        return;
 
95
                                }
 
96
                                
 
97
                                docs.Clear ();
 
98
                                foreach (DocumentEntry doc in docsFeed.Entries) {
 
99
                                        GDocsAbstractItem item = MaybeItemFromEntry (doc);
100
100
                                        if (item != null)
101
 
                                                docs.Add (item);                                        
 
101
                                                docs.Add (item);
102
102
                                }
103
103
                        }
104
104
                }
105
 
 
106
 
                static GDocsItem MaybeItemFromEntry (DocumentEntry doc)
 
105
                
 
106
                static GDocsAbstractItem MaybeItemFromEntry (DocumentEntry doc)
107
107
                {
108
108
                                string url = doc.AlternateUri.Content;
109
109
                                string title = doc.Title.Text;
110
 
 
 
110
                        
111
111
                                if (doc.IsDocument) 
112
112
                                        return new GDocsDocumentItem (title, url);
113
113
                                else if (doc.IsSpreadsheet) 
115
115
                                else if (doc.IsPresentation) 
116
116
                                        return new GDocsPresentationItem (title, url);
117
117
                                else if (doc.IsPDF)
118
 
                                        return new GDocsPDFItem (title, url);                                   
 
118
                                        return new GDocsPDFItem (title, url);
119
119
                                else
120
120
                                        return null;
121
121
                }
123
123
                public static Item UploadDocument (string fileName, string documentName)
124
124
                {
125
125
                        DocumentEntry newDoc;
126
 
                        GDocsItem newDocItem;
127
126
                        
128
127
                        try {
129
128
                                newDoc = service.UploadDocument (fileName, documentName);
130
129
                        } catch (Exception e) {
131
130
                                newDoc = null;
132
 
                                Log.Error (e.Message);
 
131
                                Log<GDocs>.Error (e.Message);
133
132
                                Services.Notifications.Notify (GetUploadFailedNotification ());
134
133
                                return null; 
135
 
                        }               
 
134
                        }
136
135
                        
137
 
                        newDocItem = MaybeItemFromEntry (newDoc);
138
 
                        if (newDocItem != null) {
139
 
                                lock (docs) {
140
 
                                        docs.Add (newDocItem);
141
 
                                }
142
 
                        }
143
 
                        return newDocItem;
 
136
                        return  MaybeItemFromEntry (newDoc);
144
137
                }
145
 
 
 
138
                
146
139
                static Notification GetUploadFailedNotification ()
147
140
                {
148
141
                        return new Notification (
150
143
                                Catalog.GetString ("An error occurred when uploading files to Google Docs."), 
151
144
                                "gDocsIcon.png@" + typeof (GDocsItemSource).Assembly.FullName);
152
145
                }
153
 
 
 
146
                
154
147
                static Notification GetDeleteDocumentFailedNotification ()
155
148
                {
156
149
                        return new Notification (
158
151
                                Catalog.GetString ("An error occurred when deleting the document at Google Docs."), 
159
152
                                "gDocsIcon.png@" + typeof (GDocsItemSource).Assembly.FullName);
160
153
                }
161
 
 
 
154
                
162
155
                static Notification GetDocumentDeletedNotification (string documentTitle)
163
156
                {
164
157
                        return new Notification (
167
160
                                "gDocsIcon.png@" + typeof (GDocsItemSource).Assembly.FullName);
168
161
                }
169
162
                
170
 
                public static void TrashDocument (GDocsItem item)
 
163
                public static void TrashDocument (GDocsAbstractItem item)
171
164
                {
172
165
                        // Search for document(s) having exactly the title,
173
166
                        // Delete the one with matching AlternateUri
177
170
                        DocumentsFeed docFeed = service.Query (query);  
178
171
                        DocumentEntry document =
179
172
                                docFeed.Entries.FirstOrDefault (e => e.AlternateUri == item.URL) as DocumentEntry;
180
 
 
 
173
                        
181
174
                        if (document == null) return;
182
 
 
 
175
                        
183
176
                        try {
184
177
                                document.Delete ();
185
178
                        } catch (Exception e) {
187
180
                                Services.Notifications.Notify (GetDeleteDocumentFailedNotification ());
188
181
                                return;
189
182
                        }
190
 
 
 
183
                        
191
184
                        Services.Notifications.Notify (GetDocumentDeletedNotification (item.Name));
192
 
                        lock (docs) {                                           
193
 
                                docs.Remove (item);
194
 
                        }
195
185
                }
196
 
 
197
186
        }
198
187
}