Second checkin, First as Lithium.
[rox-lithium.git] / battery.py
blob19c50e38ff096db9dea090df8f46c351dab5654b
1 """
2 battery.py - A Battery Status Monitor for ROX
3 (based largely on Baroque by Tilo Riemer)
4 """
6 ############################################################################
7 ##
8 ## $Id: baroque.py,v 1.13 2004/01/08 04:26:11 rds Exp $
9 ##
10 ## Copyright (C) 2002-2003 Rds <rds@rdsarts.com> and
11 ## Tilo Riemer <riemer@lincvs.org>
12 ## All rights reserved.
14 ## Baroque is a merge of BatMonitor and the old Baroque
16 ## Redistribution and use in source and binary forms, with or without
17 ## modification, are permitted provided that the following conditions
18 ## are met:
20 ## 1. Redistributions of source code must retain the above copyright
21 ## notice, this list of conditions and the following disclaimer.
22 ## 2. Redistributions in binary form must reproduce the above copyright
23 ## notice, this list of conditions and the following disclaimer in the
24 ## documentation and/or other materials provided with the distribution.
25 ## 3. The name of the author may not be used to endorse or promote products
26 ## derived from this software without specific prior written permission.
28 ## THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
29 ## IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 ## OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 ## IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 ## INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 ## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 ## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 ## THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ###############################################################################
41 # standard library modules
42 import sys, os, time, gtk, gobject, rox
43 from rox import applet, filer, tasks
44 from rox.options import Option
46 # globals
47 APP_NAME = 'Battery'
48 APP_DIR = rox.app_dir
49 APP_SIZE = [28, 28]
51 # Options.xml processing
52 from rox import Menu
53 rox.setup_app_options(APP_NAME, site='hayber.us')
54 Menu.set_save_name(APP_NAME, site='hayber.us')
56 #Options go here
57 WARN = Option('warn', True)
58 WARN_LEVEL = Option('warn_level', 10)
59 TIMER = Option('timeout', "1000")
61 #Enable notification of options changes
62 rox.app_options.notify()
64 BATT_TYPE = -1 # 0 = ACPI, 1 = APM
66 # Initalize the battery object
67 try:
68 import acpi
69 BATTERY = acpi.Acpi()
70 BATT_TYPE = 0
71 OFFLINE_STATE = acpi.OFFLINE
72 print >>sys.stderr, 'Using ACPI!'
73 except:
74 try:
75 import pmu #for PowerMac support
76 BATTERY = pmu.Pmu()
77 BATT_TYPE = 1
78 OFFLINE_STATE = pmu.OFFLINE
79 print >>sys.stderr, 'Using PMU!'
80 except:
81 try:
82 import apm
83 BATTERY = apm.Apm()
84 BATT_TYPE = 1
85 OFFLINE_STATE = apm.OFFLINE
86 print >>sys.stderr, 'Using APM!'
87 except:
88 rox.croak(_("Sorry, but we could not load a Power Management module. Your system is not configured with power management support."))
91 class Battery(applet.Applet):
92 """A Battery Status Monitor Applet"""
94 warned = False
95 msg = 0
96 vertical = False
98 images = []
100 def __init__(self, id):
101 """Initialize applet."""
102 applet.Applet.__init__(self, id)
104 # load the applet icon
105 self.image = gtk.Image()
107 for name in [
108 'battery0', 'battery1', 'battery2', 'battery3', 'battery4',
109 'battery5', 'battery6', 'battery7', 'battery8', 'battery9',
110 'battery10'
112 self.images.append(gtk.gdk.pixbuf_new_from_file(os.path.join(rox.app_dir, 'images', name+'.svg')))
114 self.pixbuf = self.images[0]
115 self.image.set_from_pixbuf(self.pixbuf)
116 self.resize_image(8)
117 self.add(self.image)
119 self.vertical = self.get_panel_orientation() in ('Right', 'Left')
120 if self.vertical:
121 self.set_size_request(8, -1)
122 else:
123 self.set_size_request(-1, 8)
125 # set the tooltip
126 self.tooltips = gtk.Tooltips()
128 # menus
129 self.build_appmenu()
131 # event handling
132 self.add_events(gtk.gdk.BUTTON_PRESS_MASK)
133 self.connect('button-press-event', self.button_press)
134 self.connect('size-allocate', self.resize)
135 self.connect('delete_event', self.quit)
136 rox.app_options.add_notify(self.get_options)
138 gobject.timeout_add(int(TIMER.int_value) * 100, self.update_display, BATTERY)
139 gobject.timeout_add(5000, self.update_tooltip)
141 self.update_display(BATTERY)
142 self.update_tooltip()
143 self.show()
145 def update_display(self, BATTERY):
146 """Updates all the parts of our applet, and cleans it up if needed."""
148 if BATT_TYPE == 0:
149 try:
150 BATTERY.update()
151 except AcpiError:
152 rox.report_exception()
153 else:
154 BATTERY.update()
156 percent = BATTERY.percent()
158 if WARN.value == 'True':
159 if (BATTERY.charging_state() == OFFLINE_STATE and percent <= WARN_LEVEL.int_value):
160 if self.warned == False:
161 rox.info(_("Warning. Battery is currently at %d%%") % (BATTERY.percent(),))
162 self.warned = True
163 else:
164 self.warned = False
166 if BATTERY.charging_state():
167 pb = self.images[0]
168 else:
169 pb = self.images[(percent/10)]
170 self.pixbuf = pb
171 self.resize_image(self.size)
173 return 1 # to keep the timer going!
176 def update_tooltip(self):
177 self.tooltips.set_tip(self, self.status() + self.percent() + '%')
178 return 1 #to keep timer running
180 def status(self):
181 txt = _("Unknown")
182 BATTERY.update()
183 if BATTERY.charging_state() == 1:
184 txt = _("AC Online: ")
185 elif BATTERY.charging_state() == 2:
186 txt = _("Charging: ")
187 else:
188 # Discharing from the battery
189 if self.msg == 1:
190 self.msg = 0
191 txt = _("Battery: ")
192 else:
193 self.msg = 1
194 if BATT_TYPE == 1:
195 temp2 = BATTERY.time()
196 temp = int(temp2 / 60)
197 temp2 -= (temp * 60)
198 if temp < 0:
199 txt = _("Calculating... ")
200 else:
201 txt = "(%d:%02d) " % (temp, temp2)
202 else:
203 try:
204 temp = BATTERY.estimated_lifetime()
205 temp2 = int(60 * (temp - int(temp)))
206 txt = "(%d:%02d) " % (temp, temp2)
207 except ValueError:
208 txt = _("Charging")
209 return txt
211 def percent(self):
212 return str(BATTERY.percent())
215 def resize(self, widget, rectangle):
216 """Called when the panel sends a size."""
218 if self.vertical:
219 size = rectangle[2]
220 else:
221 size = rectangle[3]
222 if size != self.size:
223 self.resize_image(size)
225 def resize_image(self, size):
226 """Resize the application image."""
227 scaled_pixbuf = self.pixbuf.scale_simple(size, size, gtk.gdk.INTERP_BILINEAR)
228 self.image.set_from_pixbuf(scaled_pixbuf)
229 self.size = size
231 def button_press(self, window, event):
232 """Handle mouse clicks by popping up the matching menu."""
233 if event.button == 1:
234 self.run_it()
235 elif event.button == 2:
236 self.checkit()
237 elif event.button == 3:
238 self.appmenu.popup(self, event, self.position_menu)
240 def get_panel_orientation(self):
241 """ Return panel orientation and margin for displaying a popup menu.
242 Position in ('Top', 'Bottom', 'Left', 'Right').
244 pos = self.socket.property_get('_ROX_PANEL_MENU_POS', 'STRING', False)
245 if pos: pos = pos[2]
246 if pos:
247 side, margin = pos.split(',')
248 margin = int(margin)
249 else:
250 side, margin = None, 2
251 return side
253 def get_options(self, widget=None, rebuild=False, response=False):
254 """Used as the notify callback when options change."""
255 pass
257 def show_options(self, button=None):
258 """Open the options edit dialog."""
259 rox.edit_options()
261 def get_info(self):
262 """Display an InfoWin self."""
263 from rox import InfoWin
264 InfoWin.infowin(APP_NAME)
266 def build_appmenu(self):
267 """Build the right-click app menu."""
268 items = []
269 items.append(Menu.Action(_('Info...'), 'get_info', '', gtk.STOCK_DIALOG_INFO))
270 items.append(Menu.Action(_('Options...'), 'show_options', '', gtk.STOCK_PREFERENCES))
271 items.append(Menu.Separator())
272 items.append(Menu.Action(_('Close'), 'quit', '', gtk.STOCK_CLOSE))
273 self.appmenu = Menu.Menu('other', items)
274 self.appmenu.attach(self, self)
276 def quit(self, *args):
277 """Quit applet and close everything."""
278 self.destroy()