From a48fba010295048e603fbeb5482e47a3f3e30f87 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Wed, 12 Jun 2013 15:07:39 +0000
Subject: [PATCH]  * I2PTunnel: Add advanced quantity options

---
 .../src/net/i2p/i2ptunnel/web/EditBean.java   | 38 +++++++++++++++++++
 .../src/net/i2p/i2ptunnel/web/IndexBean.java  |  7 ++++
 .../src/net/i2p/i2ptunnel/web/Messages.java   |  7 ++++
 apps/i2ptunnel/jsp/editClient.jsp             | 10 +----
 apps/i2ptunnel/jsp/editServer.jsp             | 13 +------
 5 files changed, 56 insertions(+), 19 deletions(-)

diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/EditBean.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/EditBean.java
index 0f4d6636c8..e74912a326 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/EditBean.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/EditBean.java
@@ -378,4 +378,42 @@ public class EditBean extends IndexBean {
         if (controller == null) return null;
         return controller.getClientOptionProps();
     }
+
+    private static final String PROP_ADVANCED = "routerconsole.advanced";
+    private static final int DFLT_QUANTITY = 2;
+    private static final int MAX_CLIENT_QUANTITY = 3;
+    private static final int MAX_SERVER_QUANTITY = 6;
+    private static final int MAX_ADVANCED_QUANTITY = 16;
+
+    /**
+     *  @since 0.9.7
+     */
+    public String getQuantityOptions(int tunnel) {
+        int tunnelQuantity = getTunnelQuantity(tunnel, DFLT_QUANTITY);
+        boolean advanced = _context.getBooleanProperty(PROP_ADVANCED);
+        int maxQuantity = advanced ? MAX_ADVANCED_QUANTITY :
+                                     (isClient(tunnel) ? MAX_CLIENT_QUANTITY : MAX_SERVER_QUANTITY);
+        if (tunnelQuantity > maxQuantity)
+            maxQuantity = tunnelQuantity;
+        StringBuilder buf = new StringBuilder(256);
+        for (int i = 1; i <= maxQuantity; i++) {
+             buf.append("<option value=\"").append(i).append('"');
+             if (i == tunnelQuantity)
+                 buf.append(" selected=\"selected\"");
+             buf.append('>');
+             buf.append(ngettext("{0} inbound, {0} outbound tunnel", "{0} inbound, {0} outbound tunnels", i));
+             if (i <= 3) {
+                 buf.append(" (");
+                 if (i == 1)
+                     buf.append(_("lower bandwidth and reliability"));
+                 else if (i == 2)
+                     buf.append(_("standard bandwidth and reliability"));
+                 else if (i == 3)
+                     buf.append(_("higher bandwidth and reliability"));
+                 buf.append(')');
+             }
+             buf.append("</option>\n");
+        }
+        return buf.toString();
+    }
 }
diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/IndexBean.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/IndexBean.java
index 692f3585c7..5d8ec7dccd 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/IndexBean.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/IndexBean.java
@@ -1293,4 +1293,11 @@ public class IndexBean {
     protected String _(String key) {
         return Messages._(key, _context);
     }
+
+    /** translate (ngettext)
+     *  @since 0.9.7
+     */
+    protected String ngettext(String s, String p, int n) {
+        return Messages.ngettext(s, p, n, _context);
+    }
 }
diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/Messages.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/Messages.java
index 071123a2ec..d4a2fb4bdc 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/Messages.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/Messages.java
@@ -39,4 +39,11 @@ public class Messages {
     public String _(String s, Object o) {
         return Translate.getString(s, o, _context, BUNDLE_NAME);
     }
+
+    /** translate (ngettext)
+     *  @since 0.9.7
+     */
+    public static String ngettext(String s, String p, int n, I2PAppContext ctx) {
+        return Translate.getString(n, s, p, ctx, BUNDLE_NAME);
+    }
 }
