mirror of
https://github.com/angryip/ipscan.git
synced 2025-10-26 11:18:17 +00:00
gnome default browser setting is now respected
git-svn-id: https://ipscan.svn.sourceforge.net/svnroot/ipscan/trunk@382 375186e5-ef17-0410-b0b6-91563547dcda
This commit is contained in:
parent
136fab4130
commit
a1471cd483
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user