update to latest nikiroo-utils
[jvcard.git] / src / be / nikiroo / jvcard / launcher / Main.java
index 65fbf9c635f51d4a097ca16604680faabe63c3eb..61287b3df8c486b10b1c36a0556ba115399fdb7f 100644 (file)
@@ -8,24 +8,26 @@ import java.nio.charset.Charset;
 import java.util.LinkedList;
 import java.util.List;
 
-import javax.imageio.ImageIO;
-
 import be.nikiroo.jvcard.Card;
 import be.nikiroo.jvcard.Contact;
 import be.nikiroo.jvcard.Data;
 import be.nikiroo.jvcard.TypeInfo;
 import be.nikiroo.jvcard.launcher.CardResult.MergeCallback;
+import be.nikiroo.jvcard.launcher.Optional.NotSupportedException;
 import be.nikiroo.jvcard.parsers.Format;
 import be.nikiroo.jvcard.remote.Command;
 import be.nikiroo.jvcard.remote.SimpleSocket;
-import be.nikiroo.jvcard.resources.Bundles;
-import be.nikiroo.jvcard.resources.StringUtils;
-import be.nikiroo.jvcard.resources.bundles.ColorBundle;
-import be.nikiroo.jvcard.resources.bundles.DisplayBundle;
-import be.nikiroo.jvcard.resources.bundles.RemoteBundle;
-import be.nikiroo.jvcard.resources.bundles.TransBundle;
-import be.nikiroo.jvcard.resources.enums.DisplayOption;
-import be.nikiroo.jvcard.resources.enums.StringId;
+import be.nikiroo.jvcard.resources.DisplayBundle;
+import be.nikiroo.jvcard.resources.DisplayOption;
+import be.nikiroo.jvcard.resources.RemoteBundle;
+import be.nikiroo.jvcard.resources.StringId;
+import be.nikiroo.jvcard.resources.TransBundle;
+import be.nikiroo.utils.IOUtils;
+import be.nikiroo.utils.Image;
+import be.nikiroo.utils.ImageUtils;
+import be.nikiroo.utils.StringUtils;
+import be.nikiroo.utils.Version;
+import be.nikiroo.utils.resources.Bundles;
 
 /**
  * This class contains the runnable Main method. It will parse the user supplied
@@ -33,11 +35,11 @@ import be.nikiroo.jvcard.resources.enums.StringId;
  * a MainWindow.
  * 
  * @author niki
- *
+ * 
  */
 public class Main {
+       /** The name of the program */
        static public final String APPLICATION_TITLE = "jVcard";
-       static public final String APPLICATION_VERSION = "1.0-dev";
 
        static private final int ERR_NO_FILE = 1;
        static private final int ERR_SYNTAX = 2;
@@ -48,13 +50,13 @@ public class Main {
        static private boolean forceComputedFn;
 
        enum Mode {
-               CONTACT_MANAGER, I18N, SERVER, LOAD_PHOTO, SAVE_PHOTO, SAVE_CONFIG, HELP
+               CONTACT_MANAGER, I18N, SERVER, LOAD_PHOTO, SAVE_PHOTO, SAVE_CONFIG, HELP, SAVE_TO,
        }
 
        /**
         * Translate the given {@link StringId} into user text.
         * 
-        * @param stringId
+        * @param id
         *            the ID to translate
         * @param values
         *            the values to insert instead of the place holders in the
@@ -107,6 +109,7 @@ public class Main {
                int port = -1;
                Mode mode = Mode.CONTACT_MANAGER;
                String format = null;
+               String output = null;
                for (int index = 0; index < args.length; index++) {
                        String arg = args[index];
                        if (!noMoreParams && arg.equals("--")) {
@@ -220,6 +223,20 @@ public class Main {
                                }
 
                                format = args[index];
+                       } else if (!noMoreParams && (arg.equals("--save-to"))) {
+                               if (mode != Mode.CONTACT_MANAGER) {
+                                       SERR(StringId.CLI_SERR_MODES);
+                                       return;
+                               }
+                               mode = Mode.SAVE_TO;
+
+                               index++;
+                               if (index >= args.length) {
+                                       SERR(StringId.CLI_ERR_NOFILES);
+                                       return;
+                               }
+
+                               output = args[index];
                        } else {
                                filesTried = true;
                                files.addAll(open(arg));
@@ -271,9 +288,12 @@ public class Main {
                                        }
                                }
 
-                               transService.updateFile(dir); // current lang TransBundle
-                               new TransBundle().updateFile(dir);
-                               new ColorBundle().updateFile(dir);
+                               new TransBundle().updateFile(dir); // default locale
+                               for (String lang : new TransBundle().getKnownLanguages()) {
+                                       new TransBundle(lang).updateFile(dir);
+                               }
+
+                               // new UIColors().updateFile(dir);
                                new DisplayBundle().updateFile(dir);
                                new RemoteBundle().updateFile(dir);
                        } catch (IOException e) {
@@ -286,15 +306,19 @@ public class Main {
                case SERVER: {
                        try {
                                Optional.runServer(port);
-                       } catch (Exception e) {
-                               if (e instanceof IOException) {
-                                       System.err
-                                                       .println("I/O Exception: Cannot start the server");
-                               } else {
+                       } catch (IOException e) {
+                               ERR(StringId.CLI_ERR, StringId.CLI_ERR_CANNOT_START,
+                                               ERR_INTERNAL);
+                               return;
+                       } catch (NotSupportedException e) {
+                               if (!e.isCompiledIn()) {
                                        ERR(StringId.CLI_ERR, StringId.CLI_ERR_NO_REMOTING,
                                                        ERR_INTERNAL);
                                        return;
                                }
+                               e.printStackTrace();
+                               ERR(StringId.CLI_ERR, StringId.CLI_ERR, ERR_INTERNAL);
+                               return;
                        }
                        break;
                }
@@ -302,9 +326,9 @@ public class Main {
                        try {
                                transService.updateFile(dir);
                        } catch (IOException e) {
-                               System.err.println(trans(StringId.CLI_ERR_CANNOT_CREATE_LANG,
-                                               dir));
-                               e.printStackTrace();
+                               ERR(StringId.CLI_ERR, StringId.CLI_ERR_CANNOT_CREATE_LANG,
+                                               ERR_INTERNAL);
+                               return;
                        }
                        break;
                }
@@ -327,7 +351,13 @@ public class Main {
                                                                                        .toLowerCase();
                                                                }
 
-                                                               String b64 = StringUtils.fromImage(f);
+                                                               String b64;
+                                                               Image img = new Image(IOUtils.toByteArray(f));
+                                                               try {
+                                                                       b64 = img.toBase64();
+                                                               } finally {
+                                                                       img.close();
+                                                               }
 
                                                                // remove previous photos:
                                                                for (Data photo = contact
@@ -366,10 +396,10 @@ public class Main {
                                                        String filename = contact.toString(format, "");
                                                        File f = new File(dir, filename + ".png");
                                                        System.out.println("Saving " + f);
+                                                       Image img = new Image(photo.getValue());
                                                        try {
-                                                               ImageIO.write(
-                                                                               StringUtils.toImage(photo.getValue()),
-                                                                               "png", f);
+                                                               ImageUtils.getInstance().saveAsImage(img, f,
+                                                                               "png");
                                                        } catch (IOException e) {
                                                                System.err.println(trans(
                                                                                StringId.CLI_ERR_CANNOT_SAVE_PHOTO,
@@ -387,20 +417,48 @@ public class Main {
                case CONTACT_MANAGER: {
                        try {
                                Optional.startTui(textMode, files);
-                       } catch (Exception e) {
-                               if (e instanceof IOException) {
-                                       ERR(StringId.CLI_ERR, StringId.CLI_ERR_CANNOT_START,
-                                                       ERR_NO_FILE);
-                                       return;
-                               } else {
+                       } catch (IOException e) {
+                               ERR(StringId.CLI_ERR, StringId.CLI_ERR_CANNOT_START,
+                                               ERR_NO_FILE);
+                               return;
+                       } catch (NotSupportedException e) {
+                               if (!e.isCompiledIn()) {
                                        ERR(StringId.CLI_ERR, StringId.CLI_ERR_NO_TUI, ERR_INTERNAL);
                                        return;
                                }
+                               e.printStackTrace();
+                               ERR(StringId.CLI_ERR, StringId.CLI_ERR, ERR_INTERNAL);
+                               return;
+                       }
+                       break;
+               }
+               case SAVE_TO: {
+                       try {
+                               Card total = new Card(null, getCardFormat(output));
+
+                               for (String file : files) {
+                                       try {
+                                               Card card = getCard(file, null).getCard();
+                                               card.unlink();
+                                               while (card.size() > 0) {
+                                                       total.add(card.remove(0));
+                                               }
+                                       } catch (IOException e) {
+                                               System.err.println(trans(StringId.CLI_ERR_CANNOT_OPEN,
+                                                               file));
+                                       }
+                               }
+
+                               total.saveAs(new File(output), getCardFormat(output));
+                       } catch (IOException e) {
+                               System.err.println(trans(StringId.CLI_ERR_CANNOT_OPEN, output));
                        }
+
                        break;
                }
                case HELP: {
-                       System.out.println(APPLICATION_TITLE + " " + APPLICATION_VERSION);
+                       System.out.println(APPLICATION_TITLE + " "
+                                       + Version.getCurrentVersion());
                        System.out.println();
 
                        System.out.println(trans(StringId.CLI_HELP));
@@ -421,6 +479,8 @@ public class Main {
                                        + trans(StringId.CLI_HELP_MODE_LOAD_PHOTO));
                        System.out.println("\t--save-photo DIR FORMAT ... : "
                                        + trans(StringId.CLI_HELP_MODE_SAVE_PHOTO));
+                       System.out.println("\t--save-to output(.vcf) ... : "
+                                       + trans(StringId.CLI_HELP_MODE_SAVE_TO));
                        System.out.println();
 
                        System.out.println(trans(StringId.CLI_HELP_OPTIONS));
@@ -429,7 +489,8 @@ public class Main {
                                        + trans(StringId.CLI_HELP_LANG));
                        System.out.println("\t--tui : " + trans(StringId.CLI_HELP_TUI));
                        System.out.println("\t--gui : " + trans(StringId.CLI_HELP_GUI));
-                       System.out.println("\t--noutf : " + trans(StringId.CLI_HELP_NOUTF));
+                       System.out.println("\t--noutf : "
+                                       + trans(StringId.CLI_HELP_NOUTF_OPTION));
                        System.out.println("\t--config : "
                                        + trans(StringId.CLI_HELP_CONFIG));
                        System.out.println();
@@ -437,6 +498,7 @@ public class Main {
                        System.out.println(trans(StringId.CLI_HELP_FOOTER));
                        System.out.println();
 
+                       break;
                }
                }
        }
@@ -464,20 +526,8 @@ public class Main {
         */
        static public CardResult getCard(String input, MergeCallback callback)
                        throws IOException {
-               boolean remote = false;
-               Format format = Format.Abook;
-               String ext = input;
-               if (ext.contains(".")) {
-                       String tab[] = ext.split("\\.");
-                       if (tab.length > 1 && tab[tab.length - 1].equalsIgnoreCase("vcf")) {
-                               format = Format.VCard21;
-                       }
-               }
-
-               if (input.contains("://")) {
-                       format = Format.VCard21;
-                       remote = true;
-               }
+               boolean remote = isFileRemote(input);
+               Format format = getCardFormat(input);
 
                CardResult card = null;
                try {
@@ -489,7 +539,7 @@ public class Main {
                        }
                } catch (IOException ioe) {
                        throw ioe;
-               } catch (Exception e) {
+               } catch (NotSupportedException e) {
                        throw new IOException("Remoting support not available", e);
                }
 
@@ -498,9 +548,11 @@ public class Main {
                        try {
                                for (Contact contact : card.getCard()) {
                                        Data name = contact.getPreferredData("FN");
+                                       Data n = contact.getPreferredData("N");
+                                       boolean hasN = n != null && n.getValue().length() > 0;
                                        if (name == null || name.getValue().length() == 0
-                                                       || forceComputedFn) {
-                                               name.setValue(contact.toString(defaultFn, ""));
+                                                       || (forceComputedFn && hasN)) {
+                                               name.setValue(contact.toString(defaultFn, "").trim());
                                        }
                                }
                        } catch (Exception e) {
@@ -512,6 +564,27 @@ public class Main {
                return card;
        }
 
+       static private boolean isFileRemote(String input) {
+               return input.contains("://");
+       }
+
+       static Format getCardFormat(String input) {
+               if (isFileRemote(input)) {
+                       return Format.VCard21;
+               }
+
+               Format format = Format.Abook;
+               String ext = input;
+               if (ext.contains(".")) {
+                       String tab[] = ext.split("\\.");
+                       if (tab.length > 1 && tab[tab.length - 1].equalsIgnoreCase("vcf")) {
+                               format = Format.VCard21;
+                       }
+               }
+
+               return format;
+       }
+
        /**
         * Open the given path and add all its files if it is a directory or just
         * this one if not to the returned list.