Translation: update system to support arguments, add some translations
[jvcard.git] / src / be / nikiroo / jvcard / launcher / Main.java
index a9587d275151c7817250714d907f8fb5379cd1d0..1a77291c28903120079f430661ca181f7ac4a6dd 100644 (file)
@@ -3,16 +3,15 @@ package be.nikiroo.jvcard.launcher;
 import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
 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.launcher.CardResult.MergeCallback;
 import be.nikiroo.jvcard.parsers.Format;
-import be.nikiroo.jvcard.remote.Command.Verb;
+import be.nikiroo.jvcard.remote.Command;
 import be.nikiroo.jvcard.remote.SimpleSocket;
 import be.nikiroo.jvcard.resources.Bundles;
 import be.nikiroo.jvcard.resources.StringUtils;
@@ -29,7 +28,7 @@ import be.nikiroo.jvcard.resources.Trans.StringId;
  */
 public class Main {
        static public final String APPLICATION_TITLE = "jVcard";
-       static public final String APPLICATION_VERSION = "1.0-beta2-dev";
+       static public final String APPLICATION_VERSION = "1.0-beta3-dev";
 
        static private final int ERR_NO_FILE = 1;
        static private final int ERR_SYNTAX = 2;
@@ -37,15 +36,18 @@ public class Main {
        static private Trans transService;
 
        /**
-        * Translate the given {@link StringId}.
+        * Translate the given {@link StringId} into user text.
         * 
-        * @param id
+        * @param stringId
         *            the ID to translate
+        * @param values
+        *            the values to insert instead of the place holders in the
+        *            translation
         * 
-        * @return the translation
+        * @return the translated text with the given value where required
         */
-       static public String trans(StringId id) {
-               return transService.trans(id);
+       static public String trans(StringId id, String... values) {
+               return transService.trans(id, (String[]) values);
        }
 
        /**
@@ -169,6 +171,12 @@ public class Main {
                        }
                }
 
+               // Force headless mode if we run in forced-text mode
+               if (textMode != null && textMode) {
+                       // same as -Djava.awt.headless=true
+                       System.setProperty("java.awt.headless", "true");
+               }
+
                if (unicode) {
                        utf8();
                }
@@ -205,7 +213,7 @@ public class Main {
 
                if (port != null) {
                        try {
-                               runServer(port);
+                               Optional.runServer(port);
                        } catch (Exception e) {
                                if (e instanceof IOException) {
                                        System.err
@@ -225,7 +233,7 @@ public class Main {
                        }
                } else {
                        try {
-                               startTui(textMode, files);
+                               Optional.startTui(textMode, files);
                        } catch (Exception e) {
                                if (e instanceof IOException) {
                                        System.err
@@ -245,13 +253,18 @@ public class Main {
         * @param input
         *            a filename or a remote jvcard url with named resource (e.g.:
         *            <tt>jvcard://localhost:4444/coworkers.vcf</tt>)
+        * @param callback
+        *            the {@link MergeCallback} to call in case of conflict, or NULL
+        *            to disallow conflict management (the {@link Card} will not be
+        *            allowed to synchronise in case of conflicts)
         * 
         * @return the {@link Card}
         * 
         * @throws IOException
         *             in case of IO error or remoting not available
         */
-       static public Card getCard(String input) throws IOException {
+       static public CardResult getCard(String input, MergeCallback callback)
+                       throws IOException {
                boolean remote = false;
                Format format = Format.Abook;
                String ext = input;
@@ -267,12 +280,13 @@ public class Main {
                        remote = true;
                }
 
-               Card card = null;
+               CardResult card = null;
                try {
                        if (remote) {
-                               card = syncCard(input);
+                               card = Optional.syncCard(input, callback);
                        } else {
-                               card = new Card(new File(input), format);
+                               card = new CardResult(new Card(new File(input), format), false,
+                                               false, false);
                        }
                } catch (IOException ioe) {
                        throw ioe;
@@ -283,127 +297,6 @@ public class Main {
                return card;
        }
 
-       /**
-        * Create a new jVCard server on the given port, then run it.
-        * 
-        * @param port
-        *            the port to run on
-        *
-        * @throws SecurityException
-        *             in case of internal error
-        * @throws NoSuchMethodException
-        *             in case of internal error
-        * @throws ClassNotFoundException
-        *             in case of internal error
-        * @throws IllegalAccessException
-        *             in case of internal error
-        * @throws InstantiationException
-        *             in case of internal error
-        * @throws InvocationTargetException
-        *             in case of internal error
-        * @throws IllegalArgumentException
-        *             in case of internal error
-        * @throws IOException
-        *             in case of IO error
-        */
-       @SuppressWarnings("unchecked")
-       static private void runServer(int port) throws NoSuchMethodException,
-                       SecurityException, ClassNotFoundException, InstantiationException,
-                       IllegalAccessException, IllegalArgumentException,
-                       InvocationTargetException {
-               @SuppressWarnings("rawtypes")
-               Class serverClass = Class.forName("be.nikiroo.jvcard.remote.Server");
-               Method run = serverClass.getDeclaredMethod("run", new Class[] {});
-               run.invoke(serverClass.getConstructor(int.class).newInstance(port));
-       }
-
-       /**
-        * Start the TUI program.
-        * 
-        * @param textMode
-        *            TRUE to force text mode, FALSE to force the Swing terminal
-        *            emulator, null to automatically determine the best choice
-        * @param files
-        *            the files to show at startup
-        * 
-        * @throws SecurityException
-        *             in case of internal error
-        * @throws NoSuchMethodException
-        *             in case of internal error
-        * @throws ClassNotFoundException
-        *             in case of internal error
-        * @throws IllegalAccessException
-        *             in case of internal error
-        * @throws InstantiationException
-        *             in case of internal error
-        * @throws InvocationTargetException
-        *             in case of internal error
-        * @throws IllegalArgumentException
-        *             in case of internal error
-        * @throws IOException
-        *             in case of IO error
-        */
-       @SuppressWarnings("unchecked")
-       static private void startTui(Boolean textMode, List<String> files)
-                       throws NoSuchMethodException, SecurityException,
-                       ClassNotFoundException, InstantiationException,
-                       IllegalAccessException, IllegalArgumentException,
-                       InvocationTargetException {
-               @SuppressWarnings("rawtypes")
-               Class launcherClass = Class
-                               .forName("be.nikiroo.jvcard.tui.TuiLauncher");
-               Method start = launcherClass.getDeclaredMethod("start", new Class[] {
-                               Boolean.class, List.class });
-               start.invoke(launcherClass.newInstance(), textMode, files);
-       }
-
-       /**
-        * Return the {@link Card} corresponding to the given URL, synchronised if
-        * necessary.
-        * 
-        * @param input
-        *            the jvcard:// with resource name URL (e.g.:
-        *            <tt>jvcard://localhost:4444/coworkers</tt>)
-        * 
-        * @throws SecurityException
-        *             in case of internal error
-        * @throws NoSuchMethodException
-        *             in case of internal error
-        * @throws ClassNotFoundException
-        *             in case of internal error
-        * @throws IllegalAccessException
-        *             in case of internal error
-        * @throws InstantiationException
-        *             in case of internal error
-        * @throws InvocationTargetException
-        *             in case of internal error
-        * @throws IllegalArgumentException
-        *             in case of internal error
-        * @throws IOException
-        *             in case of IO error
-        */
-       @SuppressWarnings("unchecked")
-       static private Card syncCard(String input) throws ClassNotFoundException,
-                       NoSuchMethodException, SecurityException, InstantiationException,
-                       IllegalAccessException, IllegalArgumentException,
-                       InvocationTargetException, IOException {
-               @SuppressWarnings("rawtypes")
-               Class syncClass = Class.forName("be.nikiroo.jvcard.remote.Sync");
-               Method getCache = syncClass.getDeclaredMethod("getCache",
-                               new Class[] {});
-               Method sync = syncClass.getDeclaredMethod("sync", new Class[] {
-                               Card.class, boolean.class });
-
-               Object o = syncClass.getConstructor(String.class).newInstance(input);
-
-               File file = (File) getCache.invoke(o);
-               Card card = new Card(file, Format.VCard21);
-               card.setRemote(true);
-               sync.invoke(o, card, false);
-
-               return card;
-       }
-
        /**
         * Open the given path and add all its files if it is a directory or just
         * this one if not to the returned list.
@@ -463,7 +356,7 @@ public class Main {
                                        "sync client");
                        s.open(true);
 
-                       s.sendCommand(Verb.LIST);
+                       s.sendCommand(Command.LIST_CARD);
                        for (String p : s.receiveBlock()) {
                                files.add(path
                                                + p.substring(StringUtils.fromTime(0).length() + 1));