Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/view/devices/battery.py
blob: 162885062e5da3b1c7fc86a4bbf5fa395d28fd4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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 gtk

from gettext import gettext as _
from sugar.graphics import canvasicon
from sugar.graphics import units
from sugar.graphics.palette import Palette

_ICON_NAME = 'device-battery'

class DeviceView(canvasicon.CanvasIcon):
    def __init__(self, model):
        canvasicon.CanvasIcon.__init__(self, scale=units.MEDIUM_ICON_SCALE)
        self._model = model
        self._palette = BatteryPalette(_('My Battery life'))
        self.set_palette(self._palette)

        model.connect('notify::level', self._battery_status_changed_cb)
        model.connect('notify::charging', self._battery_status_changed_cb)
        model.connect('notify::discharging', self._battery_status_changed_cb)
        self._update_info()

    def _update_info(self):
        self.props.icon_name = canvasicon.get_icon_state(
                                    _ICON_NAME, self._model.props.level)

        # Update palette
        self._palette.update_progress_bar(self._model.props.level)
        self._palette.update_charge_status(self._model.props.charging, \
            self._model.props.discharging)

    def _battery_status_changed_cb(self, pspec, param):
        self._update_info()

class BatteryPalette(Palette):
        def __init__(self, primary_text):
            Palette.__init__(self, primary_text)
            
            self._level = 0
            self._progress_bar = gtk.ProgressBar()
            self._progress_bar.show()
            self._status_label = gtk.Label()
            self._status_label.show()

            vbox = gtk.VBox()
            vbox.pack_start(self._progress_bar)
            vbox.pack_start(self._status_label)
            vbox.show()

            self.set_content(vbox)

        def update_progress_bar(self, percent):
            self._level = percent
            fraction = float(percent/100.0)
            self._progress_bar.set_fraction(fraction)

        def update_charge_status(self, charging, discharging):
            percent_string = ' (%s%%)' % self._level

            if charging:
                charge_text = _('Battery charging') + percent_string
            elif discharging:
                charge_text = _('Battery discharging') + percent_string
            else:
                charge_text = _('Battery fully charged')

            self._status_label.set_text(charge_text)