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
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 for (int index
= 0; index
< args
.length
; index
++) {
114 String arg
= args
[index
];
115 if (!noMoreParams
&& arg
.equals("--")) {
117 } else if (!noMoreParams
&& arg
.equals("--help")) {
118 if (mode
!= Mode
.CONTACT_MANAGER
) {
119 SERR(StringId
.CLI_SERR_MODES
);
123 } else if (!noMoreParams
&& arg
.equals("--tui")) {
125 } else if (!noMoreParams
&& arg
.equals("--gui")) {
127 } else if (!noMoreParams
&& arg
.equals("--noutf")) {
129 transService
.setUnicode(unicode
);
130 } else if (!noMoreParams
&& arg
.equals("--lang")) {
132 if (index
>= args
.length
) {
133 SERR(StringId
.CLI_SERR_NOLANG
);
137 language
= args
[index
];
138 transService
= new TransBundle(language
);
139 transService
.setUnicode(unicode
);
140 } else if (!noMoreParams
&& arg
.equals("--config")) {
142 if (index
>= args
.length
) {
143 SERR(StringId
.CLI_SERR_NODIR
);
147 Bundles
.setDirectory(args
[index
]);
148 transService
= new TransBundle(language
);
149 transService
.setUnicode(unicode
);
150 } else if (!noMoreParams
&& arg
.equals("--save-config")) {
152 if (index
>= args
.length
) {
153 SERR(StringId
.CLI_SERR_NODIR
);
158 if (mode
!= Mode
.CONTACT_MANAGER
) {
159 SERR(StringId
.CLI_SERR_MODES
);
162 mode
= Mode
.SAVE_CONFIG
;
163 } else if (!noMoreParams
&& arg
.equals("--server")) {
164 if (mode
!= Mode
.CONTACT_MANAGER
) {
165 SERR(StringId
.CLI_SERR_MODES
);
171 if (index
>= args
.length
) {
172 SERR(StringId
.CLI_SERR_NOPORT
);
177 port
= Integer
.parseInt(args
[index
]);
178 } catch (NumberFormatException e
) {
179 SERR(StringId
.CLI_SERR_BADPORT
, "" + args
[index
]);
182 } else if (!noMoreParams
&& arg
.equals("--i18n")) {
183 if (mode
!= Mode
.CONTACT_MANAGER
) {
184 SERR(StringId
.CLI_SERR_MODES
);
190 if (index
>= args
.length
) {
191 SERR(StringId
.CLI_SERR_NODIR
);
196 } else if (!noMoreParams
197 && (arg
.equals("--load-photo")
198 || arg
.equals("--save-photo") || arg
199 .equals("--only-photo"))) {
200 if (mode
!= Mode
.CONTACT_MANAGER
) {
201 SERR(StringId
.CLI_SERR_MODES
);
205 if (arg
.equals("--load-photo")) {
206 mode
= Mode
.LOAD_PHOTO
;
207 } else if (arg
.equals("--save-photo")) {
208 mode
= Mode
.SAVE_PHOTO
;
212 if (index
>= args
.length
) {
213 SERR(StringId
.CLI_SERR_NODIR
);
220 if (index
>= args
.length
) {
221 SERR(StringId
.CLI_SERR_NOFORMAT
);
225 format
= args
[index
];
228 files
.addAll(open(arg
));
232 // Force headless mode if we run in forced-text mode
233 if (mode
!= Mode
.CONTACT_MANAGER
|| (textMode
!= null && textMode
)) {
234 // same as -Djava.awt.headless=true
235 System
.setProperty("java.awt.headless", "true");
242 // N/FN fix information:
246 if (mode
== Mode
.SERVER
&& files
.size() > 0) {
247 SERR(StringId
.CLI_SERR_NOLANG
, "--server");
249 } else if (mode
== Mode
.I18N
&& files
.size() > 0) {
250 SERR(StringId
.CLI_SERR_NOLANG
, "--i18n");
252 } else if (mode
== Mode
.I18N
&& language
== null) {
253 SERR(StringId
.CLI_SERR_NOLANG
);
254 } else if ((mode
== Mode
.CONTACT_MANAGER
|| mode
== Mode
.SAVE_PHOTO
|| mode
== Mode
.LOAD_PHOTO
)
255 && files
.size() == 0) {
256 if (files
.size() == 0 && !filesTried
) {
257 files
.addAll(open("."));
260 if (files
.size() == 0) {
261 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_NOFILES
, ERR_NO_FILE
);
270 if (!new File(dir
).isDirectory()) {
271 if (!new File(dir
).mkdir()) {
272 System
.err
.println(trans(
273 StringId
.CLI_ERR_CANNOT_CREATE_CONFDIR
, dir
));
277 new TransBundle().updateFile(dir
); // default locale
278 for (String lang
: new TransBundle().getKnownLanguages()) {
279 new TransBundle(lang
).updateFile(dir
);
282 // new UIColors().updateFile(dir);
283 new DisplayBundle().updateFile(dir
);
284 new RemoteBundle().updateFile(dir
);
285 } catch (IOException e
) {
288 System
.exit(ERR_INTERNAL
);
294 Optional
.runServer(port
);
295 } catch (IOException e
) {
296 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_CANNOT_START
,
299 } catch (NotSupportedException e
) {
300 if (!e
.isCompiledIn()) {
301 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_NO_REMOTING
,
306 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR
, ERR_INTERNAL
);
314 transService
.updateFile(dir
);
315 } catch (IOException e
) {
316 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_CANNOT_CREATE_LANG
,
323 for (String file
: files
) {
325 Card card
= getCard(file
, null).getCard();
326 for (Contact contact
: card
) {
327 String filename
= contact
.toString(format
, "");
328 File f
= new File(dir
, filename
);
331 System
.out
.println("Loading " + f
);
333 String type
= "jpeg";
334 int dotIndex
= filename
.indexOf('.');
336 && (dotIndex
+ 1) < filename
.length()) {
337 type
= filename
.substring(dotIndex
+ 1)
342 InputStream in
= null;
344 in
= new FileInputStream(f
);
345 b64
= ImageUtils
.toBase64(in
);
352 // remove previous photos:
353 for (Data photo
= contact
354 .getPreferredData("PHOTO"); photo
!= null; photo
= contact
355 .getPreferredData("PHOTO")) {
360 List
<TypeInfo
> types
= new LinkedList
<TypeInfo
>();
361 types
.add(new TypeInfo("ENCODING", "b"));
362 types
.add(new TypeInfo("TYPE", type
));
363 Data photo
= new Data(types
, "PHOTO", b64
, null);
365 } catch (IOException e
) {
366 System
.err
.println("Cannot read photo: "
372 } catch (IOException e
) {
374 .println(trans(StringId
.CLI_ERR_CANNOT_OPEN
, file
));
380 for (String file
: files
) {
382 Card card
= getCard(file
, null).getCard();
383 for (Contact contact
: card
) {
384 Data photo
= contact
.getPreferredData("PHOTO");
386 String filename
= contact
.toString(format
, "");
387 File f
= new File(dir
, filename
+ ".png");
388 System
.out
.println("Saving " + f
);
391 ImageUtils
.fromBase64(photo
.getValue()),
393 } catch (IOException e
) {
394 System
.err
.println(trans(
395 StringId
.CLI_ERR_CANNOT_SAVE_PHOTO
,
396 contact
.getPreferredDataValue("FN")));
400 } catch (IOException e
) {
402 .println(trans(StringId
.CLI_ERR_CANNOT_OPEN
, file
));
407 case CONTACT_MANAGER
: {
409 Optional
.startTui(textMode
, files
);
410 } catch (IOException e
) {
411 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_CANNOT_START
,
414 } catch (NotSupportedException e
) {
415 if (!e
.isCompiledIn()) {
416 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR_NO_TUI
, ERR_INTERNAL
);
420 ERR(StringId
.CLI_ERR
, StringId
.CLI_ERR
, ERR_INTERNAL
);
427 System
.out
.println(APPLICATION_TITLE
+ " "
428 + Version
.getCurrentVersion());
429 System
.out
.println();
431 System
.out
.println(trans(StringId
.CLI_HELP
));
432 System
.out
.println();
434 System
.out
.println(trans(StringId
.CLI_HELP_MODES
));
435 System
.out
.println("\t--help : "
436 + trans(StringId
.CLI_HELP_MODE_HELP
));
437 System
.out
.println("\t(--tui|--gui) (--noutf) ... : "
438 + trans(StringId
.CLI_HELP_MODE_CONTACT_MANAGER
));
439 System
.out
.println("\t--server PORT ... : "
440 + trans(StringId
.CLI_HELP_MODE_SERVER
));
441 System
.out
.println("\t--save-config DIR : "
442 + trans(StringId
.CLI_HELP_MODE_SAVE_CONFIG
));
443 System
.out
.println("\t--i18n DIR ---lang LANG : "
444 + trans(StringId
.CLI_HELP_MODE_I18N
));
445 System
.out
.println("\t--load-photo DIR FORMAT ... : "
446 + trans(StringId
.CLI_HELP_MODE_LOAD_PHOTO
));
447 System
.out
.println("\t--save-photo DIR FORMAT ... : "
448 + trans(StringId
.CLI_HELP_MODE_SAVE_PHOTO
));
449 System
.out
.println();
451 System
.out
.println(trans(StringId
.CLI_HELP_OPTIONS
));
452 System
.out
.println("\t-- : " + trans(StringId
.CLI_HELP_DD
));
453 System
.out
.println("\t--lang LANG : "
454 + trans(StringId
.CLI_HELP_LANG
));
455 System
.out
.println("\t--tui : " + trans(StringId
.CLI_HELP_TUI
));
456 System
.out
.println("\t--gui : " + trans(StringId
.CLI_HELP_GUI
));
457 System
.out
.println("\t--noutf : "
458 + trans(StringId
.CLI_HELP_NOUTF_OPTION
));
459 System
.out
.println("\t--config : "
460 + trans(StringId
.CLI_HELP_CONFIG
));
461 System
.out
.println();
463 System
.out
.println(trans(StringId
.CLI_HELP_FOOTER
));
464 System
.out
.println();
471 * Return the {@link Card} corresponding to the given resource name -- a
472 * file or a remote jvcard URL.
475 * Will also fix the FN if required (see display.properties).
479 * a filename or a remote jvcard url with named resource (e.g.:
480 * <tt>jvcard://localhost:4444/coworkers.vcf</tt>)
482 * the {@link MergeCallback} to call in case of conflict, or NULL
483 * to disallow conflict management (the {@link Card} will not be
484 * allowed to synchronise in case of conflicts)
486 * @return the {@link Card}
488 * @throws IOException
489 * in case of IO error or remoting not available
491 static public CardResult
getCard(String input
, MergeCallback callback
)
493 boolean remote
= false;
494 Format format
= Format
.Abook
;
496 if (ext
.contains(".")) {
497 String tab
[] = ext
.split("\\.");
498 if (tab
.length
> 1 && tab
[tab
.length
- 1].equalsIgnoreCase("vcf")) {
499 format
= Format
.VCard21
;
503 if (input
.contains("://")) {
504 format
= Format
.VCard21
;
508 CardResult card
= null;
511 card
= Optional
.syncCard(input
, callback
);
513 card
= new CardResult(new Card(new File(input
), format
), false,
516 } catch (IOException ioe
) {
518 } catch (NotSupportedException e
) {
519 throw new IOException("Remoting support not available", e
);
523 if (defaultFn
!= null) {
525 for (Contact contact
: card
.getCard()) {
526 Data name
= contact
.getPreferredData("FN");
527 if (name
== null || name
.getValue().length() == 0
528 || forceComputedFn
) {
529 name
.setValue(contact
.toString(defaultFn
, "").trim());
532 } catch (Exception e
) {
533 // sync failed -> getCard() throws.
542 * Open the given path and add all its files if it is a directory or just
543 * this one if not to the returned list.
548 * @return the list of opened files
550 static private List
<String
> open(String path
) {
551 List
<String
> files
= new LinkedList
<String
>();
553 if (path
!= null && path
.startsWith("jvcard://")) {
554 if (path
.endsWith("/")) {
555 files
.addAll(list(path
));
560 File file
= new File(path
);
562 if (file
.isDirectory()) {
563 for (File subfile
: file
.listFiles()) {
564 if (!subfile
.isDirectory())
565 files
.add(subfile
.getAbsolutePath());
568 files
.add(file
.getAbsolutePath());
571 System
.err
.println("File or directory not found: \"" + path
580 * List all the available {@link Card}s on the given network location (which
581 * is expected to be a jVCard remote server, obviously).
584 * the jVCard remote server path (e.g.:
585 * <tt>jvcard://localhost:4444/</tt>)
587 * @return the list of {@link Card}s
589 static private List
<String
> list(String path
) {
590 List
<String
> files
= new LinkedList
<String
>();
593 String host
= path
.split("\\:")[1].substring(2);
594 int port
= Integer
.parseInt(path
.split("\\:")[2].replaceAll("/$",
596 SimpleSocket s
= new SimpleSocket(new Socket(host
, port
),
600 s
.sendCommand(Command
.LIST_CARD
);
601 for (String p
: s
.receiveBlock()) {
603 + p
.substring(StringUtils
.fromTime(0).length() + 1));
606 } catch (Exception e
) {
614 * Really, really ask for UTF-8 encoding.
616 static private void utf8() {
618 System
.setProperty("file.encoding", "UTF-8");
619 Field charset
= Charset
.class.getDeclaredField("defaultCharset");
620 charset
.setAccessible(true);
621 charset
.set(null, null);
622 } catch (SecurityException e
) {
623 } catch (NoSuchFieldException e
) {
624 } catch (IllegalArgumentException e
) {
625 } catch (IllegalAccessException e
) {
630 * Read display.properties to know if we should fix the FN field when empty,
631 * or always, or never.
633 static private void readNFN() {
634 DisplayBundle map
= new DisplayBundle();
636 defaultFn
= map
.getString(DisplayOption
.CONTACT_DETAILS_DEFAULT_FN
);
638 forceComputedFn
= map
.getBoolean(
639 DisplayOption
.CONTACT_DETAILS_SHOW_COMPUTED_FN
, false);
643 * Syntax error detected, closing the application with an error message.
646 * the syntax error case
648 static private void SERR(StringId err
, Object
... values
) {
649 ERR(StringId
.CLI_SERR
, err
, ERR_SYNTAX
, values
);
653 * Error detected, closing the application with an error message.
658 * the suberror or NULL if none
660 * the error code as declared above
662 static private void ERR(StringId err
, StringId suberr
, int CODE
,
663 Object
... subvalues
) {
665 System
.err
.println(trans(err
));
667 System
.err
.println(trans(err
, trans(suberr
, subvalues
)));