Cleaning up modality and what windows is set as parent
[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
87         def get_addressbooks(self):
88                 """
89                 @returns Iterable of (Address Book Factory, Book Id, Book Name)
90                 """
91                 if gdata is None:
92                         return
93                 feed = self._client.GetGroupsFeed()
94                 for entry in feed:
95                         id = entry.id.text
96                         name = entry.title.text
97                         yield self, id, name
98                 yield self, "all", "All"
99
100         def open_addressbook(self, bookId):
101                 if gdata is None:
102                         return
103                 if bookId == "all":
104                         return GDataAddressBook(self._client)
105                 else:
106                         return GDataAddressBook(self._client, bookId)
107
108         @staticmethod
109         def factory_name():
110                 return "GData"
111
112
113 def print_gbooks(username, password):
114         """
115         Included here for debugging.
116
117         Either insert it into the code or launch python with the "-i" flag
118         """
119         abf = GDataAddressBookFactory(username, password)
120         for book in abf.get_addressbooks():
121                 ab = abf.open_addressbook(book[1])
122                 print book
123                 for contact in ab.get_contacts():
124                         print "\t", contact
125                         for details in ab.get_contact_details(contact[0]):
126                                 print "\t\t", details