Changing some references to GC Dialer in the code
[gc-dialer] / src / dialcentral / gc_dialer.py
index a47a467..815fbb2 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/python2.5
 
-# GC Dialer - Front end for Google's Grand Central service.
+# DialCentral - Front end for Google's Grand Central service.
 # Copyright (C) 2008  Mark Bergman bergman AT merctech DOT com
 # 
 # This library is free software; you can redistribute it and/or
@@ -19,7 +19,7 @@
 
 
 """
-Grandcentral Dialer
+DialCentral: A phone dialer using GrandCentral
 """
 
 import sys
@@ -146,7 +146,7 @@ class DummyAddressBook(object):
                return self
 
        @staticmethod
-       def factory_short_name():
+       def contact_source_short_name(contactId):
                return ""
 
        @staticmethod
@@ -168,6 +168,65 @@ class DummyAddressBook(object):
                return []
 
 
+class MergedAddressBook(object):
+       """
+       Merger of all addressbooks
+       """
+
+       def __init__(self, addressbooks, sorter = None):
+               self.__addressbooks = addressbooks
+               self.__sort_contacts = sorter if sorter is not None else self.null_sorter
+
+       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):
+               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)
+               """
+               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)
+               """
+               bookIndex, originalId = contactId.split("-", 1)
+               return self.__addressbooks[int(bookIndex)].get_contact_details(originalId)
+
+       @staticmethod
+       def null_sorter(contacts):
+               return contacts
+
+       @staticmethod
+       def basic_lastname_sorter(contacts):
+               contactsWithKey = [
+                       (contactName.rsplit(" ", 1)[-1], (contactId, contactName))
+                               for (contactId, contactName) in contacts
+               ]
+               contactsWithKey.sort()
+               return (contactData for (lastName, contactData) in contactsWithKey)
+
+
 class PhoneTypeSelector(object):
 
        def __init__(self, widgetTree, gcBackend):
@@ -346,10 +405,12 @@ class Dialpad(object):
                If something can be done after the UI loads, push it here so it's not blocking the UI
                """
                
-               from gc_backend import GCDialer
-               from evo_backend import EvolutionAddressBook
+               import gc_backend
+               import evo_backend
+               import gmail_backend
+               import maemo_backend
 
-               self._gcBackend = GCDialer()
+               self._gcBackend = gc_backend.GCDialer()
 
                try:
                        import osso
@@ -379,11 +440,14 @@ class Dialpad(object):
                        warnings.warn("No Internet Connectivity API ", UserWarning, 2)
 
 
-               self._addressBookFactories = [
+               addressBooks = [
                        self._gcBackend,
+                       evo_backend.EvolutionAddressBook(),
                        DummyAddressBook(),
-                       EvolutionAddressBook(),
                ]
+               mergedBook = MergedAddressBook(addressBooks, MergedAddressBook.basic_lastname_sorter)
+               self._addressBookFactories = list(addressBooks)
+               self._addressBookFactories.insert(0, mergedBook)
                self._addressBook = None
                self.open_addressbook(*self.get_addressbooks().next()[0][0:2])
        
@@ -455,6 +519,13 @@ class Dialpad(object):
                # Add the column to the treeview
                column = gtk.TreeViewColumn("Contact")
 
+               #displayContactSource = False
+               displayContactSource = True
+               if displayContactSource:
+                       textrenderer = gtk.CellRendererText()
+                       column.pack_start(textrenderer, expand=False)
+                       column.add_attribute(textrenderer, 'text', 0)
+
                textrenderer = gtk.CellRendererText()
                column.pack_start(textrenderer, expand=True)
                column.add_attribute(textrenderer, 'text', 1)
@@ -520,8 +591,8 @@ class Dialpad(object):
                contactsview.freeze_child_notify()
                contactsview.set_model(None)
 
-               contactType = (self._addressBook.factory_short_name(),)
                for contactId, contactName in self._addressBook.get_contacts():
+                       contactType = (self._addressBook.contact_source_short_name(contactId),)
                        self._contactsmodel.append(contactType + (contactName, "", contactId) + ("",))
                        yield
 
@@ -567,6 +638,8 @@ class Dialpad(object):
                                gtk.gdk.threads_leave()
                                return True
 
+               return False
+
        def display_error_message(self, msg):
                error_dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, msg)
 
@@ -727,12 +800,15 @@ class Dialpad(object):
                self._contactsviewselection.unselect_all()
 
        def _on_notebook_switch_page(self, notebook, page, page_num):
-               if page_num == 1 and 300 < (time.time() - self._contactstime):
-                       threading.Thread(target=self._idly_populate_contactsview).start()
-               elif page_num == 2 and 300 < (time.time() - self._recenttime):
-                       threading.Thread(target=self._idly_populate_recentview).start()
-               #elif page_num == 3 and self._callbackNeedsSetup:
-               #       gobject.idle_add(self._idly_populate_callback_combo)
+               if page_num == 1:
+                       if 300 < (time.time() - self._contactstime):
+                               threading.Thread(target=self._idly_populate_contactsview).start()
+               elif page_num == 3:
+                       if 300 < (time.time() - self._recenttime):
+                               threading.Thread(target=self._idly_populate_recentview).start()
+               #elif page_num == 2:
+               #       self._callbackNeedsSetup::
+               #               gobject.idle_add(self._idly_populate_callback_combo)
 
                tabTitle = self._notebook.get_tab_label(self._notebook.get_nth_page(page_num)).get_text()
                if hildon is not None: