Fix 2 TODO items:
[jvcard.git] / src / be / nikiroo / jvcard / launcher / Main.java
index eeeae4153a57eada0b58e04da065ea2d119101bc..54238c8b86bf08e1fd872969e56d4f7aee6677ce 100644 (file)
@@ -15,6 +15,7 @@ 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;
@@ -37,7 +38,7 @@ import be.nikiroo.jvcard.resources.enums.StringId;
  */
 public class Main {
        static public final String APPLICATION_TITLE = "jVcard";
-       static public final String APPLICATION_VERSION = "1.0-dev";
+       static public final String APPLICATION_VERSION = "1.1-dev";
 
        static private final int ERR_NO_FILE = 1;
        static private final int ERR_SYNTAX = 2;
@@ -271,8 +272,11 @@ public class Main {
                                        }
                                }
 
-                               transService.updateFile(dir); // current lang TransBundle
-                               new TransBundle().updateFile(dir);
+                               new TransBundle().updateFile(dir); // default locale
+                               for (String lang : TransBundle.getKnownLanguages()) {
+                                       new TransBundle(lang).updateFile(dir);
+                               }
+
                                new ColorBundle().updateFile(dir);
                                new DisplayBundle().updateFile(dir);
                                new RemoteBundle().updateFile(dir);
@@ -286,14 +290,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;
+                               } else {
+                                       e.printStackTrace();
+                                       ERR(StringId.CLI_ERR, StringId.CLI_ERR, ERR_INTERNAL);
+                                       return;
                                }
                        }
                        break;
@@ -302,9 +311,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;
                }
@@ -319,8 +328,15 @@ public class Main {
                                                if (f.exists()) {
                                                        System.out.println("Loading " + f);
                                                        try {
-                                                               String b64 = StringUtils.fromImage(ImageIO
-                                                                               .read(f));
+                                                               String type = "jpeg";
+                                                               int dotIndex = filename.indexOf('.');
+                                                               if (dotIndex >= 0
+                                                                               && (dotIndex + 1) < filename.length()) {
+                                                                       type = filename.substring(dotIndex + 1)
+                                                                                       .toLowerCase();
+                                                               }
+
+                                                               String b64 = StringUtils.fromImage(f);
 
                                                                // remove previous photos:
                                                                for (Data photo = contact
@@ -332,7 +348,7 @@ public class Main {
 
                                                                List<TypeInfo> types = new LinkedList<TypeInfo>();
                                                                types.add(new TypeInfo("ENCODING", "b"));
-                                                               types.add(new TypeInfo("TYPE", "jpeg"));
+                                                               types.add(new TypeInfo("TYPE", type));
                                                                Data photo = new Data(types, "PHOTO", b64, null);
                                                                contact.add(photo);
                                                        } catch (IOException e) {
@@ -380,13 +396,17 @@ 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);
+                       } 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;
                                } else {
-                                       ERR(StringId.CLI_ERR, StringId.CLI_ERR_NO_TUI, ERR_INTERNAL);
+                                       e.printStackTrace();
+                                       ERR(StringId.CLI_ERR, StringId.CLI_ERR, ERR_INTERNAL);
                                        return;
                                }
                        }
@@ -422,7 +442,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();
@@ -482,7 +503,7 @@ public class Main {
                        }
                } catch (IOException ioe) {
                        throw ioe;
-               } catch (Exception e) {
+               } catch (NotSupportedException e) {
                        throw new IOException("Remoting support not available", e);
                }
 
@@ -493,7 +514,7 @@ public class Main {
                                        Data name = contact.getPreferredData("FN");
                                        if (name == null || name.getValue().length() == 0
                                                        || forceComputedFn) {
-                                               name.setValue(contact.toString(defaultFn, ""));
+                                               name.setValue(contact.toString(defaultFn, "").trim());
                                        }
                                }
                        } catch (Exception e) {