Simplify display_url to just use htmlview or webbrowser

svn: r23015
This commit is contained in:
John Ralls 2013-09-03 22:43:33 +00:00
parent 97c981af3f
commit 6203393960
2 changed files with 14 additions and 67 deletions

View File

@ -26,7 +26,7 @@ from gramps.gen.config import config
from gramps.gen.const import GRAMPS_LOCALE as glocale from gramps.gen.const import GRAMPS_LOCALE as glocale
from gramps.gui.utils import open_file_with_default_application as run_file from gramps.gui.utils import open_file_with_default_application as run_file
import os import os
import subprocess import webbrowser
#list of manuals on wiki, map locale code to wiki extension, add language codes #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 #completely, or first part, so pt_BR if Brazilian portugeze wiki manual, and
@ -64,49 +64,11 @@ 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 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)

View File

@ -363,7 +363,7 @@ def display_error_dialog (index, errorstrings):
""" """
Display a message box for errors resulting from xdg-open/open Display a message box for errors resulting from xdg-open/open
""" """
from QuestionDialog import ErrorDialog from .dialog import ErrorDialog
error = _("The external program failed to launch or experienced an error") error = _("The external program failed to launch or experienced an error")
if errorstrings: if errorstrings:
if isinstance(errorstrings, dict): if isinstance(errorstrings, dict):
@ -395,7 +395,7 @@ def poll_external ((proc, errorstrings)):
display_error_dialog(resp, errorstrings) display_error_dialog(resp, errorstrings)
return False return False
def open_file_with_default_application(uri): def open_file_with_default_application(path):
""" """
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
@ -407,26 +407,20 @@ def open_file_with_default_application(uri):
@return: nothing @return: nothing
""" """
from urlparse import urlparse
from time import sleep
errstrings = None errstrings = None
urlcomp = urlparse(uri)
if (not urlcomp.scheme or urlcomp.scheme == 'file'): norm_path = os.path.normpath(path)
norm_path = os.path.normpath(urlcomp.path)
if not os.path.exists(norm_path): if not os.path.exists(norm_path):
display_error_dialog(0, _("File does not exist")) display_error_dialog(0, _("File does not exist"))
return False return
else:
norm_path = uri
if win(): if win():
try: try:
os.startfile(norm_path) os.startfile(norm_path)
except WindowsError, msg: except WindowsError, msg:
display_error_dialog(0, str(msg)) display_error_dialog(0, str(msg))
return False
return True return
if mac(): if mac():
utility = '/usr/bin/open' utility = '/usr/bin/open'
@ -439,18 +433,9 @@ def open_file_with_default_application(uri):
proc = subprocess.Popen([utility, norm_path], stderr=subprocess.STDOUT) proc = subprocess.Popen([utility, norm_path], stderr=subprocess.STDOUT)
sleep(.1)
resp = proc.poll()
if resp is None:
from gi.repository import GLib from gi.repository import GLib
GLib.timeout_add_seconds(1, poll_external, (proc, errstrings)) GLib.timeout_add_seconds(1, poll_external, (proc, errstrings))
return True return
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):
""" """