From 7dc8d0cfecc479f07cdbdfdb55df2f978007c9b5 Mon Sep 17 00:00:00 2001 From: hypercubus Date: Sat, 21 Aug 2004 01:43:35 +0000 Subject: [PATCH] * systray now uses its own config file * added config file handler class * UrlHandler's browser launching behavior is now more sophisticated for Win32 using rundll --- .../src/net/i2p/apps/systray/ConfigFile.java | 78 +++++++++++++ .../net/i2p/apps/systray/ShellCommand.java | 50 +++++---- .../src/net/i2p/apps/systray/SysTray.java | 40 +++---- .../src/net/i2p/apps/systray/UrlLauncher.java | 105 ++++++++++++------ 4 files changed, 193 insertions(+), 80 deletions(-) create mode 100644 apps/systray/java/src/net/i2p/apps/systray/ConfigFile.java diff --git a/apps/systray/java/src/net/i2p/apps/systray/ConfigFile.java b/apps/systray/java/src/net/i2p/apps/systray/ConfigFile.java new file mode 100644 index 000000000..d13d285f6 --- /dev/null +++ b/apps/systray/java/src/net/i2p/apps/systray/ConfigFile.java @@ -0,0 +1,78 @@ +/* + * I2P - An anonymous, secure, and fully-distributed communication network. + * + * ConfigFile.java + * 2004 The I2P Project + * This code is public domain. + */ + +package net.i2p.apps.systray; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Properties; + +/** + * Simple config file handler. + * + * @author hypercubus + * + * TODO Make write operations keep original line comments intact. + */ +public class ConfigFile { + + private String _configFile; + private Properties _properties = new Properties(); + + public ConfigFile(String configFile) { + _configFile = configFile; + readConfigFile(); + } + + public String getProperty(String key) { + return _properties.getProperty(key); + } + + public String getProperty(String key, String defaultValue) { + return _properties.getProperty(key, defaultValue); + } + + public void setProperty(String key, String value) { + _properties.setProperty(key, value); + writeConfigFile(); + } + + private void readConfigFile() { + + FileInputStream fileInputStream = null; + + try { + fileInputStream = new FileInputStream(_configFile); + _properties.load(fileInputStream); + } catch (IOException e) { + System.exit(1); + } + try { + fileInputStream.close(); + } catch (IOException e1) { + // No worries. + } + } + + private void writeConfigFile() { + FileOutputStream fileOutputStream = null; + + try { + fileOutputStream = new FileOutputStream(_configFile); + _properties.store(fileOutputStream, null); + } catch (IOException e) { + System.exit(1); + } + try { + fileOutputStream.close(); + } catch (IOException e1) { + // No worries. + } + } +} diff --git a/apps/systray/java/src/net/i2p/apps/systray/ShellCommand.java b/apps/systray/java/src/net/i2p/apps/systray/ShellCommand.java index b2f4f2015..e3e8b64c1 100644 --- a/apps/systray/java/src/net/i2p/apps/systray/ShellCommand.java +++ b/apps/systray/java/src/net/i2p/apps/systray/ShellCommand.java @@ -346,12 +346,6 @@ public class ShellCommand { processStdoutConsumer = new StreamConsumer(_process.getInputStream()); processStdoutConsumer.start(); } else { - /* - * Will the following stream readers allow _process to return - * just as if _process's streams had been consumed as above? If - * so, get rid of the stream consumers and just use the - * following for all cases. - */ _errorStream = _process.getErrorStream(); _inputStream = _process.getInputStream(); _outputStream = _process.getOutputStream(); @@ -367,24 +361,15 @@ public class ShellCommand { try { _process.waitFor(); } catch (Exception e) { - if (!consumeOutput) { - _errorStream.close(); - _errorStream = null; - _inputStream.close(); - _inputStream = null; - _outputStream.close(); - _outputStream = null; - } + + if (!consumeOutput) + killStreams(); + return false; } - if (!consumeOutput) { - _errorStream.close(); - _errorStream = null; - _inputStream.close(); - _inputStream = null; - _outputStream.close(); - _outputStream = null; - } + + if (!consumeOutput) + killStreams(); if (_process.exitValue() > 0) return false; @@ -395,4 +380,25 @@ public class ShellCommand { } return true; } + + private void killStreams() { + try { + _errorStream.close(); + } catch (IOException e) { + // Fall through. + } + try { + _inputStream.close(); + } catch (IOException e1) { + // Fall through. + } + try { + _outputStream.close(); + } catch (IOException e2) { + // Fall through. + } + _errorStream = null; + _inputStream = null; + _outputStream = null; + } } diff --git a/apps/systray/java/src/net/i2p/apps/systray/SysTray.java b/apps/systray/java/src/net/i2p/apps/systray/SysTray.java index 650d789a9..ef15f9b56 100644 --- a/apps/systray/java/src/net/i2p/apps/systray/SysTray.java +++ b/apps/systray/java/src/net/i2p/apps/systray/SysTray.java @@ -20,55 +20,52 @@ import snoozesoft.systray4j.SysTrayMenuListener; * A system tray control for launching the I2P router console. * * @author hypercubus - * - * TODO Add a menu entry and dialog to let the user specify the location of their preferred web browser. */ public class SysTray implements SysTrayMenuListener { - private static String _browserString; - private BrowserChooser _browserChooser; + private String _browserString; + private ConfigFile _configFile = new ConfigFile("../systray.config"); private Frame _frame; - private SysTrayMenuItem _itemExit = new SysTrayMenuItem("Exit systray", "exit"); + private SysTrayMenuItem _itemExit = new SysTrayMenuItem("Exit I2P systray", "exit"); private SysTrayMenuItem _itemSelectBrowser = new SysTrayMenuItem("Select preferred browser...", "selectbrowser"); private SysTrayMenuIcon _sysTrayMenuIcon = new SysTrayMenuIcon("../icons/iggy"); private SysTrayMenu _sysTrayMenu = new SysTrayMenu(_sysTrayMenuIcon, "I2P Router Console"); + private UrlLauncher _urlLauncher = new UrlLauncher(); public SysTray() { + _browserString = _configFile.getProperty("browser", "default"); _sysTrayMenuIcon.addSysTrayMenuListener(this); createSysTrayMenu(); } public static void main(String[] args) { new SysTray(); - - if (args.length == 1) - _browserString = args[0]; - - while(true) - try { - Thread.sleep(2 * 1000); - } catch (InterruptedException e) { - // blah - } } public void iconLeftClicked(SysTrayMenuEvent e) {} public void iconLeftDoubleClicked(SysTrayMenuEvent e) { - if (_browserString == null || _browserString.equals("browser default")) { + if (_browserString == null || _browserString.equals("default")) { try { - new UrlLauncher().openUrl("http://localhost:7657"); + + if (_urlLauncher.openUrl("http://localhost:7657")) + return; + } catch (Exception ex) { - setBrowser(promptForBrowser("Please select another browser")); + // Fall through. } } else { try { - new UrlLauncher().openUrl("http://localhost:7657", _browserString); + + if (_urlLauncher.openUrl("http://localhost:7657", _browserString)) + return; + } catch (Exception ex) { - setBrowser(promptForBrowser("Please select another browser")); + // Fall through. } } + setBrowser(promptForBrowser("Please select another browser")); } public void menuItemSelected(SysTrayMenuEvent e) { @@ -103,7 +100,6 @@ public class SysTray implements SysTrayMenuListener { private void setBrowser(String browser) { _browserString = browser; - // change "clientApp.3.args=browser" property in clients.config here. - // System.out.println("User chose browser: " + browser); + _configFile.setProperty("browser", browser); } } \ No newline at end of file diff --git a/apps/systray/java/src/net/i2p/apps/systray/UrlLauncher.java b/apps/systray/java/src/net/i2p/apps/systray/UrlLauncher.java index d6c2cabf9..0f3f8cff4 100644 --- a/apps/systray/java/src/net/i2p/apps/systray/UrlLauncher.java +++ b/apps/systray/java/src/net/i2p/apps/systray/UrlLauncher.java @@ -8,6 +8,9 @@ package net.i2p.apps.systray; +import java.net.MalformedURLException; +import java.net.URL; + /** * A quick and simple multi-platform URL launcher. It attempts to launch the * default browser for the host platform first, then popular third-party @@ -27,64 +30,94 @@ public class UrlLauncher { * to launch the given URL using the default browser for that platform; if * unsuccessful, an attempt is made to launch the URL using the most common * browsers. + * + * @param url The URL to open. + * @return true if the operation was successful, otherwise + * false. + * + * @throws Exception */ public boolean openUrl(String url) throws Exception { String osName = System.getProperty("os.name"); - if (osName.toLowerCase().indexOf("mac") > -1) { - if (osName.toLowerCase().startsWith("mac os x")) { + if (validateUrlFormat(url)) { + if (osName.toLowerCase().indexOf("mac") > -1) { + if (osName.toLowerCase().startsWith("mac os x")) { + + if (_shellCommand.executeSilentAndWaitTimed("safari " + url, 5)) + return true; + + } else { + return false; + } + + if (_shellCommand.executeSilentAndWaitTimed("iexplore " + url, 5)) + return true; + + } else if (osName.startsWith("Windows")) { - if (_shellCommand.executeSilentAndWaitTimed("safari " + url, 5)) + if (_shellCommand.executeSilentAndWaitTimed("rundll32 url.dll,FileProtocolHandler " + url, 5)) return true; + if (_shellCommand.executeSilentAndWaitTimed("\"C:\\Program Files\\Internet Explorer\\iexplore.exe\" " + url, 5)) + return true; + } else { - return false; + + if (_shellCommand.executeSilentAndWaitTimed("konqueror " + url, 5)) + return true; + + if (_shellCommand.executeSilentAndWaitTimed("galeon " + url, 5)) + return true; } - if (_shellCommand.executeSilentAndWaitTimed("iexplore " + url, 5)) + if (_shellCommand.executeSilentAndWaitTimed("opera -newpage " + url, 5)) return true; - } else if (osName.startsWith("Windows")) { - - if (_shellCommand.executeSilentAndWaitTimed("\"C:\\Program Files\\Internet Explorer\\iexplore.exe\" " + url, 5)) + if (_shellCommand.executeSilentAndWaitTimed("firefox " + url, 5)) return true; - - } else { - - if (_shellCommand.executeSilentAndWaitTimed("konqueror " + url, 5)) + + if (_shellCommand.executeSilentAndWaitTimed("mozilla " + url, 5)) return true; - - if (_shellCommand.executeSilentAndWaitTimed("galeon " + url, 5)) + + if (_shellCommand.executeSilentAndWaitTimed("netscape " + url, 5)) + return true; + + if (_shellCommand.executeSilentAndWaitTimed("links " + url, 5)) + return true; + + if (_shellCommand.executeSilentAndWaitTimed("lynx " + url, 5)) return true; } + return false; + } - if (_shellCommand.executeSilentAndWaitTimed("firefox " + url, 5)) - return true; + /** + * Opens the given URL with the given browser. + * + * @param url The URL to open. + * @param browser The browser to use. + * @return true if the operation was successful, + * otherwise false. + * + * @throws Exception + */ + public boolean openUrl(String url, String browser) throws Exception { - if (_shellCommand.executeSilentAndWaitTimed("opera -newpage " + url, 5)) - return true; - - if (_shellCommand.executeSilentAndWaitTimed("mozilla " + url, 5)) - return true; - - if (_shellCommand.executeSilentAndWaitTimed("netscape " + url, 5)) - return true; - - if (_shellCommand.executeSilentAndWaitTimed("links " + url, 5)) - return true; - - if (_shellCommand.executeSilentAndWaitTimed("lynx " + url, 5)) - return true; + if (validateUrlFormat(url)) + if (_shellCommand.executeSilentAndWaitTimed(browser + " " + url, 5)) + return true; return false; } - public boolean openUrl(String url, String browser) throws Exception { - // System.out.println("Launching: '" + browser + " " + url + "'"); - if (_shellCommand.executeSilentAndWaitTimed(browser + " " + url, 5)) - return true; - - return false; + private boolean validateUrlFormat(String urlString) { + try { + URL url = new URL(urlString); + } catch (MalformedURLException e) { + return false; + } + return true; } }