X-Git-Url: http://git.nikiroo.be/?p=jvcard.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Ftui%2FMain.java;h=bf6ab4ba2c589a026d0285cde03fe8e397dc9f2f;hp=8e34e00b1e51a430ae93e8a29f4da07a232be61c;hb=0b6140e4a200c4952c9dc003d8389f375191564e;hpb=296a0b75515b3a7424b98292c87cbbf2272b73f9 diff --git a/src/be/nikiroo/jvcard/tui/Main.java b/src/be/nikiroo/jvcard/tui/Main.java index 8e34e00..bf6ab4b 100644 --- a/src/be/nikiroo/jvcard/tui/Main.java +++ b/src/be/nikiroo/jvcard/tui/Main.java @@ -2,22 +2,22 @@ package be.nikiroo.jvcard.tui; import java.io.File; import java.io.IOException; +import java.lang.reflect.Field; +import java.net.Socket; +import java.nio.charset.Charset; import java.util.LinkedList; import java.util.List; +import be.nikiroo.jvcard.Card; +import be.nikiroo.jvcard.i18n.Trans; +import be.nikiroo.jvcard.i18n.Trans.StringId; +import be.nikiroo.jvcard.remote.Command.Verb; +import be.nikiroo.jvcard.remote.SimpleSocket; +import be.nikiroo.jvcard.resources.Bundles; import be.nikiroo.jvcard.tui.panes.FileList; -import com.googlecode.lanterna.TextColor; -import com.googlecode.lanterna.gui2.BasicWindow; -import com.googlecode.lanterna.gui2.DefaultWindowManager; -import com.googlecode.lanterna.gui2.EmptySpace; -import com.googlecode.lanterna.gui2.MultiWindowTextGUI; import com.googlecode.lanterna.gui2.Window; -import com.googlecode.lanterna.gui2.table.Table; -import com.googlecode.lanterna.screen.Screen; -import com.googlecode.lanterna.screen.TerminalScreen; -import com.googlecode.lanterna.terminal.DefaultTerminalFactory; -import com.googlecode.lanterna.terminal.Terminal; +import com.googlecode.lanterna.input.KeyStroke; /** * This class contains the runnable Main method. It will parse the user supplied @@ -28,16 +28,66 @@ import com.googlecode.lanterna.terminal.Terminal; * */ public class Main { + // TODO: move Main to be.nikiroo.jvcard, use introspection to load the other + // main classes, allow the 3 programs to run from this new Main + // also requires StringUtils/... in a new package + private int TODO; + public static final String APPLICATION_TITLE = "jVcard"; - public static final String APPLICATION_VERSION = "1.0-beta1-dev"; + public static final String APPLICATION_VERSION = "1.0-beta2-dev"; + + static private Trans transService; + + /** + * Translate the given {@link StringId}. + * + * @param id + * the ID to translate + * + * @return the translation + */ + static public String trans(StringId id) { + if (transService == null) + return ""; + + return transService.trans(id); + } + + /** + * Translate the given {@link KeyStroke}. + * + * @param key + * the key to translate + * + * @return the translation + */ + static public String trans(KeyStroke key) { + if (transService == null) + return ""; + + return transService.trans(key); + } + /** + * Start the application. + * + * @param args + * the parameters (see --help to know which are + * supported) + */ public static void main(String[] args) { Boolean textMode = null; boolean noMoreParams = false; boolean filesTried = false; - List files = new LinkedList(); - for (String arg : args) { + // get the "system default" language to help translate the --help + // message if needed + String language = null; + transService = new Trans(language); + + List files = new LinkedList(); + for (int index = 0; index < args.length; index++) { + String arg = args[index]; if (!noMoreParams && arg.equals("--")) { noMoreParams = true; } else if (!noMoreParams && arg.equals("--help")) { @@ -46,12 +96,14 @@ public class Main { + "Usable switches:\n" + "\t--: stop looking for switches\n" + "\t--help: this here thingy\n" + + "\t--lang LANGUAGE: choose the language, for instance en_GB\n" + "\t--tui: force pure text mode even if swing treminal is available\n" + "\t--gui: force swing terminal mode\n" + "\t--noutf: force non-utf8 mode if you need it\n" - + "\t--noutfa: force non-utf8 and no accents mode if you need it\n" + + "\t--config DIRECTORY: force the given directory as a CONFIG_DIR\n" + "everyhing else is either a file to open or a directory to open\n" - + "(we will only open 1st level files in given directories)"); + + "(we will only open 1st level files in given directories)\n" + + "('jvcard://hostname:8888/file' links -- or without 'file' -- are also ok)\n"); return; } else if (!noMoreParams && arg.equals("--tui")) { textMode = true; @@ -59,12 +111,27 @@ public class Main { textMode = false; } else if (!noMoreParams && arg.equals("--noutf")) { UiColors.getInstance().setUnicode(false); + } else if (!noMoreParams && arg.equals("--lang")) { + index++; + if (index < args.length) + language = args[index]; + transService = new Trans(language); + } else if (!noMoreParams && arg.equals("--config")) { + index++; + if (index < args.length) { + Bundles.setDirectory(args[index]); + transService = new Trans(language); + } } else { filesTried = true; files.addAll(open(arg)); } } + if (UiColors.getInstance().isUnicode()) { + utf8(); + } + if (files.size() == 0) { if (filesTried) { System.exit(1); @@ -74,6 +141,8 @@ public class Main { files.addAll(open(".")); } + // TODO error case when no file + Window win = new MainWindow(new FileList(files)); try { @@ -82,23 +151,6 @@ public class Main { ioe.printStackTrace(); System.exit(2); } - - /* - * String file = args.length > 0 ? args[0] : null; String file2 = - * args.length > 1 ? args[1] : null; - * - * if (file == null) file = - * "/home/niki/workspace/rcard/utils/CVcard/test.vcf"; if (file2 == - * null) file2 = "/home/niki/workspace/rcard/utils/CVcard/test.abook"; - * - * Card card = new Card(new File(file), Format.VCard21); - * System.out.println(card.toString()); - * - * System.out.println("\n -- PINE -- \n"); - * - * card = new Card(new File(file2), Format.Abook); - * System.out.println(card.toString(Format.Abook)); - */ } /** @@ -110,55 +162,82 @@ public class Main { * * @return the list of opened files */ - static private List open(String path) { - List files = new LinkedList(); - - File file = new File(path); - if (file.exists()) { - if (file.isDirectory()) { - for (File subfile : file.listFiles()) { - if (!subfile.isDirectory()) - files.add(subfile); - } + static private List open(String path) { + List files = new LinkedList(); + + if (path != null && path.startsWith("jvcard://")) { + if (path.endsWith("/")) { + files.addAll(list(path)); } else { - files.add(file); + files.add(path); } } else { - System.err.println("File or directory not found: \"" + path + "\""); + File file = new File(path); + if (file.exists()) { + if (file.isDirectory()) { + for (File subfile : file.listFiles()) { + if (!subfile.isDirectory()) + files.add(subfile.getAbsolutePath()); + } + } else { + files.add(file.getAbsolutePath()); + } + } else { + System.err.println("File or directory not found: \"" + path + + "\""); + } } return files; } - static private void fullTestTable() throws IOException { - final Table table = new Table("Column 1", "Column 2", - "Column 3"); - table.getTableModel().addRow("1", "2", "3"); - table.setSelectAction(new Runnable() { - @Override - public void run() { - List data = table.getTableModel().getRow( - table.getSelectedRow()); - for (int i = 0; i < data.size(); i++) { - System.out.println(data.get(i)); - } - } - }); - - Window win = new BasicWindow(); - win.setComponent(table); - - DefaultTerminalFactory factory = new DefaultTerminalFactory(); - Terminal terminal = factory.createTerminal(); + /** + * List all the available {@link Card}s on the given network location (which + * is expected to be a jVCard remote server, obviously). + * + * @param path + * the jVCard remote server path (e.g.: + * jvcard://localhost:4444/) + * + * @return the list of {@link Card}s + */ + static private List list(String path) { + List files = new LinkedList(); - Screen screen = new TerminalScreen(terminal); - screen.startScreen(); + try { + String host = path.split("\\:")[1].substring(2); + int port = Integer.parseInt(path.split("\\:")[2].replaceAll("/$", + "")); + SimpleSocket s = new SimpleSocket(new Socket(host, port), + "sync client"); + s.open(true); + + s.sendCommand(Verb.LIST); + for (String p : s.receiveBlock()) { + files.add(path + + p.substring(StringUtils.fromTime(0).length() + 1)); + } + s.close(); + } catch (Exception e) { + e.printStackTrace(); + } - // Create gui and start gui - MultiWindowTextGUI gui = new MultiWindowTextGUI(screen, - new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.BLUE)); - gui.addWindowAndWait(win); + return files; + } - screen.stopScreen(); + /** + * Really, really ask for UTF-8 encoding. + */ + static private void utf8() { + try { + System.setProperty("file.encoding", "UTF-8"); + Field charset = Charset.class.getDeclaredField("defaultCharset"); + charset.setAccessible(true); + charset.set(null, null); + } catch (SecurityException e) { + } catch (NoSuchFieldException e) { + } catch (IllegalArgumentException e) { + } catch (IllegalAccessException e) { + } } }