X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Flauncher%2FMain.java;h=d893e101e710ede1013025beb47f424eb879becd;hb=f06c81000632cfb5f525ca458f719338f55f9f66;hp=85223d878560efb1d5dc55a8e6d9f5b781a8639f;hpb=88eb81220498dc5b3199bad2d1de6970b55ceaed;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/launcher/Main.java b/src/be/nikiroo/jvcard/launcher/Main.java index 85223d8..d893e10 100644 --- a/src/be/nikiroo/jvcard/launcher/Main.java +++ b/src/be/nikiroo/jvcard/launcher/Main.java @@ -1,7 +1,9 @@ package be.nikiroo.jvcard.launcher; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.Field; import java.net.Socket; import java.nio.charset.Charset; @@ -15,17 +17,19 @@ 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.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 +37,10 @@ import be.nikiroo.jvcard.resources.enums.StringId; * a MainWindow. * * @author niki - * + * */ public class Main { static public final String APPLICATION_TITLE = "jVcard"; - static public final String APPLICATION_VERSION = "1.0"; static private final int ERR_NO_FILE = 1; static private final int ERR_SYNTAX = 2; @@ -63,7 +66,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); } /** @@ -271,13 +274,17 @@ 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) { e.printStackTrace(); + System.err.flush(); System.exit(ERR_INTERNAL); } break; @@ -285,14 +292,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; @@ -301,9 +313,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 +328,26 @@ 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; + InputStream in = null; + try { + in = new FileInputStream(f); + b64 = ImageUtils.toBase64(in); + } finally { + if (in != null) { + in.close(); + } + } // remove previous photos: for (Data photo = contact @@ -330,7 +359,7 @@ public class Main { List types = new LinkedList(); 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,9 +385,10 @@ 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()), + ImageUtils.fromBase64(photo.getValue()), "png", f); } catch (IOException e) { System.err.println(trans( @@ -377,20 +407,25 @@ 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; } } 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)); @@ -419,7 +454,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(); @@ -479,7 +515,7 @@ public class Main { } } catch (IOException ioe) { throw ioe; - } catch (Exception e) { + } catch (NotSupportedException e) { throw new IOException("Remoting support not available", e); } @@ -490,7 +526,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 +666,7 @@ public class Main { else System.err.println(trans(err, trans(suberr, subvalues))); + System.err.flush(); System.exit(CODE); } }