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:
parent
4d5cf2a62b
commit
62743c6321
@ -86,26 +86,9 @@ def url(link, uistate=None):
|
||||
page = uistate.viewmanager.goto_page(cat_num, None)
|
||||
page.open(link)
|
||||
return
|
||||
if not run_file(link):
|
||||
if not open_with_default_application(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):
|
||||
"""
|
||||
Attempt of find a browswer, and launch with the browser with the
|
||||
|
@ -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)):
|
||||
"""
|
||||
Check the for completion of a task launched with
|
||||
@ -345,18 +359,10 @@ def poll_external ((proc, errorstrings)):
|
||||
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)
|
||||
|
||||
display_error(resp, errorstrings)
|
||||
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
|
||||
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
|
||||
"""
|
||||
from QuestionDialog import ErrorDialog
|
||||
from urlparse import urlparse
|
||||
from time import sleep
|
||||
errstrings = None
|
||||
norm_path = os.path.normpath( file_path )
|
||||
if not os.path.exists(norm_path):
|
||||
ErrorDialog(_("Error Opening File"), _("File does not exist"))
|
||||
return
|
||||
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):
|
||||
ErrorDialog(_("Error Opening File"), _("File does not exist"))
|
||||
return False
|
||||
else:
|
||||
norm_path = uri
|
||||
|
||||
if constfunc.win():
|
||||
try:
|
||||
os.startfile(norm_path)
|
||||
except WindowsError, msg:
|
||||
ErrorDialog(_("Error Opening File"), str(msg))
|
||||
return
|
||||
return False
|
||||
return True
|
||||
|
||||
if constfunc.mac():
|
||||
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)
|
||||
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.')}
|
||||
|
||||
proc = subprocess.Popen([utility, norm_path], stderr=subprocess.STDOUT)
|
||||
sleep(.1)
|
||||
resp = proc.poll()
|
||||
if resp is None:
|
||||
from gobject import timeout_add
|
||||
timeout_add(1000, poll_external, (proc, errstrings))
|
||||
return True
|
||||
if resp == 0:
|
||||
return True
|
||||
|
||||
from gobject import timout_add
|
||||
timeout_add(1000, poll_external, (proc, errstrings))
|
||||
return
|
||||
display_error(resp, errstrings)
|
||||
return False
|
||||
|
||||
def process_pending_events(max_count=10):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user