Work in progress on Google Voice and Google Data
[gc-dialer] / src / gmail_backend.py
1 #!/usr/bin/python
2
3 # DialCentral - Front end for Google's Grand Central service.
4 # Copyright (C) 2008  Eric Warnke ericew AT gmail DOT com
5
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # Lesser General Public License for more details.
15
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
20
21 """
22 GMail Contacts Support
23
24 @todo Look into the official API http://code.google.com/p/gdata-python-client/
25 """
26
27
28 import warnings
29
30
31 try:
32         import libgmail
33 except ImportError:
34         libgmail = None
35
36
37 class GMailAddressBook(object):
38         """
39         @note Combined the factory and the addressbook for "simplicity" and "cutting down" the number of allocations/deallocations
40         """
41
42         def __init__(self, username, password):
43                 if not self.is_supported():
44                         return
45
46                 self._account = libgmail.GmailAccount(username, password)
47                 self._gmailContacts = self._account.getContacts()
48
49         @classmethod
50         def is_supported(cls):
51                 return libgmail is not None
52
53         def get_addressbooks(self):
54                 """
55                 @returns Iterable of (Address Book Factory, Book Id, Book Name)
56                 """
57                 if not self.is_supported():
58                         return
59
60                 yield self, "", ""
61
62         def open_addressbook(self, bookId):
63                 return self
64
65         @staticmethod
66         def contact_source_short_name(contactId):
67                 return "G"
68
69         @staticmethod
70         def factory_name():
71                 return "GMail"
72
73         def get_contacts(self):
74                 """
75                 @returns Iterable of (contact id, contact name)
76                 """
77                 if not self.is_supported():
78                         return
79                 pass
80
81         def get_contact_details(self, contactId):
82                 """
83                 @returns Iterable of (Phone Type, Phone Number)
84                 """
85                 pass
86
87
88 def print_gbooks(username, password):
89         """
90         Included here for debugging.
91
92         Either insert it into the code or launch python with the "-i" flag
93         """
94         if not GMailAddressBook.is_supported():
95                 print "No GMail Support"
96                 return
97
98         gab = GMailAddressBook(username, password)
99         for book in gab.get_addressbooks():
100                 gab = gab.open_addressbook(book[1])
101                 print book
102                 for contact in gab.get_contacts():
103                         print "\t", contact
104                         for details in gab.get_contact_details(contact[0]):
105                                 print "\t\t", details
106         print gab._gmailContacts