From b885046c6403a1e6f2aedb76f3a4a42d60ba0732 Mon Sep 17 00:00:00 2001 From: mathiasdm Date: Thu, 25 Nov 2010 18:12:32 +0000 Subject: [PATCH] - Added README - Added configuration - Added option to start I2P from desktopgui - Cleanup --- apps/desktopgui/README | 13 +++ apps/desktopgui/TODO | 15 +++- apps/desktopgui/build.xml | 3 +- .../src/net/i2p/desktopgui/Main.java | 4 + .../src/net/i2p/desktopgui/TrayManager.java | 42 +++++++++- .../i2p/desktopgui/router/RouterManager.java | 43 ++++++---- .../desktopgui/util/ConfigurationManager.java | 82 +++++++++++++++++++ build.xml | 2 +- 8 files changed, 179 insertions(+), 25 deletions(-) create mode 100644 apps/desktopgui/README create mode 100644 apps/desktopgui/src/net/i2p/desktopgui/util/ConfigurationManager.java diff --git a/apps/desktopgui/README b/apps/desktopgui/README new file mode 100644 index 000000000..2a4f3a40c --- /dev/null +++ b/apps/desktopgui/README @@ -0,0 +1,13 @@ +Current setup: +* Build desktopgui: + * 'ant jar' in the desktopgui directory + OR + * 'ant desktopgui' in the i2p.i2p directory +* Place desktopgui.jar in the $I2P/lib/ directory. +* Add to .i2p/clients.config: + #Desktopgui + clientApp.6.args=--startWithI2P --I2PLocation=/SOME_LOCATION + clientApp.6.delay=5 + clientApp.6.main=net.i2p.desktopgui.Main + clientApp.6.name=desktopgui + clientApp.6.startOnLoad=true diff --git a/apps/desktopgui/TODO b/apps/desktopgui/TODO index d47410b65..5583a0068 100644 --- a/apps/desktopgui/TODO +++ b/apps/desktopgui/TODO @@ -1,8 +1,17 @@ +HIGH PRIORITY: +- Allow desktopgui to start, stop and restart I2P. - DONE +- Internationalisation +- Correct logging system +UNKNOWN: - API to allow applications to add themselves to the menu? * registerApplication(); -- should return a positive number on success, -1 on failure * unregisterApplication(int); -- should return nothing (or bool for success?), and the parameter should be the number given when registering the application -- Internationalisation - Fetch I2P localhost from the core I2P application? -- Correct logging system - Use I2PAppContext::appDir (something like that) for desktopgui data. -- Check core/java/src/net/i2p/util/FileUtil.java for dynamic jar loading +- Consider SWT as option + * Check core/java/src/net/i2p/util/FileUtil.java for dynamic jar loading + * Possible logic: + - First try to load SWT (has the most options and is not ugly) + - Then load AWT +- Access router.jar from other JVM? Is this possible? -- no: use I2CP with auth (not ready yet) +- Start desktopgui with another user than the user starting I2P (required for daemon usage). diff --git a/apps/desktopgui/build.xml b/apps/desktopgui/build.xml index 0d38f0f5d..5c7f2f496 100644 --- a/apps/desktopgui/build.xml +++ b/apps/desktopgui/build.xml @@ -43,7 +43,8 @@ - + + diff --git a/apps/desktopgui/src/net/i2p/desktopgui/Main.java b/apps/desktopgui/src/net/i2p/desktopgui/Main.java index 221de0b06..f4c1948aa 100644 --- a/apps/desktopgui/src/net/i2p/desktopgui/Main.java +++ b/apps/desktopgui/src/net/i2p/desktopgui/Main.java @@ -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() { diff --git a/apps/desktopgui/src/net/i2p/desktopgui/TrayManager.java b/apps/desktopgui/src/net/i2p/desktopgui/TrayManager.java index c7c33d7cd..fac120e06 100644 --- a/apps/desktopgui/src/net/i2p/desktopgui/TrayManager.java +++ b/apps/desktopgui/src/net/i2p/desktopgui/TrayManager.java @@ -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() { + + @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; } diff --git a/apps/desktopgui/src/net/i2p/desktopgui/router/RouterManager.java b/apps/desktopgui/src/net/i2p/desktopgui/router/RouterManager.java index c73e7c481..d7c29612f 100644 --- a/apps/desktopgui/src/net/i2p/desktopgui/router/RouterManager.java +++ b/apps/desktopgui/src/net/i2p/desktopgui/router/RouterManager.java @@ -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(); } } diff --git a/apps/desktopgui/src/net/i2p/desktopgui/util/ConfigurationManager.java b/apps/desktopgui/src/net/i2p/desktopgui/util/ConfigurationManager.java new file mode 100644 index 000000000..872b994fe --- /dev/null +++ b/apps/desktopgui/src/net/i2p/desktopgui/util/ConfigurationManager.java @@ -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 stringConfigurations = new HashMap(); + private Map booleanConfigurations = new HashMap(); + + 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 - +