From 7c135804bc53708a23c808640145e37ae3b22f6b Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 25 Feb 2007 12:36:44 +0100 Subject: [PATCH] Handle device removal --- shell/hardware/nmclient.py | 4 +++ shell/model/devices/devicesmodel.py | 45 +++++++++++++++++++------- shell/model/devices/wirednetwork.py | 3 ++ shell/model/devices/wirelessnetwork.py | 5 ++- shell/view/home/HomeBox.py | 12 ++++--- 5 files changed, 53 insertions(+), 16 deletions(-) diff --git a/shell/hardware/nmclient.py b/shell/hardware/nmclient.py index c98f1e2d..acbac920 100644 --- a/shell/hardware/nmclient.py +++ b/shell/hardware/nmclient.py @@ -130,6 +130,8 @@ class Device(gobject.GObject): gobject.TYPE_NONE, ([])), 'activated': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])), + 'deactivated': (gobject.SIGNAL_RUN_FIRST, + gobject.TYPE_NONE, ([])), 'strength-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])), @@ -179,6 +181,8 @@ class Device(gobject.GObject): if self._active: self.emit('activated') + else: + self.emit('deactivated') def _update_networks(self, net_ops, active_op): for op in net_ops: diff --git a/shell/model/devices/devicesmodel.py b/shell/model/devices/devicesmodel.py index 01db8711..dd535961 100644 --- a/shell/model/devices/devicesmodel.py +++ b/shell/model/devices/devicesmodel.py @@ -20,7 +20,7 @@ class DevicesModel(gobject.GObject): def __init__(self): gobject.GObject.__init__(self) - self._devices = [] + self._devices = {} self.add_device(battery.Device()) self._observe_network_manager() @@ -32,21 +32,44 @@ class DevicesModel(gobject.GObject): self._check_network_device(device) network_manager.connect('device-activated', - self._network_device_added_cb) + self._network_device_activated_cb) + network_manager.connect('device-removed', + self._network_device_removed_cb) - def _network_device_added_cb(self, network_manager, device): - self._check_network_device(device) + def _network_device_activated_cb(self, network_manager, nm_device): + self._check_network_device(nm_device) - def _check_network_device(self, device): - if not device.is_valid(): + def _network_device_removed_cb(self, nm_device): + self._remove_network_device(nm_device) + + def _network_device_deactivated_cb(self, nm_device): + self._remove_network_device(nm_device) + + def _check_network_device(self, nm_device): + if not nm_device.is_valid(): return - if device.get_type() == nmclient.DEVICE_TYPE_802_11_WIRELESS: - self.add_device(wirelessnetwork.Device(device)) - + if nm_device.get_type() == nmclient.DEVICE_TYPE_802_11_WIRELESS: + self._add_network_device(nm_device) + + def _get_network_device(self, nm_device): + return self._devices[nm_device.get_op()] + + def _add_network_device(self, nm_device): + self.add_device(wirelessnetwork.Device(nm_device)) + nm_device.connect('deactivated', + self._network_device_deactivated_cb) + + def _remove_network_device(self, nm_device): + self.remove_device(self._get_network_device(nm_device)) + def __iter__(self): - return iter(self._devices) + return iter(self._devices.values()) def add_device(self, device): - self._devices.append(device) + self._devices[device.get_id()] = device self.emit('device-appeared', device) + + def remove_device(self, device): + self.emit('device-disappeared', self._devices[device.get_id()]) + del self._devices[device.get_id()] diff --git a/shell/model/devices/wirednetwork.py b/shell/model/devices/wirednetwork.py index 871a552f..5ea0bb69 100644 --- a/shell/model/devices/wirednetwork.py +++ b/shell/model/devices/wirednetwork.py @@ -5,5 +5,8 @@ class Device(device.Device): device.Device.__init__(self) self._nm_device = device + def get_id(self): + return self._nm_device.get_op() + def get_type(self): return 'wirednetwork' diff --git a/shell/model/devices/wirelessnetwork.py b/shell/model/devices/wirelessnetwork.py index 6a53d341..97f16cd5 100644 --- a/shell/model/devices/wirelessnetwork.py +++ b/shell/model/devices/wirelessnetwork.py @@ -3,10 +3,13 @@ from model.devices import device class Device(device.Device): def __init__(self, nm_device): device.Device.__init__(self) - self._nm_device = device + self._nm_device = nm_device def get_type(self): return 'wirelessnetwork' + def get_id(self): + return self._nm_device.get_op() + def get_level(self): return self._nm_device.get_strength() diff --git a/shell/view/home/HomeBox.py b/shell/view/home/HomeBox.py index 4ab11409..e2483d27 100644 --- a/shell/view/home/HomeBox.py +++ b/shell/view/home/HomeBox.py @@ -45,7 +45,7 @@ class HomeBox(hippo.CanvasBox, hippo.CanvasItem): shell_model.connect('notify::state', self._shell_state_changed_cb) - self._device_icons = [] + self._device_icons = {} devices_model = shell_model.get_devices() for device in devices_model: @@ -59,13 +59,17 @@ class HomeBox(hippo.CanvasBox, hippo.CanvasItem): def _add_device(self, device): view = deviceview.create(device) self.append(view, hippo.PACK_FIXED) - self._device_icons.append(view) + self._device_icons[device.get_id()] = view + + def _remove_device(self, device): + self.remove(self._device_icons[device.get_id()]) + del self._device_icons[device.get_id()] def _device_appeared_cb(self, model, device): self._add_device(device) def _device_disappeared_cb(self, model, device): - pass + self._remove_device(device) def _shell_state_changed_cb(self, model, pspec): # FIXME handle all possible mode switches @@ -87,7 +91,7 @@ class HomeBox(hippo.CanvasBox, hippo.CanvasItem): (height - icon_height) / 2) i = 0 - for icon in self._device_icons: + for icon in self._device_icons.values(): angle = 2 * math.pi / len(self._device_icons) * i + math.pi / 2 radius = units.grid_to_pixels(5)