* 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
This commit is contained in:
hypercubus
2004-08-21 01:43:35 +00:00
committed by zzz
parent 846c393168
commit 7dc8d0cfec
4 changed files with 193 additions and 80 deletions

View File

@ -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.
}
}
}

View File

@ -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;
}
}

View File

@ -20,56 +20,53 @@ 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) {
// Fall through.
}
}
setBrowser(promptForBrowser("Please select another browser"));
}
}
}
public void menuItemSelected(SysTrayMenuEvent e) {
if (e.getActionCommand().equals("exit")) {
@ -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);
}
}

View File

@ -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,11 +30,18 @@ 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 <code>true</code> if the operation was successful, otherwise
* <code>false</code>.
*
* @throws Exception
*/
public boolean openUrl(String url) throws Exception {
String osName = System.getProperty("os.name");
if (validateUrlFormat(url)) {
if (osName.toLowerCase().indexOf("mac") > -1) {
if (osName.toLowerCase().startsWith("mac os x")) {
@ -47,6 +57,9 @@ public class UrlLauncher {
} else if (osName.startsWith("Windows")) {
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;
@ -59,10 +72,10 @@ public class UrlLauncher {
return true;
}
if (_shellCommand.executeSilentAndWaitTimed("firefox " + url, 5))
if (_shellCommand.executeSilentAndWaitTimed("opera -newpage " + url, 5))
return true;
if (_shellCommand.executeSilentAndWaitTimed("opera -newpage " + url, 5))
if (_shellCommand.executeSilentAndWaitTimed("firefox " + url, 5))
return true;
if (_shellCommand.executeSilentAndWaitTimed("mozilla " + url, 5))
@ -76,15 +89,35 @@ public class UrlLauncher {
if (_shellCommand.executeSilentAndWaitTimed("lynx " + url, 5))
return true;
}
return false;
}
/**
* Opens the given URL with the given browser.
*
* @param url The URL to open.
* @param browser The browser to use.
* @return <code>true</code> if the operation was successful,
* otherwise <code>false</code>.
*
* @throws Exception
*/
public boolean openUrl(String url, String browser) throws Exception {
// System.out.println("Launching: '" + browser + " " + url + "'");
if (validateUrlFormat(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;
}
}