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