-class MergedAddressBook(object):
- """
- Merger of all addressbooks
- """
-
- def __init__(self, addressbookFactories, sorter = None):
- self.__addressbookFactories = addressbookFactories
- self.__addressbooks = None
- self.__sort_contacts = sorter if sorter is not None else self.null_sorter
-
- def clear_caches(self):
- self.__addressbooks = None
- for factory in self.__addressbookFactories:
- factory.clear_caches()
-
- def get_addressbooks(self):
- """
- @returns Iterable of (Address Book Factory, Book Id, Book Name)
- """
- yield self, "", ""
-
- def open_addressbook(self, bookId):
- return self
-
- def contact_source_short_name(self, contactId):
- if self.__addressbooks is None:
- return ""
- bookIndex, originalId = contactId.split("-", 1)
- return self.__addressbooks[int(bookIndex)].contact_source_short_name(originalId)
-
- @staticmethod
- def factory_name():
- return "All Contacts"
-
- def get_contacts(self):
- """
- @returns Iterable of (contact id, contact name)
- """
- if self.__addressbooks is None:
- self.__addressbooks = list(
- factory.open_addressbook(id)
- for factory in self.__addressbookFactories
- for (f, id, name) in factory.get_addressbooks()
- )
- contacts = (
- ("-".join([str(bookIndex), contactId]), contactName)
- for (bookIndex, addressbook) in enumerate(self.__addressbooks)
- for (contactId, contactName) in addressbook.get_contacts()
- )
- sortedContacts = self.__sort_contacts(contacts)
- return sortedContacts
-
- def get_contact_details(self, contactId):
- """
- @returns Iterable of (Phone Type, Phone Number)
- """
- if self.__addressbooks is None:
- return []
- bookIndex, originalId = contactId.split("-", 1)
- return self.__addressbooks[int(bookIndex)].get_contact_details(originalId)
-
- @staticmethod
- def null_sorter(contacts):
- """
- Good for speed/low memory
- """
- return contacts
-
- @staticmethod
- def basic_firtname_sorter(contacts):
- """
- Expects names in "First Last" format
- """
- contactsWithKey = [
- (contactName.rsplit(" ", 1)[0], (contactId, contactName))
- for (contactId, contactName) in contacts
- ]
- contactsWithKey.sort()
- return (contactData for (lastName, contactData) in contactsWithKey)
-
- @staticmethod
- def basic_lastname_sorter(contacts):
- """
- Expects names in "First Last" format
- """
- contactsWithKey = [
- (contactName.rsplit(" ", 1)[-1], (contactId, contactName))
- for (contactId, contactName) in contacts
- ]
- contactsWithKey.sort()
- return (contactData for (lastName, contactData) in contactsWithKey)
-
- @staticmethod
- def reversed_firtname_sorter(contacts):
- """
- Expects names in "Last, First" format
- """
- contactsWithKey = [
- (contactName.split(", ", 1)[-1], (contactId, contactName))
- for (contactId, contactName) in contacts
- ]
- contactsWithKey.sort()
- return (contactData for (lastName, contactData) in contactsWithKey)
-
- @staticmethod
- def reversed_lastname_sorter(contacts):
- """
- Expects names in "Last, First" format
- """
- contactsWithKey = [
- (contactName.split(", ", 1)[0], (contactId, contactName))
- for (contactId, contactName) in contacts
- ]
- contactsWithKey.sort()
- return (contactData for (lastName, contactData) in contactsWithKey)
-
- @staticmethod
- def guess_firstname(name):
- if ", " in name:
- return name.split(", ", 1)[-1]
- else:
- return name.rsplit(" ", 1)[0]
-
- @staticmethod
- def guess_lastname(name):
- if ", " in name:
- return name.split(", ", 1)[0]
- else:
- return name.rsplit(" ", 1)[-1]
-
- @classmethod
- def advanced_firstname_sorter(cls, contacts):
- contactsWithKey = [
- (cls.guess_firstname(contactName), (contactId, contactName))
- for (contactId, contactName) in contacts
- ]
- contactsWithKey.sort()
- return (contactData for (lastName, contactData) in contactsWithKey)
-
- @classmethod
- def advanced_lastname_sorter(cls, contacts):
- contactsWithKey = [
- (cls.guess_lastname(contactName), (contactId, contactName))
- for (contactId, contactName) in contacts
- ]
- contactsWithKey.sort()
- return (contactData for (lastName, contactData) in contactsWithKey)
-
-
-class PhoneTypeSelector(object):
-
- ACTION_CANCEL = "cancel"
- ACTION_SELECT = "select"
- ACTION_DIAL = "dial"
- ACTION_SEND_SMS = "sms"
-
- def __init__(self, widgetTree, gcBackend):
- self._gcBackend = gcBackend
- self._widgetTree = widgetTree
-
- self._dialog = self._widgetTree.get_widget("phonetype_dialog")
- self._smsDialog = SmsEntryDialog(self._widgetTree)
-
- self._smsButton = self._widgetTree.get_widget("sms_button")
- self._smsButton.connect("clicked", self._on_phonetype_send_sms)
-
- self._dialButton = self._widgetTree.get_widget("dial_button")
- self._dialButton.connect("clicked", self._on_phonetype_dial)
-
- self._selectButton = self._widgetTree.get_widget("select_button")
- self._selectButton.connect("clicked", self._on_phonetype_select)
-
- self._cancelButton = self._widgetTree.get_widget("cancel_button")
- self._cancelButton.connect("clicked", self._on_phonetype_cancel)
-
- self._messagemodel = gtk.ListStore(gobject.TYPE_STRING)
- self._messagesView = self._widgetTree.get_widget("phoneSelectionMessages")
- self._scrollWindow = self._messagesView.get_parent()
-
- self._typemodel = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
- self._typeviewselection = None
- self._typeview = self._widgetTree.get_widget("phonetypes")
- self._typeview.connect("row-activated", self._on_phonetype_select)
-
- self._action = self.ACTION_CANCEL
-
- def run(self, contactDetails, messages = (), parent = None):
- self._action = self.ACTION_CANCEL
-
- # Add the column to the phone selection tree view
- self._typemodel.clear()
- self._typeview.set_model(self._typemodel)
-
- textrenderer = gtk.CellRendererText()
- numberColumn = gtk.TreeViewColumn("Phone Numbers", textrenderer, text=0)
- self._typeview.append_column(numberColumn)
-
- textrenderer = gtk.CellRendererText()
- typeColumn = gtk.TreeViewColumn("Phone Type", textrenderer, text=1)
- self._typeview.append_column(typeColumn)
-
- for phoneType, phoneNumber in contactDetails:
- display = " - ".join((phoneNumber, phoneType))
- display = phoneType
- row = (phoneNumber, display)
- self._typemodel.append(row)
-
- self._typeviewselection = self._typeview.get_selection()
- self._typeviewselection.set_mode(gtk.SELECTION_SINGLE)
- self._typeviewselection.select_iter(self._typemodel.get_iter_first())
-
- # Add the column to the messages tree view
- self._messagemodel.clear()
- self._messagesView.set_model(self._messagemodel)
-
- textrenderer = gtk.CellRendererText()
- textrenderer.set_property("wrap-mode", pango.WRAP_WORD)
- textrenderer.set_property("wrap-width", 450)
- messageColumn = gtk.TreeViewColumn("")
- messageColumn.pack_start(textrenderer, expand=True)
- messageColumn.add_attribute(textrenderer, "markup", 0)
- messageColumn.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
- self._messagesView.append_column(messageColumn)
- self._messagesView.set_headers_visible(False)
-
- if messages:
- for message in messages:
- row = (message, )
- self._messagemodel.append(row)
- self._messagesView.show()
- self._scrollWindow.show()
- messagesSelection = self._messagesView.get_selection()
- messagesSelection.select_path((len(messages)-1, ))
- else:
- self._messagesView.hide()
- self._scrollWindow.hide()
-
- if parent is not None:
- self._dialog.set_transient_for(parent)
-
- try:
- self._dialog.show()
- if messages:
- self._messagesView.scroll_to_cell((len(messages)-1, ))