* 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:
78
apps/systray/java/src/net/i2p/apps/systray/ConfigFile.java
Normal file
78
apps/systray/java/src/net/i2p/apps/systray/ConfigFile.java
Normal 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.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -346,12 +346,6 @@ public class ShellCommand {
|
|||||||
processStdoutConsumer = new StreamConsumer(_process.getInputStream());
|
processStdoutConsumer = new StreamConsumer(_process.getInputStream());
|
||||||
processStdoutConsumer.start();
|
processStdoutConsumer.start();
|
||||||
} else {
|
} 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();
|
_errorStream = _process.getErrorStream();
|
||||||
_inputStream = _process.getInputStream();
|
_inputStream = _process.getInputStream();
|
||||||
_outputStream = _process.getOutputStream();
|
_outputStream = _process.getOutputStream();
|
||||||
@ -367,24 +361,15 @@ public class ShellCommand {
|
|||||||
try {
|
try {
|
||||||
_process.waitFor();
|
_process.waitFor();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (!consumeOutput) {
|
|
||||||
_errorStream.close();
|
if (!consumeOutput)
|
||||||
_errorStream = null;
|
killStreams();
|
||||||
_inputStream.close();
|
|
||||||
_inputStream = null;
|
|
||||||
_outputStream.close();
|
|
||||||
_outputStream = null;
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!consumeOutput) {
|
|
||||||
_errorStream.close();
|
if (!consumeOutput)
|
||||||
_errorStream = null;
|
killStreams();
|
||||||
_inputStream.close();
|
|
||||||
_inputStream = null;
|
|
||||||
_outputStream.close();
|
|
||||||
_outputStream = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_process.exitValue() > 0)
|
if (_process.exitValue() > 0)
|
||||||
return false;
|
return false;
|
||||||
@ -395,4 +380,25 @@ public class ShellCommand {
|
|||||||
}
|
}
|
||||||
return true;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,55 +20,52 @@ import snoozesoft.systray4j.SysTrayMenuListener;
|
|||||||
* A system tray control for launching the I2P router console.
|
* A system tray control for launching the I2P router console.
|
||||||
*
|
*
|
||||||
* @author hypercubus
|
* @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 {
|
public class SysTray implements SysTrayMenuListener {
|
||||||
|
|
||||||
private static String _browserString;
|
|
||||||
|
|
||||||
private BrowserChooser _browserChooser;
|
private BrowserChooser _browserChooser;
|
||||||
|
private String _browserString;
|
||||||
|
private ConfigFile _configFile = new ConfigFile("../systray.config");
|
||||||
private Frame _frame;
|
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 SysTrayMenuItem _itemSelectBrowser = new SysTrayMenuItem("Select preferred browser...", "selectbrowser");
|
||||||
private SysTrayMenuIcon _sysTrayMenuIcon = new SysTrayMenuIcon("../icons/iggy");
|
private SysTrayMenuIcon _sysTrayMenuIcon = new SysTrayMenuIcon("../icons/iggy");
|
||||||
private SysTrayMenu _sysTrayMenu = new SysTrayMenu(_sysTrayMenuIcon, "I2P Router Console");
|
private SysTrayMenu _sysTrayMenu = new SysTrayMenu(_sysTrayMenuIcon, "I2P Router Console");
|
||||||
|
private UrlLauncher _urlLauncher = new UrlLauncher();
|
||||||
|
|
||||||
public SysTray() {
|
public SysTray() {
|
||||||
|
_browserString = _configFile.getProperty("browser", "default");
|
||||||
_sysTrayMenuIcon.addSysTrayMenuListener(this);
|
_sysTrayMenuIcon.addSysTrayMenuListener(this);
|
||||||
createSysTrayMenu();
|
createSysTrayMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new SysTray();
|
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 iconLeftClicked(SysTrayMenuEvent e) {}
|
||||||
|
|
||||||
public void iconLeftDoubleClicked(SysTrayMenuEvent e) {
|
public void iconLeftDoubleClicked(SysTrayMenuEvent e) {
|
||||||
if (_browserString == null || _browserString.equals("browser default")) {
|
if (_browserString == null || _browserString.equals("default")) {
|
||||||
try {
|
try {
|
||||||
new UrlLauncher().openUrl("http://localhost:7657");
|
|
||||||
|
if (_urlLauncher.openUrl("http://localhost:7657"))
|
||||||
|
return;
|
||||||
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
setBrowser(promptForBrowser("Please select another browser"));
|
// Fall through.
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
new UrlLauncher().openUrl("http://localhost:7657", _browserString);
|
|
||||||
|
if (_urlLauncher.openUrl("http://localhost:7657", _browserString))
|
||||||
|
return;
|
||||||
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
setBrowser(promptForBrowser("Please select another browser"));
|
// Fall through.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
setBrowser(promptForBrowser("Please select another browser"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void menuItemSelected(SysTrayMenuEvent e) {
|
public void menuItemSelected(SysTrayMenuEvent e) {
|
||||||
@ -103,7 +100,6 @@ public class SysTray implements SysTrayMenuListener {
|
|||||||
|
|
||||||
private void setBrowser(String browser) {
|
private void setBrowser(String browser) {
|
||||||
_browserString = browser;
|
_browserString = browser;
|
||||||
// change "clientApp.3.args=browser" property in clients.config here.
|
_configFile.setProperty("browser", browser);
|
||||||
// System.out.println("User chose browser: " + browser);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -8,6 +8,9 @@
|
|||||||
|
|
||||||
package net.i2p.apps.systray;
|
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
|
* A quick and simple multi-platform URL launcher. It attempts to launch the
|
||||||
* default browser for the host platform first, then popular third-party
|
* 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
|
* 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
|
* unsuccessful, an attempt is made to launch the URL using the most common
|
||||||
* browsers.
|
* 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 {
|
public boolean openUrl(String url) throws Exception {
|
||||||
|
|
||||||
String osName = System.getProperty("os.name");
|
String osName = System.getProperty("os.name");
|
||||||
|
|
||||||
if (osName.toLowerCase().indexOf("mac") > -1) {
|
if (validateUrlFormat(url)) {
|
||||||
if (osName.toLowerCase().startsWith("mac os x")) {
|
if (osName.toLowerCase().indexOf("mac") > -1) {
|
||||||
|
if (osName.toLowerCase().startsWith("mac os x")) {
|
||||||
|
|
||||||
if (_shellCommand.executeSilentAndWaitTimed("safari " + url, 5))
|
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("rundll32 url.dll,FileProtocolHandler " + url, 5))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (_shellCommand.executeSilentAndWaitTimed("\"C:\\Program Files\\Internet Explorer\\iexplore.exe\" " + url, 5))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} else {
|
} 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;
|
return true;
|
||||||
|
|
||||||
} else if (osName.startsWith("Windows")) {
|
if (_shellCommand.executeSilentAndWaitTimed("firefox " + url, 5))
|
||||||
|
|
||||||
if (_shellCommand.executeSilentAndWaitTimed("\"C:\\Program Files\\Internet Explorer\\iexplore.exe\" " + url, 5))
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} else {
|
if (_shellCommand.executeSilentAndWaitTimed("mozilla " + url, 5))
|
||||||
|
|
||||||
if (_shellCommand.executeSilentAndWaitTimed("konqueror " + url, 5))
|
|
||||||
return true;
|
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 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 <code>true</code> if the operation was successful,
|
||||||
|
* otherwise <code>false</code>.
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public boolean openUrl(String url, String browser) throws Exception {
|
||||||
|
|
||||||
if (_shellCommand.executeSilentAndWaitTimed("opera -newpage " + url, 5))
|
if (validateUrlFormat(url))
|
||||||
return true;
|
if (_shellCommand.executeSilentAndWaitTimed(browser + " " + 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;
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean openUrl(String url, String browser) throws Exception {
|
private boolean validateUrlFormat(String urlString) {
|
||||||
// System.out.println("Launching: '" + browser + " " + url + "'");
|
try {
|
||||||
if (_shellCommand.executeSilentAndWaitTimed(browser + " " + url, 5))
|
URL url = new URL(urlString);
|
||||||
return true;
|
} catch (MalformedURLException e) {
|
||||||
|
return false;
|
||||||
return false;
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user