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

Revision 23963, 4.8 KB checked in by rbasch, 15 years ago (diff)
In bugme: * Add a handler for timer window visibility events, to raise the window when it has been obscured (e.g. by Nautilus when it takes over the desktop). (Trac: #324) * Change the timer window from a popup to a normal toplevel window, but disallowing input focus, so that it does not obscure menu items.
  • 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        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
105if __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()
Note: See TracBrowser for help on using the repository browser.