Switching to a fremantle specific profile file
[theonering] / src / aliasing.py
index 02a483e..bf73e06 100644 (file)
@@ -6,6 +6,12 @@ import gtk_toolbox
 import handle
 
 
+USER_ALIAS_ACCOUNT = "account"
+USER_ALIAS_CALLBACK = "callback"
+
+USER_ALIAS = USER_ALIAS_ACCOUNT
+
+
 _moduleLogger = logging.getLogger('aliasing')
 
 
@@ -71,12 +77,11 @@ class AliasingMixin(telepathy.server.ConnectionInterfaceAliasing):
                """
                raise NotImplementedError("Abstract property called")
 
-       @property
-       def handle(self):
+       def handle(self, handleType, handleId):
                """
                @abstract
                """
-               raise NotImplementedError("Abstract property called")
+               raise NotImplementedError("Abstract function called")
 
        @gtk_toolbox.log_exception(_moduleLogger)
        def GetAliasFlags(self):
@@ -99,15 +104,46 @@ class AliasingMixin(telepathy.server.ConnectionInterfaceAliasing):
 
        @gtk_toolbox.log_exception(_moduleLogger)
        def SetAliases(self, aliases):
-               # @todo Look into setting callback number through this mechanism
-               raise telepathy.PermissionDenied("No user customizable aliases")
+               _moduleLogger.debug("Called SetAliases")
+               if USER_ALIAS == USER_ALIAS_ACCOUNT:
+                       raise telepathy.errors.PermissionDenied("No user customizable aliases")
+               elif USER_ALIAS != USER_ALIAS_CALLBACK:
+                       raise RuntimeError("Invalid alias type: %r" % USER_ALIAS)
+
+               # 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.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])
+               self.session.backend.set_callback_number(uglyNumber)
+
+               # Inform of change
+               changedAliases = (userHandleAndAlias, )
+               self.AliasesChanged(changedAliases)
 
        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 USER_ALIAS == USER_ALIAS_CALLBACK:
+                               aliasNumber = self.session.backend.get_callback_number()
+                       elif USER_ALIAS == USER_ALIAS_ACCOUNT:
+                               aliasNumber = self.session.backend.get_account_number()
+                       else:
+                               raise RuntimeError("Invalid alias type: %r" % USER_ALIAS)
+                       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