1 | #!/usr/bin/python -Wall |
---|
2 | |
---|
3 | import gtk |
---|
4 | import gtk.glade |
---|
5 | import gobject |
---|
6 | import time |
---|
7 | import os |
---|
8 | import sys |
---|
9 | from optparse import OptionParser |
---|
10 | |
---|
11 | gladeFile = "/usr/share/bugme/bugme.glade" |
---|
12 | |
---|
13 | class BugMe: |
---|
14 | def __init__(self): |
---|
15 | # Time limit in seconds (600) |
---|
16 | self.timeLimit = 600 |
---|
17 | # First warn the user when this many seconds remain (120) |
---|
18 | self.firstWarn = 120 |
---|
19 | # How often to warn initially (secs) |
---|
20 | self.warnInterval = 60 |
---|
21 | # How often to warn after time has expired (secs) |
---|
22 | self.annoyInterval = 30 |
---|
23 | if options.debugMode: |
---|
24 | self.timeLimit = 120 |
---|
25 | self.firstWarn = 90 |
---|
26 | self.warnInterval = 30 |
---|
27 | self.annoyInterval = 10 |
---|
28 | # (foreground, background) |
---|
29 | self.colors = ('black', 'white') |
---|
30 | try: |
---|
31 | self.xml = gtk.glade.XML(gladeFile) |
---|
32 | except: |
---|
33 | print "Failed to create GladeXML object." |
---|
34 | # Kill the child |
---|
35 | os.kill(pid, 9) |
---|
36 | sys.exit(255) |
---|
37 | self.startTime = int(time.time()) |
---|
38 | self.timerWindow = self.xml.get_widget('TimerWindow') |
---|
39 | self.timerWindow.connect("visibility_notify_event", |
---|
40 | self.visibility_event_handler) |
---|
41 | self.timerWindow.set_events(gtk.gdk.VISIBILITY_NOTIFY_MASK) |
---|
42 | self.nagDialog = self.xml.get_widget('NagDialog') |
---|
43 | self.nagLabel = self.xml.get_widget('NagLabel') |
---|
44 | self.elapsed_label = self.xml.get_widget('ElapsedLabel') |
---|
45 | self.elapsed_label.set_markup("<span font_desc=\"50\">00:00</span>") |
---|
46 | self.timer = gobject.timeout_add(1000, self.updateTimer) |
---|
47 | self.xml.signal_autoconnect(self) |
---|
48 | # 26 px should allow room for top panel |
---|
49 | self.timerWindow.move(0,26) |
---|
50 | self.timerWindow.show_all() |
---|
51 | self.nextWarn = self.startTime + (self.timeLimit - self.firstWarn) |
---|
52 | self.timeExpired = False |
---|
53 | self.timerWindow.modify_bg(gtk.STATE_NORMAL, |
---|
54 | gtk.gdk.color_parse(self.colors[1])) |
---|
55 | |
---|
56 | |
---|
57 | def updateTimer(self): |
---|
58 | if pid == os.waitpid(pid, os.WNOHANG)[0]: |
---|
59 | sys.exit(0) |
---|
60 | now = int(time.time()) |
---|
61 | elapsed = now - self.startTime |
---|
62 | elapsedTime = (elapsed / 60, elapsed % 60) |
---|
63 | self.elapsed_label.set_markup("<span foreground=\"%s\" background=\"%s\" font_desc=\"50\">%02d:%02d</span>" % (self.colors + elapsedTime)) |
---|
64 | self.timerWindow.modify_bg(gtk.STATE_NORMAL, |
---|
65 | gtk.gdk.color_parse(self.colors[1])) |
---|
66 | if elapsed >= self.timeLimit: |
---|
67 | self.colors = ('white', 'red') |
---|
68 | self.warnInterval = self.annoyInterval |
---|
69 | self.timeExpired = True |
---|
70 | if now >= self.nextWarn: |
---|
71 | if elapsed < self.timeLimit: |
---|
72 | self.colors = ('black', 'orange') |
---|
73 | self.nextWarn = now + self.warnInterval |
---|
74 | self.nag(((self.timeLimit - elapsed) / 60, |
---|
75 | (self.timeLimit - elapsed) % 60)) |
---|
76 | return True |
---|
77 | |
---|
78 | def nag(self, remainingTime): |
---|
79 | if self.timeExpired: |
---|
80 | self.nagLabel.set_markup("<span font_desc=\"20\">Please log out immediately.</span>") |
---|
81 | else: |
---|
82 | seconds = "%d second%s" % (remainingTime[1], |
---|
83 | remainingTime[1] != 1 and 's' or '') |
---|
84 | minutes = "%d minute%s" % (remainingTime[0], |
---|
85 | remainingTime[0] != 1 and 's' or '') |
---|
86 | if remainingTime[0] < 1: |
---|
87 | remaining = seconds |
---|
88 | elif remainingTime[1] == 0: |
---|
89 | remaining = minutes |
---|
90 | else: |
---|
91 | remaining = "%s, %s" % (minutes, seconds) |
---|
92 | self.nagLabel.set_markup("<span font_desc=\"20\">You have %s remaining\nin your login session.</span>" % (remaining)) |
---|
93 | |
---|
94 | self.nagDialog.show() |
---|
95 | |
---|
96 | def on_dialog_response(self, dialog, response_id): |
---|
97 | self.nagDialog.hide() |
---|
98 | |
---|
99 | def visibility_event_handler(self, widget, event): |
---|
100 | if event.state != gtk.gdk.VISIBILITY_UNOBSCURED: |
---|
101 | self.timerWindow.present() |
---|
102 | return True |
---|
103 | |
---|
104 | |
---|
105 | if __name__ == '__main__': |
---|
106 | if not os.access(gladeFile, os.R_OK): |
---|
107 | print 'error: Unable to read glade file "' + gladeFile + '"' |
---|
108 | sys.exit(255) |
---|
109 | |
---|
110 | parser = OptionParser(usage="%prog [--debug] progname [args]", |
---|
111 | version="%prog 0.1") |
---|
112 | parser.disable_interspersed_args() |
---|
113 | parser.add_option("--debug", |
---|
114 | action="store_true", dest="debugMode", default=False, |
---|
115 | help="Enable debug mode (time limit of 2 minutes)") |
---|
116 | (options, args) = parser.parse_args() |
---|
117 | if len(args) < 1: |
---|
118 | parser.error("'progname' is required") |
---|
119 | pid = os.fork() |
---|
120 | if not pid: |
---|
121 | os.putenv('ATHENA_QUICK', '1') |
---|
122 | try: |
---|
123 | os.execvp(args[0], args) |
---|
124 | except: |
---|
125 | print "error: Could not execvp(%s,%s)" % (args[0], args) |
---|
126 | else: |
---|
127 | BugMe() |
---|
128 | gtk.main() |
---|