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