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-beta2-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
));
176 if (port
!= null && files
.size() > 0) {
178 .println("Invalid syntax: you cannot both use --server and provide card files");
179 System
.exit(ERR_SYNTAX
);
180 } else if (i18nDir
!= null && files
.size() > 0) {
182 .println("Invalid syntax: you cannot both use --i18n and provide card files");
183 System
.exit(ERR_SYNTAX
);
184 } else if (port
!= null && i18nDir
!= null) {
186 .println("Invalid syntax: you cannot both use --server and --i18n");
187 System
.exit(ERR_SYNTAX
);
188 } else if (i18nDir
!= null && language
== null) {
190 .println("Invalid syntax: you cannot use --i18n without --lang");
191 System
.exit(ERR_SYNTAX
);
192 } else if (port
== null && i18nDir
== null && files
.size() == 0) {
193 if (files
.size() == 0 && !filesTried
) {
194 files
.addAll(open("."));
197 if (files
.size() == 0) {
198 System
.err
.println("No files to open");
199 System
.exit(ERR_NO_FILE
);
207 Optional
.runServer(port
);
208 } catch (Exception e
) {
209 if (e
instanceof IOException
) {
211 .println("I/O Exception: Cannot start the server");
213 System
.err
.println("Remoting support not available");
214 System
.exit(ERR_INTERNAL
);
217 } else if (i18nDir
!= null) {
219 Trans
.generateTranslationFile(i18nDir
, language
);
220 } catch (IOException e
) {
222 .println("I/O Exception: Cannot create/update a language in directory: "
227 Optional
.startTui(textMode
, files
);
228 } catch (Exception e
) {
229 if (e
instanceof IOException
) {
231 .println("I/O Exception: Cannot start the program with the given cards");
233 System
.err
.println("TUI support not available");
234 System
.exit(ERR_INTERNAL
);
241 * Return the {@link Card} corresponding to the given resource name -- a
242 * file or a remote jvcard URL
245 * a filename or a remote jvcard url with named resource (e.g.:
246 * <tt>jvcard://localhost:4444/coworkers.vcf</tt>)
248 * the {@link MergeCallback} to call in case of conflict, or NULL
249 * to disallow conflict management (the {@link Card} will not be
250 * allowed to synchronise in case of conflicts)
252 * @return the {@link Card}
254 * @throws IOException
255 * in case of IO error or remoting not available
257 static public CardResult
getCard(String input
, MergeCallback callback
)
259 boolean remote
= false;
260 Format format
= Format
.Abook
;
262 if (ext
.contains(".")) {
263 String tab
[] = ext
.split("\\.");
264 if (tab
.length
> 1 && tab
[tab
.length
- 1].equalsIgnoreCase("vcf")) {
265 format
= Format
.VCard21
;
269 if (input
.contains("://")) {
270 format
= Format
.VCard21
;
274 CardResult card
= null;
277 card
= Optional
.syncCard(input
, callback
);
279 card
= new CardResult(new Card(new File(input
), format
), false,
282 } catch (IOException ioe
) {
284 } catch (Exception e
) {
285 throw new IOException("Remoting support not available", e
);
292 * Open the given path and add all its files if it is a directory or just
293 * this one if not to the returned list.
298 * @return the list of opened files
300 static private List
<String
> open(String path
) {
301 List
<String
> files
= new LinkedList
<String
>();
303 if (path
!= null && path
.startsWith("jvcard://")) {
304 if (path
.endsWith("/")) {
305 files
.addAll(list(path
));
310 File file
= new File(path
);
312 if (file
.isDirectory()) {
313 for (File subfile
: file
.listFiles()) {
314 if (!subfile
.isDirectory())
315 files
.add(subfile
.getAbsolutePath());
318 files
.add(file
.getAbsolutePath());
321 System
.err
.println("File or directory not found: \"" + path
330 * List all the available {@link Card}s on the given network location (which
331 * is expected to be a jVCard remote server, obviously).
334 * the jVCard remote server path (e.g.:
335 * <tt>jvcard://localhost:4444/</tt>)
337 * @return the list of {@link Card}s
339 static private List
<String
> list(String path
) {
340 List
<String
> files
= new LinkedList
<String
>();
343 String host
= path
.split("\\:")[1].substring(2);
344 int port
= Integer
.parseInt(path
.split("\\:")[2].replaceAll("/$",
346 SimpleSocket s
= new SimpleSocket(new Socket(host
, port
),
350 s
.sendCommand(Command
.LIST_CARD
);
351 for (String p
: s
.receiveBlock()) {
353 + p
.substring(StringUtils
.fromTime(0).length() + 1));
356 } catch (Exception e
) {
364 * Really, really ask for UTF-8 encoding.
366 static private void utf8() {
368 System
.setProperty("file.encoding", "UTF-8");
369 Field charset
= Charset
.class.getDeclaredField("defaultCharset");
370 charset
.setAccessible(true);
371 charset
.set(null, null);
372 } catch (SecurityException e
) {
373 } catch (NoSuchFieldException e
) {
374 } catch (IllegalArgumentException e
) {
375 } catch (IllegalAccessException e
) {