Make sure that the connection we choose is connected

master
Tomeu Vizoso 14 years ago
parent ba80f2a2c1
commit c0ed611aee

@ -1,13 +1,39 @@
# Copyright (C) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import logging
from functools import partial
import dbus
from dbus import PROPERTIES_IFACE
from telepathy.interfaces import ACCOUNT, \
ACCOUNT_MANAGER
ACCOUNT_MANAGER, \
CONNECTION
from telepathy.constants import CONNECTION_STATUS_CONNECTED
ACCOUNT_MANAGER_SERVICE = 'org.freedesktop.Telepathy.AccountManager'
ACCOUNT_MANAGER_PATH = '/org/freedesktop/Telepathy/AccountManager'
class Connection(object):
def __init__(self, account_path, connection):
self.account_path = account_path
self.connection = connection
self.connected = False
class ConnectionManager(object):
def __init__(self):
self._connections_per_account = {}
@ -15,44 +41,64 @@ class ConnectionManager(object):
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)
obj.connect_to_signal('AccountPropertyChanged',
partial(self.__account_property_changed_cb, account_path))
connection_path = obj.Get(ACCOUNT, 'Connection')
if connection_path == '/':
continue
if connection_path != '/':
self._track_connection(account_path, connection_path)
def __account_property_changed_cb(self, account_path, properties):
if 'Connection' not in properties:
return
if properties['Connection'] == '/':
if account_path in self._connections_per_account:
del self._connections_per_account[account_path]
else:
self._track_connection(account_path, properties['Connection'])
def _track_connection(self, account_path, connection_path):
connection_name = connection_path.replace('/', '.')[1:]
bus = dbus.SessionBus()
connection = bus.get_object(connection_name, connection_path)
connection.connect_to_signal('StatusChanged',
partial(self.__status_changed_cb, account_path))
self._connections_per_account[account_path] = \
Connection(account_path, connection)
if connection.Get(CONNECTION, 'Status') == CONNECTION_STATUS_CONNECTED:
self._connections_per_account[account_path].connected = True
else:
self._connections_per_account[account_path].connected = False
connection_name = connection_path.replace('/', '.')[1:]
connection = bus.get_object(connection_name, connection_path)
self._connections_per_account[account_path] = connection
def __status_changed_cb(self, account_path, status, reason):
if status == CONNECTION_STATUS_CONNECTED:
self._connections_per_account[account_path].connected = True
else:
self._connections_per_account[account_path].connected = False
def get_preferred_connection(self):
best_connection = None, None
for account_path, connection in self._connections_per_account.items():
if 'salut' in connection.object_path:
best_connection = account_path, connection
elif 'gabble' in connection.object_path:
best_connection = account_path, connection
if 'salut' in account_path and connection.connected:
best_connection = account_path, connection.connection
elif 'gabble' in account_path and connection.connected:
best_connection = account_path, connection.connection
break
return best_connection
def get_connection(self, account_path):
return self._connections_per_account[account_path]
return self._connections_per_account[account_path].connection
def get_connections_per_account(self):
return self._connections_per_account
def get_account_for_connection(self, connection_path):
for account_path, connection in self._connections_per_account.items():
if connection.object_path == connection_path:
if connection.connection.object_path == connection_path:
return account_path
return None

Loading…
Cancel
Save