X-Git-Url: http://git.nikiroo.be/?p=jvcard.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Flauncher%2FMain.java;h=7f59539826155b76ada41bd9c9b9ef018879eae7;hp=9df98e2e93eb8a0306836a3516f1389d952c21a2;hb=e119a1c1a924998b9315e46c96b1c750aab1deb9;hpb=26d254a3ac6cddbd3583cbbcbf8d43aa15c6a32e diff --git a/src/be/nikiroo/jvcard/launcher/Main.java b/src/be/nikiroo/jvcard/launcher/Main.java index 9df98e2..7f59539 100644 --- a/src/be/nikiroo/jvcard/launcher/Main.java +++ b/src/be/nikiroo/jvcard/launcher/Main.java @@ -20,8 +20,12 @@ 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.Trans; -import be.nikiroo.jvcard.resources.Trans.StringId; +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; /** * This class contains the runnable Main method. It will parse the user supplied @@ -38,10 +42,13 @@ public class Main { static private final int ERR_NO_FILE = 1; static private final int ERR_SYNTAX = 2; static private final int ERR_INTERNAL = 3; - static private Trans transService; + static private TransBundle transService; + + static private String defaultFn; + static private boolean forceComputedFn; enum Mode { - CONTACT_MANAGER, I18N, SERVER, LOAD_PHOTO, SAVE_PHOTO, ONLY_PHOTO, + CONTACT_MANAGER, I18N, SERVER, LOAD_PHOTO, SAVE_PHOTO, ONLY_PHOTO, SAVE_CONFIG } /** @@ -55,8 +62,8 @@ public class Main { * * @return the translated text with the given value where required */ - static public String trans(StringId id, String... values) { - return transService.trans(id, (String[]) values); + static public String trans(StringId id, Object... values) { + return transService.getString(id, (Object[]) values); } /** @@ -92,7 +99,7 @@ public class Main { // get the "system default" language to help translate the --help // message if needed String language = null; - transService = new Trans(language); + transService = new TransBundle(language); boolean unicode = transService.isUnicode(); String dir = null; @@ -114,7 +121,8 @@ public class Main { + "\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: force the given directory as a CONFIG_DIR\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" @@ -140,7 +148,7 @@ public class Main { } language = args[index]; - transService = new Trans(language); + transService = new TransBundle(language); transService.setUnicode(unicode); } else if (!noMoreParams && arg.equals("--config")) { index++; @@ -152,17 +160,39 @@ public class Main { } Bundles.setDirectory(args[index]); - transService = new Trans(language); + transService = new TransBundle(language); transService.setUnicode(unicode); + } 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); + 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); + 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"); + + "--save-photo\n" + "--only-photo\n"); System.exit(ERR_SYNTAX); return; } @@ -187,10 +217,10 @@ public class Main { 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"); + + "--save-photo\n" + "--only-photo\n"); System.exit(ERR_SYNTAX); return; } @@ -213,10 +243,10 @@ public class Main { 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"); + + "--save-photo\n" + "--only-photo\n"); System.exit(ERR_SYNTAX); return; } @@ -262,6 +292,9 @@ public class Main { utf8(); } + // N/FN fix information: + readNFN(); + // Error management: if (mode == Mode.SERVER && files.size() > 0) { System.err @@ -290,6 +323,27 @@ public class Main { // switch (mode) { + case SAVE_CONFIG: { + try { + if (!new File(dir).isDirectory()) { + if (!new File(dir).mkdir()) { + System.err + .println("Cannot create configuration directory: " + + dir); + } + } + + transService.updateFile(dir); // current lang TransBundle + new TransBundle().updateFile(dir); + new ColorBundle().updateFile(dir); + new DisplayBundle().updateFile(dir); + new RemoteBundle().updateFile(dir); + } catch (IOException e) { + e.printStackTrace(); + System.exit(ERR_INTERNAL); + } + break; + } case SERVER: { try { Optional.runServer(port); @@ -306,11 +360,12 @@ public class Main { } case I18N: { try { - Trans.generateTranslationFile(dir, language); + transService.updateFile(dir); } catch (IOException e) { System.err .println("I/O Exception: Cannot create/update a language in directory: " + dir); + e.printStackTrace(); } break; } @@ -321,7 +376,7 @@ public class Main { Card card = getCard(file, null).getCard(); for (Contact contact : card) { String filename = contact.toString(format, ""); - File f = new File(dir, filename + ".png"); + File f = new File(dir, filename); if (f.exists()) { try { @@ -400,7 +455,11 @@ public class Main { /** * Return the {@link Card} corresponding to the given resource name -- a - * file or a remote jvcard URL + * file or a remote jvcard URL. + * + *

+ * Will also fix the FN if required (see display.properties). + *

* * @param input * a filename or a remote jvcard url with named resource (e.g.: @@ -446,6 +505,22 @@ public class Main { throw new IOException("Remoting support not available", e); } + // Fix the FN value + if (defaultFn != null) { + try { + for (Contact contact : card.getCard()) { + Data name = contact.getPreferredData("FN"); + if (name == null || name.getValue().length() == 0 + || forceComputedFn) { + name.setValue(contact.toString(defaultFn, "")); + } + } + } catch (Exception e) { + // sync failed -> getCard() throws. + // do not update. + } + } + return card; } @@ -536,4 +611,17 @@ public class Main { } catch (IllegalAccessException e) { } } + + /** + * Read display.properties to know if we should fix the FN field when empty, + * or always, or never. + */ + static private void readNFN() { + DisplayBundle map = new DisplayBundle(); + + defaultFn = map.getString(DisplayOption.CONTACT_DETAILS_DEFAULT_FN); + + forceComputedFn = map.getBoolean( + DisplayOption.CONTACT_DETAILS_SHOW_COMPUTED_FN, false); + } }