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