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)
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
self.GroupFlagsChanged(0, 0)
addressbook = connection.session.addressbook
- contacts = addressbook.get_contact_ids()
+ contacts = addressbook.get_numbers()
self._process_refresh(addressbook, set(contacts), set())
)
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
# 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:
@property
def _contactKey(self):
- contactKey = self.__otherHandle.contactID, self.__otherHandle.phoneNumber
+ contactKey = self.__otherHandle.phoneNumber
return contactKey
@gtk_toolbox.log_exception(_moduleLogger)
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)
@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)
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()
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
+ )
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:
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),
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):