2007-02-20 11:48:03 +01:00
|
|
|
import gobject
|
|
|
|
|
|
|
|
from model.devices import device
|
2007-02-25 02:00:40 +01:00
|
|
|
from model.devices import wirednetwork
|
|
|
|
from model.devices import wirelessnetwork
|
2007-02-20 11:48:03 +01:00
|
|
|
from model.devices import battery
|
2007-02-25 02:00:40 +01:00
|
|
|
from hardware import hardwaremanager
|
2007-02-25 02:28:14 +01:00
|
|
|
from hardware import nmclient
|
2007-02-20 11:48:03 +01:00
|
|
|
|
|
|
|
class DevicesModel(gobject.GObject):
|
|
|
|
__gsignals__ = {
|
|
|
|
'device-appeared' : (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'device-disappeared': (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT]))
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
|
2007-02-25 12:36:44 +01:00
|
|
|
self._devices = {}
|
2007-02-20 11:48:03 +01:00
|
|
|
self.add_device(battery.Device())
|
|
|
|
|
2007-02-25 02:00:40 +01:00
|
|
|
self._observe_network_manager()
|
|
|
|
|
|
|
|
def _observe_network_manager(self):
|
|
|
|
network_manager = hardwaremanager.get_network_manager()
|
2007-02-26 14:42:39 +01:00
|
|
|
if not network_manager:
|
|
|
|
return
|
2007-02-25 02:28:14 +01:00
|
|
|
|
2007-02-25 02:00:40 +01:00
|
|
|
for device in network_manager.get_devices():
|
|
|
|
self._check_network_device(device)
|
|
|
|
|
2007-02-25 02:28:14 +01:00
|
|
|
network_manager.connect('device-activated',
|
2007-02-25 12:36:44 +01:00
|
|
|
self._network_device_activated_cb)
|
|
|
|
network_manager.connect('device-removed',
|
|
|
|
self._network_device_removed_cb)
|
2007-02-25 02:28:14 +01:00
|
|
|
|
2007-02-25 12:36:44 +01:00
|
|
|
def _network_device_activated_cb(self, network_manager, nm_device):
|
|
|
|
self._check_network_device(nm_device)
|
2007-02-25 02:28:14 +01:00
|
|
|
|
2007-02-25 12:36:44 +01:00
|
|
|
def _network_device_removed_cb(self, nm_device):
|
|
|
|
self._remove_network_device(nm_device)
|
|
|
|
|
2007-02-26 12:23:00 +01:00
|
|
|
def _network_device_state_changed_cb(self, nm_device):
|
|
|
|
if nm_device.get_state == nmclient.DEVICE_STATE_INACTIVE:
|
|
|
|
self._remove_network_device(nm_device)
|
2007-02-25 12:36:44 +01:00
|
|
|
|
|
|
|
def _check_network_device(self, nm_device):
|
|
|
|
if not nm_device.is_valid():
|
2007-02-25 02:00:40 +01:00
|
|
|
return
|
|
|
|
|
2007-02-25 12:36:44 +01:00
|
|
|
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))
|
2007-02-26 12:23:00 +01:00
|
|
|
nm_device.connect('state-changed',
|
|
|
|
self._network_device_state_changed_cb)
|
2007-02-25 12:36:44 +01:00
|
|
|
|
|
|
|
def _remove_network_device(self, nm_device):
|
|
|
|
self.remove_device(self._get_network_device(nm_device))
|
|
|
|
|
2007-02-20 11:48:03 +01:00
|
|
|
def __iter__(self):
|
2007-02-25 12:36:44 +01:00
|
|
|
return iter(self._devices.values())
|
2007-02-20 11:48:03 +01:00
|
|
|
|
|
|
|
def add_device(self, device):
|
2007-02-25 12:36:44 +01:00
|
|
|
self._devices[device.get_id()] = device
|
2007-02-25 02:00:40 +01:00
|
|
|
self.emit('device-appeared', device)
|
2007-02-25 12:36:44 +01:00
|
|
|
|
|
|
|
def remove_device(self, device):
|
|
|
|
self.emit('device-disappeared', self._devices[device.get_id()])
|
|
|
|
del self._devices[device.get_id()]
|