~cszikszoy/do-plugins/pastebin

« back to all changes in this revision

Viewing changes to GContacts/src/GContact.cs

Adding missing files and Archive plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GContact.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.Xml;
 
23
using System.Net;
 
24
using System.Web;
 
25
using System.IO;
 
26
using System.Threading;
 
27
using System.Collections;
 
28
using System.Collections.Generic;
 
29
 
 
30
using Do.Addins;
 
31
using Do.Universe;
 
32
using Gnome.Keyring;
 
33
 
 
34
using Google.GData.Client;
 
35
 
 
36
namespace GContacts
 
37
{       
 
38
        public static class GContact
 
39
        {
 
40
                private static string auth_token, username, password;
 
41
                private static List<IItem> items;
 
42
                private static Timer ClearContactsTimer;
 
43
                const int CacheSeconds  = 350; //cache contacts
 
44
                
 
45
                static GContact () {
 
46
                        items = new List<IItem> ();
 
47
                        SetCredentials ();
 
48
                        ClearContactsTimer = new Timer (ClearContacts);
 
49
                }
 
50
                
 
51
                public static List<IItem> Contacts {
 
52
                        get { return items; }
 
53
                }
 
54
                
 
55
                public static string Auth_Token {
 
56
                        get { return auth_token; }
 
57
                        set { auth_token = value; }
 
58
                }
 
59
                
 
60
                public static string Username {
 
61
                        get { return username; }
 
62
                        set { username = value; }
 
63
                }
 
64
                
 
65
                public static string Password {
 
66
                        get { return password; }
 
67
                        set { password = value; }
 
68
                }
 
69
                
 
70
                public static void UpdateContacts ()
 
71
                {
 
72
                        if (Auth_Token == null) return;
 
73
                        try {
 
74
                                if (!Monitor.TryEnter (items)) return;
 
75
                                ClearContactsTimer.Change (CacheSeconds*1000, Timeout.Infinite);
 
76
 
 
77
                                if (items.Count > 0) {
 
78
                                        Monitor.Exit (items);
 
79
                                        return;
 
80
                                }
 
81
                                
 
82
                                XmlDocument contacts = new XmlDocument ();
 
83
                                contacts.Load (MakeRequest ().GetResponseStream ());
 
84
 
 
85
                                ContactItem buddy;
 
86
                                string key, email, phone, address, name;
 
87
                                key = email = phone = address = name = "";
 
88
                                int i = 0;
 
89
                                Dictionary<string, string> emails = new Dictionary<string,string> ();
 
90
                                
 
91
                                foreach (XmlNode contact in contacts.GetElementsByTagName ("entry")) {
 
92
                                        i = 0;
 
93
                                        foreach (XmlNode cont in contact.ChildNodes) {
 
94
                                                switch (cont.Name) {
 
95
                                                case ("title"): name = cont.InnerText; break;
 
96
                                                case ("gd:email"):
 
97
                                                        i++;
 
98
                                                        email = cont.Attributes.GetNamedItem ("address").Value;
 
99
                                                        key = "email." + i;
 
100
                                                        try {
 
101
                                                                if (cont.Attributes.GetNamedItem ("primary").Value == "true" )
 
102
                                                                        key = "email";
 
103
                                                        } catch {
 
104
                                                        }
 
105
                                                        if(!emails.ContainsKey (key))
 
106
                                                                emails.Add (key, email);
 
107
                                                        break;
 
108
                                                case ("gd:phoneNumber") : phone = cont.InnerText; break;
 
109
                                                case ("gd:postalAddress"): address = cont.InnerText; break;
 
110
                                                }
 
111
                                        }
 
112
                                        if (!name.Equals ("") && !email.Equals ("")) {
 
113
                                                buddy = ContactItem.Create (name);
 
114
                                                foreach (KeyValuePair<string, string> kvp in emails) 
 
115
                                                        buddy[kvp.Key] = kvp.Value;
 
116
                                                buddy["phone"] = phone;
 
117
                                                buddy["address"] = address;
 
118
                                                //For some reason this is not working automatically, so
 
119
                                                //let's force it to do what we want.
 
120
                                                buddy["description"] = buddy["email"];
 
121
                                                items.Add (buddy);
 
122
                                                //Console.Error.WriteLine(name + ":" + buddy["email"]);
 
123
                                                emails.Clear ();
 
124
                                        }
 
125
                                }
 
126
                                Monitor.Exit (items);
 
127
                        } catch (Exception e) {
 
128
                                Console.Error.WriteLine (e.Message);
 
129
                                return;
 
130
                        }
 
131
                }
 
132
                
 
133
                private static HttpWebResponse MakeRequest ()
 
134
                {
 
135
                        if (auth_token == null) return null;
 
136
                        string encoded_username = HttpUtility.UrlEncode (username);
 
137
                        string url = string.Format ("http://www.google.com/m8/feeds/contacts/{0}/base?max-results=5000",
 
138
                                                    encoded_username);
 
139
                        HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
 
140
                        WebHeaderCollection auth_header = new WebHeaderCollection ();
 
141
                        auth_header.Add ("Authorization: GoogleLogin auth="+auth_token);
 
142
                        req.Headers = auth_header;
 
143
                        req.Method = "GET";
 
144
                         
 
145
                        return (HttpWebResponse) req.GetResponse ();
 
146
                }
 
147
                
 
148
                private static void ClearContacts (object state)
 
149
                {
 
150
                        lock (items) {
 
151
                                items.Clear ();
 
152
                        }               
 
153
                }
 
154
                
 
155
                private static void SetCredentials ()
 
156
                {
 
157
                        string keyring_item_name = "Google Account";
 
158
                        Hashtable request_attributes = new Hashtable();
 
159
                        request_attributes["name"] = keyring_item_name;
 
160
                        try {
 
161
                                foreach(ItemData result in Ring.Find(ItemType.GenericSecret, request_attributes)) {
 
162
                                        if(!result.Attributes.ContainsKey("name") || !result.Attributes.ContainsKey("username") ||
 
163
                                                (result.Attributes["name"] as string) != keyring_item_name) 
 
164
                                                continue;
 
165
                                        
 
166
                                        username = (string)result.Attributes["username"];
 
167
                                        password = result.Secret;
 
168
 
 
169
                                        if (username == null || username == String.Empty || password == null || password == String.Empty)
 
170
                                                throw new ApplicationException ("Invalid username/password in keyring");
 
171
                                }
 
172
                        } catch (Exception e) {
 
173
                                Console.Error.WriteLine (e.Message);
 
174
                                string account_info = Environment.GetEnvironmentVariable ("HOME")
 
175
                                        + "/.config/gnome-do/google-name";
 
176
                                try {
 
177
                                        StreamReader reader = File.OpenText(account_info);
 
178
                                        username = reader.ReadLine ();
 
179
                                        password = reader.ReadLine ();
 
180
                                        WriteAccount ();
 
181
                                } catch { 
 
182
                                        Console.Error.WriteLine ("[ERROR] " + account_info + " cannot be read!");
 
183
                                } 
 
184
                        }
 
185
                }
 
186
                
 
187
                private static void WriteAccount ()
 
188
                {
 
189
                        string keyring_item_name = "Google Account";
 
190
                        string keyring;
 
191
                        try {
 
192
                                keyring = Ring.GetDefaultKeyring();
 
193
                        } catch {
 
194
                                username = null;
 
195
                                password = null;
 
196
                                return;
 
197
                        }
 
198
                        Hashtable update_request_attributes = new Hashtable();
 
199
                        update_request_attributes["name"] = keyring_item_name;
 
200
                        update_request_attributes["username"] = username;
 
201
 
 
202
                        try {
 
203
                                Ring.CreateItem(keyring, ItemType.GenericSecret, keyring_item_name,
 
204
                                                update_request_attributes, password, true);
 
205
                        } catch (Exception e) {
 
206
                                Console.WriteLine(e.Message);
 
207
                        }
 
208
                }                       
 
209
        }
 
210
}