| 1 | package be.nikiroo.jvcard.tui; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.lang.reflect.Field; |
| 6 | import java.net.Socket; |
| 7 | import java.nio.charset.Charset; |
| 8 | import java.util.LinkedList; |
| 9 | import java.util.List; |
| 10 | |
| 11 | import be.nikiroo.jvcard.Card; |
| 12 | import be.nikiroo.jvcard.i18n.Trans; |
| 13 | import be.nikiroo.jvcard.i18n.Trans.StringId; |
| 14 | import be.nikiroo.jvcard.remote.Command.Verb; |
| 15 | import be.nikiroo.jvcard.remote.SimpleSocket; |
| 16 | import be.nikiroo.jvcard.resources.Bundles; |
| 17 | import be.nikiroo.jvcard.tui.panes.FileList; |
| 18 | |
| 19 | import com.googlecode.lanterna.gui2.Window; |
| 20 | import com.googlecode.lanterna.input.KeyStroke; |
| 21 | |
| 22 | /** |
| 23 | * This class contains the runnable Main method. It will parse the user supplied |
| 24 | * parameters and take action based upon those. Most of the time, it will start |
| 25 | * a MainWindow. |
| 26 | * |
| 27 | * @author niki |
| 28 | * |
| 29 | */ |
| 30 | public class Main { |
| 31 | // TODO: move Main to be.nikiroo.jvcard, use introspection to load the other |
| 32 | // main classes, allow the 3 programs to run from this new Main |
| 33 | // also requires StringUtils/... in a new package |
| 34 | private int TODO; |
| 35 | |
| 36 | public static final String APPLICATION_TITLE = "jVcard"; |
| 37 | public static final String APPLICATION_VERSION = "1.0-beta2-dev"; |
| 38 | |
| 39 | static private Trans transService; |
| 40 | |
| 41 | /** |
| 42 | * Translate the given {@link StringId}. |
| 43 | * |
| 44 | * @param id |
| 45 | * the ID to translate |
| 46 | * |
| 47 | * @return the translation |
| 48 | */ |
| 49 | static public String trans(StringId id) { |
| 50 | if (transService == null) |
| 51 | return ""; |
| 52 | |
| 53 | return transService.trans(id); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Translate the given {@link KeyStroke}. |
| 58 | * |
| 59 | * @param key |
| 60 | * the key to translate |
| 61 | * |
| 62 | * @return the translation |
| 63 | */ |
| 64 | static public String trans(KeyStroke key) { |
| 65 | if (transService == null) |
| 66 | return ""; |
| 67 | |
| 68 | return transService.trans(key); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Start the application. |
| 73 | * |
| 74 | * @param args |
| 75 | * the parameters (see <tt>--help</tt> to know which are |
| 76 | * supported) |
| 77 | */ |
| 78 | public static void main(String[] args) { |
| 79 | Boolean textMode = null; |
| 80 | boolean noMoreParams = false; |
| 81 | boolean filesTried = false; |
| 82 | |
| 83 | // get the "system default" language to help translate the --help |
| 84 | // message if needed |
| 85 | String language = null; |
| 86 | transService = new Trans(language); |
| 87 | |
| 88 | List<String> files = new LinkedList<String>(); |
| 89 | for (int index = 0; index < args.length; index++) { |
| 90 | String arg = args[index]; |
| 91 | if (!noMoreParams && arg.equals("--")) { |
| 92 | noMoreParams = true; |
| 93 | } else if (!noMoreParams && arg.equals("--help")) { |
| 94 | System.out |
| 95 | .println("TODO: implement some help text.\n" |
| 96 | + "Usable switches:\n" |
| 97 | + "\t--: stop looking for switches\n" |
| 98 | + "\t--help: this here thingy\n" |
| 99 | + "\t--lang LANGUAGE: choose the language, for instance en_GB\n" |
| 100 | + "\t--tui: force pure text mode even if swing treminal is available\n" |
| 101 | + "\t--gui: force swing terminal mode\n" |
| 102 | + "\t--noutf: force non-utf8 mode if you need it\n" |
| 103 | + "\t--config DIRECTORY: force the given directory as a CONFIG_DIR\n" |
| 104 | + "everyhing else is either a file to open or a directory to open\n" |
| 105 | + "(we will only open 1st level files in given directories)\n" |
| 106 | + "('jvcard://hostname:8888/file' links -- or without 'file' -- are also ok)\n"); |
| 107 | return; |
| 108 | } else if (!noMoreParams && arg.equals("--tui")) { |
| 109 | textMode = true; |
| 110 | } else if (!noMoreParams && arg.equals("--gui")) { |
| 111 | textMode = false; |
| 112 | } else if (!noMoreParams && arg.equals("--noutf")) { |
| 113 | UiColors.getInstance().setUnicode(false); |
| 114 | } else if (!noMoreParams && arg.equals("--lang")) { |
| 115 | index++; |
| 116 | if (index < args.length) |
| 117 | language = args[index]; |
| 118 | transService = new Trans(language); |
| 119 | } else if (!noMoreParams && arg.equals("--config")) { |
| 120 | index++; |
| 121 | if (index < args.length) { |
| 122 | Bundles.setDirectory(args[index]); |
| 123 | transService = new Trans(language); |
| 124 | } |
| 125 | } else { |
| 126 | filesTried = true; |
| 127 | files.addAll(open(arg)); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (UiColors.getInstance().isUnicode()) { |
| 132 | utf8(); |
| 133 | } |
| 134 | |
| 135 | if (files.size() == 0) { |
| 136 | if (filesTried) { |
| 137 | System.exit(1); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | files.addAll(open(".")); |
| 142 | } |
| 143 | |
| 144 | // TODO error case when no file |
| 145 | |
| 146 | Window win = new MainWindow(new FileList(files)); |
| 147 | |
| 148 | try { |
| 149 | TuiLauncher.start(textMode, win); |
| 150 | } catch (IOException ioe) { |
| 151 | ioe.printStackTrace(); |
| 152 | System.exit(2); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Open the given path and add all its files if it is a directory or just |
| 158 | * this one if not to the returned list. |
| 159 | * |
| 160 | * @param path |
| 161 | * the path to open |
| 162 | * |
| 163 | * @return the list of opened files |
| 164 | */ |
| 165 | static private List<String> open(String path) { |
| 166 | List<String> files = new LinkedList<String>(); |
| 167 | |
| 168 | if (path != null && path.startsWith("jvcard://")) { |
| 169 | if (path.endsWith("/")) { |
| 170 | files.addAll(list(path)); |
| 171 | } else { |
| 172 | files.add(path); |
| 173 | } |
| 174 | } else { |
| 175 | File file = new File(path); |
| 176 | if (file.exists()) { |
| 177 | if (file.isDirectory()) { |
| 178 | for (File subfile : file.listFiles()) { |
| 179 | if (!subfile.isDirectory()) |
| 180 | files.add(subfile.getAbsolutePath()); |
| 181 | } |
| 182 | } else { |
| 183 | files.add(file.getAbsolutePath()); |
| 184 | } |
| 185 | } else { |
| 186 | System.err.println("File or directory not found: \"" + path |
| 187 | + "\""); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return files; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * List all the available {@link Card}s on the given network location (which |
| 196 | * is expected to be a jVCard remote server, obviously). |
| 197 | * |
| 198 | * @param path |
| 199 | * the jVCard remote server path (e.g.: |
| 200 | * <tt>jvcard://localhost:4444/</tt>) |
| 201 | * |
| 202 | * @return the list of {@link Card}s |
| 203 | */ |
| 204 | static private List<String> list(String path) { |
| 205 | List<String> files = new LinkedList<String>(); |
| 206 | |
| 207 | try { |
| 208 | String host = path.split("\\:")[1].substring(2); |
| 209 | int port = Integer.parseInt(path.split("\\:")[2].replaceAll("/$", |
| 210 | "")); |
| 211 | SimpleSocket s = new SimpleSocket(new Socket(host, port), |
| 212 | "sync client"); |
| 213 | s.open(true); |
| 214 | |
| 215 | s.sendCommand(Verb.LIST); |
| 216 | for (String p : s.receiveBlock()) { |
| 217 | files.add(path |
| 218 | + p.substring(StringUtils.fromTime(0).length() + 1)); |
| 219 | } |
| 220 | s.close(); |
| 221 | } catch (Exception e) { |
| 222 | e.printStackTrace(); |
| 223 | } |
| 224 | |
| 225 | return files; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Really, really ask for UTF-8 encoding. |
| 230 | */ |
| 231 | static private void utf8() { |
| 232 | try { |
| 233 | System.setProperty("file.encoding", "UTF-8"); |
| 234 | Field charset = Charset.class.getDeclaredField("defaultCharset"); |
| 235 | charset.setAccessible(true); |
| 236 | charset.set(null, null); |
| 237 | } catch (SecurityException e) { |
| 238 | } catch (NoSuchFieldException e) { |
| 239 | } catch (IllegalArgumentException e) { |
| 240 | } catch (IllegalAccessException e) { |
| 241 | } |
| 242 | } |
| 243 | } |