def show_url(self, url): """Open the given URL in a new browser window. Display an error dialog if everything fails.""" (r, w) = os.pipe() if os.fork() > 0: os.close(w) (pid, status) = os.wait() if status: text = _("Unable to start web browser to open %s." % url) message = os.fdopen(r).readline() if message: text += "\n" + message self.show_error(text) try: os.close(r) except OSError: pass return os.setsid() os.close(r) # If we are called through sudo, determine the real user id and run the # browser with it to get the user's web browser settings. try: uid = int(get_variable("SUDO_UID")) gid = int(get_variable("SUDO_GID")) sudo_prefix = ["sudo", "-H", "-u", "#%s" % uid] except (TypeError): uid = os.getuid() gid = None sudo_prefix = [] # figure out appropriate web browser try: # if ksmserver is running, try kfmclient try: if os.getenv("DISPLAY") and \ subprocess.call(["pgrep", "-x", "-u", str(uid), "ksmserver"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0: subprocess.call(sudo_prefix + ["kfmclient", "openURL", url]) sys.exit(0) except OSError: pass # if gnome-session is running, try gnome-open; special-case firefox # (and more generally, mozilla browsers) and epiphany to open a new window # with respectively -new-window and --new-window try: if os.getenv("DISPLAY") and \ subprocess.call(["pgrep", "-x", "-u", str(uid), "gnome-panel|gconfd-2"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0: gct = subprocess.Popen(sudo_prefix + ["gconftool", "--get", "/desktop/gnome/url-handlers/http/command"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) if gct.wait() == 0: preferred_browser = gct.communicate()[0] browser = re.match("((firefox|seamonkey|flock)[^\s]*)", preferred_browser) if browser: subprocess.call(sudo_prefix + [browser.group(0), "-new-window", url]) sys.exit(0) browser = re.match("(epiphany[^\s]*)", preferred_browser) if browser: subprocess.call(sudo_prefix + [browser.group(0), "--new-window", url]) sys.exit(0) subprocess.call(sudo_prefix + [preferred_browser % url], shell=True) sys.exit(0) if subprocess.call(sudo_prefix + ["gnome-open", url]) == 0: sys.exit(0) except OSError: pass # fall back to webbrowser if uid and gid: os.setgroups([gid]) os.setgid(gid) os.setuid(uid) remove_variable("SUDO_USER") add_variable("HOME", pwd.getpwuid(uid).pw_dir) webbrowser.open(url, new=True, autoraise=True) sys.exit(0) except Exception, e: os.write(w, str(e)) sys.exit(1)
def show_url(self, url): """Open the given URL in a new browser window. Display an error dialog if everything fails.""" # If we are called through sudo, determine the real user id and # run the browser with it to get the user's web browser settings. try: uid = int(get_variable("SUDO_UID")) gid = int(get_variable("SUDO_GID")) sudo_prefix = ["sudo", "-H", "-u", "#%s" % uid] except (TypeError): uid = os.getuid() gid = None sudo_prefix = [] # if ksmserver is running, try kfmclient try: if (os.getenv("DISPLAY") and subprocess.call( ["pgrep", "-x", "-u", str(uid), "ksmserver"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0): subprocess.Popen(sudo_prefix + ["kfmclient", "openURL", url]) return except OSError: pass # if gnome-session is running, try gnome-open; special-case # firefox (and more generally, mozilla browsers) and epiphany # to open a new window with respectively -new-window and # --new-window; special-case chromium-browser to allow file:// # URLs as needed by the checkbox report. try: if (os.getenv("DISPLAY") and subprocess.call( ["pgrep", "-x", "-u", str(uid), "gnome-panel|gconfd-2"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0): from gi.repository import Gio preferred_xml_app = Gio.app_info_get_default_for_type( "application/xml", False) if preferred_xml_app: preferred_browser = preferred_xml_app.get_executable() browser = re.match( "((firefox|seamonkey|flock)[^\s]*)", preferred_browser) if browser: subprocess.Popen( sudo_prefix + [browser.group(0), "-new-window", url]) return browser = re.match("(epiphany[^\s]*)", preferred_browser) if browser: subprocess.Popen( sudo_prefix + [browser.group(0), "--new-window", url]) return browser = re.match( "(chromium-browser[^\s]*)", preferred_browser) if browser: subprocess.Popen( sudo_prefix + [browser.group(0), "--allow-file-access-from-files", url]) return subprocess.Popen( sudo_prefix + [preferred_browser % url], shell=True) return subprocess.Popen(sudo_prefix + ["gnome-open", url]) return except OSError: pass # fall back to webbrowser if uid and gid: os.setgroups([gid]) os.setgid(gid) os.setuid(uid) remove_variable("SUDO_USER") add_variable("HOME", pwd.getpwuid(uid).pw_dir) webbrowser.open(url, new=True, autoraise=True) return