Update on remote-server
[jvcard.git] / src / be / nikiroo / jvcard / tui / Main.java
CommitLineData
0b0b2b0f
NR
1package be.nikiroo.jvcard.tui;
2
3import java.io.File;
4import java.io.IOException;
d66e24cc 5import java.lang.reflect.Field;
a046fa49 6import java.net.Socket;
d66e24cc 7import java.nio.charset.Charset;
0b0b2b0f
NR
8import java.util.LinkedList;
9import java.util.List;
10
a046fa49 11import be.nikiroo.jvcard.Card;
668268fc
NR
12import be.nikiroo.jvcard.i18n.Trans;
13import be.nikiroo.jvcard.i18n.Trans.StringId;
a046fa49
NR
14import be.nikiroo.jvcard.remote.Command.Verb;
15import be.nikiroo.jvcard.remote.SimpleSocket;
7f82bf68 16import be.nikiroo.jvcard.resources.Bundles;
0b0b2b0f
NR
17import be.nikiroo.jvcard.tui.panes.FileList;
18
0b0b2b0f 19import com.googlecode.lanterna.gui2.Window;
668268fc 20import com.googlecode.lanterna.input.KeyStroke;
0b0b2b0f 21
bcb54330
NR
22/**
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
25 * a MainWindow.
26 *
27 * @author niki
28 *
29 */
0b0b2b0f 30public class Main {
a046fa49
NR
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
34 private int TODO;
35
0b0b2b0f 36 public static final String APPLICATION_TITLE = "jVcard";
26d2bd05 37 public static final String APPLICATION_VERSION = "1.0-beta2-dev";
0b0b2b0f 38
668268fc
NR
39 static private Trans transService;
40
41 /**
42 * Translate the given {@link StringId}.
43 *
44 * @param id
45 * the ID to translate
46 *
47 * @return the translation
48 */
49 static public String trans(StringId id) {
668268fc
NR
50 if (transService == null)
51 return "";
52
53 return transService.trans(id);
54 }
55
56 /**
57 * Translate the given {@link KeyStroke}.
58 *
59 * @param key
60 * the key to translate
61 *
62 * @return the translation
63 */
64 static public String trans(KeyStroke key) {
65 if (transService == null)
66 return "";
67
68 return transService.trans(key);
69 }
70
71 /**
72 * Start the application.
73 *
74 * @param args
7f82bf68
NR
75 * the parameters (see <tt>--help</tt> to know which are
76 * supported)
668268fc 77 */
bcb54330 78 public static void main(String[] args) {
0b0b2b0f 79 Boolean textMode = null;
bcb54330
NR
80 boolean noMoreParams = false;
81 boolean filesTried = false;
0b0b2b0f 82
668268fc
NR
83 // get the "system default" language to help translate the --help
84 // message if needed
85 String language = null;
7f82bf68 86 transService = new Trans(language);
668268fc 87
7f82bf68 88 List<String> files = new LinkedList<String>();
668268fc
NR
89 for (int index = 0; index < args.length; index++) {
90 String arg = args[index];
bcb54330
NR
91 if (!noMoreParams && arg.equals("--")) {
92 noMoreParams = true;
93 } else if (!noMoreParams && arg.equals("--help")) {
94 System.out
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"
668268fc 99 + "\t--lang LANGUAGE: choose the language, for instance en_GB\n"
bcb54330
NR
100 + "\t--tui: force pure text mode even if swing treminal is available\n"
101 + "\t--gui: force swing terminal mode\n"
296a0b75 102 + "\t--noutf: force non-utf8 mode if you need it\n"
7f82bf68 103 + "\t--config DIRECTORY: force the given directory as a CONFIG_DIR\n"
bcb54330 104 + "everyhing else is either a file to open or a directory to open\n"
a046fa49
NR
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");
bcb54330
NR
107 return;
108 } else if (!noMoreParams && arg.equals("--tui")) {
109 textMode = true;
110 } else if (!noMoreParams && arg.equals("--gui")) {
111 textMode = false;
296a0b75
NR
112 } else if (!noMoreParams && arg.equals("--noutf")) {
113 UiColors.getInstance().setUnicode(false);
668268fc
NR
114 } else if (!noMoreParams && arg.equals("--lang")) {
115 index++;
116 if (index < args.length)
117 language = args[index];
118 transService = new Trans(language);
7f82bf68
NR
119 } else if (!noMoreParams && arg.equals("--config")) {
120 index++;
121 if (index < args.length) {
122 Bundles.setDirectory(args[index]);
123 transService = new Trans(language);
124 }
bcb54330
NR
125 } else {
126 filesTried = true;
127 files.addAll(open(arg));
128 }
129 }
130
d66e24cc
NR
131 if (UiColors.getInstance().isUnicode()) {
132 utf8();
133 }
134
bcb54330
NR
135 if (files.size() == 0) {
136 if (filesTried) {
137 System.exit(1);
138 return;
139 }
0b0b2b0f 140
bcb54330
NR
141 files.addAll(open("."));
142 }
143
a046fa49
NR
144 // TODO error case when no file
145
296a0b75
NR
146 Window win = new MainWindow(new FileList(files));
147
bcb54330 148 try {
296a0b75 149 TuiLauncher.start(textMode, win);
bcb54330
NR
150 } catch (IOException ioe) {
151 ioe.printStackTrace();
152 System.exit(2);
153 }
0b0b2b0f
NR
154 }
155
bcb54330
NR
156 /**
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.
159 *
160 * @param path
161 * the path to open
162 *
163 * @return the list of opened files
164 */
7f82bf68
NR
165 static private List<String> open(String path) {
166 List<String> files = new LinkedList<String>();
bcb54330 167
a046fa49
NR
168 if (path != null && path.startsWith("jvcard://")) {
169 if (path.endsWith("/")) {
170 files.addAll(list(path));
bcb54330 171 } else {
a046fa49 172 files.add(path);
bcb54330
NR
173 }
174 } else {
a046fa49
NR
175 File file = new File(path);
176 if (file.exists()) {
177 if (file.isDirectory()) {
178 for (File subfile : file.listFiles()) {
179 if (!subfile.isDirectory())
180 files.add(subfile.getAbsolutePath());
181 }
182 } else {
183 files.add(file.getAbsolutePath());
184 }
185 } else {
186 System.err.println("File or directory not found: \"" + path
187 + "\"");
188 }
189 }
190
191 return files;
192 }
193
194 /**
195 * List all the available {@link Card}s on the given network location (which
196 * is expected to be a jVCard remote server, obviously).
197 *
198 * @param path
199 * the jVCard remote server path (e.g.:
200 * <tt>jvcard://localhost:4444/</tt>)
201 *
202 * @return the list of {@link Card}s
203 */
204 static private List<String> list(String path) {
205 List<String> files = new LinkedList<String>();
206
207 try {
208 String host = path.split("\\:")[1].substring(2);
209 int port = Integer.parseInt(path.split("\\:")[2].replaceAll("/$",
210 ""));
211 SimpleSocket s = new SimpleSocket(new Socket(host, port),
212 "sync client");
213 s.open(true);
214
215 s.sendCommand(Verb.LIST);
216 for (String p : s.receiveBlock()) {
217 files.add(path
218 + p.substring(StringUtils.fromTime(0).length() + 1));
219 }
220 s.close();
221 } catch (Exception e) {
222 e.printStackTrace();
bcb54330
NR
223 }
224
225 return files;
226 }
227
d66e24cc
NR
228 /**
229 * Really, really ask for UTF-8 encoding.
230 */
231 static private void utf8() {
232 try {
233 System.setProperty("file.encoding", "UTF-8");
234 Field charset = Charset.class.getDeclaredField("defaultCharset");
235 charset.setAccessible(true);
236 charset.set(null, null);
6b6a62ca
NR
237 } catch (SecurityException e) {
238 } catch (NoSuchFieldException e) {
239 } catch (IllegalArgumentException e) {
240 } catch (IllegalAccessException e) {
d66e24cc
NR
241 }
242 }
0b0b2b0f 243}