Rearranging things a bit
[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
25
26 import warnings
27
28
29 try:
30         import libgmail
31 except ImportError:
32         libgmail = None
33
34
35 class GMailAddressBook(object):
36         """
37         @note Combined the factory and the addressbook for "simplicity" and "cutting down" the number of allocations/deallocations
38         """
39
40         def __init__(self, username, password):
41                 if not self.is_supported():
42                         return
43
44                 self._account = libgmail.GmailAccount(username, password)
45                 self._gmailContacts = self._account.getContacts()
46         
47         @classmethod
48         def is_supported(cls):
49                 return libgmail is not None
50
51         def get_addressbooks(self):
52                 """
53                 @returns Iterable of (Address Book Factory, Book Id, Book Name)
54                 """
55                 if not self.is_supported():
56                         return
57
58                 yield self, "", ""
59         
60         def open_addressbook(self, bookId):
61                 return self
62
63         @staticmethod
64         def contact_source_short_name(contactId):
65                 return "G"
66
67         @staticmethod
68         def factory_name():
69                 return "GMail"
70
71         def get_contacts(self):
72                 """
73                 @returns Iterable of (contact id, contact name)
74                 """
75                 if not self.is_supported():
76                         return
77                 pass
78         
79         def get_contact_details(self, contactId):
80                 """
81                 @returns Iterable of (Phone Type, Phone Number)
82                 """
83                 pass
84
85
86 def print_gbooks(username, password):
87         """
88         Included here for debugging.
89
90         Either insert it into the code or launch python with the "-i" flag
91         """
92         if not GMailAddressBook.is_supported():
93                 print "No GMail Support"
94                 return
95
96         gab = GMailAddressBook(username, password)
97         for book in gab.get_addressbooks():
98                 gab = gab.open_addressbook(book[1])
99                 print book
100                 for contact in gab.get_contacts():
101                         print "\t", contact
102                         for details in gab.get_contact_details(contact[0]):
103                                 print "\t\t", details
104         print gab._gmailContacts