2012-01-25 02:38:49 +00:00
package net.i2p.router.web ;
import java.util.Collection ;
import java.util.Comparator ;
import java.util.List ;
import java.util.Set ;
import java.util.TreeSet ;
import net.i2p.router.RouterContext ;
import net.i2p.util.PortMapper ;
/ * *
* For / home and / confighome
*
* @since 0 . 9
* /
public class HomeHelper extends HelperBase {
2012-01-25 16:22:04 +00:00
private static final char S = ',' ;
2012-01-25 02:38:49 +00:00
private static final String I = " /themes/console/images/ " ;
static final String PROP_SERVICES = " routerconsole.services " ;
static final String PROP_FAVORITES = " routerconsole.favorites " ;
static final String PROP_OLDHOME = " routerconsole.oldHomePage " ;
2012-04-20 13:25:29 +00:00
private static final String PROP_SEARCH = " routerconsole.showSearch " ;
2012-01-25 02:38:49 +00:00
static final String DEFAULT_SERVICES =
2012-07-14 21:56:38 +00:00
_x ( " Addressbook " ) + S + _x ( " Manage your I2P hosts file here (I2P domain name resolution) " ) + S + " /dns " + S + I + " book_addresses.png " + S +
2012-07-25 23:38:25 +00:00
_x ( " Configure Bandwidth " ) + S + _x ( " I2P Bandwidth Configuration " ) + S + " /config " + S + I + " action_log.png " + S +
2012-03-12 14:30:38 +00:00
_x ( " Configure Language " ) + S + _x ( " Console Language Selection " ) + S + " /configui " + S + I + " wrench_orange.png " + S +
2012-07-25 23:38:25 +00:00
_x ( " Customize Home Page " ) + S + _x ( " I2P Home Page Configuration " ) + S + " /confighome " + S + I + " home_page.png " + S +
2012-07-21 20:57:46 +00:00
_x ( " Email " ) + S + _x ( " Anonymous webmail client " ) + S + " /susimail/susimail " + S + I + " email.png " + S +
2012-07-24 05:17:13 +00:00
_x ( " Help " ) + S + _x ( " I2P Router Help " ) + S + " /help " + S + I + " support.png " + S +
_x ( " Router Console " ) + S + _x ( " I2P Router Console " ) + S + " /console " + S + I + " toolbox.png " + S +
_x ( " Torrents " ) + S + _x ( " Built-in anonymous BitTorrent Client " ) + S + " /i2psnark/ " + S + I + " magnet.png " + S +
_x ( " Website " ) + S + _x ( " Local web server " ) + S + " http://127.0.0.1:7658/ " + S + I + " server_32x32.png " + S +
2012-01-25 02:38:49 +00:00
" " ;
static final String DEFAULT_FAVORITES =
_x ( " Bug Reports " ) + S + _x ( " Bug tracker " ) + S + " http://trac.i2p2.i2p/report/1 " + S + I + " bug.png " + S +
2012-10-10 15:06:51 +00:00
//"colombo-bt.i2p" + S + _x("The Italian Bittorrent Resource") + S + "http://colombo-bt.i2p/" + S + I + "colomboicon.png" + S +
2012-07-19 00:08:29 +00:00
_x ( " Dev Forum " ) + S + _x ( " Development forum " ) + S + " http://zzz.i2p/ " + S + I + " eepsite.png " + S +
_x ( " diftracker " ) + S + _x ( " Bittorrent tracker " ) + S + " http://diftracker.i2p/ " + S + I + " eepsite.png " + S +
" echelon.i2p " + S + _x ( " I2P Applications " ) + S + " http://echelon.i2p/ " + S + I + " eepsite.png " + S +
2012-07-24 05:17:13 +00:00
_x ( " FAQ " ) + S + _x ( " Frequently Asked Questions " ) + S + " http://www.i2p2.i2p/faq " + S + I + " question.png " + S +
2012-07-19 00:08:29 +00:00
_x ( " Forum " ) + S + _x ( " Community forum " ) + S + " http://forum.i2p/ " + S + I + " eepsite.png " + S +
2012-07-18 02:50:13 +00:00
_x ( " Anonymous Git Hosting " ) + S + _x ( " A public anonymous Git hosting site - supports pulling via Git and HTTP and pushing via SSH " ) + S + " http://git.repo.i2p/ " + S + I + " git-logo.png " + S +
2012-07-17 21:27:22 +00:00
" Ident " + _x ( " Microblog " ) + S + _x ( " Your premier microblogging service on I2P " ) + S + " http://id3nt.i2p/ " + S + I + " ident_icon_blue.png " + S +
2012-07-24 05:17:13 +00:00
_x ( " Javadocs " ) + S + _x ( " Technical documentation " ) + S + " http://i2p-javadocs.i2p/ " + S + I + " education.png " + S +
//_x("Key Server") + S + _x("OpenPGP Keyserver") + S + "http://keys.i2p/" + S + I + "education.png" + S +
2013-09-14 15:28:36 +00:00
" killyourtv.i2p " + S + _x ( " Debian and Tahoe-LAFS repositories " ) + S + " http://killyourtv.i2p/ " + S + I + " eepsite.png " + S +
" open4you.i2p " + S + S + " http://open4you.i2p/ " + S + I + " eepsite.png " + S +
2012-07-19 00:08:29 +00:00
_x ( " Pastebin " ) + S + _x ( " I2P Pastebin " ) + S + " http://pastethis.i2p/ " + S + I + " eepsite.png " + S +
" Planet I2P " + S + _x ( " I2P News " ) + S + " http://planet.i2p/ " + S + I + " eepsite.png " + S +
2012-03-13 21:48:02 +00:00
_x ( " Plugins " ) + S + _x ( " Add-on directory " ) + S + " http://plugins.i2p/ " + S + I + " plugin.png " + S +
2012-07-19 00:08:29 +00:00
_x ( " Postman's Tracker " ) + S + _x ( " Bittorrent tracker " ) + S + " http://tracker2.postman.i2p/ " + S + I + " eepsite.png " + S +
2012-07-24 05:17:13 +00:00
_x ( " Project Website " ) + S + _x ( " I2P home page " ) + S + " http://www.i2p2.i2p/ " + S + I + " info_rhombus.png " + S +
2012-08-25 19:05:45 +00:00
" stats.i2p " + S + _x ( " I2P Network Statistics " ) + S + " http://stats.i2p/cgi-bin/dashboard.cgi " + S + I + " eepsite.png " + S +
2012-07-24 05:17:13 +00:00
_x ( " Technical Docs " ) + S + _x ( " Technical documentation " ) + S + " http://www.i2p2.i2p/how " + S + I + " education.png " + S +
2012-07-19 00:08:29 +00:00
_x ( " Trac Wiki " ) + S + S + " http://trac.i2p2.i2p/ " + S + I + " eepsite.png " + S +
_x ( " Ugha's Wiki " ) + S + S + " http://ugha.i2p/ " + S + I + " eepsite.png " + S +
_x ( " Sponge's main site " ) + S + _x ( " Seedless and the Robert BitTorrent applications " ) + S + " http://sponge.i2p/ " + S + I + " eepsite.png " + S +
2012-01-25 02:38:49 +00:00
" " ;
2012-03-12 14:30:38 +00:00
public boolean shouldShowWelcome ( ) {
return _context . getProperty ( Messages . PROP_LANG ) = = null ;
}
2012-04-20 13:25:29 +00:00
public boolean shouldShowSearch ( ) {
return _context . getBooleanProperty ( PROP_SEARCH ) ;
}
2012-01-25 02:38:49 +00:00
public String getServices ( ) {
List < App > plugins = NavHelper . getClientApps ( _context ) ;
return homeTable ( PROP_SERVICES , DEFAULT_SERVICES , plugins ) ;
}
public String getFavorites ( ) {
return homeTable ( PROP_FAVORITES , DEFAULT_FAVORITES , null ) ;
}
public String getConfigServices ( ) {
return configTable ( PROP_SERVICES , DEFAULT_SERVICES ) ;
}
public String getConfigFavorites ( ) {
return configTable ( PROP_FAVORITES , DEFAULT_FAVORITES ) ;
}
2012-01-25 14:34:54 +00:00
public String getConfigSearch ( ) {
return configTable ( SearchHelper . PROP_ENGINES , SearchHelper . ENGINES_DEFAULT ) ;
}
2012-01-25 02:38:49 +00:00
public String getConfigHome ( ) {
boolean oldHome = _context . getBooleanProperty ( PROP_OLDHOME ) ;
return oldHome ? " checked= \" true \" " : " " ;
}
public String getProxyStatus ( ) {
int port = _context . portMapper ( ) . getPort ( PortMapper . SVC_HTTP_PROXY ) ;
if ( port < = 0 )
return _ ( " The HTTP proxy is not up " ) ;
return " <img src= \" http://console.i2p/onepixel.png? " + _context . random ( ) . nextInt ( ) + " \" " +
" alt= \" " + _ ( " Your browser is not properly configured to use the HTTP proxy at {0} " ,
_context . getProperty ( ConfigUpdateHandler . PROP_PROXY_HOST , ConfigUpdateHandler . DEFAULT_PROXY_HOST ) + ':' + port ) +
" \" > " ;
}
private String homeTable ( String prop , String dflt , Collection < App > toAdd ) {
String config = _context . getProperty ( prop , dflt ) ;
Collection < App > apps = buildApps ( _context , config ) ;
if ( toAdd ! = null )
apps . addAll ( toAdd ) ;
return renderApps ( apps ) ;
}
private String configTable ( String prop , String dflt ) {
String config = _context . getProperty ( prop , dflt ) ;
2012-01-25 14:34:54 +00:00
Collection < App > apps ;
if ( prop . equals ( SearchHelper . PROP_ENGINES ) )
apps = buildSearchApps ( config ) ;
else
apps = buildApps ( _context , config ) ;
2012-01-25 02:38:49 +00:00
return renderConfig ( apps ) ;
}
static Collection < App > buildApps ( RouterContext ctx , String config ) {
String [ ] args = config . split ( " " + S ) ;
Set < App > apps = new TreeSet ( new AppComparator ( ) ) ;
for ( int i = 0 ; i < args . length - 3 ; i + = 4 ) {
String name = Messages . getString ( args [ i ] , ctx ) ;
String desc = Messages . getString ( args [ i + 1 ] , ctx ) ;
String url = args [ i + 2 ] ;
String icon = args [ i + 3 ] ;
apps . add ( new App ( name , desc , url , icon ) ) ;
}
return apps ;
}
2012-01-25 14:34:54 +00:00
static Collection < App > buildSearchApps ( String config ) {
String [ ] args = config . split ( " " + S ) ;
Set < App > apps = new TreeSet ( new AppComparator ( ) ) ;
for ( int i = 0 ; i < args . length - 1 ; i + = 2 ) {
String name = args [ i ] ;
String url = args [ i + 1 ] ;
apps . add ( new App ( name , null , url , null ) ) ;
}
return apps ;
}
static void saveApps ( RouterContext ctx , String prop , Collection < App > apps , boolean full ) {
2012-01-25 02:38:49 +00:00
StringBuilder buf = new StringBuilder ( 1024 ) ;
for ( App app : apps ) {
2012-01-25 14:34:54 +00:00
buf . append ( app . name ) . append ( S ) ;
if ( full )
buf . append ( app . desc ) . append ( S ) ;
buf . append ( app . url ) . append ( S ) ;
if ( full )
buf . append ( app . icon ) . append ( S ) ;
2012-01-25 02:38:49 +00:00
}
ctx . router ( ) . saveConfig ( prop , buf . toString ( ) ) ;
}
private static String renderApps ( Collection < App > apps ) {
StringBuilder buf = new StringBuilder ( 1024 ) ;
buf . append ( " <div class= \" appgroup \" > " ) ;
for ( App app : apps ) {
buf . append ( " <div class= \" app \" > " +
2012-03-16 12:08:43 +00:00
" <div class= \" appimg \" > " +
2012-01-25 16:22:04 +00:00
" <a href= \" " ) . append ( app . url ) . append ( " \" > " +
2012-01-25 02:38:49 +00:00
" <img class= \" " ) ;
// toopie is 54x68, not 16x16, needs special alignment and sizing
if ( app . icon . endsWith ( " /itoopie_sm.png " ) )
buf . append ( " app2p " ) ;
else
buf . append ( " app " ) ;
2012-03-16 12:08:43 +00:00
buf . append ( " \" alt= \" \" title= \" " ) . append ( app . desc ) . append ( " \" src= \" " ) . append ( app . icon ) . append ( " \" ></a> \ n " +
" </div> " +
2012-01-25 02:38:49 +00:00
" <table class= \" app \" ><tr class= \" app \" ><td class= \" app \" > " +
" <div class= \" applabel \" > " +
" <a href= \" " ) . append ( app . url ) . append ( " \" title= \" " ) . append ( app . desc ) . append ( " \" > " ) . append ( app . name ) . append ( " </a> " +
" </div> " +
" </td></tr></table> " +
" </div> \ n " ) ;
}
buf . append ( " </div> \ n " ) ;
return buf . toString ( ) ;
}
private String renderConfig ( Collection < App > apps ) {
StringBuilder buf = new StringBuilder ( 1024 ) ;
buf . append ( " <table><tr><th> " )
. append ( _ ( " Remove " ) )
2012-01-25 14:34:54 +00:00
. append ( " </th><th colspan= \" 2 \" > " )
2012-01-25 02:38:49 +00:00
. append ( _ ( " Name " ) )
. append ( " </th><th> " )
. append ( _ ( " URL " ) )
. append ( " </th></tr> \ n " ) ;
for ( App app : apps ) {
buf . append ( " <tr><td align= \" center \" ><input type= \" checkbox \" class= \" optbox \" name= \" delete_ " )
. append ( app . name )
2012-01-25 14:34:54 +00:00
. append ( " \" ></td><td align= \" center \" > " ) ;
if ( app . icon ! = null ) {
2012-03-03 22:55:24 +00:00
buf . append ( " <img height= \" 16 \" alt= \" \" src= \" " ) . append ( app . icon ) . append ( " \" > " ) ;
2012-01-25 14:34:54 +00:00
}
buf . append ( " </td><td align= \" left \" > " )
2012-01-25 02:38:49 +00:00
. append ( app . name )
. append ( " </td><td align= \" left \" ><a href= \" " )
2012-03-03 22:55:24 +00:00
. append ( app . url . replace ( " & " , " & " ) )
2012-01-25 02:38:49 +00:00
. append ( " \" > " )
2012-03-03 22:55:24 +00:00
. append ( app . url . replace ( " & " , " & " ) )
2012-01-25 02:38:49 +00:00
. append ( " </a></td></tr> \ n " ) ;
}
2012-01-25 14:34:54 +00:00
buf . append ( " <tr><td colspan= \" 2 \" align= \" center \" ><b> " )
2012-01-25 02:38:49 +00:00
. append ( _ ( " Add " ) ) . append ( " :</b> " +
" </td><td align= \" left \" ><input type= \" text \" name= \" name \" ></td> " +
" <td align= \" left \" ><input type= \" text \" size= \" 40 \" name= \" url \" ></td></tr> " ) ;
buf . append ( " </table> \ n " ) ;
return buf . toString ( ) ;
}
static class App {
public final String name ;
public final String desc ;
public final String url ;
public final String icon ;
public App ( String name , String desc , String url , String icon ) {
this . name = name ;
this . desc = desc ;
this . url = url ;
this . icon = icon ;
}
}
/** ignore case, current locale */
private static class AppComparator implements Comparator < App > {
public int compare ( App l , App r ) {
return l . name . toLowerCase ( ) . compareTo ( r . name . toLowerCase ( ) ) ;
}
}
}