Consolidating timeout location and increasing it for slower connections
[gc-dialer] / src / gdata_backend.py
1 #!/usr/bin/python
2
3 import sys
4 sys.path.insert(0,"../gdata/src/")
5
6
7 try:
8         import atom
9         import gdata
10         gdata.contacts
11 except (ImportError, AttributeError):
12         atom = None
13         gdata = None
14
15
16 class GDataAddressBook(object):
17         """
18         """
19
20         def __init__(self, client, id = None):
21                 self._client = client
22                 self._id = id
23                 self._contacts = []
24
25         def clear_caches(self):
26                 del self._contacts[:]
27
28         @staticmethod
29         def factory_name():
30                 return "GData"
31
32         @staticmethod
33         def contact_source_short_name(contactId):
34                 return "gd"
35
36         def get_contacts(self):
37                 """
38                 @returns Iterable of (contact id, contact name)
39                 """
40                 return []
41
42         def get_contact_details(self, contactId):
43                 """
44                 @returns Iterable of (Phone Type, Phone Number)
45                 """
46                 return []
47
48         def _get_contacts(self):
49                 if len(self._contacts) != 0:
50                         return self._contacts
51                 feed = self._get_feed()
52                 for entry in feed:
53                         name = entry.title.text
54                         print name
55                         for extendedProperty in entry.extended_property:
56                                 if extendedProperty.value:
57                                         print extendedProperty.value
58                                 else:
59                                         print extendedProperty.GetXmlBlobString()
60
61         def _get_feed(self):
62                 if self._id is None:
63                         return self._client.GetContactsFeed()
64                 else:
65                         pass
66
67
68 class GDataAddressBookFactory(object):
69
70         def __init__(self, username, password):
71                 self._username = username
72                 self._password = password
73                 self._client = None
74
75                 if gdata is None:
76                         return
77                 self._client = gdata.contacts.service.ContactsService()
78                 self._client.email = username
79                 self._client.password = password
80                 self._client.source = "DialCentra"
81                 self._client.ProgrammaticLogin()
82
83         def clear_caches(self):
84                 if gdata is None:
85                         return
86                 pass
87
88         def get_addressbooks(self):
89                 """
90                 @returns Iterable of (Address Book Factory, Book Id, Book Name)
91                 """
92                 if gdata is None:
93                         return
94                 feed = self._client.GetGroupsFeed()
95                 for entry in feed:
96                         id = entry.id.text
97                         name = entry.title.text
98                         yield self, id, name
99                 yield self, "all", "All"
100
101         def open_addressbook(self, bookId):
102                 if gdata is None:
103                         return
104                 if bookId == "all":
105                         return GDataAddressBook(self._client)
106                 else:
107                         return GDataAddressBook(self._client, bookId)
108
109         @staticmethod
110         def factory_name():
111                 return "GData"
112
113
114 def print_gbooks(username, password):
115         """
116         Included here for debugging.
117
118         Either insert it into the code or launch python with the "-i" flag
119         """
120         abf = GDataAddressBookFactory(username, password)
121         for book in abf.get_addressbooks():
122                 ab = abf.open_addressbook(book[1])
123                 print book
124                 for contact in ab.get_contacts():
125                         print "\t", contact
126                         for details in ab.get_contact_details(contact[0]):
127                                 print "\t\t", details