From 449d1366b1058a782cbc5d8566093576e6299e8f Mon Sep 17 00:00:00 2001 From: John Ralls Date: Fri, 30 Aug 2013 00:01:46 +0000 Subject: [PATCH] [r22949]Bug 7017 GUI stuck when external viewer launched Use a Glib timeout event to poll the open process and handle any error returns svn: r22952 --- src/gui/utils.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/gui/utils.py b/src/gui/utils.py index e22eda629..1440c7059 100644 --- a/src/gui/utils.py +++ b/src/gui/utils.py @@ -328,6 +328,34 @@ class ProgressMeter(object): # # #------------------------------------------------------------------------- +def poll_external ((proc, errorstrings)): + """ + Check the for completion of a task launched with + subprocess.Popen(). This function is intended to be passed to + GLib.timeout_add_seconds, so the arguments are in a tuple because that + function takes only a single data argument. + + @proc the process, returned from subprocess.Popen() + @errorstrings a dict of possible response values and the corresponding messages to display. + @returns False when the function has completed. + """ + from QuestionDialog import ErrorDialog + resp = proc.poll() + if resp is None: + return True + + if resp != 0: + error = "The external program failed to launch or experienced an error" + if errorstrings: + try: + error = errorstrings[resp] + except KeyError: + pass + + ErrorDialog(_("Error from external program"), error) + + return False + def open_file_with_default_application( file_path ): """ Launch a program to open an arbitrary file. The file will be opened using @@ -340,6 +368,7 @@ def open_file_with_default_application( file_path ): @return: nothing """ from QuestionDialog import ErrorDialog + errstrings = None norm_path = os.path.normpath( file_path ) if not os.path.exists(norm_path): ErrorDialog(_("Error Opening File"), _("File does not exist")) @@ -356,11 +385,20 @@ def open_file_with_default_application( file_path ): utility = '/usr/bin/open' else: utility = 'xdg-open' + errstrings = {1:'Error in command line syntax.', + 2:'One of the files passed on the command line did not exist.', + 3:' A required tool could not be found.', + 4:'The action failed.'} + try: subprocess.check_output([utility, norm_path], stderr=subprocess.STDOUT) except subprocess.CalledProcessError as err: ErrorDialog(_("Error Opening File"), err.output) + proc = subprocess.Popen([utility, norm_path], stderr=subprocess.STDOUT) + + from gobject import timout_add + timeout_add(1000, poll_external, (proc, errstrings)) return def process_pending_events(max_count=10):