diff --git a/shell/model/devices/battery.py b/shell/model/devices/battery.py index a4f37f1d..0244b242 100644 --- a/shell/model/devices/battery.py +++ b/shell/model/devices/battery.py @@ -10,7 +10,9 @@ class Device(device.Device): def __init__(self): device.Device.__init__(self) + self._level = 0 + self._timeout_id = gobject.timeout_add(2000, self._check_battery_level) def do_get_property(self, pspec): if pspec.name == 'level': @@ -18,3 +20,30 @@ class Device(device.Device): def get_type(self): return 'battery' + + def _check_battery_level(self): + new_level = self._get_battery_level() + + if new_level != self._level: + self._level = new_level + self.notify('level') + + return True + + def _get_battery_level(self): + battery_class_path = '/sys/class/battery/psu_0/' + + capacity_path = battery_class_path + 'capacity_percentage' + try: + f = open(capacity_path, 'r') + val = f.read().split('\n') + level = int(val[0]) + f.close() + except: + level = 0 + + return level + + def __del__(self): + gobject.source_remove(self._timeout_id) + self._timeout_id = 0 diff --git a/shell/view/devices/battery.py b/shell/view/devices/battery.py index 6a667a86..56e3ffbe 100644 --- a/shell/view/devices/battery.py +++ b/shell/view/devices/battery.py @@ -7,7 +7,7 @@ class DeviceView(canvasicon.CanvasIcon): canvasicon.CanvasIcon.__init__(self) self._model = model - model.connect('notify::strength', self._level_changed_cb) + model.connect('notify::level', self._level_changed_cb) self._update_level() @@ -15,6 +15,6 @@ class DeviceView(canvasicon.CanvasIcon): self.props.icon_name = canvasicon.get_icon_state( _ICON_NAME, self._model.props.level) - def _level_changed_cb(self, pspec): + def _level_changed_cb(self, pspec, param): self._update_level()