~d6g/do-plugins/Timer

« back to all changes in this revision

Viewing changes to GDocs/src/GDocs.cs

  • Committer: Alex Launi
  • Date: 2008-10-20 22:21:07 UTC
  • mfrom: (248.1.8 GDocs)
  • Revision ID: alex@eriktorvaldsonn-20081020222107-xl42p0fnsg2p0tsb
Added GDocs plugin from Peng Deng

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GDocs.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
 
 
22
using System;
 
23
using System.Net;
 
24
using System.Xml;
 
25
using System.Threading;
 
26
using System.Collections.Generic;
 
27
using Mono.Unix;
 
28
 
 
29
using Google.GData.Client;
 
30
using Google.GData.Documents;
 
31
using Google.GData.Extensions;
 
32
 
 
33
using Do.Universe;
 
34
 
 
35
namespace GDocs {
 
36
        public static class GDocs {
 
37
                private static DocumentsService service;
 
38
                private static List<IItem> docs;
 
39
                private static object docs_lock;
 
40
                
 
41
                const string FeedUri = "http://docs.google.com/feeds/documents/private/full";
 
42
                private static string gAppName = "pengDeng-gnomeDoGDocsPlugin-1.0";
 
43
                
 
44
                static GDocs ()
 
45
                {
 
46
                        string username, password;
 
47
                        //Google works over SSL, we need accept the cert.
 
48
                        System.Net.ServicePointManager.CertificatePolicy = new CertHandler ();
 
49
                        
 
50
                        docs = new List<IItem> ();
 
51
                        docs_lock = new object ();
 
52
                        
 
53
                        Configuration.GetAccountData (out username, out password,
 
54
                                                      typeof (Configuration));
 
55
                        Connect (username, password);
 
56
                }
 
57
                
 
58
                public static string GAppName {
 
59
                        get { return gAppName; }
 
60
                }
 
61
                
 
62
                public static bool Connect (string username, string password)
 
63
                {
 
64
                        try {
 
65
                                service = new DocumentsService (gAppName);
 
66
                                service.setUserCredentials (username, password);
 
67
                        } catch (Exception e) {
 
68
                                Console.Error.WriteLine (e.Message);
 
69
                                return false;
 
70
                        }
 
71
                        return true;
 
72
                }
 
73
                
 
74
                public static List<IItem> Docs {
 
75
                        get { return docs; }
 
76
                }
 
77
                
 
78
                public static void UpdateDocs ()
 
79
                {
 
80
                        DocumentsFeed docsFeed;
 
81
                        DocumentsListQuery query = new DocumentsListQuery ();
 
82
                        query.Uri = new Uri (FeedUri);
 
83
                        
 
84
                        try {
 
85
                                docsFeed = service.Query (query);
 
86
                        } catch (Exception e) {
 
87
                                docsFeed = null;
 
88
                                Console.Error.WriteLine (e.Message);
 
89
                                return;
 
90
                        }
 
91
                        
 
92
                        lock (docs_lock) {                              
 
93
                                docs.Clear ();                          
 
94
                                foreach (DocumentEntry doc in docsFeed.Entries) {                                       
 
95
                                        string doc_url = doc.AlternateUri.Content;
 
96
                                        string doc_title = doc.Title.Text;
 
97
                                        
 
98
                                        if (doc.IsDocument) 
 
99
                                                docs.Add (new GDocsDocumentItem (doc_title, doc_url));
 
100
                                        else if (doc.IsSpreadsheet) 
 
101
                                                docs.Add (new GDocsSpreadsheetItem (doc_title, doc_url));
 
102
                                        else if (doc.IsPresentation) 
 
103
                                                docs.Add (new GDocsPresentationItem (doc_title, doc_url));
 
104
                                        else if (doc.IsPDF)
 
105
                                                docs.Add (new GDocsPDFItem (doc_title, doc_url));                                       
 
106
                                }
 
107
                        }
 
108
                }
 
109
                
 
110
                public static IItem UploadDocument (string fileName, string documentName)
 
111
                {
 
112
                        DocumentEntry newDoc;
 
113
                        GDocsItem newDocItem;
 
114
                        
 
115
                        try {
 
116
                                newDoc = service.UploadDocument (fileName, documentName);
 
117
                        } catch (Exception e) {
 
118
                                newDoc = null;
 
119
                                Console.Error.WriteLine (e.Message);
 
120
                                Do.Addins.NotificationBridge.ShowMessage (Catalog.GetString ("Uploading failed."), 
 
121
                                    Catalog.GetString ("An error occurred when uploading file to Google Docs."));
 
122
 
 
123
                                return null; 
 
124
                        }               
 
125
                        
 
126
                        string doc_url = newDoc.AlternateUri.Content;
 
127
                        string doc_title = newDoc.Title.Text;
 
128
                
 
129
                        if (newDoc.IsDocument)
 
130
                                newDocItem = new GDocsDocumentItem (doc_title, doc_url);
 
131
                        else if (newDoc.IsSpreadsheet)
 
132
                                newDocItem = new GDocsSpreadsheetItem (doc_title, doc_url);
 
133
                        else if (newDoc.IsPresentation)
 
134
                                newDocItem = new GDocsPresentationItem (doc_title, doc_url);
 
135
                        else if (newDoc.IsPDF)
 
136
                                newDocItem = new GDocsPDFItem (doc_title, doc_url);
 
137
                        else
 
138
                                return null;
 
139
                        
 
140
                        lock (docs_lock) {
 
141
                                docs.Add (newDocItem);
 
142
                        }
 
143
                        return newDocItem;
 
144
                }
 
145
                
 
146
                public static void TrashDocument (GDocsItem docItem)
 
147
                {
 
148
                        string doc_title = docItem.Name;
 
149
                        string doc_url   = docItem.URL;
 
150
                        
 
151
                        // Search for document(s) having exactly the title,
 
152
                        // Delete the one with matching AlternateUri
 
153
                        DocumentsListQuery query = new DocumentsListQuery ();
 
154
                        query.Title = doc_title;
 
155
                        query.TitleExact = true;
 
156
                        DocumentsFeed docFeed = service.Query (query);  
 
157
 
 
158
                        if (docFeed.Entries.Count >= 1) {
 
159
                                foreach (DocumentEntry docEntry in docFeed.Entries) {   
 
160
                                        if (docEntry.AlternateUri == doc_url) {
 
161
                                                try {
 
162
                                                        docEntry.Delete ();
 
163
                                                } catch (Exception e) {
 
164
                                                        Console.Error.WriteLine (e.Message);
 
165
                                                        Do.Addins.NotificationBridge.ShowMessage (Catalog.GetString ("Deleting failed"),
 
166
                                                            Catalog.GetString ("An error occurred when deleting the document at Google Docs."));
 
167
                                                        return;
 
168
                                                }
 
169
                                                
 
170
                                                Do.Addins.NotificationBridge.ShowMessage (Catalog.GetString ("Document deleted."),
 
171
                                                    Catalog.GetString (String.Format ("The document '{0}' has been successfully moved into Trash at Google Docs.", 
 
172
                                                                                      doc_title)));
 
173
                                                lock (docs_lock) {                                              
 
174
                                                        docs.Remove (docItem);
 
175
                                                }
 
176
                                        }
 
177
                                }
 
178
                        }
 
179
                }
 
180
        }
 
181
}
 
 
b'\\ No newline at end of file'