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
) {
292 .println("I/O Exception: Cannot start the server");
294 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_NO_REMOTING
,
303 transService
.updateFile(dir
);
304 } catch (IOException e
) {
305 System
.err
.println(trans(StringId
.CLI_ERR_CANNOT_CREATE_LANG
,
312 for (String file
: files
) {
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
);
320 System
.out
.println("Loading " + f
);
322 String type
= "jpeg";
323 int dotIndex
= filename
.indexOf('.');
325 && (dotIndex
+ 1) < filename
.length()) {
326 type
= filename
.substring(dotIndex
+ 1)
330 String b64
= StringUtils
.fromImage(f
);
332 // remove previous photos:
333 for (Data photo
= contact
334 .getPreferredData("PHOTO"); photo
!= null; photo
= contact
335 .getPreferredData("PHOTO")) {
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);
345 } catch (IOException e
) {
346 System
.err
.println("Cannot read photo: "
352 } catch (IOException e
) {
354 .println(trans(StringId
.CLI_ERR_CANNOT_OPEN
, file
));
360 for (String file
: files
) {
362 Card card
= getCard(file
, null).getCard();
363 for (Contact contact
: card
) {
364 Data photo
= contact
.getPreferredData("PHOTO");
366 String filename
= contact
.toString(format
, "");
367 File f
= new File(dir
, filename
+ ".png");
368 System
.out
.println("Saving " + f
);
371 StringUtils
.toImage(photo
.getValue()),
373 } catch (IOException e
) {
374 System
.err
.println(trans(
375 StringId
.CLI_ERR_CANNOT_SAVE_PHOTO
,
376 contact
.getPreferredDataValue("FN")));
380 } catch (IOException e
) {
382 .println(trans(StringId
.CLI_ERR_CANNOT_OPEN
, file
));
387 case CONTACT_MANAGER
: {
389 Optional
.startTui(textMode
, files
);
390 } catch (Exception e
) {
391 if (e
instanceof IOException
) {
392 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_CANNOT_START
,
396 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_NO_TUI
, ERR_INTERNAL
);
403 System
.out
.println(APPLICATION_TITLE
+ " " + APPLICATION_VERSION
);
404 System
.out
.println();
406 System
.out
.println(trans(StringId
.CLI_HELP
));
407 System
.out
.println();
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();
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 : " + trans(StringId
.CLI_HELP_NOUTF_OPTION
));
433 System
.out
.println("\t--config : "
434 + trans(StringId
.CLI_HELP_CONFIG
));
435 System
.out
.println();
437 System
.out
.println(trans(StringId
.CLI_HELP_FOOTER
));
438 System
.out
.println();
445 * Return the {@link Card} corresponding to the given resource name -- a
446 * file or a remote jvcard URL.
449 * Will also fix the FN if required (see display.properties).
453 * a filename or a remote jvcard url with named resource (e.g.:
454 * <tt>jvcard://localhost:4444/coworkers.vcf</tt>)
456 * the {@link MergeCallback} to call in case of conflict, or NULL
457 * to disallow conflict management (the {@link Card} will not be
458 * allowed to synchronise in case of conflicts)
460 * @return the {@link Card}
462 * @throws IOException
463 * in case of IO error or remoting not available
465 static public CardResult
getCard(String input
, MergeCallback callback
)
467 boolean remote
= false;
468 Format format
= Format
.Abook
;
470 if (ext
.contains(".")) {
471 String tab
[] = ext
.split("\\.");
472 if (tab
.length
> 1 && tab
[tab
.length
- 1].equalsIgnoreCase("vcf")) {
473 format
= Format
.VCard21
;
477 if (input
.contains("://")) {
478 format
= Format
.VCard21
;
482 CardResult card
= null;
485 card
= Optional
.syncCard(input
, callback
);
487 card
= new CardResult(new Card(new File(input
), format
), false,
490 } catch (IOException ioe
) {
492 } catch (Exception e
) {
493 throw new IOException("Remoting support not available", e
);
497 if (defaultFn
!= null) {
499 for (Contact contact
: card
.getCard()) {
500 Data name
= contact
.getPreferredData("FN");
501 if (name
== null || name
.getValue().length() == 0
502 || forceComputedFn
) {
503 name
.setValue(contact
.toString(defaultFn
, ""));
506 } catch (Exception e
) {
507 // sync failed -> getCard() throws.
516 * Open the given path and add all its files if it is a directory or just
517 * this one if not to the returned list.
522 * @return the list of opened files
524 static private List
<String
> open(String path
) {
525 List
<String
> files
= new LinkedList
<String
>();
527 if (path
!= null && path
.startsWith("jvcard://")) {
528 if (path
.endsWith("/")) {
529 files
.addAll(list(path
));
534 File file
= new File(path
);
536 if (file
.isDirectory()) {
537 for (File subfile
: file
.listFiles()) {
538 if (!subfile
.isDirectory())
539 files
.add(subfile
.getAbsolutePath());
542 files
.add(file
.getAbsolutePath());
545 System
.err
.println("File or directory not found: \"" + path
554 * List all the available {@link Card}s on the given network location (which
555 * is expected to be a jVCard remote server, obviously).
558 * the jVCard remote server path (e.g.:
559 * <tt>jvcard://localhost:4444/</tt>)
561 * @return the list of {@link Card}s
563 static private List
<String
> list(String path
) {
564 List
<String
> files
= new LinkedList
<String
>();
567 String host
= path
.split("\\:")[1].substring(2);
568 int port
= Integer
.parseInt(path
.split("\\:")[2].replaceAll("/$",
570 SimpleSocket s
= new SimpleSocket(new Socket(host
, port
),
574 s
.sendCommand(Command
.LIST_CARD
);
575 for (String p
: s
.receiveBlock()) {
577 + p
.substring(StringUtils
.fromTime(0).length() + 1));
580 } catch (Exception e
) {
588 * Really, really ask for UTF-8 encoding.
590 static private void utf8() {
592 System
.setProperty("file.encoding", "UTF-8");
593 Field charset
= Charset
.class.getDeclaredField("defaultCharset");
594 charset
.setAccessible(true);
595 charset
.set(null, null);
596 } catch (SecurityException e
) {
597 } catch (NoSuchFieldException e
) {
598 } catch (IllegalArgumentException e
) {
599 } catch (IllegalAccessException e
) {
604 * Read display.properties to know if we should fix the FN field when empty,
605 * or always, or never.
607 static private void readNFN() {
608 DisplayBundle map
= new DisplayBundle();
610 defaultFn
= map
.getString(DisplayOption
.CONTACT_DETAILS_DEFAULT_FN
);
612 forceComputedFn
= map
.getBoolean(
613 DisplayOption
.CONTACT_DETAILS_SHOW_COMPUTED_FN
, false);
617 * Syntax error detected, closing the application with an error message.
620 * the syntax error case
622 static private void SERR(StringId err
, Object
... values
) {
623 ERR(StringId
.CLI_SERR
, err
, ERR_SYNTAX
, values
);
627 * Error detected, closing the application with an error message.
632 * the suberror or NULL if none
634 * the error code as declared above
636 static private void ERR(StringId err
, StringId suberr
, int CODE
,
637 Object
... subvalues
) {
639 System
.err
.println(trans(err
));
641 System
.err
.println(trans(err
, trans(suberr
, subvalues
)));