Bug 7017: Consolidate run_file into open_file_with_default_application

Also fix botched implementation in r22952, extract display_error function

svn: r23002
This commit is contained in:
John Ralls 2013-09-02 19:31:32 +00:00
parent 4d5cf2a62b
commit 62743c6321
2 changed files with 44 additions and 45 deletions

View File

@ -86,26 +86,9 @@ def url(link, uistate=None):
page = uistate.viewmanager.goto_page(cat_num, None) page = uistate.viewmanager.goto_page(cat_num, None)
page.open(link) page.open(link)
return return
if not run_file(link): if not open_with_default_application(link):
run_browser(link) run_browser(link)
def run_file(file_name):
"""
Open a file or url with the default application. This should work
on GNOME, KDE, XFCE, ... as we use a freedesktop application
"""
if constfunc.is_quartz():
prog = find_binary('open')
else:
prog = find_binary('xdg-open')
if prog:
try:
subprocess.check_call([prog, file_name])
except subprocess.CalledProcessError:
return False
return True
return False
def run_browser(url): def run_browser(url):
""" """
Attempt of find a browswer, and launch with the browser with the Attempt of find a browswer, and launch with the browser with the

View File

@ -328,6 +328,20 @@ class ProgressMeter(object):
# #
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
def display_error_dialog (index, errorstrings):
"""
Display a message box for errors resulting from xdg-open/open
"""
from QuestionDialog import ErrorDialog
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)
def poll_external ((proc, errorstrings)): def poll_external ((proc, errorstrings)):
""" """
Check the for completion of a task launched with Check the for completion of a task launched with
@ -345,18 +359,10 @@ def poll_external ((proc, errorstrings)):
return True return True
if resp != 0: if resp != 0:
error = "The external program failed to launch or experienced an error" display_error(resp, errorstrings)
if errorstrings:
try:
error = errorstrings[resp]
except KeyError:
pass
ErrorDialog(_("Error from external program"), error)
return False return False
def open_file_with_default_application( file_path ): def open_file_with_default_application(uri):
""" """
Launch a program to open an arbitrary file. The file will be opened using Launch a program to open an arbitrary file. The file will be opened using
whatever program is configured on the host as the default program for that whatever program is configured on the host as the default program for that
@ -368,38 +374,48 @@ def open_file_with_default_application( file_path ):
@return: nothing @return: nothing
""" """
from QuestionDialog import ErrorDialog from QuestionDialog import ErrorDialog
from urlparse import urlparse
from time import sleep
errstrings = None errstrings = None
norm_path = os.path.normpath( file_path ) urlcomp = urlparse(uri)
if (not urlcomp.scheme or urlcomp.scheme == 'file'):
norm_path = os.path.normpath(urlcomp.path)
if not os.path.exists(norm_path): if not os.path.exists(norm_path):
ErrorDialog(_("Error Opening File"), _("File does not exist")) ErrorDialog(_("Error Opening File"), _("File does not exist"))
return return False
else:
norm_path = uri
if constfunc.win(): if constfunc.win():
try: try:
os.startfile(norm_path) os.startfile(norm_path)
except WindowsError, msg: except WindowsError, msg:
ErrorDialog(_("Error Opening File"), str(msg)) ErrorDialog(_("Error Opening File"), str(msg))
return return False
return True
if constfunc.mac(): if constfunc.mac():
utility = '/usr/bin/open' utility = '/usr/bin/open'
else: else:
utility = 'xdg-open' utility = 'xdg-open'
errstrings = {1:'Error in command line syntax.', errstrings = {1:_('Error in command line syntax.'),
2:'One of the files passed on the command line did not exist.', 2:_('One of the files passed on the command line did not exist.'),
3:' A required tool could not be found.', 3:_('A required tool could not be found.'),
4:'The action failed.'} 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) proc = subprocess.Popen([utility, norm_path], stderr=subprocess.STDOUT)
sleep(.1)
from gobject import timout_add resp = proc.poll()
if resp is None:
from gobject import timeout_add
timeout_add(1000, poll_external, (proc, errstrings)) timeout_add(1000, poll_external, (proc, errstrings))
return return True
if resp == 0:
return True
display_error(resp, errstrings)
return False
def process_pending_events(max_count=10): def process_pending_events(max_count=10):
""" """