Battery status update

This commit is contained in:
Eduardo Silva 2007-03-12 11:02:36 -04:00
parent acaac29906
commit 84f6bdd0c9
2 changed files with 31 additions and 2 deletions

View File

@ -10,7 +10,9 @@ class Device(device.Device):
def __init__(self): def __init__(self):
device.Device.__init__(self) device.Device.__init__(self)
self._level = 0 self._level = 0
self._timeout_id = gobject.timeout_add(2000, self._check_battery_level)
def do_get_property(self, pspec): def do_get_property(self, pspec):
if pspec.name == 'level': if pspec.name == 'level':
@ -18,3 +20,30 @@ class Device(device.Device):
def get_type(self): def get_type(self):
return 'battery' 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

View File

@ -7,7 +7,7 @@ class DeviceView(canvasicon.CanvasIcon):
canvasicon.CanvasIcon.__init__(self) canvasicon.CanvasIcon.__init__(self)
self._model = model self._model = model
model.connect('notify::strength', self._level_changed_cb) model.connect('notify::level', self._level_changed_cb)
self._update_level() self._update_level()
@ -15,6 +15,6 @@ class DeviceView(canvasicon.CanvasIcon):
self.props.icon_name = canvasicon.get_icon_state( self.props.icon_name = canvasicon.get_icon_state(
_ICON_NAME, self._model.props.level) _ICON_NAME, self._model.props.level)
def _level_changed_cb(self, pspec): def _level_changed_cb(self, pspec, param):
self._update_level() self._update_level()