Creating a hollow shell of a UI
[gc-dialer] / src / backends / merge_backend.py
1 import logging
2
3
4 _moduleLogger = logging.getLogger(__name__)
5
6
7 class MergedAddressBook(object):
8         """
9         Merger of all addressbooks
10         """
11
12         def __init__(self, addressbookFactories, sorter = None):
13                 self.__addressbookFactories = addressbookFactories
14                 self.__addressbooks = None
15                 self.__sort_contacts = sorter if sorter is not None else self.null_sorter
16
17         def clear_caches(self):
18                 self.__addressbooks = None
19                 for factory in self.__addressbookFactories:
20                         factory.clear_caches()
21
22         def get_addressbooks(self):
23                 """
24                 @returns Iterable of (Address Book Factory, Book Id, Book Name)
25                 """
26                 yield self, "", ""
27
28         def open_addressbook(self, bookId):
29                 return self
30
31         def contact_source_short_name(self, contactId):
32                 if self.__addressbooks is None:
33                         return ""
34                 bookIndex, originalId = contactId.split("-", 1)
35                 return self.__addressbooks[int(bookIndex)].contact_source_short_name(originalId)
36
37         @staticmethod
38         def factory_name():
39                 return "All Contacts"
40
41         def get_contacts(self):
42                 """
43                 @returns Iterable of (contact id, contact name)
44                 """
45                 if self.__addressbooks is None:
46                         self.__addressbooks = list(
47                                 factory.open_addressbook(id)
48                                 for factory in self.__addressbookFactories
49                                 for (f, id, name) in factory.get_addressbooks()
50                         )
51                 contacts = (
52                         ("-".join([str(bookIndex), contactId]), contactName)
53                                 for (bookIndex, addressbook) in enumerate(self.__addressbooks)
54                                         for (contactId, contactName) in addressbook.get_contacts()
55                 )
56                 sortedContacts = self.__sort_contacts(contacts)
57                 return sortedContacts
58
59         def get_contact_details(self, contactId):
60                 """
61                 @returns Iterable of (Phone Type, Phone Number)
62                 """
63                 if self.__addressbooks is None:
64                         return []
65                 bookIndex, originalId = contactId.split("-", 1)
66                 return self.__addressbooks[int(bookIndex)].get_contact_details(originalId)
67
68         @staticmethod
69         def null_sorter(contacts):
70                 """
71                 Good for speed/low memory
72                 """
73                 return contacts
74
75         @staticmethod
76         def basic_firtname_sorter(contacts):
77                 """
78                 Expects names in "First Last" format
79                 """
80                 contactsWithKey = [
81                         (contactName.rsplit(" ", 1)[0], (contactId, contactName))
82                                 for (contactId, contactName) in contacts
83                 ]
84                 contactsWithKey.sort()
85                 return (contactData for (lastName, contactData) in contactsWithKey)
86
87         @staticmethod
88         def basic_lastname_sorter(contacts):
89                 """
90                 Expects names in "First Last" format
91                 """
92                 contactsWithKey = [
93                         (contactName.rsplit(" ", 1)[-1], (contactId, contactName))
94                                 for (contactId, contactName) in contacts
95                 ]
96                 contactsWithKey.sort()
97                 return (contactData for (lastName, contactData) in contactsWithKey)
98
99         @staticmethod
100         def reversed_firtname_sorter(contacts):
101                 """
102                 Expects names in "Last, First" format
103                 """
104                 contactsWithKey = [
105                         (contactName.split(", ", 1)[-1], (contactId, contactName))
106                                 for (contactId, contactName) in contacts
107                 ]
108                 contactsWithKey.sort()
109                 return (contactData for (lastName, contactData) in contactsWithKey)
110
111         @staticmethod
112         def reversed_lastname_sorter(contacts):
113                 """
114                 Expects names in "Last, First" format
115                 """
116                 contactsWithKey = [
117                         (contactName.split(", ", 1)[0], (contactId, contactName))
118                                 for (contactId, contactName) in contacts
119                 ]
120                 contactsWithKey.sort()
121                 return (contactData for (lastName, contactData) in contactsWithKey)
122
123         @staticmethod
124         def guess_firstname(name):
125                 if ", " in name:
126                         return name.split(", ", 1)[-1]
127                 else:
128                         return name.rsplit(" ", 1)[0]
129
130         @staticmethod
131         def guess_lastname(name):
132                 if ", " in name:
133                         return name.split(", ", 1)[0]
134                 else:
135                         return name.rsplit(" ", 1)[-1]
136
137         @classmethod
138         def advanced_firstname_sorter(cls, contacts):
139                 contactsWithKey = [
140                         (cls.guess_firstname(contactName), (contactId, contactName))
141                                 for (contactId, contactName) in contacts
142                 ]
143                 contactsWithKey.sort()
144                 return (contactData for (lastName, contactData) in contactsWithKey)
145
146         @classmethod
147         def advanced_lastname_sorter(cls, contacts):
148                 contactsWithKey = [
149                         (cls.guess_lastname(contactName), (contactId, contactName))
150                                 for (contactId, contactName) in contacts
151                 ]
152                 contactsWithKey.sort()
153                 return (contactData for (lastName, contactData) in contactsWithKey)