From 2f7994e68a7b6bff87c62682244248551a108ad1 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Wed, 4 Sep 2013 16:37:22 +0000 Subject: [PATCH] Simplify display_url to just use htmlview or webbrowser svn: r23033 --- src/GrampsDisplay.py | 45 ++++---------------------------------------- src/gui/utils.py | 40 +++++++++++++-------------------------- 2 files changed, 17 insertions(+), 68 deletions(-) diff --git a/src/GrampsDisplay.py b/src/GrampsDisplay.py index e8b48a786..1dd7b4994 100644 --- a/src/GrampsDisplay.py +++ b/src/GrampsDisplay.py @@ -25,7 +25,7 @@ import constfunc import config import locale import os -import subprocess +import webbrowser #list of manuals on wiki, map locale code to wiki extension, add language codes #completely, or first part, so pt_BR if Brazilian portugeze wiki manual, and @@ -75,10 +75,10 @@ def help(webpage='', section=''): if section: link = link + '#' + section url(link) - + def url(link, uistate=None): """ - Open the specified URL in a browser. + Open the specified URL in a browser. """ from gui.utils import open_file_with_default_application if uistate and config.get('htmlview.url-handler'): @@ -87,42 +87,5 @@ def url(link, uistate=None): page = uistate.viewmanager.goto_page(cat_num, None) page.open(link) return - if not open_file_with_default_application(link, display_error=False): - run_browser(link) - -def run_browser(url): - """ - Attempt of find a browswer, and launch with the browser with the - specified URL - Use run_file first! - """ - try: - import webbrowser - webbrowser.open_new_tab(url) - except ImportError: - for browser in ['firefox', 'konqueror', 'epiphany', - 'galeon', 'mozilla']: - prog = find_binary(browser) - if prog: - os.spawnvpe(os.P_NOWAIT, prog, [prog, url], os.environ) - return - - # If we did not find a browser in the path, try this - try: - os.startfile(url) - except: - pass - -def find_binary(file): - """ - Find the binary (executable) of a filename in the PATH, and return full - path if found, else return None. - """ - import os - search = os.environ['PATH'].split(':') - for path in search: - prog = os.path.join(path, file) - if os.path.isfile(prog): - return prog - return None + webbrowser.open_new_tab(link) diff --git a/src/gui/utils.py b/src/gui/utils.py index fa5da7f21..05a47414a 100644 --- a/src/gui/utils.py +++ b/src/gui/utils.py @@ -345,7 +345,7 @@ def display_error_dialog (index, errorstrings): ErrorDialog(_("Error from external program"), error) -def poll_external ((proc, errorstrings)): +def poll_external (args): """ Check the for completion of a task launched with subprocess.Popen(). This function is intended to be passed to @@ -356,7 +356,7 @@ def poll_external ((proc, errorstrings)): @errorstrings a dict of possible response values and the corresponding messages to display. @returns False when the function has completed. """ - from QuestionDialog import ErrorDialog + (proc, errorstrings)= args resp = proc.poll() if resp is None: return True @@ -365,37 +365,31 @@ def poll_external ((proc, errorstrings)): display_error_dialog(resp, errorstrings) return False -def open_file_with_default_application(uri, display_error=True): +def open_file_with_default_application(file_name): """ 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 type of file. @param file_path: The path to the file to be opened. - Example: "c:\foo.txt" + Example: "c:\\foo.txt" @type file_path: string @return: nothing """ - from urlparse import urlparse - from time import sleep 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 + norm_path = os.path.normpath(file_name) + if not os.path.exists(norm_path): + display_error_dialog(0, _("File does not exist")) + return if constfunc.win(): try: os.startfile(norm_path) except WindowsError, msg: display_error_dialog(0, str(msg)) - return False - return True + + return if constfunc.mac(): utility = '/usr/bin/open' @@ -407,18 +401,10 @@ def open_file_with_default_application(uri, display_error=True): 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 - if display_error: - display_error_dialog(resp, errstrings) - return False + from gobject import timeout_add + timeout_add(1000, poll_external, (proc, errstrings)) + return def process_pending_events(max_count=10): """