source: trunk/athena/bin/bugme/bugme @ 25443

Revision 25443, 5.0 KB checked in by geofft, 13 years ago (diff)
In bugme: * Set always-on-top window manager hint (Trac: #386). Patch from Luke Faraone.
  • Property svn:executable set to *
Line 
1#!/usr/bin/python -Wall
2
3import gtk
4import gtk.glade
5import gobject
6import time
7import os
8import sys
9from optparse import OptionParser
10
11gladeFile = "/usr/share/bugme/bugme.glade"
12
13class 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        # It's a little odd to put this here, but it doesn't work in
77        # __init__; see comments in Trac #386
78        self.timerWindow.set_keep_above(True)
79        return True
80
81    def nag(self, remainingTime):
82        if self.timeExpired:
83            self.nagLabel.set_markup("<span font_desc=\"20\">Please log out immediately.</span>")
84        else:
85            seconds = "%d second%s" % (remainingTime[1],
86                                       remainingTime[1] != 1 and 's' or '')
87            minutes = "%d minute%s" % (remainingTime[0],
88                                       remainingTime[0] != 1 and 's' or '')
89            if remainingTime[0] < 1:
90                remaining = seconds
91            elif remainingTime[1] == 0:
92                remaining = minutes
93            else:
94                remaining = "%s, %s" % (minutes, seconds)
95            self.nagLabel.set_markup("<span font_desc=\"20\">You have %s remaining\nin your login session.</span>" % (remaining))
96
97        self.nagDialog.show()
98
99    def on_dialog_response(self, dialog, response_id):
100        self.nagDialog.hide()
101
102    def visibility_event_handler(self, widget, event):
103        if event.state != gtk.gdk.VISIBILITY_UNOBSCURED:
104            self.timerWindow.present()
105        return True
106
107
108if __name__ == '__main__':
109    if not os.access(gladeFile, os.R_OK):
110        print 'error: Unable to read glade file "' + gladeFile + '"'
111        sys.exit(255)
112
113    parser = OptionParser(usage="%prog [--debug] progname [args]",
114                          version="%prog 0.1")
115    parser.disable_interspersed_args()
116    parser.add_option("--debug",
117                     action="store_true", dest="debugMode", default=False,
118                     help="Enable debug mode (time limit of 2 minutes)")
119    (options, args) = parser.parse_args()
120    if len(args) < 1:
121        parser.error("'progname' is required")
122    pid = os.fork()
123    if not pid:
124        os.putenv('ATHENA_QUICK', '1')
125        try:
126            os.execvp(args[0], args)
127        except:
128            print "error: Could not execvp(%s,%s)" % (args[0], args)
129    else:
130        BugMe()
131        gtk.main()
Note: See TracBrowser for help on using the repository browser.