diff --git a/apps/i2ptunnel/jsp/editClient.jsp b/apps/i2ptunnel/jsp/editClient.jsp
index 0e2184fea5..b76b4cbb73 100644
--- a/apps/i2ptunnel/jsp/editClient.jsp
+++ b/apps/i2ptunnel/jsp/editClient.jsp
@@ -247,14 +247,8 @@ input.default { width: 1px; height: 1px; visibility: hidden; }
                     <%=intl._("Count")%>(<span class="accessKey">C</span>):
                 </label>
                 <select id="tunnelQuantity" name="tunnelQuantity" title="Number of Tunnels in Group" class="selectbox">
-                    <% int tunnelQuantity = editBean.getTunnelQuantity(curTunnel, 2);
-                  %><option value="1"<%=(tunnelQuantity == 1 ? " selected=\"selected\"" : "") %>><%=intl._("1 inbound, 1 outbound tunnel  (low bandwidth usage, less reliability)")%></option>
-                    <option value="2"<%=(tunnelQuantity == 2 ? " selected=\"selected\"" : "") %>><%=intl._("2 inbound, 2 outbound tunnels (standard bandwidth usage, standard reliability)")%></option>
-                    <option value="3"<%=(tunnelQuantity == 3 ? " selected=\"selected\"" : "") %>><%=intl._("3 inbound, 3 outbound tunnels (higher bandwidth usage, higher reliability)")%></option>
-                <% if (tunnelQuantity > 3) {
-                %>    <option value="<%=tunnelQuantity%>" selected="selected"><%=tunnelQuantity%>&nbsp;<%=intl._("tunnels")%></option>
-                <% }
-              %></select>                
+                    <%=editBean.getQuantityOptions(curTunnel)%>
+                </select>                
             </div>
             <div id="backupField" class="rowItem">
                 <label for="tunnelBackupQuantity" accesskey="b">
diff --git a/apps/i2ptunnel/jsp/editServer.jsp b/apps/i2ptunnel/jsp/editServer.jsp
index d56414d790..dce9e1f798 100644
--- a/apps/i2ptunnel/jsp/editServer.jsp
+++ b/apps/i2ptunnel/jsp/editServer.jsp
@@ -264,17 +264,8 @@ input.default { width: 1px; height: 1px; visibility: hidden; }
                     <%=intl._("Count")%>(<span class="accessKey">C</span>):
                 </label>
                 <select id="tunnelQuantity" name="tunnelQuantity" title="Number of Tunnels in Group" class="selectbox">
-                    <% int tunnelQuantity = editBean.getTunnelQuantity(curTunnel, 2);
-                  %><option value="1"<%=(tunnelQuantity == 1 ? " selected=\"selected\"" : "") %>><%=intl._("1 inbound, 1 outbound tunnel  (low bandwidth usage, less reliability)")%></option>
-                    <option value="2"<%=(tunnelQuantity == 2 ? " selected=\"selected\"" : "") %>><%=intl._("2 inbound, 2 outbound tunnels (standard bandwidth usage, standard reliability)")%></option>
-                    <option value="3"<%=(tunnelQuantity == 3 ? " selected=\"selected\"" : "") %>><%=intl._("3 inbound, 3 outbound tunnels (higher bandwidth usage, higher reliability)")%></option>
-                    <option value="4"<%=(tunnelQuantity == 4 ? " selected=\"selected\"" : "") %>><%=intl._("4 in, 4 out (high traffic server)")%></option>
-                    <option value="5"<%=(tunnelQuantity == 5 ? " selected=\"selected\"" : "") %>><%=intl._("5 in, 5 out (high traffic server)")%></option>
-                    <option value="6"<%=(tunnelQuantity == 6 ? " selected=\"selected\"" : "") %>><%=intl._("6 in, 6 out (high traffic server)")%></option>
-                <% if (tunnelQuantity > 6) {
-                %>    <option value="<%=tunnelQuantity%>" selected="selected"><%=tunnelQuantity%>&nbsp;<%=intl._("tunnels")%></option>
-                <% }
-              %></select>                
+                    <%=editBean.getQuantityOptions(curTunnel)%>
+                </select>                
             </div>
             <div id="backupField" class="rowItem">
                 <label for="tunnelBackupQuantity" accesskey="b">
-- 
GitLab