~francesco-marella/specto/blacklist-plugins

« back to all changes in this revision

Viewing changes to spectlib/main.py

  • Committer: Jean-François Fortin Tam
  • Date: 2009-12-29 21:09:45 UTC
  • mfrom: (140.1.3 raise-window-if-running)
  • Revision ID: nekohayo@gmail.com-20091229210945-asrue5nhdd85miaj
Merge Emilio's fix: try raising the window if already running

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
import os
31
31
import sys
 
32
import signal
32
33
import gobject
33
34
import gettext
34
35
 
136
137
            if self.GTK:
137
138
                for watch in self.watch_db:
138
139
                    self.notifier.add_notifier_entry(watch.id)
139
 
 
 
140
                
 
141
                # Listen for USR1. If received, answer and show the window
 
142
                def listen_for_USR1(signum, frame):
 
143
                    f = open(self.SPECTO_DIR + "/" + "specto.pid.boot")
 
144
                    pid = int(f.readline())
 
145
                    f.close()
 
146
                    os.kill(pid, signal.SIGUSR1)
 
147
                    # If window was not shown, make it appear
 
148
                    if not self.notifier.get_state():
 
149
                        self.logger.log("Showing window, the user ran another instance of specto", "debug", "specto")
 
150
                        self.toggle_notifier()
 
151
                    else:
 
152
                        # Based on http://www.pygtk.org/docs/pygtk/class-gtkwindow.html#method-gtkwindow--present
 
153
                        self.logger.log("Window is already visible! Raising it to the front.", "debug", "specto")
 
154
                        self.notifier.notifier.present()
 
155
                
 
156
                signal.signal(signal.SIGUSR1, listen_for_USR1)
 
157
                
140
158
                self.notifier.refresh_all_watches()
141
159
            else:
142
160
                self.console.start_watches()
213
231
            p_name = os.popen("ps -f --pid " + pid).read()
214
232
            if p == 0 and "specto" in p_name:
215
233
                if self.GTK:
216
 
                    self.already_running_dialog()
 
234
                    # Save our pid and prepare a 'pong' system
 
235
                    f = open(pidfile + ".boot", "w")
 
236
                    f.write(str(os.getpid()))
 
237
                    f.close()
 
238
                    
 
239
                    def not_responding(signum, frame):
 
240
                        """ Launch the already running dialog if the
 
241
                            other instance doesn't respond """
 
242
                        os.unlink(pidfile + ".boot")
 
243
                        self.already_running_dialog()
 
244
                        
 
245
                    def response_received(signum, frame):
 
246
                        """ Kill this specto if the other one answers """
 
247
                        signal.alarm(0)
 
248
                        os.unlink(pidfile + ".boot")
 
249
                        self.logger.log("Specto is already running! The old instance will be brought to front.", "debug", "specto")
 
250
                        sys.exit(0)
 
251
                        
 
252
                    signal.signal(signal.SIGALRM, not_responding)
 
253
                    signal.signal(signal.SIGUSR1, response_received)
 
254
                    signal.alarm(5)
 
255
                    
 
256
                    # Send signal to raise window
 
257
                    os.kill(int(pid), signal.SIGUSR1)
 
258
                    
 
259
                    # Wait for signals
 
260
                    signal.pause()
 
261
                    
217
262
                    return True
218
263
                elif DEBUG:
219
264
                    self.logger.log(_("Specto is already running!"), "critical", "specto")