cbed08a6e647a935e02ed6d186a8d04f08495a02
[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 It seems the evolution contact API used is specific to the desktop.  evolution.ebook combined with abook is what is needed for Maemo.
25         http://maemo.org/maemo_release_documentation/maemo4.1.x/node8.html#SECTION00870000000000000000
26         https://garage.maemo.org/svn/pymaemo/packages/python-abook/trunk/tests/ especially contact_get_iter amd filter_model
27         http://pymaemo.garage.maemo.org/documentation/api/abook/index.html
28 """
29
30
31 try:
32         import evolution
33 except ImportError:
34         evolution = None
35
36
37 class EvolutionAddressBook(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, bookId = None):
43                 if not self.is_supported():
44                         return
45
46                 self._phoneTypes = None
47                 if bookId is not None:
48                         self._bookId = bookId
49                 else:
50                         try:
51                                 self._bookId = [
52                                         bookData[1]
53                                                 for bookData in self.get_addressbooks()
54                                 ][0]
55                         except IndexError:
56                                 global evolution
57                                 evolution = None
58                                 return
59                 self._book = evolution.ebook.open_addressbook(self._bookId)
60
61         @classmethod
62         def is_supported(cls):
63                 return evolution is not None
64
65         def get_addressbooks(self):
66                 """
67                 @returns Iterable of (Address Book Factory, Book Id, Book Name)
68                 """
69                 if not self.is_supported():
70                         return
71
72                 if len(evolution.ebook.list_addressbooks()) == 0 and evolution.ebook.open_addressbook('default') is not None:
73                         # It appears that Maemo's e-d-s does not always list the default addressbook, so we're faking it being listed
74                         yield self, "default", "Maemo"
75
76                 for bookId in evolution.ebook.list_addressbooks():
77                         yield self, bookId[1], bookId[0]
78
79         def open_addressbook(self, bookId):
80                 self._bookId = bookId
81                 self._book = evolution.ebook.open_addressbook(self._bookId)
82                 return self
83
84         @staticmethod
85         def contact_source_short_name(contactId):
86                 return "Evo"
87
88         @staticmethod
89         def factory_name():
90                 return "Evolution"
91
92         def get_contacts(self):
93                 """
94                 @returns Iterable of (contact id, contact name)
95                 """
96                 if not self.is_supported():
97                         return
98
99                 for contact in self._book.get_all_contacts():
100                         yield str(contact.get_uid()), contact.props.full_name
101
102         def get_contact_details(self, contactId):
103                 """
104                 @returns Iterable of (Phone Type, Phone Number)
105                 """
106                 contact = self._book.get_contact(int(contactId))
107
108                 if self._phoneTypes is None and contact is not None:
109                         self._phoneTypes = [pt for pt in dir(contact.props) if "phone" in pt.lower()]
110
111                 for phoneType in self._phoneTypes:
112                         phoneNumber = getattr(contact.props, phoneType)
113                         if isinstance(phoneNumber, str):
114                                 yield phoneType, phoneNumber
115
116
117 def print_evobooks():
118         """
119         Included here for debugging.
120
121         Either insert it into the code or launch python with the "-i" flag
122         """
123         if not EvolutionAddressBook.is_supported():
124                 print "No Evolution Support"
125                 return
126
127         eab = EvolutionAddressBook()
128         for book in eab.get_addressbooks():
129                 eab = eab.open_addressbook(book[1])
130                 print book
131                 for contact in eab.get_contacts():
132                         print "\t", contact
133                         for details in eab.get_contact_details(contact[0]):
134                                 print "\t\t", details