Rearranging things a bit
[gc-dialer] / src / evo_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 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         @note Combined the factory and the addressbook for "simplicity" and "cutting down" the number of allocations/deallocations
35         """
36
37         def __init__(self, bookId = None):
38                 if not self.is_supported():
39                         return
40
41                 self._phoneTypes = None
42                 if bookId is not None:
43                         self._bookId = bookId
44                 else:
45                         try:
46                                 self._bookId = [
47                                         bookData[1]
48                                                 for bookData in self.get_addressbooks()
49                                 ][0]
50                         except IndexError:
51                                 global evolution
52                                 evolution = None
53                 self._book = evolution.ebook.open_addressbook(self._bookId)
54         
55         @classmethod
56         def is_supported(cls):
57                 return evolution is not None
58
59         def get_addressbooks(self):
60                 """
61                 @returns Iterable of (Address Book Factory, Book Id, Book Name)
62                 """
63                 if not self.is_supported():
64                         return
65
66                 if len(evolution.ebook.list_addressbooks()) == 0 and evolution.ebook.open_addressbook('default') is not None:
67                         # It appears that Maemo's e-d-s does not always list the default addressbook, so we're faking it being listed
68                         yield self, "default", "Maemo"
69
70                 for bookId in evolution.ebook.list_addressbooks():
71                         yield self, bookId[1], bookId[0]
72         
73         def open_addressbook(self, bookId):
74                 self._bookId = bookId
75                 self._book = evolution.ebook.open_addressbook(self._bookId)
76                 return self
77
78         @staticmethod
79         def contact_source_short_name(contactId):
80                 return "Evo"
81
82         @staticmethod
83         def factory_name():
84                 return "Evolution"
85
86         def get_contacts(self):
87                 """
88                 @returns Iterable of (contact id, contact name)
89                 """
90                 if not self.is_supported():
91                         return
92
93                 for contact in self._book.get_all_contacts():
94                         yield str(contact.get_uid()), contact.props.full_name
95         
96         def get_contact_details(self, contactId):
97                 """
98                 @returns Iterable of (Phone Type, Phone Number)
99                 """
100                 contact = self._book.get_contact(int(contactId))
101
102                 if self._phoneTypes is None and contact is not None:
103                         self._phoneTypes = [pt for pt in dir(contact.props) if "phone" in pt.lower()]
104
105                 for phoneType in self._phoneTypes:
106                         phoneNumber = getattr(contact.props, phoneType)
107                         if isinstance(phoneNumber, str):
108                                 yield phoneType, phoneNumber
109
110
111 def print_evobooks():
112         """
113         Included here for debugging.
114
115         Either insert it into the code or launch python with the "-i" flag
116         """
117         if not EvolutionAddressBook.is_supported():
118                 print "No Evolution Support"
119                 return
120
121         eab = EvolutionAddressBook()
122         for book in eab.get_addressbooks():
123                 eab = eab.open_addressbook(book[1])
124                 print book
125                 for contact in eab.get_contacts():
126                         print "\t", contact
127                         for details in eab.get_contact_details(contact[0]):
128                                 print "\t\t", details