A small assert because I'm annoyed with leaking states
[theonering] / src / aliasing.py
index 4d6c3a3..c22f6bb 100644 (file)
@@ -3,6 +3,7 @@ import logging
 import telepathy
 
 import gtk_toolbox
+import util.misc as util_misc
 import handle
 
 
@@ -35,7 +36,7 @@ def make_pretty(phonenumber):
        if phonenumber is None or phonenumber is "":
                return ""
 
-       phonenumber = handle.strip_number(phonenumber)
+       phonenumber = util_misc.strip_number(phonenumber)
 
        if len(phonenumber) < 3:
                return phonenumber
@@ -61,6 +62,9 @@ def make_pretty(phonenumber):
 
 class AliasingMixin(telepathy.server.ConnectionInterfaceAliasing):
 
+       USER_ALIAS_ACCOUNT = "account"
+       USER_ALIAS_CALLBACK = "callback"
+
        def __init__(self):
                telepathy.server.ConnectionInterfaceAliasing.__init__(self)
 
@@ -72,12 +76,18 @@ class AliasingMixin(telepathy.server.ConnectionInterfaceAliasing):
                raise NotImplementedError("Abstract property called")
 
        @property
-       def handle(self):
+       def userAliasType(self):
                """
                @abstract
                """
                raise NotImplementedError("Abstract property called")
 
+       def handle(self, handleType, handleId):
+               """
+               @abstract
+               """
+               raise NotImplementedError("Abstract function called")
+
        @gtk_toolbox.log_exception(_moduleLogger)
        def GetAliasFlags(self):
                return 0
@@ -100,20 +110,24 @@ class AliasingMixin(telepathy.server.ConnectionInterfaceAliasing):
        @gtk_toolbox.log_exception(_moduleLogger)
        def SetAliases(self, aliases):
                _moduleLogger.debug("Called SetAliases")
+               if self.userAliasType == self.USER_ALIAS_ACCOUNT:
+                       raise telepathy.errors.PermissionDenied("No user customizable aliases")
+               elif self.userAliasType != self.USER_ALIAS_CALLBACK:
+                       raise RuntimeError("Invalid alias type: %r" % self.userAliasType)
 
                # first validate that no other handle types are included
                userHandleAndAlias = None
                for handleId, alias in aliases.iteritems():
                        h = self.handle(telepathy.HANDLE_TYPE_CONTACT, handleId)
                        if not isinstance(h, handle.ConnectionHandle):
-                               raise telepathy.PermissionDenied("No user customizable aliases")
+                               raise telepathy.errors.PermissionDenied("No user customizable aliases")
                        userHandleAndAlias = h, alias
                if userHandleAndAlias is None:
                        _moduleLogger.debug("No user handle")
                        return
 
                # Update callback
-               uglyNumber = handle.strip_number(userHandleAndAlias[1])
+               uglyNumber = util_misc.strip_number(userHandleAndAlias[1])
                self.session.backend.set_callback_number(uglyNumber)
 
                # Inform of change
@@ -123,9 +137,18 @@ class AliasingMixin(telepathy.server.ConnectionInterfaceAliasing):
        def _get_alias(self, handleId):
                h = self.handle(telepathy.HANDLE_TYPE_CONTACT, handleId)
                if isinstance(h, handle.ConnectionHandle):
-                       callbackNumber = self.session.backend.get_callback_number()
-                       userAlias = make_pretty(callbackNumber)
+                       if self.userAliasType == self.USER_ALIAS_CALLBACK:
+                               aliasNumber = self.session.backend.get_callback_number()
+                       elif self.userAliasType == self.USER_ALIAS_ACCOUNT:
+                               aliasNumber = self.session.backend.get_account_number()
+                       else:
+                               raise RuntimeError("Invalid alias type: %r" % self.userAliasType)
+                       userAlias = make_pretty(aliasNumber)
                        return userAlias
                else:
-                       contactAlias = self.session.addressbook.get_contact_name(h.contactID)
+                       contactId = h.contactID
+                       if contactId:
+                               contactAlias = self.session.addressbook.get_contact_name(contactId)
+                       else:
+                               contactAlias = make_pretty(h.phoneNumber)
                        return contactAlias