Adding a backend for QtMobility Contacts
[gc-dialer] / src / backends / qt_backend.py
1 #!/usr/bin/env python
2
3 from __future__ import with_statement
4 from __future__ import division
5
6 import logging
7
8 import util.qt_compat as qt_compat
9 if qt_compat.USES_PYSIDE:
10         import QtMobility.Contacts as _QtContacts
11         QtContacts = _QtContacts
12 else:
13         QtContacts = None
14
15 import null_backend
16
17
18 _moduleLogger = logging.getLogger(__name__)
19
20
21 class QtContactsAddressBook(object):
22
23         def __init__(self, name, uri):
24                 self._name = name
25                 self._uri = uri
26                 self._manager = QtContacts.QContactManager.fromUri(uri)
27                 self._contacts = None
28
29         @property
30         def name(self):
31                 return self._name
32
33         @property
34         def error(self):
35                 return self._manager.error()
36
37         def update_account(self, force = True):
38                 if not force and self._contacts is not None:
39                         return
40                 self._contacts = dict(self._get_contacts())
41
42         def get_contacts(self):
43                 if self._contacts is None:
44                         self._contacts = dict(self._get_contacts())
45                 return self._contacts
46
47         def _get_contacts(self):
48                 contacts = self._manager.contacts()
49                 for contact in contacts:
50                         contactId = contact.localId()
51                         contactName = contact.displayLabel()
52                         phoneDetails = contact.details(QtContacts.QContactPhoneNumber().DefinitionName)
53                         phones = [{"phoneType": "Phone", "phoneNumber": phone.value(QtContacts.QContactPhoneNumber().FieldNumber)} for phone in phoneDetails]
54                         contactDetails = phones
55                         if 0 < len(contactDetails):
56                                 yield str(contactId), {
57                                         "contactId": str(contactId),
58                                         "name": contactName,
59                                         "numbers": contactDetails,
60                                 }
61
62
63 class _QtContactsAddressBookFactory(object):
64
65         def __init__(self):
66                 self._availableManagers = {}
67
68                 availableMgrs = QtContacts.QContactManager.availableManagers()
69                 availableMgrs.remove("invalid")
70                 for managerName in availableMgrs:
71                         params = {}
72                         managerUri = QtContacts.QContactManager.buildUri(managerName, params)
73                         self._availableManagers[managerName] =  managerUri
74
75         def get_addressbooks(self):
76                 for name, uri in self._availableManagers.iteritems():
77                         book = QtContactsAddressBook(name, uri)
78                         if book.error:
79                                 _moduleLogger.info("Could not load %r due to %r" % (name, book.error))
80                         else:
81                                 yield book
82
83
84 class _EmptyAddressBookFactory(object):
85
86         def get_addressbooks(self):
87                 if False:
88                         yield None
89
90
91 if QtContacts is not None:
92         QtContactsAddressBookFactory = _QtContactsAddressBookFactory
93 else:
94         QtContactsAddressBookFactory = _EmptyAddressBookFactory
95         _moduleLogger.info("QtContacts support not available")
96
97
98 if __name__ == "__main__":
99         factory = QtContactsAddressBookFactory()
100         books = factory.get_addressbooks()
101         for book in books:
102                 print book.name
103                 print book.get_contacts()