update to latest nikiroo-utils
[jvcard.git] / src / be / nikiroo / jvcard / launcher / Main.java
1 package be.nikiroo.jvcard.launcher;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.lang.reflect.Field;
6 import java.net.Socket;
7 import java.nio.charset.Charset;
8 import java.util.LinkedList;
9 import java.util.List;
10
11 import be.nikiroo.jvcard.Card;
12 import be.nikiroo.jvcard.Contact;
13 import be.nikiroo.jvcard.Data;
14 import be.nikiroo.jvcard.TypeInfo;
15 import be.nikiroo.jvcard.launcher.CardResult.MergeCallback;
16 import be.nikiroo.jvcard.launcher.Optional.NotSupportedException;
17 import be.nikiroo.jvcard.parsers.Format;
18 import be.nikiroo.jvcard.remote.Command;
19 import be.nikiroo.jvcard.remote.SimpleSocket;
20 import be.nikiroo.jvcard.resources.DisplayBundle;
21 import be.nikiroo.jvcard.resources.DisplayOption;
22 import be.nikiroo.jvcard.resources.RemoteBundle;
23 import be.nikiroo.jvcard.resources.StringId;
24 import be.nikiroo.jvcard.resources.TransBundle;
25 import be.nikiroo.utils.IOUtils;
26 import be.nikiroo.utils.Image;
27 import be.nikiroo.utils.ImageUtils;
28 import be.nikiroo.utils.StringUtils;
29 import be.nikiroo.utils.Version;
30 import be.nikiroo.utils.resources.Bundles;
31
32 /**
33 * This class contains the runnable Main method. It will parse the user supplied
34 * parameters and take action based upon those. Most of the time, it will start
35 * a MainWindow.
36 *
37 * @author niki
38 *
39 */
40 public class Main {
41 /** The name of the program */
42 static public final String APPLICATION_TITLE = "jVcard";
43
44 static private final int ERR_NO_FILE = 1;
45 static private final int ERR_SYNTAX = 2;
46 static private final int ERR_INTERNAL = 3;
47 static private TransBundle transService;
48
49 static private String defaultFn;
50 static private boolean forceComputedFn;
51
52 enum Mode {
53 CONTACT_MANAGER, I18N, SERVER, LOAD_PHOTO, SAVE_PHOTO, SAVE_CONFIG, HELP, SAVE_TO,
54 }
55
56 /**
57 * Translate the given {@link StringId} into user text.
58 *
59 * @param id
60 * the ID to translate
61 * @param values
62 * the values to insert instead of the place holders in the
63 * translation
64 *
65 * @return the translated text with the given value where required
66 */
67 static public String trans(StringId id, Object... values) {
68 return transService.getString(id, values);
69 }
70
71 /**
72 * Check if unicode characters should be used.
73 *
74 * @return TRUE to allow unicode
75 */
76 static public boolean isUnicode() {
77 return transService.isUnicode();
78 }
79
80 /**
81 * Start the application.
82 *
83 * <p>
84 * The returned exit codes are:
85 * <ul>
86 * <li>1: no files to open</li>
87 * <li>2: invalid syntax</li>
88 * <li>3: internal error</li>
89 * </ul>
90 * </p>
91 *
92 * @param args
93 * the parameters (see <tt>--help</tt> to know which are
94 * supported)
95 */
96 public static void main(String[] args) {
97 Boolean textMode = null;
98 boolean noMoreParams = false;
99 boolean filesTried = false;
100
101 // get the "system default" language to help translate the --help
102 // message if needed
103 String language = null;
104 transService = new TransBundle(language);
105
106 boolean unicode = transService.isUnicode();
107 String dir = null;
108 List<String> files = new LinkedList<String>();
109 int port = -1;
110 Mode mode = Mode.CONTACT_MANAGER;
111 String format = null;
112 String output = null;
113 for (int index = 0; index < args.length; index++) {
114 String arg = args[index];
115 if (!noMoreParams && arg.equals("--")) {
116 noMoreParams = true;
117 } else if (!noMoreParams && arg.equals("--help")) {
118 if (mode != Mode.CONTACT_MANAGER) {
119 SERR(StringId.CLI_SERR_MODES);
120 return;
121 }
122 mode = Mode.HELP;
123 } else if (!noMoreParams && arg.equals("--tui")) {
124 textMode = true;
125 } else if (!noMoreParams && arg.equals("--gui")) {
126 textMode = false;
127 } else if (!noMoreParams && arg.equals("--noutf")) {
128 unicode = false;
129 transService.setUnicode(unicode);
130 } else if (!noMoreParams && arg.equals("--lang")) {
131 index++;
132 if (index >= args.length) {
133 SERR(StringId.CLI_SERR_NOLANG);
134 return;
135 }
136
137 language = args[index];
138 transService = new TransBundle(language);
139 transService.setUnicode(unicode);
140 } else if (!noMoreParams && arg.equals("--config")) {
141 index++;
142 if (index >= args.length) {
143 SERR(StringId.CLI_SERR_NODIR);
144 return;
145 }
146
147 Bundles.setDirectory(args[index]);
148 transService = new TransBundle(language);
149 transService.setUnicode(unicode);
150 } else if (!noMoreParams && arg.equals("--save-config")) {
151 index++;
152 if (index >= args.length) {
153 SERR(StringId.CLI_SERR_NODIR);
154 return;
155 }
156 dir = args[index];
157
158 if (mode != Mode.CONTACT_MANAGER) {
159 SERR(StringId.CLI_SERR_MODES);
160 return;
161 }
162 mode = Mode.SAVE_CONFIG;
163 } else if (!noMoreParams && arg.equals("--server")) {
164 if (mode != Mode.CONTACT_MANAGER) {
165 SERR(StringId.CLI_SERR_MODES);
166 return;
167 }
168 mode = Mode.SERVER;
169
170 index++;
171 if (index >= args.length) {
172 SERR(StringId.CLI_SERR_NOPORT);
173 return;
174 }
175
176 try {
177 port = Integer.parseInt(args[index]);
178 } catch (NumberFormatException e) {
179 SERR(StringId.CLI_SERR_BADPORT, "" + args[index]);
180 return;
181 }
182 } else if (!noMoreParams && arg.equals("--i18n")) {
183 if (mode != Mode.CONTACT_MANAGER) {
184 SERR(StringId.CLI_SERR_MODES);
185 return;
186 }
187 mode = Mode.I18N;
188
189 index++;
190 if (index >= args.length) {
191 SERR(StringId.CLI_SERR_NODIR);
192 return;
193 }
194
195 dir = args[index];
196 } else if (!noMoreParams
197 && (arg.equals("--load-photo")
198 || arg.equals("--save-photo") || arg
199 .equals("--only-photo"))) {
200 if (mode != Mode.CONTACT_MANAGER) {
201 SERR(StringId.CLI_SERR_MODES);
202 return;
203 }
204
205 if (arg.equals("--load-photo")) {
206 mode = Mode.LOAD_PHOTO;
207 } else if (arg.equals("--save-photo")) {
208 mode = Mode.SAVE_PHOTO;
209 }
210
211 index++;
212 if (index >= args.length) {
213 SERR(StringId.CLI_SERR_NODIR);
214 return;
215 }
216
217 dir = args[index];
218
219 index++;
220 if (index >= args.length) {
221 SERR(StringId.CLI_SERR_NOFORMAT);
222 return;
223 }
224
225 format = args[index];
226 } else if (!noMoreParams && (arg.equals("--save-to"))) {
227 if (mode != Mode.CONTACT_MANAGER) {
228 SERR(StringId.CLI_SERR_MODES);
229 return;
230 }
231 mode = Mode.SAVE_TO;
232
233 index++;
234 if (index >= args.length) {
235 SERR(StringId.CLI_ERR_NOFILES);
236 return;
237 }
238
239 output = args[index];
240 } else {
241 filesTried = true;
242 files.addAll(open(arg));
243 }
244 }
245
246 // Force headless mode if we run in forced-text mode
247 if (mode != Mode.CONTACT_MANAGER || (textMode != null && textMode)) {
248 // same as -Djava.awt.headless=true
249 System.setProperty("java.awt.headless", "true");
250 }
251
252 if (unicode) {
253 utf8();
254 }
255
256 // N/FN fix information:
257 readNFN();
258
259 // Error management:
260 if (mode == Mode.SERVER && files.size() > 0) {
261 SERR(StringId.CLI_SERR_NOLANG, "--server");
262 return;
263 } else if (mode == Mode.I18N && files.size() > 0) {
264 SERR(StringId.CLI_SERR_NOLANG, "--i18n");
265 return;
266 } else if (mode == Mode.I18N && language == null) {
267 SERR(StringId.CLI_SERR_NOLANG);
268 } else if ((mode == Mode.CONTACT_MANAGER || mode == Mode.SAVE_PHOTO || mode == Mode.LOAD_PHOTO)
269 && files.size() == 0) {
270 if (files.size() == 0 && !filesTried) {
271 files.addAll(open("."));
272 }
273
274 if (files.size() == 0) {
275 ERR(StringId.CLI_ERR, StringId.CLI_ERR_NOFILES, ERR_NO_FILE);
276 return;
277 }
278 }
279 //
280
281 switch (mode) {
282 case SAVE_CONFIG: {
283 try {
284 if (!new File(dir).isDirectory()) {
285 if (!new File(dir).mkdir()) {
286 System.err.println(trans(
287 StringId.CLI_ERR_CANNOT_CREATE_CONFDIR, dir));
288 }
289 }
290
291 new TransBundle().updateFile(dir); // default locale
292 for (String lang : new TransBundle().getKnownLanguages()) {
293 new TransBundle(lang).updateFile(dir);
294 }
295
296 // new UIColors().updateFile(dir);
297 new DisplayBundle().updateFile(dir);
298 new RemoteBundle().updateFile(dir);
299 } catch (IOException e) {
300 e.printStackTrace();
301 System.err.flush();
302 System.exit(ERR_INTERNAL);
303 }
304 break;
305 }
306 case SERVER: {
307 try {
308 Optional.runServer(port);
309 } catch (IOException e) {
310 ERR(StringId.CLI_ERR, StringId.CLI_ERR_CANNOT_START,
311 ERR_INTERNAL);
312 return;
313 } catch (NotSupportedException e) {
314 if (!e.isCompiledIn()) {
315 ERR(StringId.CLI_ERR, StringId.CLI_ERR_NO_REMOTING,
316 ERR_INTERNAL);
317 return;
318 }
319 e.printStackTrace();
320 ERR(StringId.CLI_ERR, StringId.CLI_ERR, ERR_INTERNAL);
321 return;
322 }
323 break;
324 }
325 case I18N: {
326 try {
327 transService.updateFile(dir);
328 } catch (IOException e) {
329 ERR(StringId.CLI_ERR, StringId.CLI_ERR_CANNOT_CREATE_LANG,
330 ERR_INTERNAL);
331 return;
332 }
333 break;
334 }
335 case LOAD_PHOTO: {
336 for (String file : files) {
337 try {
338 Card card = getCard(file, null).getCard();
339 for (Contact contact : card) {
340 String filename = contact.toString(format, "");
341 File f = new File(dir, filename);
342
343 if (f.exists()) {
344 System.out.println("Loading " + f);
345 try {
346 String type = "jpeg";
347 int dotIndex = filename.indexOf('.');
348 if (dotIndex >= 0
349 && (dotIndex + 1) < filename.length()) {
350 type = filename.substring(dotIndex + 1)
351 .toLowerCase();
352 }
353
354 String b64;
355 Image img = new Image(IOUtils.toByteArray(f));
356 try {
357 b64 = img.toBase64();
358 } finally {
359 img.close();
360 }
361
362 // remove previous photos:
363 for (Data photo = contact
364 .getPreferredData("PHOTO"); photo != null; photo = contact
365 .getPreferredData("PHOTO")) {
366 photo.delete();
367 }
368 //
369
370 List<TypeInfo> types = new LinkedList<TypeInfo>();
371 types.add(new TypeInfo("ENCODING", "b"));
372 types.add(new TypeInfo("TYPE", type));
373 Data photo = new Data(types, "PHOTO", b64, null);
374 contact.add(photo);
375 } catch (IOException e) {
376 System.err.println("Cannot read photo: "
377 + filename);
378 }
379 }
380 }
381 card.save();
382 } catch (IOException e) {
383 System.err
384 .println(trans(StringId.CLI_ERR_CANNOT_OPEN, file));
385 }
386 }
387 break;
388 }
389 case SAVE_PHOTO: {
390 for (String file : files) {
391 try {
392 Card card = getCard(file, null).getCard();
393 for (Contact contact : card) {
394 Data photo = contact.getPreferredData("PHOTO");
395 if (photo != null) {
396 String filename = contact.toString(format, "");
397 File f = new File(dir, filename + ".png");
398 System.out.println("Saving " + f);
399 Image img = new Image(photo.getValue());
400 try {
401 ImageUtils.getInstance().saveAsImage(img, f,
402 "png");
403 } catch (IOException e) {
404 System.err.println(trans(
405 StringId.CLI_ERR_CANNOT_SAVE_PHOTO,
406 contact.getPreferredDataValue("FN")));
407 }
408 }
409 }
410 } catch (IOException e) {
411 System.err
412 .println(trans(StringId.CLI_ERR_CANNOT_OPEN, file));
413 }
414 }
415 break;
416 }
417 case CONTACT_MANAGER: {
418 try {
419 Optional.startTui(textMode, files);
420 } catch (IOException e) {
421 ERR(StringId.CLI_ERR, StringId.CLI_ERR_CANNOT_START,
422 ERR_NO_FILE);
423 return;
424 } catch (NotSupportedException e) {
425 if (!e.isCompiledIn()) {
426 ERR(StringId.CLI_ERR, StringId.CLI_ERR_NO_TUI, ERR_INTERNAL);
427 return;
428 }
429 e.printStackTrace();
430 ERR(StringId.CLI_ERR, StringId.CLI_ERR, ERR_INTERNAL);
431 return;
432 }
433 break;
434 }
435 case SAVE_TO: {
436 try {
437 Card total = new Card(null, getCardFormat(output));
438
439 for (String file : files) {
440 try {
441 Card card = getCard(file, null).getCard();
442 card.unlink();
443 while (card.size() > 0) {
444 total.add(card.remove(0));
445 }
446 } catch (IOException e) {
447 System.err.println(trans(StringId.CLI_ERR_CANNOT_OPEN,
448 file));
449 }
450 }
451
452 total.saveAs(new File(output), getCardFormat(output));
453 } catch (IOException e) {
454 System.err.println(trans(StringId.CLI_ERR_CANNOT_OPEN, output));
455 }
456
457 break;
458 }
459 case HELP: {
460 System.out.println(APPLICATION_TITLE + " "
461 + Version.getCurrentVersion());
462 System.out.println();
463
464 System.out.println(trans(StringId.CLI_HELP));
465 System.out.println();
466
467 System.out.println(trans(StringId.CLI_HELP_MODES));
468 System.out.println("\t--help : "
469 + trans(StringId.CLI_HELP_MODE_HELP));
470 System.out.println("\t(--tui|--gui) (--noutf) ... : "
471 + trans(StringId.CLI_HELP_MODE_CONTACT_MANAGER));
472 System.out.println("\t--server PORT ... : "
473 + trans(StringId.CLI_HELP_MODE_SERVER));
474 System.out.println("\t--save-config DIR : "
475 + trans(StringId.CLI_HELP_MODE_SAVE_CONFIG));
476 System.out.println("\t--i18n DIR ---lang LANG : "
477 + trans(StringId.CLI_HELP_MODE_I18N));
478 System.out.println("\t--load-photo DIR FORMAT ... : "
479 + trans(StringId.CLI_HELP_MODE_LOAD_PHOTO));
480 System.out.println("\t--save-photo DIR FORMAT ... : "
481 + trans(StringId.CLI_HELP_MODE_SAVE_PHOTO));
482 System.out.println("\t--save-to output(.vcf) ... : "
483 + trans(StringId.CLI_HELP_MODE_SAVE_TO));
484 System.out.println();
485
486 System.out.println(trans(StringId.CLI_HELP_OPTIONS));
487 System.out.println("\t-- : " + trans(StringId.CLI_HELP_DD));
488 System.out.println("\t--lang LANG : "
489 + trans(StringId.CLI_HELP_LANG));
490 System.out.println("\t--tui : " + trans(StringId.CLI_HELP_TUI));
491 System.out.println("\t--gui : " + trans(StringId.CLI_HELP_GUI));
492 System.out.println("\t--noutf : "
493 + trans(StringId.CLI_HELP_NOUTF_OPTION));
494 System.out.println("\t--config : "
495 + trans(StringId.CLI_HELP_CONFIG));
496 System.out.println();
497
498 System.out.println(trans(StringId.CLI_HELP_FOOTER));
499 System.out.println();
500
501 break;
502 }
503 }
504 }
505
506 /**
507 * Return the {@link Card} corresponding to the given resource name -- a
508 * file or a remote jvcard URL.
509 *
510 * <p>
511 * Will also fix the FN if required (see display.properties).
512 * </p>
513 *
514 * @param input
515 * a filename or a remote jvcard url with named resource (e.g.:
516 * <tt>jvcard://localhost:4444/coworkers.vcf</tt>)
517 * @param callback
518 * the {@link MergeCallback} to call in case of conflict, or NULL
519 * to disallow conflict management (the {@link Card} will not be
520 * allowed to synchronise in case of conflicts)
521 *
522 * @return the {@link Card}
523 *
524 * @throws IOException
525 * in case of IO error or remoting not available
526 */
527 static public CardResult getCard(String input, MergeCallback callback)
528 throws IOException {
529 boolean remote = isFileRemote(input);
530 Format format = getCardFormat(input);
531
532 CardResult card = null;
533 try {
534 if (remote) {
535 card = Optional.syncCard(input, callback);
536 } else {
537 card = new CardResult(new Card(new File(input), format), false,
538 false, false);
539 }
540 } catch (IOException ioe) {
541 throw ioe;
542 } catch (NotSupportedException e) {
543 throw new IOException("Remoting support not available", e);
544 }
545
546 // Fix the FN value
547 if (defaultFn != null) {
548 try {
549 for (Contact contact : card.getCard()) {
550 Data name = contact.getPreferredData("FN");
551 Data n = contact.getPreferredData("N");
552 boolean hasN = n != null && n.getValue().length() > 0;
553 if (name == null || name.getValue().length() == 0
554 || (forceComputedFn && hasN)) {
555 name.setValue(contact.toString(defaultFn, "").trim());
556 }
557 }
558 } catch (Exception e) {
559 // sync failed -> getCard() throws.
560 // do not update.
561 }
562 }
563
564 return card;
565 }
566
567 static private boolean isFileRemote(String input) {
568 return input.contains("://");
569 }
570
571 static Format getCardFormat(String input) {
572 if (isFileRemote(input)) {
573 return Format.VCard21;
574 }
575
576 Format format = Format.Abook;
577 String ext = input;
578 if (ext.contains(".")) {
579 String tab[] = ext.split("\\.");
580 if (tab.length > 1 && tab[tab.length - 1].equalsIgnoreCase("vcf")) {
581 format = Format.VCard21;
582 }
583 }
584
585 return format;
586 }
587
588 /**
589 * Open the given path and add all its files if it is a directory or just
590 * this one if not to the returned list.
591 *
592 * @param path
593 * the path to open
594 *
595 * @return the list of opened files
596 */
597 static private List<String> open(String path) {
598 List<String> files = new LinkedList<String>();
599
600 if (path != null && path.startsWith("jvcard://")) {
601 if (path.endsWith("/")) {
602 files.addAll(list(path));
603 } else {
604 files.add(path);
605 }
606 } else {
607 File file = new File(path);
608 if (file.exists()) {
609 if (file.isDirectory()) {
610 for (File subfile : file.listFiles()) {
611 if (!subfile.isDirectory())
612 files.add(subfile.getAbsolutePath());
613 }
614 } else {
615 files.add(file.getAbsolutePath());
616 }
617 } else {
618 System.err.println("File or directory not found: \"" + path
619 + "\"");
620 }
621 }
622
623 return files;
624 }
625
626 /**
627 * List all the available {@link Card}s on the given network location (which
628 * is expected to be a jVCard remote server, obviously).
629 *
630 * @param path
631 * the jVCard remote server path (e.g.:
632 * <tt>jvcard://localhost:4444/</tt>)
633 *
634 * @return the list of {@link Card}s
635 */
636 static private List<String> list(String path) {
637 List<String> files = new LinkedList<String>();
638
639 try {
640 String host = path.split("\\:")[1].substring(2);
641 int port = Integer.parseInt(path.split("\\:")[2].replaceAll("/$",
642 ""));
643 SimpleSocket s = new SimpleSocket(new Socket(host, port),
644 "sync client");
645 s.open(true);
646
647 s.sendCommand(Command.LIST_CARD);
648 for (String p : s.receiveBlock()) {
649 files.add(path
650 + p.substring(StringUtils.fromTime(0).length() + 1));
651 }
652 s.close();
653 } catch (Exception e) {
654 e.printStackTrace();
655 }
656
657 return files;
658 }
659
660 /**
661 * Really, really ask for UTF-8 encoding.
662 */
663 static private void utf8() {
664 try {
665 System.setProperty("file.encoding", "UTF-8");
666 Field charset = Charset.class.getDeclaredField("defaultCharset");
667 charset.setAccessible(true);
668 charset.set(null, null);
669 } catch (SecurityException e) {
670 } catch (NoSuchFieldException e) {
671 } catch (IllegalArgumentException e) {
672 } catch (IllegalAccessException e) {
673 }
674 }
675
676 /**
677 * Read display.properties to know if we should fix the FN field when empty,
678 * or always, or never.
679 */
680 static private void readNFN() {
681 DisplayBundle map = new DisplayBundle();
682
683 defaultFn = map.getString(DisplayOption.CONTACT_DETAILS_DEFAULT_FN);
684
685 forceComputedFn = map.getBoolean(
686 DisplayOption.CONTACT_DETAILS_SHOW_COMPUTED_FN, false);
687 }
688
689 /**
690 * Syntax error detected, closing the application with an error message.
691 *
692 * @param err
693 * the syntax error case
694 */
695 static private void SERR(StringId err, Object... values) {
696 ERR(StringId.CLI_SERR, err, ERR_SYNTAX, values);
697 }
698
699 /**
700 * Error detected, closing the application with an error message.
701 *
702 * @param err
703 * the error case
704 * @param suberr
705 * the suberror or NULL if none
706 * @param CODE
707 * the error code as declared above
708 */
709 static private void ERR(StringId err, StringId suberr, int CODE,
710 Object... subvalues) {
711 if (suberr == null)
712 System.err.println(trans(err));
713 else
714 System.err.println(trans(err, trans(suberr, subvalues)));
715
716 System.err.flush();
717 System.exit(CODE);
718 }
719 }