1 package be
.nikiroo
.jvcard
.launcher
;
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
;
13 import javax
.imageio
.ImageIO
;
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
;
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
43 static public final String APPLICATION_TITLE
= "jVcard";
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
;
50 static private String defaultFn
;
51 static private boolean forceComputedFn
;
54 CONTACT_MANAGER
, I18N
, SERVER
, LOAD_PHOTO
, SAVE_PHOTO
, SAVE_CONFIG
, HELP
, SAVE_TO
,
58 * Translate the given {@link StringId} into user text.
63 * the values to insert instead of the place holders in the
66 * @return the translated text with the given value where required
68 static public String
trans(StringId id
, Object
... values
) {
69 return transService
.getString(id
, values
);
73 * Check if unicode characters should be used.
75 * @return TRUE to allow unicode
77 static public boolean isUnicode() {
78 return transService
.isUnicode();
82 * Start the application.
85 * The returned exit codes are:
87 * <li>1: no files to open</li>
88 * <li>2: invalid syntax</li>
89 * <li>3: internal error</li>
94 * the parameters (see <tt>--help</tt> to know which are
97 public static void main(String
[] args
) {
98 Boolean textMode
= null;
99 boolean noMoreParams
= false;
100 boolean filesTried
= false;
102 // get the "system default" language to help translate the --help
104 String language
= null;
105 transService
= new TransBundle(language
);
107 boolean unicode
= transService
.isUnicode();
109 List
<String
> files
= new LinkedList
<String
>();
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("--")) {
118 } else if (!noMoreParams
&& arg
.equals("--help")) {
119 if (mode
!= Mode
.CONTACT_MANAGER
) {
120 SERR(StringId
.CLI_SERR_MODES
);
124 } else if (!noMoreParams
&& arg
.equals("--tui")) {
126 } else if (!noMoreParams
&& arg
.equals("--gui")) {
128 } else if (!noMoreParams
&& arg
.equals("--noutf")) {
130 transService
.setUnicode(unicode
);
131 } else if (!noMoreParams
&& arg
.equals("--lang")) {
133 if (index
>= args
.length
) {
134 SERR(StringId
.CLI_SERR_NOLANG
);
138 language
= args
[index
];
139 transService
= new TransBundle(language
);
140 transService
.setUnicode(unicode
);
141 } else if (!noMoreParams
&& arg
.equals("--config")) {
143 if (index
>= args
.length
) {
144 SERR(StringId
.CLI_SERR_NODIR
);
148 Bundles
.setDirectory(args
[index
]);
149 transService
= new TransBundle(language
);
150 transService
.setUnicode(unicode
);
151 } else if (!noMoreParams
&& arg
.equals("--save-config")) {
153 if (index
>= args
.length
) {
154 SERR(StringId
.CLI_SERR_NODIR
);
159 if (mode
!= Mode
.CONTACT_MANAGER
) {
160 SERR(StringId
.CLI_SERR_MODES
);
163 mode
= Mode
.SAVE_CONFIG
;
164 } else if (!noMoreParams
&& arg
.equals("--server")) {
165 if (mode
!= Mode
.CONTACT_MANAGER
) {
166 SERR(StringId
.CLI_SERR_MODES
);
172 if (index
>= args
.length
) {
173 SERR(StringId
.CLI_SERR_NOPORT
);
178 port
= Integer
.parseInt(args
[index
]);
179 } catch (NumberFormatException e
) {
180 SERR(StringId
.CLI_SERR_BADPORT
, "" + args
[index
]);
183 } else if (!noMoreParams
&& arg
.equals("--i18n")) {
184 if (mode
!= Mode
.CONTACT_MANAGER
) {
185 SERR(StringId
.CLI_SERR_MODES
);
191 if (index
>= args
.length
) {
192 SERR(StringId
.CLI_SERR_NODIR
);
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
);
206 if (arg
.equals("--load-photo")) {
207 mode
= Mode
.LOAD_PHOTO
;
208 } else if (arg
.equals("--save-photo")) {
209 mode
= Mode
.SAVE_PHOTO
;
213 if (index
>= args
.length
) {
214 SERR(StringId
.CLI_SERR_NODIR
);
221 if (index
>= args
.length
) {
222 SERR(StringId
.CLI_SERR_NOFORMAT
);
226 format
= args
[index
];
227 } else if (!noMoreParams
&& (arg
.equals("--save-to"))) {
228 if (mode
!= Mode
.CONTACT_MANAGER
) {
229 SERR(StringId
.CLI_SERR_MODES
);
235 if (index
>= args
.length
) {
236 SERR(StringId
.CLI_ERR_NOFILES
);
240 output
= args
[index
];
243 files
.addAll(open(arg
));
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");
257 // N/FN fix information:
261 if (mode
== Mode
.SERVER
&& files
.size() > 0) {
262 SERR(StringId
.CLI_SERR_NOLANG
, "--server");
264 } else if (mode
== Mode
.I18N
&& files
.size() > 0) {
265 SERR(StringId
.CLI_SERR_NOLANG
, "--i18n");
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("."));
275 if (files
.size() == 0) {
276 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_NOFILES
, ERR_NO_FILE
);
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
));
292 new TransBundle().updateFile(dir
); // default locale
293 for (String lang
: new TransBundle().getKnownLanguages()) {
294 new TransBundle(lang
).updateFile(dir
);
297 // new UIColors().updateFile(dir);
298 new DisplayBundle().updateFile(dir
);
299 new RemoteBundle().updateFile(dir
);
300 } catch (IOException e
) {
303 System
.exit(ERR_INTERNAL
);
309 Optional
.runServer(port
);
310 } catch (IOException e
) {
311 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_CANNOT_START
,
314 } catch (NotSupportedException e
) {
315 if (!e
.isCompiledIn()) {
316 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_NO_REMOTING
,
321 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR
, ERR_INTERNAL
);
328 transService
.updateFile(dir
);
329 } catch (IOException e
) {
330 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_CANNOT_CREATE_LANG
,
337 for (String file
: files
) {
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
);
345 System
.out
.println("Loading " + f
);
347 String type
= "jpeg";
348 int dotIndex
= filename
.indexOf('.');
350 && (dotIndex
+ 1) < filename
.length()) {
351 type
= filename
.substring(dotIndex
+ 1)
356 InputStream in
= null;
358 in
= new FileInputStream(f
);
359 b64
= ImageUtils
.toBase64(in
);
366 // remove previous photos:
367 for (Data photo
= contact
368 .getPreferredData("PHOTO"); photo
!= null; photo
= contact
369 .getPreferredData("PHOTO")) {
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);
379 } catch (IOException e
) {
380 System
.err
.println("Cannot read photo: "
386 } catch (IOException e
) {
388 .println(trans(StringId
.CLI_ERR_CANNOT_OPEN
, file
));
394 for (String file
: files
) {
396 Card card
= getCard(file
, null).getCard();
397 for (Contact contact
: card
) {
398 Data photo
= contact
.getPreferredData("PHOTO");
400 String filename
= contact
.toString(format
, "");
401 File f
= new File(dir
, filename
+ ".png");
402 System
.out
.println("Saving " + f
);
405 ImageUtils
.fromBase64(photo
.getValue()),
407 } catch (IOException e
) {
408 System
.err
.println(trans(
409 StringId
.CLI_ERR_CANNOT_SAVE_PHOTO
,
410 contact
.getPreferredDataValue("FN")));
414 } catch (IOException e
) {
416 .println(trans(StringId
.CLI_ERR_CANNOT_OPEN
, file
));
421 case CONTACT_MANAGER
: {
423 Optional
.startTui(textMode
, files
);
424 } catch (IOException e
) {
425 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_CANNOT_START
,
428 } catch (NotSupportedException e
) {
429 if (!e
.isCompiledIn()) {
430 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_NO_TUI
, ERR_INTERNAL
);
434 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR
, ERR_INTERNAL
);
441 Card total
= new Card(null, getCardFormat(output
));
443 for (String file
: files
) {
445 Card card
= getCard(file
, null).getCard();
447 while (card
.size() > 0) {
448 total
.add(card
.remove(0));
450 } catch (IOException e
) {
451 System
.err
.println(trans(StringId
.CLI_ERR_CANNOT_OPEN
,
456 total
.saveAs(new File(output
), getCardFormat(output
));
457 } catch (IOException e
) {
458 System
.err
.println(trans(StringId
.CLI_ERR_CANNOT_OPEN
, output
));
464 System
.out
.println(APPLICATION_TITLE
+ " "
465 + Version
.getCurrentVersion());
466 System
.out
.println();
468 System
.out
.println(trans(StringId
.CLI_HELP
));
469 System
.out
.println();
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();
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();
502 System
.out
.println(trans(StringId
.CLI_HELP_FOOTER
));
503 System
.out
.println();
511 * Return the {@link Card} corresponding to the given resource name -- a
512 * file or a remote jvcard URL.
515 * Will also fix the FN if required (see display.properties).
519 * a filename or a remote jvcard url with named resource (e.g.:
520 * <tt>jvcard://localhost:4444/coworkers.vcf</tt>)
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)
526 * @return the {@link Card}
528 * @throws IOException
529 * in case of IO error or remoting not available
531 static public CardResult
getCard(String input
, MergeCallback callback
)
533 boolean remote
= isFileRemote(input
);
534 Format format
= getCardFormat(input
);
536 CardResult card
= null;
539 card
= Optional
.syncCard(input
, callback
);
541 card
= new CardResult(new Card(new File(input
), format
), false,
544 } catch (IOException ioe
) {
546 } catch (NotSupportedException e
) {
547 throw new IOException("Remoting support not available", e
);
551 if (defaultFn
!= null) {
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());
562 } catch (Exception e
) {
563 // sync failed -> getCard() throws.
571 static private boolean isFileRemote(String input
) {
572 return input
.contains("://");
575 static Format
getCardFormat(String input
) {
576 if (isFileRemote(input
)) {
577 return Format
.VCard21
;
580 Format format
= Format
.Abook
;
582 if (ext
.contains(".")) {
583 String tab
[] = ext
.split("\\.");
584 if (tab
.length
> 1 && tab
[tab
.length
- 1].equalsIgnoreCase("vcf")) {
585 format
= Format
.VCard21
;
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.
599 * @return the list of opened files
601 static private List
<String
> open(String path
) {
602 List
<String
> files
= new LinkedList
<String
>();
604 if (path
!= null && path
.startsWith("jvcard://")) {
605 if (path
.endsWith("/")) {
606 files
.addAll(list(path
));
611 File file
= new File(path
);
613 if (file
.isDirectory()) {
614 for (File subfile
: file
.listFiles()) {
615 if (!subfile
.isDirectory())
616 files
.add(subfile
.getAbsolutePath());
619 files
.add(file
.getAbsolutePath());
622 System
.err
.println("File or directory not found: \"" + path
631 * List all the available {@link Card}s on the given network location (which
632 * is expected to be a jVCard remote server, obviously).
635 * the jVCard remote server path (e.g.:
636 * <tt>jvcard://localhost:4444/</tt>)
638 * @return the list of {@link Card}s
640 static private List
<String
> list(String path
) {
641 List
<String
> files
= new LinkedList
<String
>();
644 String host
= path
.split("\\:")[1].substring(2);
645 int port
= Integer
.parseInt(path
.split("\\:")[2].replaceAll("/$",
647 SimpleSocket s
= new SimpleSocket(new Socket(host
, port
),
651 s
.sendCommand(Command
.LIST_CARD
);
652 for (String p
: s
.receiveBlock()) {
654 + p
.substring(StringUtils
.fromTime(0).length() + 1));
657 } catch (Exception e
) {
665 * Really, really ask for UTF-8 encoding.
667 static private void utf8() {
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
) {
681 * Read display.properties to know if we should fix the FN field when empty,
682 * or always, or never.
684 static private void readNFN() {
685 DisplayBundle map
= new DisplayBundle();
687 defaultFn
= map
.getString(DisplayOption
.CONTACT_DETAILS_DEFAULT_FN
);
689 forceComputedFn
= map
.getBoolean(
690 DisplayOption
.CONTACT_DETAILS_SHOW_COMPUTED_FN
, false);
694 * Syntax error detected, closing the application with an error message.
697 * the syntax error case
699 static private void SERR(StringId err
, Object
... values
) {
700 ERR(StringId
.CLI_SERR
, err
, ERR_SYNTAX
, values
);
704 * Error detected, closing the application with an error message.
709 * the suberror or NULL if none
711 * the error code as declared above
713 static private void ERR(StringId err
, StringId suberr
, int CODE
,
714 Object
... subvalues
) {
716 System
.err
.println(trans(err
));
718 System
.err
.println(trans(err
, trans(suberr
, subvalues
)));