From a1471cd4835c005ace0af36e5adc2fd1e61bb826 Mon Sep 17 00:00:00 2001 From: angryziber Date: Wed, 9 Apr 2008 21:24:23 +0000 Subject: [PATCH] gnome default browser setting is now respected git-svn-id: https://ipscan.svn.sourceforge.net/svnroot/ipscan/trunk@382 375186e5-ef17-0410-b0b6-91563547dcda --- .../ipscan/gui/actions/BrowserLauncher.java | 44 +++++++++++++++---- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/net/azib/ipscan/gui/actions/BrowserLauncher.java b/src/net/azib/ipscan/gui/actions/BrowserLauncher.java index e9d47888..79cb239e 100755 --- a/src/net/azib/ipscan/gui/actions/BrowserLauncher.java +++ b/src/net/azib/ipscan/gui/actions/BrowserLauncher.java @@ -5,6 +5,8 @@ */ package net.azib.ipscan.gui.actions; +import java.io.BufferedReader; +import java.io.InputStreamReader; import java.lang.reflect.Method; import net.azib.ipscan.config.Platform; @@ -17,6 +19,9 @@ import net.azib.ipscan.core.UserErrorException; */ public class BrowserLauncher { + private static final String[] BROWSERS = {"htmlview", "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"}; + private static String browser; + /** * Opens an URL in the default browser. * Supports Linux/Unix, MacOS, and Windows @@ -30,23 +35,44 @@ public class BrowserLauncher { else if (Platform.MAC_OS) { Class fileMgr = Class.forName("com.apple.eio.FileManager"); - Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class }); + Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] {String.class}); openURL.invoke(null, new Object[] { url }); } else { // assume Linux or other Unix // TODO: what if browser is already running as another user, not root? - String[] browsers = { "htmlview", "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" }; - String browser = null; - for (int count = 0; count < browsers.length && browser == null; count++) - if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0) - browser = browsers[count]; - if (browser == null) - throw new Exception("Could not find web browser"); - Runtime.getRuntime().exec(new String[] { browser, url }); + + if (browser == null) { + // try to detect gnome default browser + browser = execAndReturn("gconftool", "-g", "/desktop/gnome/applications/browser/exec"); + // fall back to searching for known browsers + if (browser == null) { + for (int count = 0; count < BROWSERS.length && browser == null; count++) + if (Runtime.getRuntime().exec(new String[] {"which", BROWSERS[count]}).waitFor() == 0) + browser = BROWSERS[count]; + } + if (browser == null) + throw new Exception("Could not find web browser"); + } + Runtime.getRuntime().exec(new String[] {browser, url}); } } catch (Exception e) { throw new UserErrorException("openURL.failed", url); } } + + private static String execAndReturn(String... exec) { + try { + Process p = Runtime.getRuntime().exec(exec); + if (p.waitFor() == 0) { + BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); + String result = reader.readLine(); + reader.close(); + return result; + } + } + catch (Exception e) { + } + return null; + } }