[Telepathy-commits] [telepathy-qt4/master] account-manager.py: allow creation and deletion of accounts

Simon McVittie simon.mcvittie at collabora.co.uk
Wed Jan 7 05:23:29 PST 2009


---
 tests/account-manager.py |  147 +++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 144 insertions(+), 3 deletions(-)

diff --git a/tests/account-manager.py b/tests/account-manager.py
index 36393a5..b3b5e7d 100644
--- a/tests/account-manager.py
+++ b/tests/account-manager.py
@@ -3,6 +3,7 @@
 # A small implementation of a Telepathy AccountManager.
 
 import sys
+import re
 
 import dbus
 from dbus.bus import NAME_FLAG_DO_NOT_QUEUE, REQUEST_NAME_REPLY_EXISTS
@@ -20,6 +21,16 @@ ACCOUNT_IFACE = TP + '.Account'
 ACCOUNT_OBJECT_PATH_BASE = '/' + ACCOUNT_IFACE.replace('.', '/') + '/'
 
 
+Connection_Status_Disconnected = dbus.UInt32(2)
+Connection_Status_Reason_None_Specified = dbus.UInt32(0)
+Connection_Presence_Type_Offline = dbus.UInt32(1)
+Connection_Presence_Type_Available = dbus.UInt32(2)
+
+
+VALID_CONNECTION_MANAGER_NAME = re.compile(r'^[A-Za-z0-9][_A-Za-z0-9]+$')
+VALID_PROTOCOL_NAME = re.compile(r'^[A-Za-z0-9][-A-Za-z0-9]+$')
+
+
 class AccountManager(Object):
     def __init__(self, bus=None):
         #: map from object path to Account
@@ -53,9 +64,9 @@ class AccountManager(Object):
         return dbus.Dictionary({
             'Interfaces': dbus.Array([], signature='s'),
             'ValidAccounts': dbus.Array(self._valid_accounts.keys(),
-                signature='o'),
+                signature='s'),
             'InvalidAccounts': dbus.Array(self._invalid_accounts.keys(),
-                signature='o'),
+                signature='s'),
         }, signature='sv')
 
     @method(dbus.PROPERTIES_IFACE,
@@ -105,8 +116,138 @@ class AccountManager(Object):
 
     @method(AM_IFACE, in_signature='sssa{sv}', out_signature='o')
     def CreateAccount(self, cm, protocol, display_name, parameters):
-        raise NotImplementedError
 
+        if not VALID_CONNECTION_MANAGER_NAME.match(cm):
+            raise ValueError('Invalid CM name')
+
+        if not VALID_PROTOCOL_NAME.match(protocol):
+            raise ValueError('Invalid protocol name')
+
+        base = ACCOUNT_OBJECT_PATH_BASE + cm + '/' + protocol.replace('-', '_')
+
+        # FIXME: This is a stupid way to generate the paths - we should
+        # incorporate the display name somehow. However, it's spec-compliant
+        i = 0
+        while 1:
+            path = '%s/Account%d' % (base, i)
+
+            if (path not in self._valid_accounts and
+                path not in self._invalid_accounts):
+                account = Account(self, path,
+                        '%s (account %d)' % (display_name, i), parameters)
+
+                # put it in the wrong set and move it to the right one -
+                # that's probably the simplest implementation
+                if account._is_valid():
+                    self._invalid_accounts[path] = account
+                    self.AccountValidityChanged(path, True)
+                    assert path not in self._invalid_accounts
+                    assert path in self._valid_accounts
+                else:
+                    self._valid_accounts[path] = account
+                    self.AccountValidityChanged(path, False)
+                    assert path not in self._valid_accounts
+                    assert path in self._invalid_accounts
+
+                return path
+
+            i += 1
+
+        raise AssertionError('Not reached')
+
+class Account(Object):
+    def __init__(self, am, path, display_name, parameters):
+        Object.__init__(self, am.connection, path)
+        self._am = am
+
+        self._display_name = display_name
+        self._icon = u'bob.png'
+        self._enabled = True
+        self._nickname = u'Bob'
+        self._parameters = parameters
+        self._connect_automatically = True
+        self._normalized_name = u'bob'
+        self._automatic_presence = dbus.Struct(
+                (Connection_Presence_Type_Available, 'available', ''),
+                signature='uss')
+        self._current_presence = dbus.Struct(
+                (Connection_Presence_Type_Offline, 'offline', ''),
+                signature='uss')
+        self._requested_presence = dbus.Struct(
+                (Connection_Presence_Type_Offline, 'offline', ''),
+                signature='uss')
+
+    def _is_valid(self):
+        return True
+
+    @method(ACCOUNT_IFACE, in_signature='a{sv}as', out_signature='')
+    def UpdateParameters(self, set_, unset):
+        for (key, value) in set_.iteritems():
+            self._parameters[key] = value
+        for key in unset:
+            self._parameters.pop(key, None)
+
+        AccountPropertyChanged({'Parameters': self._parameters})
+
+    @signal(ACCOUNT_IFACE, signature='a{sv}')
+    def AccountPropertyChanged(self, delta):
+        pass
+
+    @method(ACCOUNT_IFACE, in_signature='', out_signature='')
+    def Remove(self):
+        self.Removed()
+
+    @signal(ACCOUNT_IFACE, signature='')
+    def Removed(self):
+        self._am.AccountRemoved(self.__dbus_object_path__)
+        self.remove_from_connection()
+
+    def _account_props(self):
+        return dbus.Dictionary({
+            'Interfaces': dbus.Array([], signature='s'),
+            'DisplayName': self._display_name,
+            'Icon': self._icon,
+            'Valid': self._is_valid(),
+            'Enabled': self._enabled,
+            'Nickname': self._nickname,
+            'Parameters': self._parameters,
+            'AutomaticPresence': self._automatic_presence,
+            'ConnectAutomatically': self._connect_automatically,
+            'Connection': dbus.ObjectPath('/'),
+            'ConnectionStatus': Connection_Status_Disconnected,
+            'ConnectionStatusReason': Connection_Status_Reason_None_Specified,
+            'CurrentPresence': self._current_presence,
+            'RequestedPresence': self._requested_presence,
+            'NormalizedName': self._normalized_name,
+        }, signature='sv')
+
+    @method(dbus.PROPERTIES_IFACE,
+            in_signature='s',
+            out_signature='a{sv}')
+    def GetAll(self, iface):
+        if iface == ACCOUNT_IFACE:
+            return self._account_props()
+        else:
+            raise ValueError('No such interface')
+
+    @method(dbus.PROPERTIES_IFACE,
+            in_signature='ss',
+            out_signature='v')
+    def Get(self, iface, prop):
+        if iface == ACCOUNT_IFACE:
+            props = self._am_props()
+        else:
+            raise ValueError('No such interface')
+
+        if prop in props:
+            return props[prop]
+        else:
+            raise ValueError('No such property')
+
+    @method(dbus.PROPERTIES_IFACE,
+            in_signature='ssv')
+    def Set(self, iface, prop, value):
+        raise NotImplementedError('Not implemented')
 
 if __name__ == '__main__':
     DBusGMainLoop(set_as_default=True)
-- 
1.5.6.5




More information about the Telepathy-commits mailing list