[sugar] [PATCH] Add speaker device and icon by default

Martin Dengler martin at martindengler.com
Wed Apr 23 22:13:19 EDT 2008


Add speaker device and icon by default, as in
http://wiki.laptop.org/go/Designs/Frame#07
---
 src/model/devices/devicesmodel.py |    3 +
 src/model/devices/speaker.py      |   59 ++++++++++++++++++++++
 src/view/devices/speaker.py       |   99 +++++++++++++++++++++++++++++++++++++
 3 files changed, 161 insertions(+), 0 deletions(-)
 create mode 100644 src/model/devices/speaker.py
 create mode 100644 src/view/devices/speaker.py

diff --git a/src/model/devices/devicesmodel.py b/src/model/devices/devicesmodel.py
index 691e19e..32c77da 100644
--- a/src/model/devices/devicesmodel.py
+++ b/src/model/devices/devicesmodel.py
@@ -23,6 +23,7 @@ from model.devices import device
 from model.devices.network import wireless
 from model.devices.network import mesh
 from model.devices import battery
+from model.devices import speaker
 from hardware import hardwaremanager
 from hardware import nmclient
 
@@ -54,6 +55,8 @@ class DevicesModel(gobject.GObject):
         for udi in hal_manager.FindDeviceByCapability('battery'):
             self.add_device(battery.Device(udi))
 
+        self.add_device(speaker.Device())
+
     def _observe_network_manager(self):
         network_manager = hardwaremanager.get_network_manager()
         if not network_manager:
diff --git a/src/model/devices/speaker.py b/src/model/devices/speaker.py
new file mode 100644
index 0000000..9f84b81
--- /dev/null
+++ b/src/model/devices/speaker.py
@@ -0,0 +1,59 @@
+# Copyright (C) 2008 Martin Dengler
+#
+# 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 hardwaremanager
+from model.devices import device
+
+class Device(device.Device):
+    __gproperties__ = {
+        'level'   : (int, None, None, 0, 100, 0, gobject.PARAM_READWRITE),
+        'muted'   : (bool, None, None, False, gobject.PARAM_READWRITE),
+    }
+
+    def __init__(self):
+        device.Device.__init__(self)
+        self._manager = hardwaremanager.get_manager()        
+
+    def _get_level(self):
+        return self._manager.get_volume()
+
+    def _set_level(self, new_volume):
+        self._manager.set_volume(new_volume)
+        self.notify('level')
+
+    def _get_muted(self):
+        return self.props.level == 0.0
+
+    def _set_muted(self, mute):
+        self._manager.set_mute(mute)
+        self.notify('muted')
+
+    def get_type(self):
+        return 'speaker'
+
+    def do_get_property(self, pspec):
+        if pspec.name == "level":
+            return self._get_level()
+        elif pspec.name == "muted":
+            return self._get_muted()
+
+    def do_set_property(self, pspec, value):
+        if pspec.name == "level":
+            self._set_level(value)
+        elif pspec.name == "muted":
+            self._set_muted(value)
diff --git a/src/view/devices/speaker.py b/src/view/devices/speaker.py
new file mode 100644
index 0000000..3fe36eb
--- /dev/null
+++ b/src/view/devices/speaker.py
@@ -0,0 +1,99 @@
+# Copyright (C) 2008 Martin Dengler
+#
+# 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 gettext import gettext as _
+
+import gtk
+
+from sugar import profile
+from sugar.graphics import style
+from sugar.graphics.icon import get_icon_state
+from sugar.graphics.tray import TrayIcon
+from sugar.graphics.palette import Palette
+from sugar.graphics.xocolor import XoColor
+
+from view.frame.frameinvoker import FrameWidgetInvoker
+
+_ICON_NAME = 'speaker'
+
+class DeviceView(TrayIcon):
+    def __init__(self, model):
+        TrayIcon.__init__(self, icon_name=_ICON_NAME,
+                          xo_color=profile.get_color())
+
+        self._model = model
+        self.palette = SpeakerPalette(_('My Speakers'), model=model)
+        self.set_palette(self.palette)
+        self.palette.props.invoker = FrameWidgetInvoker(self)
+        self.palette.set_group_id('frame')
+
+        model.connect('notify::level', self._speaker_status_changed_cb)
+        model.connect('notify::muted', self._speaker_status_changed_cb)
+        model.connect('notify::unmuted', self._speaker_status_changed_cb)
+        self.connect('expose-event', self._expose_event_cb)
+
+        self._update_info()
+
+    def _update_info(self):
+        name = _ICON_NAME
+        current_level = self._model.props.level
+        xo_color = profile.get_color()
+        badge_name = None
+
+        if self._model.props.muted:
+            name += "-muted"
+            xo_color = XoColor('%s,%s' % (style.COLOR_WHITE.get_svg(),
+                                          style.COLOR_WHITE.get_svg()))
+
+        self.icon.props.icon_name = get_icon_state(name, current_level)
+        self.icon.props.xo_color = xo_color
+        self.icon.props.badge_name = badge_name
+
+    def _speaker_status_changed_cb(self, pspec, param):
+        self._update_info()
+
+    def _expose_event_cb(self, *args):
+        self._update_info()
+
+class SpeakerPalette(Palette):
+
+    def __init__(self, primary_text, model):
+        Palette.__init__(self, primary_text)
+
+        self._model = model
+
+        current_level = self._model.props.level
+        self._adjustment = gtk.Adjustment(
+            self._model.props.level, 0.0, 101.0, 1, 1, 1)
+        self._hscale = gtk.HScale(self._adjustment)
+        self._hscale.set_digits(0)
+        self._hscale.set_draw_value(False)
+        self._hscale.show()
+
+        vbox = gtk.VBox()
+        vbox.pack_start(self._hscale)
+        vbox.show()
+
+        self.set_content(vbox)
+
+        self._adjustment.connect("value_changed", self._cb_adjustment_changed)
+        self.connect('popup', self._cb_popup)
+
+    def _cb_adjustment_changed(self, adj_):
+        self._model.props.level = int(self._adjustment.value)
+
+    def _cb_popup(self, palette_):
+        self._adjustment.value = int(self._model.props.level)
-- 
1.5.4.1



More information about the Sugar mailing list