update to latest nikiroo-utils
[jvcard.git] / src / be / nikiroo / jvcard / launcher / Main.java
index 7f59539826155b76ada41bd9c9b9ef018879eae7..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-beta3-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, ONLY_PHOTO, SAVE_CONFIG
+               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
@@ -63,7 +65,7 @@ public class Main {
         * @return the translated text with the given value where required
         */
        static public String trans(StringId id, Object... values) {
-               return transService.getString(id, (Object[]) values);
+               return transService.getString(id, values);
        }
 
        /**
@@ -107,31 +109,17 @@ 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("--")) {
                                noMoreParams = true;
                        } else if (!noMoreParams && arg.equals("--help")) {
-                               System.out
-                                               .println("TODO: implement some help text.\n"
-                                                               + "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--config DIRECTORY: use the given directory as a CONFIG_DIR\n"
-                                                               + "\t--save-config DIRECTORY: save the current config to DIRECTORY (lang: only current)\n"
-                                                               + "\t--server PORT: start a remoting server instead of a client\n"
-                                                               + "\t--i18n DIR: generate the translation file for the given language (can be \"\") to/from the .properties given dir\n"
-                                                               + "\t--save-photo DIR FORMAT: save the contacts' photos to DIR, named after FORMAT\n"
-                                                               + "\t--load-photo DIR FORMAT: load the contacts' photos from DIR, named after FORMAT\n"
-                                                               + "\t--only-photo DIR FORMAT: load the contacts' photos from DIR, named after FORMAT, overwrite all other photos of selected contacts\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)\n"
-                                                               + "('jvcard://hostname:8888/file' links -- or without 'file' -- are also ok)\n");
-                               return;
+                               if (mode != Mode.CONTACT_MANAGER) {
+                                       SERR(StringId.CLI_SERR_MODES);
+                                       return;
+                               }
+                               mode = Mode.HELP;
                        } else if (!noMoreParams && arg.equals("--tui")) {
                                textMode = true;
                        } else if (!noMoreParams && arg.equals("--gui")) {
@@ -142,8 +130,7 @@ public class Main {
                        } else if (!noMoreParams && arg.equals("--lang")) {
                                index++;
                                if (index >= args.length) {
-                                       System.err.println("Syntax error: no language given");
-                                       System.exit(ERR_SYNTAX);
+                                       SERR(StringId.CLI_SERR_NOLANG);
                                        return;
                                }
 
@@ -153,9 +140,7 @@ public class Main {
                        } else if (!noMoreParams && arg.equals("--config")) {
                                index++;
                                if (index >= args.length) {
-                                       System.err
-                                                       .println("Syntax error: no config directory given");
-                                       System.exit(ERR_SYNTAX);
+                                       SERR(StringId.CLI_SERR_NODIR);
                                        return;
                                }
 
@@ -165,72 +150,45 @@ public class Main {
                        } else if (!noMoreParams && arg.equals("--save-config")) {
                                index++;
                                if (index >= args.length) {
-                                       System.err
-                                                       .println("Syntax error: no config directory given");
-                                       System.exit(ERR_SYNTAX);
+                                       SERR(StringId.CLI_SERR_NODIR);
                                        return;
                                }
                                dir = args[index];
 
                                if (mode != Mode.CONTACT_MANAGER) {
-                                       System.err
-                                                       .println("Syntax error: you can only use one of: \n"
-                                                                       + "--server\n"
-                                                                       + "--save-config\n"
-                                                                       + "--i18n\n"
-                                                                       + "--load-photo\n"
-                                                                       + "--save-photo\n" + "--only-photo\n");
-                                       System.exit(ERR_SYNTAX);
+                                       SERR(StringId.CLI_SERR_MODES);
                                        return;
                                }
                                mode = Mode.SAVE_CONFIG;
                        } else if (!noMoreParams && arg.equals("--server")) {
                                if (mode != Mode.CONTACT_MANAGER) {
-                                       System.err
-                                                       .println("Syntax error: you can only use one of: \n"
-                                                                       + "--server\n"
-                                                                       + "--save-config\n"
-                                                                       + "--i18n\n"
-                                                                       + "--load-photo\n"
-                                                                       + "--save-photo\n" + "--only-photo\n");
-                                       System.exit(ERR_SYNTAX);
+                                       SERR(StringId.CLI_SERR_MODES);
                                        return;
                                }
                                mode = Mode.SERVER;
 
                                index++;
                                if (index >= args.length) {
-                                       System.err.println("Syntax error: no port given");
-                                       System.exit(ERR_SYNTAX);
+                                       SERR(StringId.CLI_SERR_NOPORT);
                                        return;
                                }
 
                                try {
                                        port = Integer.parseInt(args[index]);
                                } catch (NumberFormatException e) {
-                                       System.err.println("Invalid port number: " + args[index]);
-                                       System.exit(ERR_SYNTAX);
+                                       SERR(StringId.CLI_SERR_BADPORT, "" + args[index]);
                                        return;
                                }
                        } else if (!noMoreParams && arg.equals("--i18n")) {
                                if (mode != Mode.CONTACT_MANAGER) {
-                                       System.err
-                                                       .println("Syntax error: you can only use one of: \n"
-                                                                       + "--server\n"
-                                                                       + "--save-config\n"
-                                                                       + "--i18n\n"
-                                                                       + "--load-photo\n"
-                                                                       + "--save-photo\n" + "--only-photo\n");
-                                       System.exit(ERR_SYNTAX);
+                                       SERR(StringId.CLI_SERR_MODES);
                                        return;
                                }
                                mode = Mode.I18N;
 
                                index++;
                                if (index >= args.length) {
-                                       System.err
-                                                       .println("Syntax error: no .properties directory given");
-                                       System.exit(ERR_SYNTAX);
+                                       SERR(StringId.CLI_SERR_NODIR);
                                        return;
                                }
 
@@ -240,14 +198,7 @@ public class Main {
                                                        || arg.equals("--save-photo") || arg
                                                                .equals("--only-photo"))) {
                                if (mode != Mode.CONTACT_MANAGER) {
-                                       System.err
-                                                       .println("Syntax error: you can only use one of: \n"
-                                                                       + "--server\n"
-                                                                       + "--save-config\n"
-                                                                       + "--i18n\n"
-                                                                       + "--load-photo\n"
-                                                                       + "--save-photo\n" + "--only-photo\n");
-                                       System.exit(ERR_SYNTAX);
+                                       SERR(StringId.CLI_SERR_MODES);
                                        return;
                                }
 
@@ -255,14 +206,11 @@ public class Main {
                                        mode = Mode.LOAD_PHOTO;
                                } else if (arg.equals("--save-photo")) {
                                        mode = Mode.SAVE_PHOTO;
-                               } else {
-                                       mode = Mode.ONLY_PHOTO;
                                }
 
                                index++;
                                if (index >= args.length) {
-                                       System.err.println("Syntax error: photo directory given");
-                                       System.exit(ERR_SYNTAX);
+                                       SERR(StringId.CLI_SERR_NODIR);
                                        return;
                                }
 
@@ -270,12 +218,25 @@ public class Main {
 
                                index++;
                                if (index >= args.length) {
-                                       System.err.println("Syntax error: photo format given");
-                                       System.exit(ERR_SYNTAX);
+                                       SERR(StringId.CLI_SERR_NOFORMAT);
                                        return;
                                }
 
                                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));
@@ -297,17 +258,13 @@ public class Main {
 
                // Error management:
                if (mode == Mode.SERVER && files.size() > 0) {
-                       System.err
-                                       .println("Invalid syntax: you cannot both use --server and provide card files");
-                       System.exit(ERR_SYNTAX);
+                       SERR(StringId.CLI_SERR_NOLANG, "--server");
+                       return;
                } else if (mode == Mode.I18N && files.size() > 0) {
-                       System.err
-                                       .println("Invalid syntax: you cannot both use --i18n and provide card files");
-                       System.exit(ERR_SYNTAX);
+                       SERR(StringId.CLI_SERR_NOLANG, "--i18n");
+                       return;
                } else if (mode == Mode.I18N && language == null) {
-                       System.err
-                                       .println("Invalid syntax: you cannot use --i18n without --lang");
-                       System.exit(ERR_SYNTAX);
+                       SERR(StringId.CLI_SERR_NOLANG);
                } else if ((mode == Mode.CONTACT_MANAGER || mode == Mode.SAVE_PHOTO || mode == Mode.LOAD_PHOTO)
                                && files.size() == 0) {
                        if (files.size() == 0 && !filesTried) {
@@ -315,8 +272,7 @@ public class Main {
                        }
 
                        if (files.size() == 0) {
-                               System.err.println("No files to open");
-                               System.exit(ERR_NO_FILE);
+                               ERR(StringId.CLI_ERR, StringId.CLI_ERR_NOFILES, ERR_NO_FILE);
                                return;
                        }
                }
@@ -327,19 +283,22 @@ public class Main {
                        try {
                                if (!new File(dir).isDirectory()) {
                                        if (!new File(dir).mkdir()) {
-                                               System.err
-                                                               .println("Cannot create configuration directory: "
-                                                                               + dir);
+                                               System.err.println(trans(
+                                                               StringId.CLI_ERR_CANNOT_CREATE_CONFDIR, dir));
                                        }
                                }
 
-                               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) {
                                e.printStackTrace();
+                               System.err.flush();
                                System.exit(ERR_INTERNAL);
                        }
                        break;
@@ -347,14 +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 {
-                                       System.err.println("Remoting support not available");
-                                       System.exit(ERR_INTERNAL);
+                       } 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;
                }
@@ -362,14 +326,12 @@ public class Main {
                        try {
                                transService.updateFile(dir);
                        } catch (IOException e) {
-                               System.err
-                                               .println("I/O Exception: Cannot create/update a language in directory: "
-                                                               + dir);
-                               e.printStackTrace();
+                               ERR(StringId.CLI_ERR, StringId.CLI_ERR_CANNOT_CREATE_LANG,
+                                               ERR_INTERNAL);
+                               return;
                        }
                        break;
                }
-               case ONLY_PHOTO:
                case LOAD_PHOTO: {
                        for (String file : files) {
                                try {
@@ -379,21 +341,35 @@ public class Main {
                                                File f = new File(dir, filename);
 
                                                if (f.exists()) {
+                                                       System.out.println("Loading " + f);
                                                        try {
-                                                               String b64 = StringUtils.fromImage(ImageIO
-                                                                               .read(f));
-
-                                                               if (mode == Mode.ONLY_PHOTO) {
-                                                                       for (Data photo = contact
-                                                                                       .getPreferredData("PHOTO"); photo != null; photo = contact
-                                                                                       .getPreferredData("PHOTO")) {
-                                                                               photo.delete();
-                                                                       }
+                                                               String type = "jpeg";
+                                                               int dotIndex = filename.indexOf('.');
+                                                               if (dotIndex >= 0
+                                                                               && (dotIndex + 1) < filename.length()) {
+                                                                       type = filename.substring(dotIndex + 1)
+                                                                                       .toLowerCase();
+                                                               }
+
+                                                               String b64;
+                                                               Image img = new Image(IOUtils.toByteArray(f));
+                                                               try {
+                                                                       b64 = img.toBase64();
+                                                               } finally {
+                                                                       img.close();
                                                                }
 
+                                                               // remove previous photos:
+                                                               for (Data photo = contact
+                                                                               .getPreferredData("PHOTO"); photo != null; photo = contact
+                                                                               .getPreferredData("PHOTO")) {
+                                                                       photo.delete();
+                                                               }
+                                                               //
+
                                                                List<TypeInfo> types = new LinkedList<TypeInfo>();
                                                                types.add(new TypeInfo("ENCODING", "b"));
-                                                               types.add(new TypeInfo("TYPE", "png"));
+                                                               types.add(new TypeInfo("TYPE", type));
                                                                Data photo = new Data(types, "PHOTO", b64, null);
                                                                contact.add(photo);
                                                        } catch (IOException e) {
@@ -404,7 +380,8 @@ public class Main {
                                        }
                                        card.save();
                                } catch (IOException e) {
-                                       System.err.println("Card cannot be opened: " + file);
+                                       System.err
+                                                       .println(trans(StringId.CLI_ERR_CANNOT_OPEN, file));
                                }
                        }
                        break;
@@ -418,20 +395,21 @@ public class Main {
                                                if (photo != null) {
                                                        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("Cannot save photo of contact: "
-                                                                                               + contact
-                                                                                                               .getPreferredDataValue("FN"));
+                                                               System.err.println(trans(
+                                                                               StringId.CLI_ERR_CANNOT_SAVE_PHOTO,
+                                                                               contact.getPreferredDataValue("FN")));
                                                        }
                                                }
                                        }
                                } catch (IOException e) {
-                                       System.err.println("Card cannot be opened: " + file);
+                                       System.err
+                                                       .println(trans(StringId.CLI_ERR_CANNOT_OPEN, file));
                                }
                        }
                        break;
@@ -439,15 +417,87 @@ public class Main {
                case CONTACT_MANAGER: {
                        try {
                                Optional.startTui(textMode, files);
-                       } catch (Exception e) {
-                               if (e instanceof IOException) {
-                                       System.err
-                                                       .println("I/O Exception: Cannot start the program with the given cards");
-                               } else {
-                                       System.err.println("TUI support not available");
-                                       System.exit(ERR_INTERNAL);
+                       } 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 + " "
+                                       + Version.getCurrentVersion());
+                       System.out.println();
+
+                       System.out.println(trans(StringId.CLI_HELP));
+                       System.out.println();
+
+                       System.out.println(trans(StringId.CLI_HELP_MODES));
+                       System.out.println("\t--help : "
+                                       + trans(StringId.CLI_HELP_MODE_HELP));
+                       System.out.println("\t(--tui|--gui) (--noutf) ... : "
+                                       + trans(StringId.CLI_HELP_MODE_CONTACT_MANAGER));
+                       System.out.println("\t--server PORT ... : "
+                                       + trans(StringId.CLI_HELP_MODE_SERVER));
+                       System.out.println("\t--save-config DIR : "
+                                       + trans(StringId.CLI_HELP_MODE_SAVE_CONFIG));
+                       System.out.println("\t--i18n DIR ---lang LANG : "
+                                       + trans(StringId.CLI_HELP_MODE_I18N));
+                       System.out.println("\t--load-photo DIR FORMAT ... : "
+                                       + 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));
+                       System.out.println("\t-- : " + trans(StringId.CLI_HELP_DD));
+                       System.out.println("\t--lang LANG : "
+                                       + 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_OPTION));
+                       System.out.println("\t--config : "
+                                       + trans(StringId.CLI_HELP_CONFIG));
+                       System.out.println();
+
+                       System.out.println(trans(StringId.CLI_HELP_FOOTER));
+                       System.out.println();
+
                        break;
                }
                }
@@ -476,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 {
@@ -501,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);
                }
 
@@ -510,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) {
@@ -524,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.
@@ -624,4 +685,35 @@ public class Main {
                forceComputedFn = map.getBoolean(
                                DisplayOption.CONTACT_DETAILS_SHOW_COMPUTED_FN, false);
        }
+
+       /**
+        * Syntax error detected, closing the application with an error message.
+        * 
+        * @param err
+        *            the syntax error case
+        */
+       static private void SERR(StringId err, Object... values) {
+               ERR(StringId.CLI_SERR, err, ERR_SYNTAX, values);
+       }
+
+       /**
+        * Error detected, closing the application with an error message.
+        * 
+        * @param err
+        *            the error case
+        * @param suberr
+        *            the suberror or NULL if none
+        * @param CODE
+        *            the error code as declared above
+        */
+       static private void ERR(StringId err, StringId suberr, int CODE,
+                       Object... subvalues) {
+               if (suberr == null)
+                       System.err.println(trans(err));
+               else
+                       System.err.println(trans(err, trans(suberr, subvalues)));
+
+               System.err.flush();
+               System.exit(CODE);
+       }
 }