Minor code cleanup
[gc-dialer] / src / evo_backend.py
1 #!/usr/bin/python
2
3 # GC Dialer - 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 Evolution Contact Support
23 """
24
25
26 try:
27         import evolution
28 except ImportError:
29         evolution = None
30
31
32 class EvolutionAddressBook(object):
33
34         def __init__(self, bookId = None):
35                 if not self.is_supported():
36                         return
37
38                 self._phoneTypes = None
39                 self._bookId = bookId if bookId is not None else self.get_addressbooks().next()[1]
40                 self._book = evolution.ebook.open_addressbook(self._bookId)
41         
42         @classmethod
43         def is_supported(cls):
44                 return evolution is not None
45
46         def get_addressbooks(self):
47                 """
48                 @returns Iterable of (Address Book Factory, Book Id, Book Name)
49                 """
50                 if not self.is_supported():
51                         return
52
53                 for bookId in evolution.ebook.list_addressbooks():
54                         yield self, bookId[1], bookId[0]
55         
56         def open_addressbook(self, bookId):
57                 self._bookId = bookId
58                 self._book = evolution.ebook.open_addressbook(self._bookId)
59                 return self
60
61         @staticmethod
62         def factory_name():
63                 return "Evo"
64
65         def get_contacts(self):
66                 """
67                 @returns Iterable of (contact id, contact name)
68                 """
69                 if not self.is_supported():
70                         return
71
72                 for contact in self._book.get_all_contacts():
73                         yield contact.get_uid(), contact.props.full_name
74         
75         def get_contact_details(self, contactId):
76                 """
77                 @returns Iterable of (Phone Type, Phone Number)
78                 """
79                 contact = self._book.get_contact(contactId)
80
81                 if self._phoneTypes is None and contact is not None:
82                         self._phoneTypes = [pt for pt in dir(contact.props) if "phone" in pt.lower()]
83
84                 for phoneType in self._phoneTypes:
85                         phoneNumber = getattr(contact.props, phoneType)
86                         if isinstance(phoneNumber, str):
87                                 yield phoneType, phoneNumber
88
89 def print_addressbooks():
90         """
91         Included here for debugging.
92
93         Either insert it into the code or launch python with the "-i" flag
94         """
95         if not EvolutionAddressBook.is_supported():
96                 print "No Evolution Support"
97                 return
98
99         eab = EvolutionAddressBook()
100         for book in eab.get_addressbooks():
101                 eab = eab.open_addressbook(book[1])
102                 print book
103                 for contact in eab.get_contacts():
104                         print "\t", contact
105                         for details in eab.get_contact_details(contact[0]):
106                                 print "\t\t", details