1 package be
.nikiroo
.jvcard
.tui
;
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
.i18n
.Trans
;
13 import be
.nikiroo
.jvcard
.i18n
.Trans
.StringId
;
14 import be
.nikiroo
.jvcard
.remote
.Command
.Verb
;
15 import be
.nikiroo
.jvcard
.remote
.SimpleSocket
;
16 import be
.nikiroo
.jvcard
.resources
.Bundles
;
17 import be
.nikiroo
.jvcard
.tui
.panes
.FileList
;
19 import com
.googlecode
.lanterna
.gui2
.Window
;
20 import com
.googlecode
.lanterna
.input
.KeyStroke
;
23 * This class contains the runnable Main method. It will parse the user supplied
24 * parameters and take action based upon those. Most of the time, it will start
31 // TODO: move Main to be.nikiroo.jvcard, use introspection to load the other
32 // main classes, allow the 3 programs to run from this new Main
33 // also requires StringUtils/... in a new package
36 public static final String APPLICATION_TITLE
= "jVcard";
37 public static final String APPLICATION_VERSION
= "1.0-beta2-dev";
39 static private Trans transService
;
42 * Translate the given {@link StringId}.
47 * @return the translation
49 static public String
trans(StringId id
) {
50 if (transService
== null)
53 return transService
.trans(id
);
57 * Translate the given {@link KeyStroke}.
60 * the key to translate
62 * @return the translation
64 static public String
trans(KeyStroke key
) {
65 if (transService
== null)
68 return transService
.trans(key
);
72 * Start the application.
75 * the parameters (see <tt>--help</tt> to know which are
78 public static void main(String
[] args
) {
79 Boolean textMode
= null;
80 boolean noMoreParams
= false;
81 boolean filesTried
= false;
83 // get the "system default" language to help translate the --help
85 String language
= null;
86 transService
= new Trans(language
);
88 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 + "everyhing else is either a file to open or a directory to open\n"
105 + "(we will only open 1st level files in given directories)\n"
106 + "('jvcard://hostname:8888/file' links -- or without 'file' -- are also ok)\n");
108 } else if (!noMoreParams
&& arg
.equals("--tui")) {
110 } else if (!noMoreParams
&& arg
.equals("--gui")) {
112 } else if (!noMoreParams
&& arg
.equals("--noutf")) {
113 UiColors
.getInstance().setUnicode(false);
114 } else if (!noMoreParams
&& arg
.equals("--lang")) {
116 if (index
< args
.length
)
117 language
= args
[index
];
118 transService
= new Trans(language
);
119 } else if (!noMoreParams
&& arg
.equals("--config")) {
121 if (index
< args
.length
) {
122 Bundles
.setDirectory(args
[index
]);
123 transService
= new Trans(language
);
127 files
.addAll(open(arg
));
131 if (UiColors
.getInstance().isUnicode()) {
135 if (files
.size() == 0) {
141 files
.addAll(open("."));
144 // TODO error case when no file
146 Window win
= new MainWindow(new FileList(files
));
149 TuiLauncher
.start(textMode
, win
);
150 } catch (IOException ioe
) {
151 ioe
.printStackTrace();
157 * Open the given path and add all its files if it is a directory or just
158 * this one if not to the returned list.
163 * @return the list of opened files
165 static private List
<String
> open(String path
) {
166 List
<String
> files
= new LinkedList
<String
>();
168 if (path
!= null && path
.startsWith("jvcard://")) {
169 if (path
.endsWith("/")) {
170 files
.addAll(list(path
));
175 File file
= new File(path
);
177 if (file
.isDirectory()) {
178 for (File subfile
: file
.listFiles()) {
179 if (!subfile
.isDirectory())
180 files
.add(subfile
.getAbsolutePath());
183 files
.add(file
.getAbsolutePath());
186 System
.err
.println("File or directory not found: \"" + path
195 * List all the available {@link Card}s on the given network location (which
196 * is expected to be a jVCard remote server, obviously).
199 * the jVCard remote server path (e.g.:
200 * <tt>jvcard://localhost:4444/</tt>)
202 * @return the list of {@link Card}s
204 static private List
<String
> list(String path
) {
205 List
<String
> files
= new LinkedList
<String
>();
208 String host
= path
.split("\\:")[1].substring(2);
209 int port
= Integer
.parseInt(path
.split("\\:")[2].replaceAll("/$",
211 SimpleSocket s
= new SimpleSocket(new Socket(host
, port
),
215 s
.sendCommand(Verb
.LIST
);
216 for (String p
: s
.receiveBlock()) {
218 + p
.substring(StringUtils
.fromTime(0).length() + 1));
221 } catch (Exception e
) {
229 * Really, really ask for UTF-8 encoding.
231 static private void utf8() {
233 System
.setProperty("file.encoding", "UTF-8");
234 Field charset
= Charset
.class.getDeclaredField("defaultCharset");
235 charset
.setAccessible(true);
236 charset
.set(null, null);
237 } catch (SecurityException e
) {
238 } catch (NoSuchFieldException e
) {
239 } catch (IllegalArgumentException e
) {
240 } catch (IllegalAccessException e
) {