ContactDetailsRaw: list of types on ADD + Main: better syserr
[jvcard.git] / src / be / nikiroo / jvcard / launcher / Main.java
index 66625f7e37d892f9585c4a21a041191dd1f7e945..6d552febfae8c16955cde0ad95480f32ef5bdc40 100644 (file)
@@ -63,7 +63,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);
        }
 
        /**
@@ -278,6 +278,7 @@ public class Main {
                                new RemoteBundle().updateFile(dir);
                        } catch (IOException e) {
                                e.printStackTrace();
+                               System.err.flush();
                                System.exit(ERR_INTERNAL);
                        }
                        break;
@@ -287,12 +288,17 @@ public class Main {
                                Optional.runServer(port);
                        } catch (Exception e) {
                                if (e instanceof IOException) {
-                                       System.err
-                                                       .println("I/O Exception: Cannot start the server");
-                               } else {
+                                       ERR(StringId.CLI_ERR, StringId.CLI_ERR_CANNOT_START,
+                                                       ERR_INTERNAL);
+                                       return;
+                               } else if (e instanceof ClassNotFoundException) {
                                        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;
@@ -301,9 +307,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;
                }
@@ -316,9 +322,17 @@ 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));
+                                                               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
@@ -330,7 +344,7 @@ public class Main {
 
                                                                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) {
@@ -356,6 +370,7 @@ public class Main {
                                                if (photo != null) {
                                                        String filename = contact.toString(format, "");
                                                        File f = new File(dir, filename + ".png");
+                                                       System.out.println("Saving " + f);
                                                        try {
                                                                ImageIO.write(
                                                                                StringUtils.toImage(photo.getValue()),
@@ -382,9 +397,13 @@ public class Main {
                                        ERR(StringId.CLI_ERR, StringId.CLI_ERR_CANNOT_START,
                                                        ERR_NO_FILE);
                                        return;
-                               } else {
+                               } else if (e instanceof ClassNotFoundException) {
                                        ERR(StringId.CLI_ERR, StringId.CLI_ERR_NO_TUI, ERR_INTERNAL);
                                        return;
+                               } else {
+                                       e.printStackTrace();
+                                       ERR(StringId.CLI_ERR, StringId.CLI_ERR, ERR_INTERNAL);
+                                       return;
                                }
                        }
                        break;
@@ -419,7 +438,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();
@@ -490,7 +510,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) {
@@ -630,6 +650,7 @@ public class Main {
                else
                        System.err.println(trans(err, trans(suberr, subvalues)));
 
+               System.err.flush();
                System.exit(CODE);
        }
 }