diff options
Diffstat (limited to 'src/model/devices')
-rw-r--r-- | src/model/devices/Makefile.am | 8 | ||||
-rw-r--r-- | src/model/devices/__init__.py | 16 | ||||
-rw-r--r-- | src/model/devices/battery.py | 96 | ||||
-rw-r--r-- | src/model/devices/device.py | 45 | ||||
-rw-r--r-- | src/model/devices/devicesmodel.py | 136 | ||||
-rw-r--r-- | src/model/devices/network/Makefile.am | 6 | ||||
-rw-r--r-- | src/model/devices/network/__init__.py | 16 | ||||
-rw-r--r-- | src/model/devices/network/mesh.py | 75 | ||||
-rw-r--r-- | src/model/devices/network/wired.py | 28 | ||||
-rw-r--r-- | src/model/devices/network/wireless.py | 96 |
10 files changed, 522 insertions, 0 deletions
diff --git a/src/model/devices/Makefile.am b/src/model/devices/Makefile.am new file mode 100644 index 0000000..5440eeb --- /dev/null +++ b/src/model/devices/Makefile.am @@ -0,0 +1,8 @@ +SUBDIRS = network + +sugardir = $(pkgdatadir)/shell/model/devices +sugar_PYTHON = \ + __init__.py \ + device.py \ + devicesmodel.py \ + battery.py diff --git a/src/model/devices/__init__.py b/src/model/devices/__init__.py new file mode 100644 index 0000000..a9dd95a --- /dev/null +++ b/src/model/devices/__init__.py @@ -0,0 +1,16 @@ +# Copyright (C) 2006-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 + diff --git a/src/model/devices/battery.py b/src/model/devices/battery.py new file mode 100644 index 0000000..853d00e --- /dev/null +++ b/src/model/devices/battery.py @@ -0,0 +1,96 @@ +# Copyright (C) 2006-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 +import dbus + +from model.devices import device + +_LEVEL_PROP = 'battery.charge_level.percentage' +_CHARGING_PROP = 'battery.rechargeable.is_charging' +_DISCHARGING_PROP = 'battery.rechargeable.is_discharging' + +class Device(device.Device): + __gproperties__ = { + 'level' : (int, None, None, 0, 100, 0, + gobject.PARAM_READABLE), + 'charging' : (bool, None, None, False, + gobject.PARAM_READABLE), + 'discharging' : (bool, None, None, False, + gobject.PARAM_READABLE) + } + + def __init__(self, udi): + device.Device.__init__(self, udi) + + bus = dbus.Bus(dbus.Bus.TYPE_SYSTEM) + proxy = bus.get_object('org.freedesktop.Hal', udi) + self._battery = dbus.Interface(proxy, 'org.freedesktop.Hal.Device') + bus.add_signal_receiver(self._battery_changed, + 'PropertyModified', + 'org.freedesktop.Hal.Device', + 'org.freedesktop.Hal', + udi) + + self._level = self._get_level() + self._charging = self._get_charging() + self._discharging = self._get_discharging() + + def _get_level(self): + try: + return self._battery.GetProperty(_LEVEL_PROP) + except dbus.DBusException: + logging.error('Cannot access %s' % _LEVEL_PROP) + return 0 + + def _get_charging(self): + try: + return self._battery.GetProperty(_CHARGING_PROP) + except dbus.DBusException: + logging.error('Cannot access %s' % _CHARGING_PROP) + return False + + def _get_discharging(self): + try: + return self._battery.GetProperty(_DISCHARGING_PROP) + except dbus.DBusException: + logging.error('Cannot access %s' % _DISCHARGING_PROP) + return False + + def do_get_property(self, pspec): + if pspec.name == 'level': + return self._level + if pspec.name == 'charging': + return self._charging + if pspec.name == 'discharging': + return self._discharging + + def get_type(self): + return 'battery' + + def _battery_changed(self, num_changes, changes_list): + for change in changes_list: + if change[0] == _LEVEL_PROP: + self._level = self._get_level() + self.notify('level') + elif change[0] == _CHARGING_PROP: + self._charging = self._get_charging() + self.notify('charging') + elif change[0] == _DISCHARGING_PROP: + self._discharging = self._get_discharging() + self.notify('discharging') diff --git a/src/model/devices/device.py b/src/model/devices/device.py new file mode 100644 index 0000000..d7105b5 --- /dev/null +++ b/src/model/devices/device.py @@ -0,0 +1,45 @@ +# +# 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 gobject +from hardware import nmclient + +from sugar import util + +STATE_ACTIVATING = 0 +STATE_ACTIVATED = 1 +STATE_INACTIVE = 2 + +_nm_state_to_state = { + nmclient.DEVICE_STATE_ACTIVATING : STATE_ACTIVATING, + nmclient.DEVICE_STATE_ACTIVATED : STATE_ACTIVATED, + nmclient.DEVICE_STATE_INACTIVE : STATE_INACTIVE +} + +class Device(gobject.GObject): + def __init__(self, device_id=None): + gobject.GObject.__init__(self) + if device_id: + self._id = device_id + else: + self._id = util.unique_id() + + def get_type(self): + return 'unknown' + + def get_id(self): + return self._id diff --git a/src/model/devices/devicesmodel.py b/src/model/devices/devicesmodel.py new file mode 100644 index 0000000..fab9fa4 --- /dev/null +++ b/src/model/devices/devicesmodel.py @@ -0,0 +1,136 @@ +# +# 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 +import dbus + +from model.devices import device +from model.devices.network import wired +from model.devices.network import wireless +from model.devices.network import mesh +from model.devices import battery +from hardware import hardwaremanager +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) + + self._devices = {} + self._sigids = {} + + self._observe_hal_manager() + self._observe_network_manager() + + 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)) + + def _observe_network_manager(self): + network_manager = hardwaremanager.get_network_manager() + if not network_manager: + return + + 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) + network_manager.connect('device-activated', + self._network_device_activated_cb) + network_manager.connect('device-removed', + self._network_device_removed_cb) + + 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): + self._check_network_device(nm_device) + + def _network_device_activated_cb(self, network_manager, nm_device): + pass + + def _network_device_removed_cb(self, network_manager, nm_device): + if self._devices.has_key(str(nm_device.get_op())): + self.remove_device(self._get_network_device(nm_device)) + + def _check_network_device(self, nm_device): + if not nm_device.is_valid(): + logging.debug("Device %s not valid" % nm_device.get_op()) + return + + dtype = nm_device.get_type() + if dtype == nmclient.DEVICE_TYPE_802_11_WIRELESS \ + or dtype == nmclient.DEVICE_TYPE_802_11_MESH_OLPC: + self._add_network_device(nm_device) + + def _get_network_device(self, nm_device): + return self._devices[str(nm_device.get_op())] + + def _network_device_state_changed_cb(self, dev, param): + if dev.props.state == device.STATE_INACTIVE: + self.remove_device(dev) + + 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 + + dtype = nm_device.get_type() + if dtype == nmclient.DEVICE_TYPE_802_11_WIRELESS: + dev = wireless.Device(nm_device) + self.add_device(dev) + sigid = dev.connect('notify::state', self._network_device_state_changed_cb) + self._sigids[dev] = sigid + if dtype == nmclient.DEVICE_TYPE_802_11_MESH_OLPC: + dev = mesh.Device(nm_device) + self.add_device(dev) + sigid = dev.connect('notify::state', self._network_device_state_changed_cb) + self._sigids[dev] = sigid + + def __iter__(self): + return iter(self._devices.values()) + + def add_device(self, 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()]) + device.disconnect(self._sigids[device]) + del self._sigids[device] + del self._devices[device.get_id()] diff --git a/src/model/devices/network/Makefile.am b/src/model/devices/network/Makefile.am new file mode 100644 index 0000000..04074e5 --- /dev/null +++ b/src/model/devices/network/Makefile.am @@ -0,0 +1,6 @@ +sugardir = $(pkgdatadir)/shell/model/devices/network +sugar_PYTHON = \ + __init__.py \ + mesh.py \ + wired.py \ + wireless.py diff --git a/src/model/devices/network/__init__.py b/src/model/devices/network/__init__.py new file mode 100644 index 0000000..a9dd95a --- /dev/null +++ b/src/model/devices/network/__init__.py @@ -0,0 +1,16 @@ +# Copyright (C) 2006-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 + diff --git a/src/model/devices/network/mesh.py b/src/model/devices/network/mesh.py new file mode 100644 index 0000000..0152d8a --- /dev/null +++ b/src/model/devices/network/mesh.py @@ -0,0 +1,75 @@ +# +# Copyright (C) 2006-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 gobject + +from model.devices import device +from hardware import nmclient + +class Device(device.Device): + __gproperties__ = { + 'strength' : (int, None, None, 0, 100, 0, + gobject.PARAM_READABLE), + 'state' : (int, None, None, device.STATE_ACTIVATING, + device.STATE_INACTIVE, 0, gobject.PARAM_READABLE), + 'activation-stage': (int, None, None, 0, 7, 0, gobject.PARAM_READABLE), + 'frequency': (float, None, None, 0, 2.72, 0, gobject.PARAM_READABLE), + 'mesh-step': (int, None, None, 0, 4, 0, gobject.PARAM_READABLE), + } + + def __init__(self, nm_device): + device.Device.__init__(self) + self._nm_device = nm_device + + self._nm_device.connect('strength-changed', + self._strength_changed_cb) + self._nm_device.connect('state-changed', + self._state_changed_cb) + self._nm_device.connect('activation-stage-changed', + self._activation_stage_changed_cb) + + def _strength_changed_cb(self, nm_device): + self.notify('strength') + + def _state_changed_cb(self, nm_device): + self.notify('state') + + def _activation_stage_changed_cb(self, nm_device): + self.notify('activation-stage') + + def do_get_property(self, pspec): + if pspec.name == 'strength': + return self._nm_device.get_strength() + elif pspec.name == 'state': + nm_state = self._nm_device.get_state() + return device._nm_state_to_state[nm_state] + elif pspec.name == 'activation-stage': + return self._nm_device.get_activation_stage() + elif pspec.name == 'frequency': + return self._nm_device.get_frequency() + elif pspec.name == 'mesh-step': + return self._nm_device.get_mesh_step() + + def get_type(self): + return 'network.mesh' + + def get_id(self): + return str(self._nm_device.get_op()) + + def get_nm_device(self): + return self._nm_device + diff --git a/src/model/devices/network/wired.py b/src/model/devices/network/wired.py new file mode 100644 index 0000000..66c5206 --- /dev/null +++ b/src/model/devices/network/wired.py @@ -0,0 +1,28 @@ +# Copyright (C) 2006-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 + +from model.devices import device + +class Device(device.Device): + def __init__(self, nm_device): + device.Device.__init__(self) + self._nm_device = device + + def get_id(self): + return str(self._nm_device.get_op()) + + def get_type(self): + return 'network.wired' diff --git a/src/model/devices/network/wireless.py b/src/model/devices/network/wireless.py new file mode 100644 index 0000000..c45a08e --- /dev/null +++ b/src/model/devices/network/wireless.py @@ -0,0 +1,96 @@ +# +# Copyright (C) 2006-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 gobject + +from model.devices import device +from hardware import nmclient + +def freq_to_channel(freq): + ftoc = { 2.412: 1, 2.417: 2, 2.422: 3, 2.427: 4, + 2.432: 5, 2.437: 6, 2.442: 7, 2.447: 8, + 2.452: 9, 2.457: 10, 2.462: 11, 2.467: 12, + 2.472: 13 + } + return ftoc[freq] + +def channel_to_freq(channel): + ctof = { 1: 2.412, 2: 2.417, 3: 2.422, 4: 2.427, + 5: 2.432, 6: 2.437, 7: 2.442, 8: 2.447, + 9: 2.452, 10: 2.457, 11: 2.462, 12: 2.467, + 13: 2.472 + } + return ctof[channel] + + +class Device(device.Device): + __gproperties__ = { + 'name' : (str, None, None, None, + gobject.PARAM_READABLE), + 'strength' : (int, None, None, 0, 100, 0, + gobject.PARAM_READABLE), + 'state' : (int, None, None, device.STATE_ACTIVATING, + device.STATE_INACTIVE, 0, gobject.PARAM_READABLE), + 'frequency': (float, None, None, 0.0, 9999.99, 0.0, + gobject.PARAM_READABLE) + } + + def __init__(self, nm_device): + device.Device.__init__(self) + self._nm_device = nm_device + + self._nm_device.connect('strength-changed', + self._strength_changed_cb) + self._nm_device.connect('ssid-changed', + self._ssid_changed_cb) + self._nm_device.connect('state-changed', + self._state_changed_cb) + + def _strength_changed_cb(self, nm_device): + self.notify('strength') + + def _ssid_changed_cb(self, nm_device): + self.notify('name') + + def _state_changed_cb(self, nm_device): + self.notify('state') + + def do_get_property(self, pspec): + if pspec.name == 'strength': + return self._nm_device.get_strength() + elif pspec.name == 'name': + import logging + logging.debug('wireless.Device.props.name: %s' % self._nm_device.get_ssid()) + return self._nm_device.get_ssid() + elif pspec.name == 'state': + nm_state = self._nm_device.get_state() + return device._nm_state_to_state[nm_state] + elif pspec.name == 'frequency': + return self._nm_device.get_frequency() + + def get_type(self): + return 'network.wireless' + + def get_id(self): + return str(self._nm_device.get_op()) + + def get_active_network_colors(self): + net = self._nm_device.get_active_network() + if not net: + return (None, None) + return net.get_colors() + |