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/>.
26
using System.Threading;
27
using System.Collections;
28
using System.Collections.Generic;
34
using Google.GData.Client;
38
public static class GContact
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
46
items = new List<IItem> ();
48
ClearContactsTimer = new Timer (ClearContacts);
51
public static List<IItem> Contacts {
55
public static string Auth_Token {
56
get { return auth_token; }
57
set { auth_token = value; }
60
public static string Username {
61
get { return username; }
62
set { username = value; }
65
public static string Password {
66
get { return password; }
67
set { password = value; }
70
public static void UpdateContacts ()
72
if (Auth_Token == null) return;
74
if (!Monitor.TryEnter (items)) return;
75
ClearContactsTimer.Change (CacheSeconds*1000, Timeout.Infinite);
77
if (items.Count > 0) {
82
XmlDocument contacts = new XmlDocument ();
83
contacts.Load (MakeRequest ().GetResponseStream ());
86
string key, email, phone, address, name;
87
key = email = phone = address = name = "";
89
Dictionary<string, string> emails = new Dictionary<string,string> ();
91
foreach (XmlNode contact in contacts.GetElementsByTagName ("entry")) {
93
foreach (XmlNode cont in contact.ChildNodes) {
95
case ("title"): name = cont.InnerText; break;
98
email = cont.Attributes.GetNamedItem ("address").Value;
101
if (cont.Attributes.GetNamedItem ("primary").Value == "true" )
105
if(!emails.ContainsKey (key))
106
emails.Add (key, email);
108
case ("gd:phoneNumber") : phone = cont.InnerText; break;
109
case ("gd:postalAddress"): address = cont.InnerText; break;
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"];
122
//Console.Error.WriteLine(name + ":" + buddy["email"]);
126
Monitor.Exit (items);
127
} catch (Exception e) {
128
Console.Error.WriteLine (e.Message);
133
private static HttpWebResponse MakeRequest ()
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",
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;
145
return (HttpWebResponse) req.GetResponse ();
148
private static void ClearContacts (object state)
155
private static void SetCredentials ()
157
string keyring_item_name = "Google Account";
158
Hashtable request_attributes = new Hashtable();
159
request_attributes["name"] = keyring_item_name;
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)
166
username = (string)result.Attributes["username"];
167
password = result.Secret;
169
if (username == null || username == String.Empty || password == null || password == String.Empty)
170
throw new ApplicationException ("Invalid username/password in keyring");
172
} catch (Exception e) {
173
Console.Error.WriteLine (e.Message);
174
string account_info = Environment.GetEnvironmentVariable ("HOME")
175
+ "/.config/gnome-do/google-name";
177
StreamReader reader = File.OpenText(account_info);
178
username = reader.ReadLine ();
179
password = reader.ReadLine ();
182
Console.Error.WriteLine ("[ERROR] " + account_info + " cannot be read!");
187
private static void WriteAccount ()
189
string keyring_item_name = "Google Account";
192
keyring = Ring.GetDefaultKeyring();
198
Hashtable update_request_attributes = new Hashtable();
199
update_request_attributes["name"] = keyring_item_name;
200
update_request_attributes["username"] = username;
203
Ring.CreateItem(keyring, ItemType.GenericSecret, keyring_item_name,
204
update_request_attributes, password, true);
205
} catch (Exception e) {
206
Console.WriteLine(e.Message);