Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/services/console/interface/xo/battery.py
blob: a5bff6b493bc5f27dcd0ce83b4c3dfc0db2bbfa3 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python

# Copyright (C) 2007, Eduardo Silva (edsiper@gmail.com).
#
# 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
import gobject

from label import Label
from graphics.box import BoxGraphic

class XO_Battery(gtk.Fixed):

    def __init__(self):
        gtk.Fixed.__init__(self)

        self.frame = gtk.Frame('Battery Status')
        self.set_border_width(10)
                
        self._battery_charge = self._get_battery_status()

        self._battery_box = BoxGraphic()
        self._battery_box.set_size_request(70, 150)
        self._battery_box.set_capacity(self._battery_charge)

        fixed = gtk.Fixed();
        fixed.set_border_width(10)
        fixed.add(self._battery_box)

        hbox = gtk.HBox(False, 0)
        hbox.pack_start(fixed, False, False, 4)
        
        # Battery info
        table = gtk.Table(2, 2)
        table.set_border_width(5)
        table.set_col_spacings(7)
        table.set_row_spacings(7)
        
        label_charge = Label('Charge: ' , Label.DESCRIPTION)
        self.label_charge_value = Label(str(self._battery_charge) + '%', Label.DESCRIPTION)

        table.attach(label_charge, 0, 1, 0, 1)
        table.attach(self.label_charge_value, 1,2, 0,1)

        # Charging
        """
        hbox_charging = gtk.HBox(False, 2)
        l_charging = gtk.Label('Charging: ')
        l_charging.set_justify(gtk.JUSTIFY_LEFT)
        hbox_charging.pack_start(l_charging, False, False, 0)
        
        self._label_charging = gtk.Label('No')
        self._label_charging.set_justify(gtk.JUSTIFY_LEFT)
        
        hbox_charging.pack_start(self._label_charging, False, False, 0)
        """
        
        alignment = gtk.Alignment(0,0,0,0)
        alignment.add(table)

        hbox.pack_start(alignment, False, False, 0)
        self.frame.add(hbox)
        self.add(self.frame)
        self.show_all()

        # Update every 2 seconds
        gobject.timeout_add(2000, self._update_battery_status)
        
    def _update_battery_status(self):
        
        new_charge = self._get_battery_status()
        
        if new_charge != self._battery_charge:
            self._battery_charge = self._get_battery_status()
            self.label_charge_value.set_text(str(self._battery_charge) + '%')
            self._battery_box.set_capacity(self._battery_charge)

        return True

    def _get_battery_status(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')
            capacity = int(val[0])
            f.close()
        except:
            capacity = 0

        return capacity