forked from I2P_Developers/i2p.i2p
- Added README
- Added configuration - Added option to start I2P from desktopgui - Cleanup
This commit is contained in:
@@ -9,6 +9,7 @@ import javax.swing.UIManager;
|
||||
import javax.swing.UnsupportedLookAndFeelException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import net.i2p.desktopgui.util.*;
|
||||
|
||||
/**
|
||||
* The main class of the application.
|
||||
@@ -39,7 +40,10 @@ public class Main {
|
||||
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
|
||||
ConfigurationManager.getInstance().loadArguments(args);
|
||||
|
||||
final Main main = new Main();
|
||||
|
||||
main.launchForeverLoop();
|
||||
//We'll be doing GUI work, so let's stay in the event dispatcher thread.
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.awt.SystemTray;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.TrayIcon;
|
||||
import java.awt.Desktop.Action;
|
||||
import java.awt.TrayIcon.MessageType;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.IOException;
|
||||
@@ -20,6 +21,7 @@ import java.util.Properties;
|
||||
import javax.swing.SwingWorker;
|
||||
|
||||
import net.i2p.desktopgui.router.RouterManager;
|
||||
import net.i2p.desktopgui.util.*;
|
||||
import net.i2p.router.Router;
|
||||
|
||||
/**
|
||||
@@ -54,6 +56,8 @@ public class TrayManager {
|
||||
* @return popup menu
|
||||
*/
|
||||
public PopupMenu getMainMenu() {
|
||||
boolean inI2P = ConfigurationManager.getInstance().getBooleanConfiguration("startWithI2P", false);
|
||||
|
||||
PopupMenu popup = new PopupMenu();
|
||||
MenuItem browserLauncher = new MenuItem("Launch I2P Browser");
|
||||
browserLauncher.addActionListener(new ActionListener() {
|
||||
@@ -95,6 +99,32 @@ public class TrayManager {
|
||||
});
|
||||
popup.add(browserLauncher);
|
||||
popup.addSeparator();
|
||||
MenuItem startItem = new MenuItem("Start I2P");
|
||||
startItem.addActionListener(new ActionListener() {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
new SwingWorker<Object, Object>() {
|
||||
|
||||
@Override
|
||||
protected Object doInBackground() throws Exception {
|
||||
RouterManager.start();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void done() {
|
||||
trayIcon.displayMessage("Starting", "I2P is starting!", TrayIcon.MessageType.INFO);
|
||||
tray.remove(trayIcon);
|
||||
}
|
||||
|
||||
}.execute();
|
||||
}
|
||||
|
||||
});
|
||||
if(!inI2P) {
|
||||
popup.add(startItem);
|
||||
}
|
||||
MenuItem restartItem = new MenuItem("Restart I2P");
|
||||
restartItem.addActionListener(new ActionListener() {
|
||||
|
||||
@@ -104,7 +134,7 @@ public class TrayManager {
|
||||
|
||||
@Override
|
||||
protected Object doInBackground() throws Exception {
|
||||
RouterManager.shutDown(Router.EXIT_GRACEFUL_RESTART);
|
||||
RouterManager.restart();
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -113,7 +143,9 @@ public class TrayManager {
|
||||
}
|
||||
|
||||
});
|
||||
popup.add(restartItem);
|
||||
if(inI2P) {
|
||||
popup.add(restartItem);
|
||||
}
|
||||
MenuItem stopItem = new MenuItem("Stop I2P");
|
||||
stopItem.addActionListener(new ActionListener() {
|
||||
|
||||
@@ -123,7 +155,7 @@ public class TrayManager {
|
||||
|
||||
@Override
|
||||
protected Object doInBackground() throws Exception {
|
||||
RouterManager.shutDown(Router.EXIT_GRACEFUL);
|
||||
RouterManager.shutDown();
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -132,7 +164,9 @@ public class TrayManager {
|
||||
}
|
||||
|
||||
});
|
||||
popup.add(stopItem);
|
||||
if(inI2P) {
|
||||
popup.add(stopItem);
|
||||
}
|
||||
return popup;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +1,34 @@
|
||||
package net.i2p.desktopgui.router;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import org.tanukisoftware.wrapper.WrapperManager;
|
||||
import java.io.IOException;
|
||||
|
||||
import net.i2p.desktopgui.util.ConfigurationManager;
|
||||
import net.i2p.router.Router;
|
||||
import net.i2p.router.RouterContext;
|
||||
|
||||
public class RouterManager {
|
||||
|
||||
private static Router getRouter() {
|
||||
return RouterContext.listContexts().get(0).router();
|
||||
}
|
||||
|
||||
public static void start() {
|
||||
try {
|
||||
String location = ConfigurationManager.getInstance().getStringConfiguration("I2PLocation", "/home/i2p");
|
||||
//TODO: detect I2P directory
|
||||
//TODO: crossplatform
|
||||
Runtime.getRuntime().exec(location + "/i2psvc " + location + "/wrapper.config");
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void restart() {
|
||||
getRouter().restart();
|
||||
}
|
||||
|
||||
public static void shutDown(final int exitCode) {
|
||||
I2PAppContext.getGlobalContext().addShutdownTask(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
WrapperManager.signalStopped(exitCode);
|
||||
}
|
||||
catch(Throwable t) {
|
||||
//TODO: log
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
public static void shutDown() {
|
||||
getRouter().shutdownGracefully();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package net.i2p.desktopgui.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Manage the configuration of desktopgui.
|
||||
* @author mathias
|
||||
*
|
||||
*/
|
||||
public class ConfigurationManager {
|
||||
|
||||
private static ConfigurationManager instance;
|
||||
private Map<String, String> stringConfigurations = new HashMap<String, String>();
|
||||
private Map<String, Boolean> booleanConfigurations = new HashMap<String, Boolean>();
|
||||
|
||||
private ConfigurationManager() {}
|
||||
|
||||
public static ConfigurationManager getInstance() {
|
||||
if(instance == null) {
|
||||
instance = new ConfigurationManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void loadArguments(String[] args) {
|
||||
//Match pattern of the form --word=true or --word=false
|
||||
Pattern booleanConfiguration = Pattern.compile("--(\\w)");
|
||||
//Match pattern of the form --word=word
|
||||
Pattern stringConfiguration = Pattern.compile("--(\\w)=(\\w)");
|
||||
//Match pattern of the form --word
|
||||
Pattern existsConfiguration = Pattern.compile("--(\\w)");
|
||||
for(int i=0; i<args.length; i++) {
|
||||
String arg = args[i];
|
||||
if(arg.startsWith("--")) {
|
||||
arg = arg.substring(2);
|
||||
if(arg.length() < 1) {
|
||||
continue;
|
||||
}
|
||||
int equals = arg.indexOf('=');
|
||||
if(equals != -1 && equals < arg.length() - 1) { //String configuration
|
||||
loadStringConfiguration(arg, equals);
|
||||
}
|
||||
else { //Boolean configuration
|
||||
loadBooleanConfiguration(arg);
|
||||
}
|
||||
}
|
||||
else if(arg.startsWith("-")) { //Boolean configuration
|
||||
loadBooleanConfiguration(arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void loadBooleanConfiguration(String arg) {
|
||||
booleanConfigurations.put(arg, Boolean.TRUE);
|
||||
}
|
||||
|
||||
public void loadStringConfiguration(String arg, int equalsPosition) {
|
||||
String key = arg.substring(0, equalsPosition);
|
||||
String value = arg.substring(equalsPosition+1);
|
||||
stringConfigurations.put(key, value);
|
||||
}
|
||||
|
||||
public boolean getBooleanConfiguration(String arg, boolean defaultValue) {
|
||||
Boolean value = ((Boolean) booleanConfigurations.get("startWithI2P"));
|
||||
System.out.println(value);
|
||||
if(value != null) {
|
||||
return value;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public String getStringConfiguration(String arg, String defaultValue) {
|
||||
String value = stringConfigurations.get(arg);
|
||||
System.out.println(value);
|
||||
if(value != null) {
|
||||
return value;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user