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.nagDialog = self.xml.get_widget('NagDialog') |
---|
40 | self.nagLabel = self.xml.get_widget('NagLabel') |
---|
41 | self.elapsed_label = self.xml.get_widget('ElapsedLabel') |
---|
42 | self.elapsed_label.set_markup("<span font_desc=\"50\">00:00</span>") |
---|
43 | self.timer = gobject.timeout_add(1000, self.updateTimer) |
---|
44 | self.xml.signal_autoconnect(self) |
---|
45 | # 26 px should allow room for top panel |
---|
46 | self.timerWindow.move(0,26) |
---|
47 | self.timerWindow.show_all() |
---|
48 | self.nextWarn = self.startTime + (self.timeLimit - self.firstWarn) |
---|
49 | self.timeExpired = False |
---|
50 | self.timerWindow.modify_bg(gtk.STATE_NORMAL, |
---|
51 | gtk.gdk.color_parse(self.colors[1])) |
---|
52 | |
---|
53 | |
---|
54 | def updateTimer(self): |
---|
55 | if pid == os.waitpid(pid, os.WNOHANG)[0]: |
---|
56 | sys.exit(0) |
---|
57 | now = int(time.time()) |
---|
58 | elapsed = now - self.startTime |
---|
59 | elapsedTime = (elapsed / 60, elapsed % 60) |
---|
60 | self.elapsed_label.set_markup("<span foreground=\"%s\" background=\"%s\" font_desc=\"50\">%02d:%02d</span>" % (self.colors + elapsedTime)) |
---|
61 | self.timerWindow.modify_bg(gtk.STATE_NORMAL, |
---|
62 | gtk.gdk.color_parse(self.colors[1])) |
---|
63 | if elapsed >= self.timeLimit: |
---|
64 | self.colors = ('white', 'red') |
---|
65 | self.warnInterval = self.annoyInterval |
---|
66 | self.timeExpired = True |
---|
67 | if now >= self.nextWarn: |
---|
68 | if elapsed < self.timeLimit: |
---|
69 | self.colors = ('black', 'orange') |
---|
70 | self.nextWarn = now + self.warnInterval |
---|
71 | self.nag(((self.timeLimit - elapsed) / 60, |
---|
72 | (self.timeLimit - elapsed) % 60)) |
---|
73 | return True |
---|
74 | |
---|
75 | def nag(self, remainingTime): |
---|
76 | if self.timeExpired: |
---|
77 | self.nagLabel.set_markup("<span font_desc=\"20\">Please log out immediately.</span>") |
---|
78 | else: |
---|
79 | seconds = "%d second%s" % (remainingTime[1], |
---|
80 | remainingTime[1] != 1 and 's' or '') |
---|
81 | minutes = "%d minute%s" % (remainingTime[0], |
---|
82 | remainingTime[0] != 1 and 's' or '') |
---|
83 | if remainingTime[0] < 1: |
---|
84 | remaining = seconds |
---|
85 | elif remainingTime[1] == 0: |
---|
86 | remaining = minutes |
---|
87 | else: |
---|
88 | remaining = "%s, %s" % (minutes, seconds) |
---|
89 | self.nagLabel.set_markup("<span font_desc=\"20\">You have %s remaining\nin your login session.</span>" % (remaining)) |
---|
90 | |
---|
91 | self.nagDialog.show() |
---|
92 | |
---|
93 | def on_dialog_response(self, dialog, response_id): |
---|
94 | self.nagDialog.hide() |
---|
95 | |
---|
96 | |
---|
97 | if __name__ == '__main__': |
---|
98 | if not os.access(gladeFile, os.R_OK): |
---|
99 | print 'error: Unable to read glade file "' + gladeFile + '"' |
---|
100 | sys.exit(255) |
---|
101 | |
---|
102 | parser = OptionParser(usage="%prog [--debug] progname [args]", |
---|
103 | version="%prog 0.1") |
---|
104 | parser.disable_interspersed_args() |
---|
105 | parser.add_option("--debug", |
---|
106 | action="store_true", dest="debugMode", default=False, |
---|
107 | help="Enable debug mode (time limit of 2 minutes)") |
---|
108 | (options, args) = parser.parse_args() |
---|
109 | if len(args) < 1: |
---|
110 | parser.error("'progname' is required") |
---|
111 | pid = os.fork() |
---|
112 | if not pid: |
---|
113 | os.putenv('ATHENA_QUICK', '1') |
---|
114 | try: |
---|
115 | os.execvp(args[0], args) |
---|
116 | except: |
---|
117 | print "error: Could not execvp(%s,%s)" % (args[0], args) |
---|
118 | else: |
---|
119 | BugMe() |
---|
120 | gtk.main() |
---|