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
;
10 import java
.util
.MissingResourceException
;
11 import java
.util
.ResourceBundle
;
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
.parsers
.Format
;
21 import be
.nikiroo
.jvcard
.remote
.Command
;
22 import be
.nikiroo
.jvcard
.remote
.SimpleSocket
;
23 import be
.nikiroo
.jvcard
.resources
.Bundles
;
24 import be
.nikiroo
.jvcard
.resources
.StringUtils
;
25 import be
.nikiroo
.jvcard
.resources
.Trans
;
26 import be
.nikiroo
.jvcard
.resources
.Trans
.StringId
;
29 * This class contains the runnable Main method. It will parse the user supplied
30 * parameters and take action based upon those. Most of the time, it will start
37 static public final String APPLICATION_TITLE
= "jVcard";
38 static public final String APPLICATION_VERSION
= "1.0-beta3-dev";
40 static private final int ERR_NO_FILE
= 1;
41 static private final int ERR_SYNTAX
= 2;
42 static private final int ERR_INTERNAL
= 3;
43 static private Trans transService
;
45 static private String defaultFn
;
46 static private boolean forceComputedFn
;
49 CONTACT_MANAGER
, I18N
, SERVER
, LOAD_PHOTO
, SAVE_PHOTO
, ONLY_PHOTO
,
53 * Translate the given {@link StringId} into user text.
58 * the values to insert instead of the place holders in the
61 * @return the translated text with the given value where required
63 static public String
trans(StringId id
, String
... values
) {
64 return transService
.trans(id
, (String
[]) values
);
68 * Check if unicode characters should be used.
70 * @return TRUE to allow unicode
72 static public boolean isUnicode() {
73 return transService
.isUnicode();
77 * Start the application.
80 * The returned exit codes are:
82 * <li>1: no files to open</li>
83 * <li>2: invalid syntax</li>
84 * <li>3: internal error</li>
89 * the parameters (see <tt>--help</tt> to know which are
92 public static void main(String
[] args
) {
93 Boolean textMode
= null;
94 boolean noMoreParams
= false;
95 boolean filesTried
= false;
97 // get the "system default" language to help translate the --help
99 String language
= null;
100 transService
= new Trans(language
);
102 boolean unicode
= transService
.isUnicode();
104 List
<String
> files
= new LinkedList
<String
>();
106 Mode mode
= Mode
.CONTACT_MANAGER
;
107 String format
= null;
108 for (int index
= 0; index
< args
.length
; index
++) {
109 String arg
= args
[index
];
110 if (!noMoreParams
&& arg
.equals("--")) {
112 } else if (!noMoreParams
&& arg
.equals("--help")) {
114 .println("TODO: implement some help text.\n"
115 + "Usable switches:\n"
116 + "\t--: stop looking for switches\n"
117 + "\t--help: this here thingy\n"
118 + "\t--lang LANGUAGE: choose the language, for instance en_GB\n"
119 + "\t--tui: force pure text mode even if swing treminal is available\n"
120 + "\t--gui: force swing terminal mode\n"
121 + "\t--noutf: force non-utf8 mode if you need it\n"
122 + "\t--config DIRECTORY: force the given directory as a CONFIG_DIR\n"
123 + "\t--server PORT: start a remoting server instead of a client\n"
124 + "\t--i18n DIR: generate the translation file for the given language (can be \"\") to/from the .properties given dir\n"
125 + "\t--save-photo DIR FORMAT: save the contacts' photos to DIR, named after FORMAT\n"
126 + "\t--load-photo DIR FORMAT: load the contacts' photos from DIR, named after FORMAT\n"
127 + "\t--only-photo DIR FORMAT: load the contacts' photos from DIR, named after FORMAT, overwrite all other photos of selected contacts\n"
128 + "everyhing else is either a file to open or a directory to open\n"
129 + "(we will only open 1st level files in given directories)\n"
130 + "('jvcard://hostname:8888/file' links -- or without 'file' -- are also ok)\n");
132 } else if (!noMoreParams
&& arg
.equals("--tui")) {
134 } else if (!noMoreParams
&& arg
.equals("--gui")) {
136 } else if (!noMoreParams
&& arg
.equals("--noutf")) {
138 transService
.setUnicode(unicode
);
139 } else if (!noMoreParams
&& arg
.equals("--lang")) {
141 if (index
>= args
.length
) {
142 System
.err
.println("Syntax error: no language given");
143 System
.exit(ERR_SYNTAX
);
147 language
= args
[index
];
148 transService
= new Trans(language
);
149 transService
.setUnicode(unicode
);
150 } else if (!noMoreParams
&& arg
.equals("--config")) {
152 if (index
>= args
.length
) {
154 .println("Syntax error: no config directory given");
155 System
.exit(ERR_SYNTAX
);
159 Bundles
.setDirectory(args
[index
]);
160 transService
= new Trans(language
);
161 transService
.setUnicode(unicode
);
162 } else if (!noMoreParams
&& arg
.equals("--server")) {
163 if (mode
!= Mode
.CONTACT_MANAGER
) {
165 .println("Syntax error: you can only use one of: \n"
171 System
.exit(ERR_SYNTAX
);
177 if (index
>= args
.length
) {
178 System
.err
.println("Syntax error: no port given");
179 System
.exit(ERR_SYNTAX
);
184 port
= Integer
.parseInt(args
[index
]);
185 } catch (NumberFormatException e
) {
186 System
.err
.println("Invalid port number: " + args
[index
]);
187 System
.exit(ERR_SYNTAX
);
190 } else if (!noMoreParams
&& arg
.equals("--i18n")) {
191 if (mode
!= Mode
.CONTACT_MANAGER
) {
193 .println("Syntax error: you can only use one of: \n"
199 System
.exit(ERR_SYNTAX
);
205 if (index
>= args
.length
) {
207 .println("Syntax error: no .properties directory given");
208 System
.exit(ERR_SYNTAX
);
213 } else if (!noMoreParams
214 && (arg
.equals("--load-photo")
215 || arg
.equals("--save-photo") || arg
216 .equals("--only-photo"))) {
217 if (mode
!= Mode
.CONTACT_MANAGER
) {
219 .println("Syntax error: you can only use one of: \n"
225 System
.exit(ERR_SYNTAX
);
229 if (arg
.equals("--load-photo")) {
230 mode
= Mode
.LOAD_PHOTO
;
231 } else if (arg
.equals("--save-photo")) {
232 mode
= Mode
.SAVE_PHOTO
;
234 mode
= Mode
.ONLY_PHOTO
;
238 if (index
>= args
.length
) {
239 System
.err
.println("Syntax error: photo directory given");
240 System
.exit(ERR_SYNTAX
);
247 if (index
>= args
.length
) {
248 System
.err
.println("Syntax error: photo format given");
249 System
.exit(ERR_SYNTAX
);
253 format
= args
[index
];
256 files
.addAll(open(arg
));
260 // Force headless mode if we run in forced-text mode
261 if (mode
!= Mode
.CONTACT_MANAGER
|| (textMode
!= null && textMode
)) {
262 // same as -Djava.awt.headless=true
263 System
.setProperty("java.awt.headless", "true");
270 // N/FN fix information:
274 if (mode
== Mode
.SERVER
&& files
.size() > 0) {
276 .println("Invalid syntax: you cannot both use --server and provide card files");
277 System
.exit(ERR_SYNTAX
);
278 } else if (mode
== Mode
.I18N
&& files
.size() > 0) {
280 .println("Invalid syntax: you cannot both use --i18n and provide card files");
281 System
.exit(ERR_SYNTAX
);
282 } else if (mode
== Mode
.I18N
&& language
== null) {
284 .println("Invalid syntax: you cannot use --i18n without --lang");
285 System
.exit(ERR_SYNTAX
);
286 } else if ((mode
== Mode
.CONTACT_MANAGER
|| mode
== Mode
.SAVE_PHOTO
|| mode
== Mode
.LOAD_PHOTO
)
287 && files
.size() == 0) {
288 if (files
.size() == 0 && !filesTried
) {
289 files
.addAll(open("."));
292 if (files
.size() == 0) {
293 System
.err
.println("No files to open");
294 System
.exit(ERR_NO_FILE
);
303 Optional
.runServer(port
);
304 } catch (Exception e
) {
305 if (e
instanceof IOException
) {
307 .println("I/O Exception: Cannot start the server");
309 System
.err
.println("Remoting support not available");
310 System
.exit(ERR_INTERNAL
);
317 Trans
.generateTranslationFile(dir
, language
);
318 } catch (IOException e
) {
320 .println("I/O Exception: Cannot create/update a language in directory: "
327 for (String file
: files
) {
329 Card card
= getCard(file
, null).getCard();
330 for (Contact contact
: card
) {
331 String filename
= contact
.toString(format
, "");
332 File f
= new File(dir
, filename
);
336 String b64
= StringUtils
.fromImage(ImageIO
339 if (mode
== Mode
.ONLY_PHOTO
) {
340 for (Data photo
= contact
341 .getPreferredData("PHOTO"); photo
!= null; photo
= contact
342 .getPreferredData("PHOTO")) {
347 List
<TypeInfo
> types
= new LinkedList
<TypeInfo
>();
348 types
.add(new TypeInfo("ENCODING", "b"));
349 types
.add(new TypeInfo("TYPE", "png"));
350 Data photo
= new Data(types
, "PHOTO", b64
, null);
352 } catch (IOException e
) {
353 System
.err
.println("Cannot read photo: "
359 } catch (IOException e
) {
360 System
.err
.println("Card cannot be opened: " + file
);
366 for (String file
: files
) {
368 Card card
= getCard(file
, null).getCard();
369 for (Contact contact
: card
) {
370 Data photo
= contact
.getPreferredData("PHOTO");
372 String filename
= contact
.toString(format
, "");
373 File f
= new File(dir
, filename
+ ".png");
376 StringUtils
.toImage(photo
.getValue()),
378 } catch (IOException e
) {
380 .println("Cannot save photo of contact: "
382 .getPreferredDataValue("FN"));
386 } catch (IOException e
) {
387 System
.err
.println("Card cannot be opened: " + file
);
392 case CONTACT_MANAGER
: {
394 Optional
.startTui(textMode
, files
);
395 } catch (Exception e
) {
396 if (e
instanceof IOException
) {
398 .println("I/O Exception: Cannot start the program with the given cards");
400 System
.err
.println("TUI support not available");
401 System
.exit(ERR_INTERNAL
);
410 * Return the {@link Card} corresponding to the given resource name -- a
411 * file or a remote jvcard URL.
414 * Will also fix the FN if required (see display.properties).
418 * a filename or a remote jvcard url with named resource (e.g.:
419 * <tt>jvcard://localhost:4444/coworkers.vcf</tt>)
421 * the {@link MergeCallback} to call in case of conflict, or NULL
422 * to disallow conflict management (the {@link Card} will not be
423 * allowed to synchronise in case of conflicts)
425 * @return the {@link Card}
427 * @throws IOException
428 * in case of IO error or remoting not available
430 static public CardResult
getCard(String input
, MergeCallback callback
)
432 boolean remote
= false;
433 Format format
= Format
.Abook
;
435 if (ext
.contains(".")) {
436 String tab
[] = ext
.split("\\.");
437 if (tab
.length
> 1 && tab
[tab
.length
- 1].equalsIgnoreCase("vcf")) {
438 format
= Format
.VCard21
;
442 if (input
.contains("://")) {
443 format
= Format
.VCard21
;
447 CardResult card
= null;
450 card
= Optional
.syncCard(input
, callback
);
452 card
= new CardResult(new Card(new File(input
), format
), false,
455 } catch (IOException ioe
) {
457 } catch (Exception e
) {
458 throw new IOException("Remoting support not available", e
);
462 if (defaultFn
!= null) {
464 for (Contact contact
: card
.getCard()) {
465 Data name
= contact
.getPreferredData("FN");
466 if (name
== null || name
.getValue().length() == 0
467 || forceComputedFn
) {
468 name
.setValue(contact
.toString(defaultFn
, ""));
471 } catch (Exception e
) {
472 // sync failed -> getCard() throws.
481 * Open the given path and add all its files if it is a directory or just
482 * this one if not to the returned list.
487 * @return the list of opened files
489 static private List
<String
> open(String path
) {
490 List
<String
> files
= new LinkedList
<String
>();
492 if (path
!= null && path
.startsWith("jvcard://")) {
493 if (path
.endsWith("/")) {
494 files
.addAll(list(path
));
499 File file
= new File(path
);
501 if (file
.isDirectory()) {
502 for (File subfile
: file
.listFiles()) {
503 if (!subfile
.isDirectory())
504 files
.add(subfile
.getAbsolutePath());
507 files
.add(file
.getAbsolutePath());
510 System
.err
.println("File or directory not found: \"" + path
519 * List all the available {@link Card}s on the given network location (which
520 * is expected to be a jVCard remote server, obviously).
523 * the jVCard remote server path (e.g.:
524 * <tt>jvcard://localhost:4444/</tt>)
526 * @return the list of {@link Card}s
528 static private List
<String
> list(String path
) {
529 List
<String
> files
= new LinkedList
<String
>();
532 String host
= path
.split("\\:")[1].substring(2);
533 int port
= Integer
.parseInt(path
.split("\\:")[2].replaceAll("/$",
535 SimpleSocket s
= new SimpleSocket(new Socket(host
, port
),
539 s
.sendCommand(Command
.LIST_CARD
);
540 for (String p
: s
.receiveBlock()) {
542 + p
.substring(StringUtils
.fromTime(0).length() + 1));
545 } catch (Exception e
) {
553 * Really, really ask for UTF-8 encoding.
555 static private void utf8() {
557 System
.setProperty("file.encoding", "UTF-8");
558 Field charset
= Charset
.class.getDeclaredField("defaultCharset");
559 charset
.setAccessible(true);
560 charset
.set(null, null);
561 } catch (SecurityException e
) {
562 } catch (NoSuchFieldException e
) {
563 } catch (IllegalArgumentException e
) {
564 } catch (IllegalAccessException e
) {
569 * Read display.properties to know if we should fix the FN field when empty,
570 * or always, or never.
572 static private void readNFN() {
573 ResourceBundle map
= Bundles
.getBundle("display");
575 defaultFn
= map
.getString("CONTACT_DETAILS_DEFAULT_FN");
576 if (defaultFn
.trim().length() == 0)
578 } catch (MissingResourceException e
) {
583 String forceComputedFnStr
= map
584 .getString("CONTACT_DETAILS_SHOW_COMPUTED_FN");
585 if (forceComputedFnStr
.length() > 0
586 && forceComputedFnStr
.equalsIgnoreCase("true"))
587 forceComputedFn
= true;
588 } catch (MissingResourceException e
) {