[23647] | 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 | class BugMe: |
---|
| 12 | def __init__(self): |
---|
| 13 | # Time limit in seconds (600) |
---|
| 14 | self.timeLimit = 600 |
---|
| 15 | # First warn the user when this many seconds remain (120) |
---|
| 16 | self.firstWarn = 120 |
---|
| 17 | # How often to warn initially (secs) |
---|
| 18 | self.warnInterval = 60 |
---|
| 19 | # How often to warn after time has expired (secs) |
---|
| 20 | self.annoyInterval = 30 |
---|
[25525] | 21 | # Hard time limit; user will be killed after this time |
---|
| 22 | self.killTime = self.timeLimit * 1.5 |
---|
[23647] | 23 | if options.debugMode: |
---|
| 24 | self.timeLimit = 120 |
---|
| 25 | self.firstWarn = 90 |
---|
| 26 | self.warnInterval = 30 |
---|
| 27 | self.annoyInterval = 10 |
---|
[25525] | 28 | self.killTime = 180 |
---|
[23647] | 29 | # (foreground, background) |
---|
| 30 | self.colors = ('black', 'white') |
---|
[25697] | 31 | defaultScreen = gtk.gdk.screen_get_default() |
---|
| 32 | self.monitorGeometry = defaultScreen.get_monitor_geometry(defaultScreen.get_primary_monitor()) |
---|
[23647] | 33 | try: |
---|
[25525] | 34 | self.xml = gtk.glade.XML(options.gladeFile) |
---|
[23647] | 35 | except: |
---|
| 36 | print "Failed to create GladeXML object." |
---|
| 37 | # Kill the child |
---|
| 38 | os.kill(pid, 9) |
---|
| 39 | sys.exit(255) |
---|
| 40 | self.startTime = int(time.time()) |
---|
| 41 | self.timerWindow = self.xml.get_widget('TimerWindow') |
---|
[23963] | 42 | self.timerWindow.connect("visibility_notify_event", |
---|
| 43 | self.visibility_event_handler) |
---|
| 44 | self.timerWindow.set_events(gtk.gdk.VISIBILITY_NOTIFY_MASK) |
---|
[23647] | 45 | self.nagDialog = self.xml.get_widget('NagDialog') |
---|
| 46 | self.nagLabel = self.xml.get_widget('NagLabel') |
---|
| 47 | self.elapsed_label = self.xml.get_widget('ElapsedLabel') |
---|
| 48 | self.elapsed_label.set_markup("<span font_desc=\"50\">00:00</span>") |
---|
| 49 | self.timer = gobject.timeout_add(1000, self.updateTimer) |
---|
| 50 | self.xml.signal_autoconnect(self) |
---|
[25697] | 51 | if (options.corner == "NW"): |
---|
| 52 | # 26 px should allow room for top panel |
---|
| 53 | self.timerWindow.move(0,26) |
---|
| 54 | elif (options.corner == "SE"): |
---|
| 55 | self.timerWindow.set_gravity(gtk.gdk.GRAVITY_SOUTH_EAST) |
---|
| 56 | width, height = self.timerWindow.get_size() |
---|
| 57 | self.timerWindow.move(self.monitorGeometry.x + (self.monitorGeometry.width - width), self.monitorGeometry.y + (self.monitorGeometry.height - height)) |
---|
[23647] | 58 | self.timerWindow.show_all() |
---|
| 59 | self.nextWarn = self.startTime + (self.timeLimit - self.firstWarn) |
---|
| 60 | self.timeExpired = False |
---|
| 61 | self.timerWindow.modify_bg(gtk.STATE_NORMAL, |
---|
| 62 | gtk.gdk.color_parse(self.colors[1])) |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | def updateTimer(self): |
---|
| 66 | if pid == os.waitpid(pid, os.WNOHANG)[0]: |
---|
| 67 | sys.exit(0) |
---|
| 68 | now = int(time.time()) |
---|
| 69 | elapsed = now - self.startTime |
---|
| 70 | elapsedTime = (elapsed / 60, elapsed % 60) |
---|
| 71 | self.elapsed_label.set_markup("<span foreground=\"%s\" background=\"%s\" font_desc=\"50\">%02d:%02d</span>" % (self.colors + elapsedTime)) |
---|
| 72 | self.timerWindow.modify_bg(gtk.STATE_NORMAL, |
---|
| 73 | gtk.gdk.color_parse(self.colors[1])) |
---|
| 74 | if elapsed >= self.timeLimit: |
---|
| 75 | self.colors = ('white', 'red') |
---|
| 76 | self.warnInterval = self.annoyInterval |
---|
| 77 | self.timeExpired = True |
---|
| 78 | if now >= self.nextWarn: |
---|
| 79 | if elapsed < self.timeLimit: |
---|
| 80 | self.colors = ('black', 'orange') |
---|
| 81 | self.nextWarn = now + self.warnInterval |
---|
| 82 | self.nag(((self.timeLimit - elapsed) / 60, |
---|
| 83 | (self.timeLimit - elapsed) % 60)) |
---|
[25525] | 84 | if options.killMode and elapsed >= self.killTime: |
---|
| 85 | # The user has outstayed their welcome, nuke their session |
---|
| 86 | print "Quitting." |
---|
| 87 | sys.exit(0) |
---|
| 88 | |
---|
[25443] | 89 | # It's a little odd to put this here, but it doesn't work in |
---|
| 90 | # __init__; see comments in Trac #386 |
---|
| 91 | self.timerWindow.set_keep_above(True) |
---|
[25643] | 92 | # And the above code doesn't work if the user has an "Always |
---|
| 93 | # On Top" window above it. The following does, and bugme lives |
---|
| 94 | # up to its name: |
---|
| 95 | if elapsed % 10 == 0: |
---|
| 96 | self.timerWindow.present() |
---|
[23647] | 97 | return True |
---|
| 98 | |
---|
| 99 | def nag(self, remainingTime): |
---|
| 100 | if self.timeExpired: |
---|
[25525] | 101 | msg="Please log out immediately." |
---|
| 102 | if options.killMode: |
---|
| 103 | msg += "\n\nYour session will be terminated in %d seconds." % (self.killTime - (int(time.time()) - self.startTime)) |
---|
| 104 | self.nagLabel.set_markup("<span font_desc=\"20\">"+msg+"</span>") |
---|
[23647] | 105 | else: |
---|
| 106 | seconds = "%d second%s" % (remainingTime[1], |
---|
| 107 | remainingTime[1] != 1 and 's' or '') |
---|
| 108 | minutes = "%d minute%s" % (remainingTime[0], |
---|
| 109 | remainingTime[0] != 1 and 's' or '') |
---|
| 110 | if remainingTime[0] < 1: |
---|
| 111 | remaining = seconds |
---|
| 112 | elif remainingTime[1] == 0: |
---|
| 113 | remaining = minutes |
---|
| 114 | else: |
---|
| 115 | remaining = "%s, %s" % (minutes, seconds) |
---|
[25525] | 116 | self.nagLabel.set_markup("<span font_desc=\"20\">You have %s remaining\nin this session.</span>" % (remaining)) |
---|
[23647] | 117 | |
---|
| 118 | self.nagDialog.show() |
---|
| 119 | |
---|
| 120 | def on_dialog_response(self, dialog, response_id): |
---|
[25550] | 121 | if response_id == gtk.RESPONSE_CLOSE: |
---|
| 122 | sys.exit(0) |
---|
| 123 | elif response_id == gtk.RESPONSE_OK: |
---|
| 124 | self.nagDialog.hide() |
---|
[23647] | 125 | |
---|
[23963] | 126 | def visibility_event_handler(self, widget, event): |
---|
| 127 | if event.state != gtk.gdk.VISIBILITY_UNOBSCURED: |
---|
| 128 | self.timerWindow.present() |
---|
| 129 | return True |
---|
[23647] | 130 | |
---|
[23963] | 131 | |
---|
[23647] | 132 | if __name__ == '__main__': |
---|
| 133 | parser = OptionParser(usage="%prog [--debug] progname [args]", |
---|
| 134 | version="%prog 0.1") |
---|
| 135 | parser.disable_interspersed_args() |
---|
| 136 | parser.add_option("--debug", |
---|
| 137 | action="store_true", dest="debugMode", default=False, |
---|
| 138 | help="Enable debug mode (time limit of 2 minutes)") |
---|
[25525] | 139 | parser.add_option("--fatal", |
---|
| 140 | action="store_true", dest="killMode", default=False, |
---|
| 141 | help="Kill the user session after 1.5 times the time limit.") |
---|
| 142 | parser.add_option("--glade", action="store", type="string", |
---|
| 143 | dest="gladeFile", |
---|
| 144 | default="/usr/share/bugme/bugme.glade", |
---|
| 145 | help="Specify an alternate Glade file for debugging") |
---|
[25697] | 146 | #TODO: --geometry |
---|
| 147 | parser.add_option("--corner", action="store", type="string", |
---|
| 148 | dest="corner", |
---|
| 149 | default="NW", |
---|
| 150 | help="Specify a corner for the timer") |
---|
[23647] | 151 | (options, args) = parser.parse_args() |
---|
[25697] | 152 | if options.corner not in ("SE", "NW"): |
---|
| 153 | print "Sorry, I don't know about the %s corner of the screen" % (options.corner) |
---|
| 154 | sys.exit(255) |
---|
[25525] | 155 | if not os.access(options.gladeFile, os.R_OK): |
---|
| 156 | print 'error: Unable to read glade file "' + gladeFile + '"' |
---|
| 157 | sys.exit(255) |
---|
[23647] | 158 | if len(args) < 1: |
---|
| 159 | parser.error("'progname' is required") |
---|
| 160 | pid = os.fork() |
---|
| 161 | if not pid: |
---|
| 162 | os.putenv('ATHENA_QUICK', '1') |
---|
| 163 | try: |
---|
| 164 | os.execvp(args[0], args) |
---|
| 165 | except: |
---|
| 166 | print "error: Could not execvp(%s,%s)" % (args[0], args) |
---|
| 167 | else: |
---|
| 168 | BugMe() |
---|
| 169 | gtk.main() |
---|