sugar-toolkit-gtk3/shell/model/devices/devicesmodel.py

137 lines
5.1 KiB
Python
Raw Normal View History

2007-04-05 05:48:09 +02:00
#
# Copyright (C) 2007, Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import logging
import gobject
2007-07-04 11:12:22 +02:00
import dbus
from model.devices import device
2007-03-15 21:37:15 +01:00
from model.devices.network import wired
from model.devices.network import wireless
2007-03-29 16:10:34 +02:00
from model.devices.network import mesh
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
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-04-05 05:48:09 +02:00
self._sigids = {}
2007-07-04 11:12:22 +02:00
self._observe_hal_manager()
2007-02-25 02:00:40 +01:00
self._observe_network_manager()
2007-07-04 11:12:22 +02:00
def _observe_hal_manager(self):
bus = dbus.Bus(dbus.Bus.TYPE_SYSTEM)
proxy = bus.get_object('org.freedesktop.Hal',
'/org/freedesktop/Hal/Manager')
hal_manager = dbus.Interface(proxy, 'org.freedesktop.Hal.Manager')
for udi in hal_manager.FindDeviceByCapability('battery'):
self.add_device(battery.Device(udi))
2007-02-25 02:00:40 +01:00
def _observe_network_manager(self):
network_manager = hardwaremanager.get_network_manager()
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)
network_manager.connect('device-added',
self._network_device_added_cb)
network_manager.connect('device-activating',
self._network_device_activating_cb)
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
def _network_device_added_cb(self, network_manager, nm_device):
state = nm_device.get_state()
if state == nmclient.DEVICE_STATE_ACTIVATING \
or state == nmclient.DEVICE_STATE_ACTIVATED:
self._check_network_device(nm_device)
def _network_device_activating_cb(self, network_manager, nm_device):
2007-02-25 12:36:44 +01:00
self._check_network_device(nm_device)
2007-02-25 02:28:14 +01:00
def _network_device_activated_cb(self, network_manager, nm_device):
pass
2007-04-05 05:48:09 +02:00
def _network_device_removed_cb(self, network_manager, nm_device):
2007-04-05 06:22:13 +02:00
if self._devices.has_key(str(nm_device.get_op())):
self.remove_device(self._get_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():
logging.debug("Device %s not valid" % nm_device.get_op())
2007-02-25 02:00:40 +01:00
return
2007-03-29 16:10:34 +02:00
dtype = nm_device.get_type()
if dtype == nmclient.DEVICE_TYPE_802_11_WIRELESS \
or dtype == nmclient.DEVICE_TYPE_802_11_MESH_OLPC:
2007-02-25 12:36:44 +01:00
self._add_network_device(nm_device)
def _get_network_device(self, nm_device):
2007-03-29 16:10:34 +02:00
return self._devices[str(nm_device.get_op())]
2007-02-25 12:36:44 +01:00
2007-04-05 05:48:09 +02:00
def _network_device_state_changed_cb(self, dev, param):
if dev.props.state == device.STATE_INACTIVE:
self.remove_device(dev)
2007-02-25 12:36:44 +01:00
def _add_network_device(self, nm_device):
if self._devices.has_key(str(nm_device.get_op())):
logging.debug("Tried to add device %s twice" % nm_device.get_op())
return
2007-03-29 16:10:34 +02:00
dtype = nm_device.get_type()
if dtype == nmclient.DEVICE_TYPE_802_11_WIRELESS:
2007-04-05 05:48:09 +02:00
dev = wireless.Device(nm_device)
self.add_device(dev)
sigid = dev.connect('notify::state', self._network_device_state_changed_cb)
self._sigids[dev] = sigid
2007-03-29 16:10:34 +02:00
if dtype == nmclient.DEVICE_TYPE_802_11_MESH_OLPC:
2007-04-05 05:48:09 +02:00
dev = mesh.Device(nm_device)
self.add_device(dev)
sigid = dev.connect('notify::state', self._network_device_state_changed_cb)
self._sigids[dev] = sigid
2007-02-25 12:36:44 +01:00
def __iter__(self):
2007-02-25 12:36:44 +01:00
return iter(self._devices.values())
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()])
2007-04-05 05:48:09 +02:00
device.disconnect(self._sigids[device])
del self._sigids[device]
2007-02-25 12:36:44 +01:00
del self._devices[device.get_id()]