1 package be
.nikiroo
.jvcard
.launcher
;
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
;
11 import javax
.imageio
.ImageIO
;
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
;
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
39 static public final String APPLICATION_TITLE
= "jVcard";
40 static public final String APPLICATION_VERSION
= "1.0-dev";
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
;
47 static private String defaultFn
;
48 static private boolean forceComputedFn
;
51 CONTACT_MANAGER
, I18N
, SERVER
, LOAD_PHOTO
, SAVE_PHOTO
, SAVE_CONFIG
, HELP
55 * Translate the given {@link StringId} into user text.
60 * the values to insert instead of the place holders in the
63 * @return the translated text with the given value where required
65 static public String
trans(StringId id
, Object
... values
) {
66 return transService
.getString(id
, values
);
70 * Check if unicode characters should be used.
72 * @return TRUE to allow unicode
74 static public boolean isUnicode() {
75 return transService
.isUnicode();
79 * Start the application.
82 * The returned exit codes are:
84 * <li>1: no files to open</li>
85 * <li>2: invalid syntax</li>
86 * <li>3: internal error</li>
91 * the parameters (see <tt>--help</tt> to know which are
94 public static void main(String
[] args
) {
95 Boolean textMode
= null;
96 boolean noMoreParams
= false;
97 boolean filesTried
= false;
99 // get the "system default" language to help translate the --help
101 String language
= null;
102 transService
= new TransBundle(language
);
104 boolean unicode
= transService
.isUnicode();
106 List
<String
> files
= new LinkedList
<String
>();
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("--")) {
114 } else if (!noMoreParams
&& arg
.equals("--help")) {
115 if (mode
!= Mode
.CONTACT_MANAGER
) {
116 SERR(StringId
.CLI_SERR_MODES
);
120 } else if (!noMoreParams
&& arg
.equals("--tui")) {
122 } else if (!noMoreParams
&& arg
.equals("--gui")) {
124 } else if (!noMoreParams
&& arg
.equals("--noutf")) {
126 transService
.setUnicode(unicode
);
127 } else if (!noMoreParams
&& arg
.equals("--lang")) {
129 if (index
>= args
.length
) {
130 SERR(StringId
.CLI_SERR_NOLANG
);
134 language
= args
[index
];
135 transService
= new TransBundle(language
);
136 transService
.setUnicode(unicode
);
137 } else if (!noMoreParams
&& arg
.equals("--config")) {
139 if (index
>= args
.length
) {
140 SERR(StringId
.CLI_SERR_NODIR
);
144 Bundles
.setDirectory(args
[index
]);
145 transService
= new TransBundle(language
);
146 transService
.setUnicode(unicode
);
147 } else if (!noMoreParams
&& arg
.equals("--save-config")) {
149 if (index
>= args
.length
) {
150 SERR(StringId
.CLI_SERR_NODIR
);
155 if (mode
!= Mode
.CONTACT_MANAGER
) {
156 SERR(StringId
.CLI_SERR_MODES
);
159 mode
= Mode
.SAVE_CONFIG
;
160 } else if (!noMoreParams
&& arg
.equals("--server")) {
161 if (mode
!= Mode
.CONTACT_MANAGER
) {
162 SERR(StringId
.CLI_SERR_MODES
);
168 if (index
>= args
.length
) {
169 SERR(StringId
.CLI_SERR_NOPORT
);
174 port
= Integer
.parseInt(args
[index
]);
175 } catch (NumberFormatException e
) {
176 SERR(StringId
.CLI_SERR_BADPORT
, "" + args
[index
]);
179 } else if (!noMoreParams
&& arg
.equals("--i18n")) {
180 if (mode
!= Mode
.CONTACT_MANAGER
) {
181 SERR(StringId
.CLI_SERR_MODES
);
187 if (index
>= args
.length
) {
188 SERR(StringId
.CLI_SERR_NODIR
);
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
);
202 if (arg
.equals("--load-photo")) {
203 mode
= Mode
.LOAD_PHOTO
;
204 } else if (arg
.equals("--save-photo")) {
205 mode
= Mode
.SAVE_PHOTO
;
209 if (index
>= args
.length
) {
210 SERR(StringId
.CLI_SERR_NODIR
);
217 if (index
>= args
.length
) {
218 SERR(StringId
.CLI_SERR_NOFORMAT
);
222 format
= args
[index
];
225 files
.addAll(open(arg
));
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");
239 // N/FN fix information:
243 if (mode
== Mode
.SERVER
&& files
.size() > 0) {
244 SERR(StringId
.CLI_SERR_NOLANG
, "--server");
246 } else if (mode
== Mode
.I18N
&& files
.size() > 0) {
247 SERR(StringId
.CLI_SERR_NOLANG
, "--i18n");
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("."));
257 if (files
.size() == 0) {
258 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_NOFILES
, ERR_NO_FILE
);
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
));
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
) {
282 System
.exit(ERR_INTERNAL
);
288 Optional
.runServer(port
);
289 } catch (Exception e
) {
290 if (e
instanceof IOException
) {
291 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_CANNOT_START
,
294 } else if (e
instanceof ClassNotFoundException
) {
295 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_NO_REMOTING
,
300 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR
, ERR_INTERNAL
);
308 transService
.updateFile(dir
);
309 } catch (IOException e
) {
310 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_CANNOT_CREATE_LANG
,
317 for (String file
: files
) {
319 Card card
= getCard(file
, null).getCard();
320 for (Contact contact
: card
) {
321 String filename
= contact
.toString(format
, "");
322 File f
= new File(dir
, filename
);
325 System
.out
.println("Loading " + f
);
327 String type
= "jpeg";
328 int dotIndex
= filename
.indexOf('.');
330 && (dotIndex
+ 1) < filename
.length()) {
331 type
= filename
.substring(dotIndex
+ 1)
335 String b64
= StringUtils
.fromImage(f
);
337 // remove previous photos:
338 for (Data photo
= contact
339 .getPreferredData("PHOTO"); photo
!= null; photo
= contact
340 .getPreferredData("PHOTO")) {
345 List
<TypeInfo
> types
= new LinkedList
<TypeInfo
>();
346 types
.add(new TypeInfo("ENCODING", "b"));
347 types
.add(new TypeInfo("TYPE", type
));
348 Data photo
= new Data(types
, "PHOTO", b64
, null);
350 } catch (IOException e
) {
351 System
.err
.println("Cannot read photo: "
357 } catch (IOException e
) {
359 .println(trans(StringId
.CLI_ERR_CANNOT_OPEN
, file
));
365 for (String file
: files
) {
367 Card card
= getCard(file
, null).getCard();
368 for (Contact contact
: card
) {
369 Data photo
= contact
.getPreferredData("PHOTO");
371 String filename
= contact
.toString(format
, "");
372 File f
= new File(dir
, filename
+ ".png");
373 System
.out
.println("Saving " + f
);
376 StringUtils
.toImage(photo
.getValue()),
378 } catch (IOException e
) {
379 System
.err
.println(trans(
380 StringId
.CLI_ERR_CANNOT_SAVE_PHOTO
,
381 contact
.getPreferredDataValue("FN")));
385 } catch (IOException e
) {
387 .println(trans(StringId
.CLI_ERR_CANNOT_OPEN
, file
));
392 case CONTACT_MANAGER
: {
394 Optional
.startTui(textMode
, files
);
395 } catch (Exception e
) {
396 if (e
instanceof IOException
) {
397 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_CANNOT_START
,
400 } else if (e
instanceof ClassNotFoundException
) {
401 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_NO_TUI
, ERR_INTERNAL
);
405 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR
, ERR_INTERNAL
);
412 System
.out
.println(APPLICATION_TITLE
+ " " + APPLICATION_VERSION
);
413 System
.out
.println();
415 System
.out
.println(trans(StringId
.CLI_HELP
));
416 System
.out
.println();
418 System
.out
.println(trans(StringId
.CLI_HELP_MODES
));
419 System
.out
.println("\t--help : "
420 + trans(StringId
.CLI_HELP_MODE_HELP
));
421 System
.out
.println("\t(--tui|--gui) (--noutf) ... : "
422 + trans(StringId
.CLI_HELP_MODE_CONTACT_MANAGER
));
423 System
.out
.println("\t--server PORT ... : "
424 + trans(StringId
.CLI_HELP_MODE_SERVER
));
425 System
.out
.println("\t--save-config DIR : "
426 + trans(StringId
.CLI_HELP_MODE_SAVE_CONFIG
));
427 System
.out
.println("\t--i18n DIR ---lang LANG : "
428 + trans(StringId
.CLI_HELP_MODE_I18N
));
429 System
.out
.println("\t--load-photo DIR FORMAT ... : "
430 + trans(StringId
.CLI_HELP_MODE_LOAD_PHOTO
));
431 System
.out
.println("\t--save-photo DIR FORMAT ... : "
432 + trans(StringId
.CLI_HELP_MODE_SAVE_PHOTO
));
433 System
.out
.println();
435 System
.out
.println(trans(StringId
.CLI_HELP_OPTIONS
));
436 System
.out
.println("\t-- : " + trans(StringId
.CLI_HELP_DD
));
437 System
.out
.println("\t--lang LANG : "
438 + trans(StringId
.CLI_HELP_LANG
));
439 System
.out
.println("\t--tui : " + trans(StringId
.CLI_HELP_TUI
));
440 System
.out
.println("\t--gui : " + trans(StringId
.CLI_HELP_GUI
));
441 System
.out
.println("\t--noutf : "
442 + trans(StringId
.CLI_HELP_NOUTF_OPTION
));
443 System
.out
.println("\t--config : "
444 + trans(StringId
.CLI_HELP_CONFIG
));
445 System
.out
.println();
447 System
.out
.println(trans(StringId
.CLI_HELP_FOOTER
));
448 System
.out
.println();
455 * Return the {@link Card} corresponding to the given resource name -- a
456 * file or a remote jvcard URL.
459 * Will also fix the FN if required (see display.properties).
463 * a filename or a remote jvcard url with named resource (e.g.:
464 * <tt>jvcard://localhost:4444/coworkers.vcf</tt>)
466 * the {@link MergeCallback} to call in case of conflict, or NULL
467 * to disallow conflict management (the {@link Card} will not be
468 * allowed to synchronise in case of conflicts)
470 * @return the {@link Card}
472 * @throws IOException
473 * in case of IO error or remoting not available
475 static public CardResult
getCard(String input
, MergeCallback callback
)
477 boolean remote
= false;
478 Format format
= Format
.Abook
;
480 if (ext
.contains(".")) {
481 String tab
[] = ext
.split("\\.");
482 if (tab
.length
> 1 && tab
[tab
.length
- 1].equalsIgnoreCase("vcf")) {
483 format
= Format
.VCard21
;
487 if (input
.contains("://")) {
488 format
= Format
.VCard21
;
492 CardResult card
= null;
495 card
= Optional
.syncCard(input
, callback
);
497 card
= new CardResult(new Card(new File(input
), format
), false,
500 } catch (IOException ioe
) {
502 } catch (Exception e
) {
503 throw new IOException("Remoting support not available", e
);
507 if (defaultFn
!= null) {
509 for (Contact contact
: card
.getCard()) {
510 Data name
= contact
.getPreferredData("FN");
511 if (name
== null || name
.getValue().length() == 0
512 || forceComputedFn
) {
513 name
.setValue(contact
.toString(defaultFn
, "").trim());
516 } catch (Exception e
) {
517 // sync failed -> getCard() throws.
526 * Open the given path and add all its files if it is a directory or just
527 * this one if not to the returned list.
532 * @return the list of opened files
534 static private List
<String
> open(String path
) {
535 List
<String
> files
= new LinkedList
<String
>();
537 if (path
!= null && path
.startsWith("jvcard://")) {
538 if (path
.endsWith("/")) {
539 files
.addAll(list(path
));
544 File file
= new File(path
);
546 if (file
.isDirectory()) {
547 for (File subfile
: file
.listFiles()) {
548 if (!subfile
.isDirectory())
549 files
.add(subfile
.getAbsolutePath());
552 files
.add(file
.getAbsolutePath());
555 System
.err
.println("File or directory not found: \"" + path
564 * List all the available {@link Card}s on the given network location (which
565 * is expected to be a jVCard remote server, obviously).
568 * the jVCard remote server path (e.g.:
569 * <tt>jvcard://localhost:4444/</tt>)
571 * @return the list of {@link Card}s
573 static private List
<String
> list(String path
) {
574 List
<String
> files
= new LinkedList
<String
>();
577 String host
= path
.split("\\:")[1].substring(2);
578 int port
= Integer
.parseInt(path
.split("\\:")[2].replaceAll("/$",
580 SimpleSocket s
= new SimpleSocket(new Socket(host
, port
),
584 s
.sendCommand(Command
.LIST_CARD
);
585 for (String p
: s
.receiveBlock()) {
587 + p
.substring(StringUtils
.fromTime(0).length() + 1));
590 } catch (Exception e
) {
598 * Really, really ask for UTF-8 encoding.
600 static private void utf8() {
602 System
.setProperty("file.encoding", "UTF-8");
603 Field charset
= Charset
.class.getDeclaredField("defaultCharset");
604 charset
.setAccessible(true);
605 charset
.set(null, null);
606 } catch (SecurityException e
) {
607 } catch (NoSuchFieldException e
) {
608 } catch (IllegalArgumentException e
) {
609 } catch (IllegalAccessException e
) {
614 * Read display.properties to know if we should fix the FN field when empty,
615 * or always, or never.
617 static private void readNFN() {
618 DisplayBundle map
= new DisplayBundle();
620 defaultFn
= map
.getString(DisplayOption
.CONTACT_DETAILS_DEFAULT_FN
);
622 forceComputedFn
= map
.getBoolean(
623 DisplayOption
.CONTACT_DETAILS_SHOW_COMPUTED_FN
, false);
627 * Syntax error detected, closing the application with an error message.
630 * the syntax error case
632 static private void SERR(StringId err
, Object
... values
) {
633 ERR(StringId
.CLI_SERR
, err
, ERR_SYNTAX
, values
);
637 * Error detected, closing the application with an error message.
642 * the suberror or NULL if none
644 * the error code as declared above
646 static private void ERR(StringId err
, StringId suberr
, int CODE
,
647 Object
... subvalues
) {
649 System
.err
.println(trans(err
));
651 System
.err
.println(trans(err
, trans(suberr
, subvalues
)));