[Sugar-devel] [DESIGN] [PATCH RFC sugar] Wireless network Frame device: add suport for disabling wireless devices

Sascha Silbe silbe at activitycentral.com
Mon Jun 27 07:27:23 EDT 2011


This duplicates the existing functionality in the Control Panel for more
convenient access. Disabling wifi devices can save power, thereby increasing
battery life.

Signed-off-by: Sascha Silbe <silbe at activitycentral.com>
---

This is fully working but I'm not sure about the correct way to change
the label of a MenuItem. Accessing it via MenuItem.child seems hacky.
Suggestions?

We could also do with a better icon. Maybe something like the circles
from emblem-outofrange combined with the arrow from emblem-charging,
striking out the arrow depending on the current action (disable or enable
wifi)?

Since the wiki is down I've uploaded the screenshot to the scratch area on my
own server [1,2].

[1] http://sascha.silbe.org/tmp/sugar-wifi-conn-disable.png
[2] http://sascha.silbe.org/tmp/sugar-wifi-enable.png

 extensions/deviceicon/network.py |   57 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/extensions/deviceicon/network.py b/extensions/deviceicon/network.py
index 9a74237..85ca884 100644
--- a/extensions/deviceicon/network.py
+++ b/extensions/deviceicon/network.py
@@ -72,12 +72,15 @@ class WirelessPalette(Palette):
     __gsignals__ = {
         'deactivate-connection': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                                   ([])),
+        'disable-wifi': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, []),
+        'enable-wifi': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, []),
     }

     def __init__(self, primary_text):
         Palette.__init__(self, label=primary_text)

         self._disconnect_item = None
+        self._enabled = True

         self._channel_label = gtk.Label()
         self._channel_label.props.xalign = 0.0
@@ -109,6 +112,13 @@ class WirelessPalette(Palette):
                                       self.__disconnect_activate_cb)
         self.menu.append(self._disconnect_item)

+        label = glib.markup_escape_text(_('Disable all wireless devices'))
+        self._powercontrol_item = MenuItem(label, 'system-shutdown')
+        self._powercontrol_item.connect('activate',
+                                        self.__powercontrol_activate_cb)
+        self._powercontrol_item.show()
+        self.menu.append(self._powercontrol_item)
+
     def set_connecting(self):
         self.props.secondary_text = _('Connecting...')

@@ -132,9 +142,26 @@ class WirelessPalette(Palette):
         self._disconnect_item.hide()
         self.set_content(None)

+    def set_enabled(self, enabled):
+        self._enabled = enabled
+        if enabled:
+            label = glib.markup_escape_text(_('Disable all wireless devices'))
+        else:
+            self.set_disconnected()
+            label = glib.markup_escape_text(_('Enable wireless devices'))
+
+        # FIXME: what's the right way to do this?
+        self._powercontrol_item.child.set_markup(label)
+
     def __disconnect_activate_cb(self, menuitem):
         self.emit('deactivate-connection')

+    def __powercontrol_activate_cb(self, menuitem):
+        if self._enabled:
+            self.emit('disable-wifi')
+        else:
+            self.emit('enable-wifi')
+
     def _set_frequency(self, frequency):
         channel = network.frequency_to_channel(frequency)
         self._set_channel(channel)
@@ -402,6 +429,8 @@ class WirelessDeviceView(ToolButton):
         self._palette = WirelessPalette(self._name)
         self._palette.connect('deactivate-connection',
                               self.__deactivate_connection_cb)
+        self._palette.connect('disable-wifi', self.__disable_wifi_cb)
+        self._palette.connect('enable-wifi', self.__enable_wifi_cb)
         self.set_palette(self._palette)
         self._palette.set_group_id('frame')

@@ -420,6 +449,16 @@ class WirelessDeviceView(ToolButton):
                                       path=self._device.object_path,
                                       dbus_interface=_NM_DEVICE_IFACE)

+        obj = self._bus.get_object(_NM_SERVICE, _NM_PATH)
+        network_manager = dbus.Interface(obj, _NM_IFACE)
+        self._nm_props = dbus.Interface(network_manager, dbus.PROPERTIES_IFACE)
+        self._nm_props.connect_to_signal('PropertiesChanged',
+                                         self.__nm_props_changed_cb,
+                                         dbus_interface=_NM_IFACE)
+        self._nm_props.Get(_NM_IFACE, 'XWirelessEnabled',
+                           reply_handler=self.__nm_wifi_enabled_reply_cb,
+                           error_handler=self.__nm_wifi_enabled_error_cb)
+
     def disconnect(self):
         self._bus.remove_signal_receiver(self.__state_changed_cb,
                                          signal_name='StateChanged',
@@ -578,6 +617,24 @@ class WirelessDeviceView(ToolButton):
     def __activate_error_cb(self, err):
         logging.debug('Failed to create network: %s', err)

+    def __nm_wifi_enabled_reply_cb(self, enabled):
+        self._palette.set_enabled(enabled)
+
+    def __nm_wifi_enabled_error_cb(self, error):
+        logging.error('Could not determine whether wifi is enabled: %r', error)
+
+    def __nm_props_changed_cb(self, changed):
+        if 'WirelessEnabled' not in changed:
+            return
+
+        self._palette.set_enabled(changed['WirelessEnabled'])
+
+    def __disable_wifi_cb(self, palette_):
+        self._nm_props.Set(_NM_IFACE, 'WirelessEnabled', False)
+
+    def __enable_wifi_cb(self, palette_):
+        self._nm_props.Set(_NM_IFACE, 'WirelessEnabled', True)
+

 class OlpcMeshDeviceView(ToolButton):
     _ICON_NAME = 'network-mesh'
--
1.7.2.5



More information about the Sugar-devel mailing list