Adding casper 1.77+debian-7.
[live-initramfs-grml.git] / caspermon / caspermon-applet
1 #!/usr/bin/python
2
3 import pygtk
4 pygtk.require('2.0')
5
6 import gtk
7 import gnome.applet
8 import subprocess
9 import os
10
11 # A simple applet to display the utilization of the snapshot device
12 # during a casper session
13 #
14 # Matt Zimmerman <mdz@canonical.com>
15
16 # TODO:
17 # - tooltip with details
18 # - flash at threshold
19
20 class CasperApplet:
21     def __init__(self, applet, iid):
22         self.timeout_interval = 1000
23         self.device = 'casper-snapshot'
24         self.capacity = [0,0]
25         self.datafile = '/var/lib/casper/snapshot-status'
26         
27         # initializate the gnome internals
28         gnome.init("casper", "0.1")
29
30         self.applet = applet
31
32         self.tooltips = gtk.Tooltips()
33         self.hbox = gtk.HBox()
34         applet.add(self.hbox)
35
36         # add the second button event for the popup menu and the enter mouse event to change the tooltip value
37         self.ev_box = gtk.EventBox()
38         #self.ev_box.connect("button-press-event",self.button_press)
39         self.ev_box.connect("enter-notify-event", self.update_info)
40         self.hbox.add(self.ev_box)
41
42         self.prog = gtk.ProgressBar()
43         self.ev_box.add(self.prog)
44
45         self.update_info()
46         
47         gtk.timeout_add(self.timeout_interval,self.update_info, self)
48
49         applet.connect("destroy",self.cleanup)
50         applet.show_all()
51       
52     def update_info(self, event=None):
53         self.capacity = self.read_info()
54         self.prog.set_fraction(float(self.capacity[0]) / self.capacity[1])
55         self.prog.update()
56
57     def read_info(self):
58         fields = open(self.datafile).readline().split()
59         if fields[2] != 'snapshot':
60             return None
61
62         return map(int,fields[3].split('/', 1))
63
64     def cleanup(self):
65         # what goes here?
66         pass
67
68 def casper_factory(applet, iid):
69     CasperApplet(applet, iid)
70
71     return gtk.TRUE
72
73 gnome.applet.bonobo_factory("OAFIID:GNOME_PythonAppletCasper_Factory", 
74                              gnome.applet.Applet.__gtype__, 
75                              "casper", "0", casper_factory)