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 be
.nikiroo
.jvcard
.Card
;
12 import be
.nikiroo
.jvcard
.launcher
.CardResult
.MergeCallback
;
13 import be
.nikiroo
.jvcard
.parsers
.Format
;
14 import be
.nikiroo
.jvcard
.remote
.Command
;
15 import be
.nikiroo
.jvcard
.remote
.SimpleSocket
;
16 import be
.nikiroo
.jvcard
.resources
.Bundles
;
17 import be
.nikiroo
.jvcard
.resources
.StringUtils
;
18 import be
.nikiroo
.jvcard
.resources
.Trans
;
19 import be
.nikiroo
.jvcard
.resources
.Trans
.StringId
;
22 * This class contains the runnable Main method. It will parse the user supplied
23 * parameters and take action based upon those. Most of the time, it will start
30 static public final String APPLICATION_TITLE
= "jVcard";
31 static public final String APPLICATION_VERSION
= "1.0-beta3-dev";
33 static private final int ERR_NO_FILE
= 1;
34 static private final int ERR_SYNTAX
= 2;
35 static private final int ERR_INTERNAL
= 3;
36 static private Trans transService
;
39 * Translate the given {@link StringId}.
44 * @return the translation
46 static public String
trans(StringId id
) {
47 return transService
.trans(id
);
51 * Check if unicode characters should be used.
53 * @return TRUE to allow unicode
55 static public boolean isUnicode() {
56 return transService
.isUnicode();
60 * Start the application.
63 * The returned exit codes are:
65 * <li>1: no files to open</li>
66 * <li>2: invalid syntax</li>
67 * <li>3: internal error</li>
72 * the parameters (see <tt>--help</tt> to know which are
75 public static void main(String
[] args
) {
76 Boolean textMode
= null;
77 boolean noMoreParams
= false;
78 boolean filesTried
= false;
80 // get the "system default" language to help translate the --help
82 String language
= null;
83 transService
= new Trans(language
);
85 boolean unicode
= transService
.isUnicode();
86 String i18nDir
= null;
87 List
<String
> files
= new LinkedList
<String
>();
89 for (int index
= 0; index
< args
.length
; index
++) {
90 String arg
= args
[index
];
91 if (!noMoreParams
&& arg
.equals("--")) {
93 } else if (!noMoreParams
&& arg
.equals("--help")) {
95 .println("TODO: implement some help text.\n"
96 + "Usable switches:\n"
97 + "\t--: stop looking for switches\n"
98 + "\t--help: this here thingy\n"
99 + "\t--lang LANGUAGE: choose the language, for instance en_GB\n"
100 + "\t--tui: force pure text mode even if swing treminal is available\n"
101 + "\t--gui: force swing terminal mode\n"
102 + "\t--noutf: force non-utf8 mode if you need it\n"
103 + "\t--config DIRECTORY: force the given directory as a CONFIG_DIR\n"
104 + "\t--server PORT: start a remoting server instead of a client\n"
105 + "\t--i18n DIR: generate the translation file for the given language (can be \"\") to/from the .properties given dir\n"
106 + "everyhing else is either a file to open or a directory to open\n"
107 + "(we will only open 1st level files in given directories)\n"
108 + "('jvcard://hostname:8888/file' links -- or without 'file' -- are also ok)\n");
110 } else if (!noMoreParams
&& arg
.equals("--tui")) {
112 } else if (!noMoreParams
&& arg
.equals("--gui")) {
114 } else if (!noMoreParams
&& arg
.equals("--noutf")) {
116 transService
.setUnicode(unicode
);
117 } else if (!noMoreParams
&& arg
.equals("--lang")) {
119 if (index
>= args
.length
) {
120 System
.err
.println("Syntax error: no language given");
121 System
.exit(ERR_SYNTAX
);
125 language
= args
[index
];
126 transService
= new Trans(language
);
127 transService
.setUnicode(unicode
);
128 } else if (!noMoreParams
&& arg
.equals("--config")) {
130 if (index
>= args
.length
) {
132 .println("Syntax error: no config directory given");
133 System
.exit(ERR_SYNTAX
);
137 Bundles
.setDirectory(args
[index
]);
138 transService
= new Trans(language
);
139 transService
.setUnicode(unicode
);
140 } else if (!noMoreParams
&& arg
.equals("--server")) {
142 if (index
>= args
.length
) {
143 System
.err
.println("Syntax error: no port given");
144 System
.exit(ERR_SYNTAX
);
149 port
= Integer
.parseInt(args
[index
]);
150 } catch (NumberFormatException e
) {
151 System
.err
.println("Invalid port number: " + args
[index
]);
152 System
.exit(ERR_SYNTAX
);
155 } else if (!noMoreParams
&& arg
.equals("--i18n")) {
157 if (index
>= args
.length
) {
159 .println("Syntax error: no .properties directory given");
160 System
.exit(ERR_SYNTAX
);
164 i18nDir
= args
[index
];
167 files
.addAll(open(arg
));
171 // Force headless mode if we run in forced-text mode
172 if (textMode
!= null && textMode
) {
173 // same as -Djava.awt.headless=true
174 System
.setProperty("java.awt.headless", "true");
182 if (port
!= null && files
.size() > 0) {
184 .println("Invalid syntax: you cannot both use --server and provide card files");
185 System
.exit(ERR_SYNTAX
);
186 } else if (i18nDir
!= null && files
.size() > 0) {
188 .println("Invalid syntax: you cannot both use --i18n and provide card files");
189 System
.exit(ERR_SYNTAX
);
190 } else if (port
!= null && i18nDir
!= null) {
192 .println("Invalid syntax: you cannot both use --server and --i18n");
193 System
.exit(ERR_SYNTAX
);
194 } else if (i18nDir
!= null && language
== null) {
196 .println("Invalid syntax: you cannot use --i18n without --lang");
197 System
.exit(ERR_SYNTAX
);
198 } else if (port
== null && i18nDir
== null && files
.size() == 0) {
199 if (files
.size() == 0 && !filesTried
) {
200 files
.addAll(open("."));
203 if (files
.size() == 0) {
204 System
.err
.println("No files to open");
205 System
.exit(ERR_NO_FILE
);
213 Optional
.runServer(port
);
214 } catch (Exception e
) {
215 if (e
instanceof IOException
) {
217 .println("I/O Exception: Cannot start the server");
219 System
.err
.println("Remoting support not available");
220 System
.exit(ERR_INTERNAL
);
223 } else if (i18nDir
!= null) {
225 Trans
.generateTranslationFile(i18nDir
, language
);
226 } catch (IOException e
) {
228 .println("I/O Exception: Cannot create/update a language in directory: "
233 Optional
.startTui(textMode
, files
);
234 } catch (Exception e
) {
235 if (e
instanceof IOException
) {
237 .println("I/O Exception: Cannot start the program with the given cards");
239 System
.err
.println("TUI support not available");
240 System
.exit(ERR_INTERNAL
);
247 * Return the {@link Card} corresponding to the given resource name -- a
248 * file or a remote jvcard URL
251 * a filename or a remote jvcard url with named resource (e.g.:
252 * <tt>jvcard://localhost:4444/coworkers.vcf</tt>)
254 * the {@link MergeCallback} to call in case of conflict, or NULL
255 * to disallow conflict management (the {@link Card} will not be
256 * allowed to synchronise in case of conflicts)
258 * @return the {@link Card}
260 * @throws IOException
261 * in case of IO error or remoting not available
263 static public CardResult
getCard(String input
, MergeCallback callback
)
265 boolean remote
= false;
266 Format format
= Format
.Abook
;
268 if (ext
.contains(".")) {
269 String tab
[] = ext
.split("\\.");
270 if (tab
.length
> 1 && tab
[tab
.length
- 1].equalsIgnoreCase("vcf")) {
271 format
= Format
.VCard21
;
275 if (input
.contains("://")) {
276 format
= Format
.VCard21
;
280 CardResult card
= null;
283 card
= Optional
.syncCard(input
, callback
);
285 card
= new CardResult(new Card(new File(input
), format
), false,
288 } catch (IOException ioe
) {
290 } catch (Exception e
) {
291 throw new IOException("Remoting support not available", e
);
298 * Open the given path and add all its files if it is a directory or just
299 * this one if not to the returned list.
304 * @return the list of opened files
306 static private List
<String
> open(String path
) {
307 List
<String
> files
= new LinkedList
<String
>();
309 if (path
!= null && path
.startsWith("jvcard://")) {
310 if (path
.endsWith("/")) {
311 files
.addAll(list(path
));
316 File file
= new File(path
);
318 if (file
.isDirectory()) {
319 for (File subfile
: file
.listFiles()) {
320 if (!subfile
.isDirectory())
321 files
.add(subfile
.getAbsolutePath());
324 files
.add(file
.getAbsolutePath());
327 System
.err
.println("File or directory not found: \"" + path
336 * List all the available {@link Card}s on the given network location (which
337 * is expected to be a jVCard remote server, obviously).
340 * the jVCard remote server path (e.g.:
341 * <tt>jvcard://localhost:4444/</tt>)
343 * @return the list of {@link Card}s
345 static private List
<String
> list(String path
) {
346 List
<String
> files
= new LinkedList
<String
>();
349 String host
= path
.split("\\:")[1].substring(2);
350 int port
= Integer
.parseInt(path
.split("\\:")[2].replaceAll("/$",
352 SimpleSocket s
= new SimpleSocket(new Socket(host
, port
),
356 s
.sendCommand(Command
.LIST_CARD
);
357 for (String p
: s
.receiveBlock()) {
359 + p
.substring(StringUtils
.fromTime(0).length() + 1));
362 } catch (Exception e
) {
370 * Really, really ask for UTF-8 encoding.
372 static private void utf8() {
374 System
.setProperty("file.encoding", "UTF-8");
375 Field charset
= Charset
.class.getDeclaredField("defaultCharset");
376 charset
.setAccessible(true);
377 charset
.set(null, null);
378 } catch (SecurityException e
) {
379 } catch (NoSuchFieldException e
) {
380 } catch (IllegalArgumentException e
) {
381 } catch (IllegalAccessException e
) {