Switching away from using contact ids
authorEd Page <eopage@byu.net>
Tue, 19 Jan 2010 01:16:14 +0000 (19:16 -0600)
committerEd Page <eopage@byu.net>
Tue, 19 Jan 2010 01:16:14 +0000 (19:16 -0600)
src/aliasing.py
src/channel/contact_list.py
src/channel/text.py
src/connection.py
src/gvoice/addressbook.py
src/gvoice/conversations.py
src/handle.py

index 936ff70..cd107ab 100644 (file)
@@ -137,18 +137,18 @@ class AliasingMixin(tp.ConnectionInterfaceAliasing):
                else:
                        raise telepathy.errors.PermissionDenied("No user customizable aliases")
 
-               if len(alias) == 0:
+               uglyNumber = util_misc.normalize_number(alias)
+               if len(uglyNumber) == 0:
                        # Reset to the original from login if one was provided
-                       alias = self.callbackNumberParameter
-               if not util_misc.is_valid_number(alias):
-                       raise telepathy.errors.InvalidArgument("Invalid phone number %r" % (alias, ))
+                       uglyNumber = self.callbackNumberParameter
+               if not util_misc.is_valid_number(uglyNumber):
+                       raise telepathy.errors.InvalidArgument("Invalid phone number %r" % (uglyNumber, ))
 
                # Update callback
-               uglyNumber = util_misc.normalize_number(alias)
                self.session.backend.set_callback_number(uglyNumber)
 
                # Inform of change
-               userAlias = make_pretty(alias)
+               userAlias = make_pretty(uglyNumber)
                changedAliases = ((handleId, userAlias), )
                self.AliasesChanged(changedAliases)
 
@@ -159,9 +159,9 @@ class AliasingMixin(tp.ConnectionInterfaceAliasing):
                        userAlias = make_pretty(aliasNumber)
                        return userAlias
                else:
-                       contactId = h.contactID
-                       if contactId:
-                               contactAlias = self.session.addressbook.get_contact_name(contactId)
+                       number = h.phoneNumber
+                       if number:
+                               contactAlias = self.session.addressbook.get_contact_name(number)
                        else:
-                               contactAlias = make_pretty(h.phoneNumber)
+                               contactAlias = make_pretty(number)
                        return contactAlias
