Bug 7017: Consolidate run_file into open_file_with_default_application

svn: r23009
This commit is contained in:
John Ralls 2013-09-03 16:33:25 +00:00
parent ceccf0c442
commit 6422964a40
2 changed files with 52 additions and 42 deletions

View File

@ -60,34 +60,18 @@ def display_help(webpage='', section=''):
link = link + '#' + section link = link + '#' + section
display_url(link) display_url(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 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 display_url(link, uistate=None): def display_url(link, uistate=None):
""" """
Open the specified URL in a browser. Open the specified URL in a browser.
""" """
from .utils import open_file_with_default_application
if uistate and config.get('htmlview.url-handler'): if uistate and config.get('htmlview.url-handler'):
cat_num = uistate.viewmanager.get_category('Web') cat_num = uistate.viewmanager.get_category('Web')
if cat_num is not None: if cat_num is not 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_file_with_default_application(link, display_error=False):
run_browser(link) run_browser(link)
def run_browser(url): def run_browser(url):

View File

@ -359,7 +359,24 @@ class SystemFonts(object):
# #
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
def poll_external (args): 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:
if isinstance(errorstrings, dict):
try:
error = errorstrings[index]
except KeyError:
pass
else:
error = errorstrings
ErrorDialog(_("Error from external program"), error)
def poll_external ((proc, errorstrings)):
""" """
Check the for completion of a task launched with Check the for completion of a task launched with
subprocess.Popen(). This function is intended to be passed to subprocess.Popen(). This function is intended to be passed to
@ -370,25 +387,16 @@ def poll_external (args):
@errorstrings a dict of possible response values and the corresponding messages to display. @errorstrings a dict of possible response values and the corresponding messages to display.
@returns False when the function has completed. @returns False when the function has completed.
""" """
from .dialog import ErrorDialog from QuestionDialog import ErrorDialog
(proc, errorstrings) = args
resp = proc.poll() resp = proc.poll()
if resp is None: if resp is None:
return True return True
if resp != 0: if resp != 0:
error = "The external program failed to launch or experienced an error" display_error_dialog(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
@ -399,19 +407,28 @@ def open_file_with_default_application( file_path ):
@type file_path: string @type file_path: string
@return: nothing @return: nothing
""" """
norm_path = os.path.normpath( file_path )
if not os.path.exists(norm_path): from urlparse import urlparse
ErrorDialog(_("Error Opening File"), _("File does not exist")) from time import sleep
return errstrings = None
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):
display_error_dialog(0, _("File does not exist"))
return False
else:
norm_path = uri
if win(): if win():
try: try:
os.startfile(norm_path) os.startfile(norm_path)
except WindowsError as msg: except WindowsError, msg:
ErrorDialog(_("Error Opening File"), str(msg)) display_error_dialog(0, str(msg))
return return False
return True
errstrings = None
if mac(): if mac():
utility = '/usr/bin/open' utility = '/usr/bin/open'
else: else:
@ -423,9 +440,18 @@ def open_file_with_default_application( file_path ):
proc = subprocess.Popen([utility, norm_path], stderr=subprocess.STDOUT) proc = subprocess.Popen([utility, norm_path], stderr=subprocess.STDOUT)
from gi.repository import GLib sleep(.1)
GLib.timeout_add_seconds(1, poll_external, (proc, errstrings)) resp = proc.poll()
return if resp is None:
from gi.repository import GLib
GLib.timeout_add_seconds(1, poll_external, (proc, errstrings))
return True
if resp == 0:
return True
if display_error:
display_error_dialog(resp, errstrings)
return False
def process_pending_events(max_count=10): def process_pending_events(max_count=10):
""" """