diff --git a/src/sugar/presence/util.py b/src/sugar/presence/util.py new file mode 100644 index 00000000..265e3902 --- /dev/null +++ b/src/sugar/presence/util.py @@ -0,0 +1,53 @@ +import logging + +import dbus +from dbus import PROPERTIES_IFACE +from telepathy.interfaces import ACCOUNT, \ + ACCOUNT_MANAGER + +ACCOUNT_MANAGER_SERVICE = 'org.freedesktop.Telepathy.AccountManager' +ACCOUNT_MANAGER_PATH = '/org/freedesktop/Telepathy/AccountManager' + +class ConnectionManager(object): + def __init__(self): + self.connections_per_account = [] + + bus = dbus.SessionBus() + obj = bus.get_object(ACCOUNT_MANAGER_SERVICE, ACCOUNT_MANAGER_PATH) + account_manager = dbus.Interface(obj, ACCOUNT_MANAGER) + + logging.info('KILL_PS listen for accounts coming and going') + #account_manager.connect_to_signal('AccountValidityChanged', + # self.__account_validity_changed_cb) + + account_paths = account_manager.Get(ACCOUNT_MANAGER, 'ValidAccounts', + dbus_interface=PROPERTIES_IFACE) + for account_path in account_paths: + obj = bus.get_object(ACCOUNT_MANAGER_SERVICE, account_path) + #obj.connect_to_signal('AccountPropertyChanged', + # self.__account_property_changed_cb) + connection_path = obj.Get(ACCOUNT, 'Connection') + if connection_path == '/': + continue + + connection_name = connection_path.replace('/', '.')[1:] + connection = bus.get_object(connection_name, connection_path) + self.connections[account_path] = connection + + def get_preferred_connection(self): + best_connection = None, None + for account_path, connection in self.connections.items(): + if 'salut' in connection.object_path: + best_connection = account_path, connection + elif 'gabble' in connection.object_path: + best_connection = account_path, connection + break + return best_connection + +_connection_manager = None + +def get_connection_manager(): + global _connection_manager + if not _connection_manager: + _connection_manager = ConnectionManager() + return _connection_manager