package net.i2p.router.web; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import net.i2p.I2PAppContext; public class NavHelper { // both indexed by standard (untranslated) app name private static final Map _apps = new ConcurrentHashMap(4); private static final Map _binary = new ConcurrentHashMap(4); /** * To register a new client application so that it shows up on the router * console's nav bar, it should be registered with this singleton. * * @param name standard name for the app (plugin) * @param displayName translated name the app will be called in the link * warning, this is the display name aka ConsoleLinkName, not the plugin name * @param path full path pointing to the application's root * (e.g. /i2ptunnel/index.jsp), non-null * @param tooltip HTML escaped text or null * @param iconpath path-only URL starting with /, HTML escaped, or null * @since 0.9.20 added iconpath parameter */ public static void registerApp(String appName, String displayName, String path, String tooltip, String iconpath) { if (iconpath != null && !iconpath.startsWith("/")) iconpath = null; _apps.put(appName, new App(displayName, tooltip, path, iconpath)); } /** * @param name standard name for the app */ public static void unregisterApp(String name) { _apps.remove(name); } /** * Retrieve binary icon for a plugin * @param name plugin name * @return null if not found * @since 0.9.25 */ public static byte[] getBinary(String name){ if(name != null) return _binary.get(name); else return null; } /** * Store binary icon for a plugin * @param name plugin name * @since 0.9.25 */ public static void setBinary(String name, byte[] arr){ _binary.put(name, arr); } /** * Translated string is loaded by PluginStarter * @return map of translated name to HTML string, or null if none */ public static Map getClientAppLinks() { if (_apps.isEmpty()) return null; Map rv = new HashMap(_apps.size()); StringBuilder buf = new StringBuilder(128); for (Map.Entry e : _apps.entrySet()) { String appName = e.getKey(); App app = e.getValue(); String path = app.url; if (path == null) continue; String name = app.name; buf.setLength(0); buf.append(""); getClientAppImg(buf, appName, app.icon); buf.append("').append(name.replace(" ", " ")).append("\n"); rv.put(name, buf.toString()); } return rv; } /** * Get 16x16 icon img and append to buf * @param name standard app name * @since 0.9.45 */ private static void getClientAppImg(StringBuilder buf, String name, String iconpath) { if (iconpath != null) { buf.append("\"\""); } else if (name.equals("orchid")) { buf.append("\"\""); } else if (name.equals("i2pbote")) { buf.append("\"\""); } else { buf.append("\"\""); } } /** * For HomeHelper. 32x32 icon paths. * @param ctx unused * @return non-null, possibly empty, unsorted * @since 0.9, public since 0.9.33, was package private */ public static List getClientApps(I2PAppContext ctx) { if (_apps.isEmpty()) return Collections.emptyList(); List rv = new ArrayList(_apps.size()); for (Map.Entry e : _apps.entrySet()) { String name = e.getKey(); App mapp = e.getValue(); if (mapp.url == null) continue; String tip = mapp.desc; if (tip == null) tip = ""; String icon = mapp.icon; if (icon == null) { // hardcoded hack if (name.equals("i2pbote")) icon = "/themes/console/images/email.png"; else icon = "/themes/console/images/plugin.png"; } App app = new App(mapp.name, tip, mapp.url, icon); rv.add(app); } return rv; } }