index cd7175a..4352645 100644 (file)
@@ -42,7 +42,7 @@ class AllContactsListChannel(
                self.GroupFlagsChanged(0, 0)
 
                addressbook = connection.session.addressbook
-               contacts = addressbook.get_contact_ids()
+               contacts = addressbook.get_numbers()
                self._process_refresh(addressbook, set(contacts), set())
 
 
@@ -69,14 +69,12 @@ class AllContactsListChannel(
                )
                connection = self._conn
                handlesAdded = [
-                       handle.create_handle(connection, "contact", contactId, phoneNumber)
-                       for contactId in added
-                       for (phoneType, phoneNumber) in addressbook.get_contact_details(contactId)
+                       handle.create_handle(connection, "contact", contactNumber)
+                       for contactNumber in added
                ]
                handlesRemoved = [
-                       handle.create_handle(connection, "contact", contactId, phoneNumber)
-                       for contactId in removed
-                       for (phoneType, phoneNumber) in addressbook.get_contact_details(contactId)
+                       handle.create_handle(connection, "contact", contactNumber)
+                       for contactNumber in removed
                ]
                message = ""
                actor = 0
index bf162f4..6a6e60a 100644 (file)
@@ -41,7 +41,6 @@ class TextChannel(tp.ChannelTypeText):
 
                # The only reason there should be anything in the conversation is if
                # its new, so report it all
-               # @bug When a channel is supposed to have some with and without a contact id, this will return weird results.  I might have to just drop the whole contactid thing
                try:
                        mergedConversations = self._conn.session.voicemails.get_conversation(self._contactKey)
                except KeyError:
@@ -84,7 +83,7 @@ class TextChannel(tp.ChannelTypeText):
 
        @property
        def _contactKey(self):
-               contactKey = self.__otherHandle.contactID, self.__otherHandle.phoneNumber
+               contactKey = self.__otherHandle.phoneNumber
                return contactKey
 
        @gtk_toolbox.log_exception(_moduleLogger)
index cc98363..29fbf9a 100644 (file)
@@ -137,17 +137,7 @@ class TheOneRingConnection(
                requestedHandleName = handleName.encode('utf-8')
                if handleType == telepathy.HANDLE_TYPE_CONTACT:
                        _moduleLogger.info("get_handle_by_name Contact: %s" % requestedHandleName)
-                       requestedContactId, requestedContactNumber = handle.ContactHandle.from_handle_name(
-                               requestedHandleName
-                       )
-                       if not requestedContactId:
-                               # Sometimes GV doesn't give us a contactid for contacts, so
-                               # let's slow things down just a tad for better consistency for
-                               # the user
-                               ids = list(self.session.addressbook.find_contacts_with_number(requestedContactNumber))
-                               if ids:
-                                       requestedContactId = ids[0]
-                       h = handle.create_handle(self, 'contact', requestedContactId, requestedContactNumber)
+                       h = handle.create_handle(self, 'contact', requestedHandleName)
                elif handleType == telepathy.HANDLE_TYPE_LIST:
                        # Support only server side (immutable) lists
                        _moduleLogger.info("get_handle_by_name List: %s" % requestedHandleName)
@@ -292,9 +282,8 @@ class TheOneRingConnection(
        @gtk_toolbox.log_exception(_moduleLogger)
        def _on_conversations_updated(self, conv, conversationIds):
                _moduleLogger.debug("Incoming messages from: %r" % (conversationIds, ))
-               for contactId, phoneNumber in conversationIds:
-                       handleName = handle.ContactHandle.to_handle_name(contactId, phoneNumber)
-                       h = self.get_handle_by_name(telepathy.HANDLE_TYPE_CONTACT, handleName)
+               for phoneNumber in conversationIds:
+                       h = self.get_handle_by_name(telepathy.HANDLE_TYPE_CONTACT, phoneNumber)
                        # Just let the TextChannel decide whether it should be reported to the user or not
                        props = self._generate_props(telepathy.CHANNEL_TYPE_TEXT, h, False)
                        chan = self.__channelManager.channel_for_props(props, signal=True)
@@ -303,6 +292,8 @@ class TheOneRingConnection(
        def _on_connection_change(self, connection, event):
                """
                @note Maemo specific
+
+               @todo Make this delayed to handle background switching of networks
                """
                status = event.get_status()
                error = event.get_error()
index 839c8f6..99cd721 100644 (file)
@@ -14,70 +14,55 @@ class Addressbook(object):
 
        def __init__(self, backend):
                self._backend = backend
-               self._contacts = {}
+               self._numbers = {}
 
                self.updateSignalHandler = coroutines.CoTee()
 
        def update(self, force=False):
-               if not force and self._contacts:
+               if not force and self._numbers:
                        return
-               oldContacts = self._contacts
-               oldContactIds = set(self.get_contact_ids())
+               oldContacts = self._numbers
+               oldContactNumbers = set(self.get_numbers())
 
-               self._contacts = {}
+               self._numbers = {}
                self._populate_contacts()
-               newContactIds = set(self.get_contact_ids())
+               newContactNumbers = set(self.get_numbers())
 
-               addedContacts = newContactIds - oldContactIds
-               removedContacts = oldContactIds - newContactIds
+               addedContacts = newContactNumbers - oldContactNumbers
+               removedContacts = oldContactNumbers - newContactNumbers
                changedContacts = set(
-                       contactId
-                       for contactId in newContactIds.intersection(oldContactIds)
-                       if self._has_contact_changed(contactId, oldContacts)
+                       contactNumber
+                       for contactNumber in newContactNumbers.intersection(oldContactNumbers)
+                       if self._numbers[contactNumber] != oldContacts[contactNumber]
                )
 
                if addedContacts or removedContacts or changedContacts:
                        message = self, addedContacts, removedContacts, changedContacts
                        self.updateSignalHandler.stage.send(message)
 
-       def get_contact_ids(self):
-               return self._contacts.iterkeys()
+       def get_numbers(self):
+               return self._numbers.iterkeys()
 
-       def get_contact_name(self, contactId):
-               return self._contacts[contactId][0]
+       def get_contact_name(self, strippedNumber):
+               return self._numbers[strippedNumber][0]
 
-       def get_contact_details(self, contactId):
-               return iter(self._contacts[contactId][1])
-
-       def find_contacts_with_number(self, queryNumber):
-               strippedQueryNumber = util_misc.normalize_number(queryNumber)
-               for contactId, (contactName, contactDetails) in self._contacts.iteritems():
-                       for phoneType, number in contactDetails:
-                               if number == strippedQueryNumber:
-                                       yield contactId
+       def get_phone_type(self, strippedNumber):
+               return self._numbers[strippedNumber][1]
 
        def _populate_contacts(self):
-               if self._contacts:
+               if self._numbers:
                        return
                contacts = self._backend.get_contacts()
                for contactId, contactDetails in contacts:
                        contactName = contactDetails["name"]
-                       contactNumbers = [
+                       contactNumbers = (
                                (
                                        numberDetails.get("phoneType", "Mobile"),
                                        util_misc.normalize_number(numberDetails["phoneNumber"]),
                                )
                                for numberDetails in contactDetails["numbers"]
-                       ]
-                       self._contacts[contactId] = (contactName, contactNumbers)
-
-       def _has_contact_changed(self, contactId, oldContacts):
-               oldContact = oldContacts[contactId]
-               oldContactName = oldContact[0]
-               oldContactDetails = oldContact[1]
-               if oldContactName != self.get_contact_name(contactId):
-                       return True
-               if not oldContactDetails:
-                       return False
-               # if its already in the old cache, purposefully add it into the new cache
-               return oldContactDetails != self.get_contact_details(contactId)
+                       )
+                       self._numbers.update(
+                               (number, (contactName, phoneType))
+                               for (phoneType, number) in contactNumbers
+                       )
index 1ad9ae9..338ebc6 100644 (file)
@@ -54,7 +54,7 @@ class Conversations(object):
                conversations = list(self._get_raw_conversations())
                conversations.sort()
                for conversation in conversations:
-                       key = conversation.contactId, util_misc.normalize_number(conversation.number)
+                       key = util_misc.normalize_number(conversation.number)
                        try:
                                mergedConversations = self._conversations[key]
                        except KeyError:
@@ -112,7 +112,7 @@ class MergedConversations(object):
                if not self._conversations:
                        return
 
-               for constantField in ("contactId", "number"):
+               for constantField in ("number", ):
                        assert getattr(self._conversations[0], constantField) == getattr(newConversation, constantField), "Constant field changed, soemthing is seriously messed up: %r v %r" % (
                                getattr(self._conversations[0], constantField),
                                getattr(newConversation, constantField),
index c2e4bf8..02e5137 100644 (file)
@@ -41,65 +41,24 @@ class ConnectionHandle(TheOneRingHandle):
 
 class ContactHandle(TheOneRingHandle):
 
-       _DELIMETER = "|"
+       def __init__(self, connection, id, phoneNumber):
+               self._phoneNumber = util_misc.normalize_number(phoneNumber)
 
-       def __init__(self, connection, id, contactId, phoneNumber):
                handleType = telepathy.HANDLE_TYPE_CONTACT
-               handleName = self.to_handle_name(contactId, phoneNumber)
+               handleName = self._phoneNumber
                TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
 
-               self._contactId = contactId
-               self._phoneNumber = util_misc.normalize_number(phoneNumber)
-
-       @classmethod
-       def from_handle_name(cls, handleName):
-               """
-               >>> ContactHandle.from_handle_name("+1 555 123-1234")
-               ('', '+15551231234')
-               >>> ContactHandle.from_handle_name("+15551231234")
-               ('', '+15551231234')
-               >>> ContactHandle.from_handle_name("123456|+15551231234")
-               ('123456', '+15551231234')
-               """
-               parts = handleName.split(cls._DELIMETER, 1)
-               if len(parts) == 2:
-                       contactId, contactNumber = parts[0:2]
-               elif len(parts) == 1:
-                       contactId, contactNumber = "", handleName
-               else:
-                       raise RuntimeError("Invalid handle: %s" % handleName)
-
-               contactNumber = util_misc.normalize_number(contactNumber)
-               return contactId, contactNumber
-
-       @classmethod
-       def to_handle_name(cls, contactId, contactNumber):
-               """
-               >>> ContactHandle.to_handle_name('', "+1 555 123-1234")
-               '+15551231234'
-               >>> ContactHandle.to_handle_name('', "+15551231234")
-               '+15551231234'
-               >>> ContactHandle.to_handle_name('123456', "+15551231234")
-               '123456|+15551231234'
-               """
-               contactNumber = util_misc.normalize_number(contactNumber)
-               if contactId:
-                       handleName = cls._DELIMETER.join((contactId, contactNumber))
-               else:
-                       handleName = contactNumber
-               return handleName
-
-       @property
-       def contactID(self):
-               return self._contactId
-
        @property
        def phoneNumber(self):
                return self._phoneNumber
 
        @property
+       def contact_name(self):
+               return self._conn.session.addressbook.get_contact_name(self.phoneNumber)
+
+       @property
        def contactDetails(self):
-               return self._conn.addressbook.get_contact_details(self._id)
+               return self._conn.session.addressbook.get_phone_type(self.phoneNumber)
 
 
 class ListHandle(TheOneRingHandle):