Commit | Line | Data |
---|---|---|
16a81ef7 | 1 | package be.nikiroo.fanfix.reader.tui; |
c1873e56 | 2 | |
e2d017a3 NR |
3 | import java.awt.Toolkit; |
4 | import java.awt.datatransfer.DataFlavor; | |
c1873e56 | 5 | import java.io.IOException; |
6322ab64 | 6 | import java.net.URL; |
4f9478dc | 7 | import java.net.UnknownHostException; |
c1873e56 NR |
8 | |
9 | import jexer.TApplication; | |
e2d017a3 NR |
10 | import jexer.TCommand; |
11 | import jexer.TKeypress; | |
c1873e56 | 12 | import jexer.TMessageBox; |
cd6b47c9 NR |
13 | import jexer.TMessageBox.Result; |
14 | import jexer.TMessageBox.Type; | |
e2d017a3 | 15 | import jexer.TStatusBar; |
cf9c5ed1 | 16 | import jexer.TWidget; |
6322ab64 | 17 | import jexer.TWindow; |
bce88a00 | 18 | import jexer.event.TCommandEvent; |
e2d017a3 NR |
19 | import jexer.event.TMenuEvent; |
20 | import jexer.menu.TMenu; | |
a8209dd0 | 21 | import be.nikiroo.fanfix.Instance; |
c1873e56 | 22 | import be.nikiroo.fanfix.data.MetaData; |
6322ab64 NR |
23 | import be.nikiroo.fanfix.data.Story; |
24 | import be.nikiroo.fanfix.library.BasicLibrary; | |
16a81ef7 NR |
25 | import be.nikiroo.fanfix.reader.BasicReader; |
26 | import be.nikiroo.fanfix.reader.Reader; | |
b2274262 | 27 | import be.nikiroo.fanfix.reader.tui.TuiReaderMainWindow.Mode; |
6322ab64 | 28 | import be.nikiroo.utils.Progress; |
c1873e56 | 29 | |
6322ab64 | 30 | /** |
bc2ea776 NR |
31 | * Manages the TUI general mode and links and manages the {@link TWindow}s. |
32 | * <p> | |
33 | * It will also enclose a {@link Reader} and simply handle the reading part | |
34 | * differently (it will create the required sub-windows and display them). | |
6322ab64 NR |
35 | * |
36 | * @author niki | |
37 | */ | |
38 | class TuiReaderApplication extends TApplication implements Reader { | |
e2d017a3 NR |
39 | public static final int MENU_OPEN = 1025; |
40 | public static final int MENU_IMPORT_URL = 1026; | |
41 | public static final int MENU_IMPORT_FILE = 1027; | |
42 | public static final int MENU_EXPORT = 1028; | |
cd6b47c9 NR |
43 | public static final int MENU_DELETE = 1029; |
44 | public static final int MENU_LIBRARY = 1030; | |
45 | public static final int MENU_EXIT = 1031; | |
e2d017a3 | 46 | |
cf9c5ed1 NR |
47 | public static final TCommand CMD_EXIT = new TCommand(MENU_EXIT) { |
48 | }; | |
49 | ||
6322ab64 | 50 | private Reader reader; |
e2d017a3 | 51 | private TuiReaderMainWindow main; |
4f66bfa8 | 52 | |
bc2ea776 NR |
53 | // start reading if meta present |
54 | public TuiReaderApplication(Reader reader, BackendType backend) | |
55 | throws Exception { | |
56 | super(backend); | |
57 | init(reader); | |
c1873e56 | 58 | |
350bc060 NR |
59 | if (getMeta() != null) { |
60 | read(false); | |
e0fb1417 | 61 | } |
6322ab64 NR |
62 | } |
63 | ||
e2d017a3 | 64 | public TuiReaderApplication(Reader reader, String source, |
bc2ea776 | 65 | TApplication.BackendType backend) throws Exception { |
c1873e56 | 66 | super(backend); |
bc2ea776 | 67 | init(reader); |
c1873e56 | 68 | |
e0fb1417 | 69 | showMain(); |
b2274262 | 70 | main.setMode(Mode.SOURCE, source); |
4f66bfa8 NR |
71 | } |
72 | ||
211f7ddb | 73 | @Override |
350bc060 NR |
74 | public void read(boolean sync) throws IOException { |
75 | read(getStory(null), sync); | |
c1873e56 | 76 | } |
6322ab64 | 77 | |
211f7ddb | 78 | @Override |
bc2ea776 NR |
79 | public MetaData getMeta() { |
80 | return reader.getMeta(); | |
81 | } | |
82 | ||
211f7ddb | 83 | @Override |
bc2ea776 NR |
84 | public Story getStory(Progress pg) { |
85 | return reader.getStory(pg); | |
6322ab64 NR |
86 | } |
87 | ||
211f7ddb | 88 | @Override |
6322ab64 NR |
89 | public BasicLibrary getLibrary() { |
90 | return reader.getLibrary(); | |
91 | } | |
92 | ||
211f7ddb | 93 | @Override |
bc2ea776 | 94 | public void setLibrary(BasicLibrary lib) { |
6322ab64 NR |
95 | reader.setLibrary(lib); |
96 | } | |
97 | ||
211f7ddb | 98 | @Override |
bc2ea776 NR |
99 | public void setMeta(MetaData meta) throws IOException { |
100 | reader.setMeta(meta); | |
101 | } | |
102 | ||
211f7ddb | 103 | @Override |
bc2ea776 NR |
104 | public void setMeta(String luid) throws IOException { |
105 | reader.setMeta(luid); | |
6322ab64 NR |
106 | } |
107 | ||
211f7ddb | 108 | @Override |
bc2ea776 NR |
109 | public void setMeta(URL source, Progress pg) throws IOException { |
110 | reader.setMeta(source, pg); | |
6322ab64 NR |
111 | } |
112 | ||
211f7ddb | 113 | @Override |
6322ab64 NR |
114 | public void browse(String source) { |
115 | reader.browse(source); | |
116 | } | |
bc2ea776 | 117 | |
211f7ddb | 118 | @Override |
bc2ea776 NR |
119 | public int getChapter() { |
120 | return reader.getChapter(); | |
121 | } | |
122 | ||
211f7ddb | 123 | @Override |
bc2ea776 NR |
124 | public void setChapter(int chapter) { |
125 | reader.setChapter(chapter); | |
126 | } | |
127 | ||
350bc060 NR |
128 | /** |
129 | * Open the given {@link Story} for reading. This may or may not start an | |
130 | * external program to read said {@link Story}. | |
131 | * | |
132 | * @param story | |
133 | * the {@link Story} to read | |
134 | * @param sync | |
135 | * execute the process synchronously (wait until it is terminated | |
136 | * before returning) | |
137 | * | |
138 | * @throws IOException | |
139 | * in case of I/O errors | |
140 | */ | |
141 | public void read(Story story, boolean sync) throws IOException { | |
e0fb1417 NR |
142 | if (story == null) { |
143 | throw new IOException("No story to read"); | |
144 | } | |
145 | ||
146 | // TODO: open in editor + external option | |
147 | if (!story.getMeta().isImageDocument()) { | |
148 | TWindow window = new TuiReaderStoryWindow(this, story, getChapter()); | |
149 | window.maximize(); | |
150 | } else { | |
151 | try { | |
350bc060 | 152 | openExternal(getLibrary(), story.getMeta().getLuid(), sync); |
e0fb1417 NR |
153 | } catch (IOException e) { |
154 | messageBox("Error when trying to open the story", | |
155 | e.getMessage(), TMessageBox.Type.OK); | |
156 | } | |
157 | } | |
158 | } | |
159 | ||
5b00c122 NR |
160 | /** |
161 | * Set the default status bar when this window appear. | |
162 | * <p> | |
163 | * Some shortcuts are always visible, and will be put here. | |
164 | * <p> | |
165 | * Note that shortcuts placed this way on menu won't work unless the menu | |
166 | * also implement them. | |
167 | * | |
168 | * @param window | |
169 | * the new window or menu on screen | |
170 | * @param description | |
171 | * the description to show on the status ba | |
172 | */ | |
173 | public TStatusBar setStatusBar(TWindow window, String description) { | |
174 | TStatusBar statusBar = window.newStatusBar(description); | |
cf9c5ed1 | 175 | statusBar.addShortcutKeypress(TKeypress.kbF10, CMD_EXIT, "Exit"); |
5b00c122 NR |
176 | return statusBar; |
177 | ||
178 | } | |
179 | ||
e0fb1417 | 180 | private void showMain() { |
5b00c122 NR |
181 | if (main != null && main.isVisible()) { |
182 | main.activate(); | |
183 | } else { | |
184 | if (main != null) { | |
185 | main.close(); | |
186 | } | |
187 | main = new TuiReaderMainWindow(this); | |
11758a0f | 188 | main.maximize(); |
5b00c122 NR |
189 | } |
190 | } | |
191 | ||
bc2ea776 NR |
192 | private void init(Reader reader) { |
193 | this.reader = reader; | |
194 | ||
581d42c0 NR |
195 | // TODO: traces/errors? |
196 | Instance.setTraceHandler(null); | |
a8209dd0 | 197 | |
e2d017a3 NR |
198 | // Add the menus TODO: i18n |
199 | TMenu fileMenu = addMenu("&File"); | |
cd6b47c9 | 200 | fileMenu.addItem(MENU_OPEN, "&Open..."); |
e2d017a3 | 201 | fileMenu.addItem(MENU_EXPORT, "&Save as..."); |
cd6b47c9 | 202 | fileMenu.addItem(MENU_DELETE, "&Delete..."); |
e2d017a3 NR |
203 | // TODO: Move to... |
204 | fileMenu.addSeparator(); | |
205 | fileMenu.addItem(MENU_IMPORT_URL, "Import &URL..."); | |
206 | fileMenu.addItem(MENU_IMPORT_FILE, "Import &file..."); | |
207 | fileMenu.addSeparator(); | |
4f66bfa8 NR |
208 | fileMenu.addItem(MENU_LIBRARY, "Lib&rary"); |
209 | fileMenu.addSeparator(); | |
e2d017a3 NR |
210 | fileMenu.addItem(MENU_EXIT, "E&xit"); |
211 | ||
5b00c122 | 212 | setStatusBar(fileMenu, "File-management " |
e2d017a3 | 213 | + "commands (Open, Save, Print, etc.)"); |
e2d017a3 NR |
214 | |
215 | // TODO: Edit: re-download, delete | |
216 | ||
217 | // | |
218 | ||
bc2ea776 | 219 | addWindowMenu(); |
bc2ea776 NR |
220 | |
221 | getBackend().setTitle("Fanfix"); | |
222 | } | |
e2d017a3 | 223 | |
bce88a00 NR |
224 | @Override |
225 | protected boolean onCommand(TCommandEvent command) { | |
226 | if (command.getCmd().equals(TuiReaderMainWindow.CMD_SEARCH)) { | |
227 | messageBox("title", "caption"); | |
228 | return true; | |
229 | } | |
230 | return super.onCommand(command); | |
231 | } | |
232 | ||
e2d017a3 NR |
233 | @Override |
234 | protected boolean onMenu(TMenuEvent menu) { | |
235 | // TODO: i18n | |
236 | switch (menu.getId()) { | |
237 | case MENU_EXIT: | |
cf9c5ed1 | 238 | close(this); |
e0fb1417 NR |
239 | return true; |
240 | case MENU_OPEN: | |
241 | String openfile = null; | |
242 | try { | |
243 | openfile = fileOpenBox("."); | |
244 | reader.setMeta(BasicReader.getUrl(openfile), null); | |
350bc060 | 245 | read(false); |
e0fb1417 NR |
246 | } catch (IOException e) { |
247 | // TODO: i18n | |
248 | error("Fail to open file" | |
249 | + (openfile == null ? "" : ": " + openfile), | |
250 | "Import error", e); | |
251 | } | |
252 | ||
cd6b47c9 NR |
253 | return true; |
254 | case MENU_DELETE: | |
255 | String luid = null; | |
256 | String story = null; | |
257 | MetaData meta = null; | |
258 | if (main != null) { | |
259 | meta = main.getSelectedMeta(); | |
260 | } | |
261 | if (meta != null) { | |
262 | luid = meta.getLuid(); | |
263 | story = luid + ": " + meta.getTitle(); | |
264 | } | |
265 | ||
266 | // TODO: i18n | |
267 | TMessageBox mbox = messageBox("Delete story", "Delete story \"" | |
268 | + story + "\"", Type.OKCANCEL); | |
269 | if (mbox.getResult() == Result.OK) { | |
270 | try { | |
271 | reader.getLibrary().delete(luid); | |
272 | if (main != null) { | |
273 | main.refreshStories(); | |
274 | } | |
275 | } catch (IOException e) { | |
276 | // TODO: i18n | |
277 | error("Fail to delete the story: \"" + story + "\"", | |
278 | "Error", e); | |
279 | } | |
280 | } | |
281 | ||
e2d017a3 NR |
282 | return true; |
283 | case MENU_IMPORT_URL: | |
284 | String clipboard = ""; | |
285 | try { | |
286 | clipboard = ("" + Toolkit.getDefaultToolkit() | |
287 | .getSystemClipboard().getData(DataFlavor.stringFlavor)) | |
288 | .trim(); | |
289 | } catch (Exception e) { | |
290 | // No data will be handled | |
291 | } | |
292 | ||
293 | if (clipboard == null || !clipboard.startsWith("http")) { | |
294 | clipboard = ""; | |
295 | } | |
296 | ||
297 | String url = inputBox("Import story", "URL to import", clipboard) | |
298 | .getText(); | |
299 | ||
4f9478dc NR |
300 | try { |
301 | if (!imprt(url)) { | |
e0fb1417 | 302 | // TODO: i18n |
4f9478dc NR |
303 | error("URK not supported: " + url, "Import error"); |
304 | } | |
305 | } catch (IOException e) { | |
e0fb1417 | 306 | // TODO: i18n |
4f9478dc | 307 | error("Fail to import URL: " + url, "Import error", e); |
e2d017a3 NR |
308 | } |
309 | ||
310 | return true; | |
311 | case MENU_IMPORT_FILE: | |
4f9478dc | 312 | String filename = null; |
e2d017a3 | 313 | try { |
4f9478dc | 314 | filename = fileOpenBox("."); |
e2d017a3 | 315 | if (!imprt(filename)) { |
e0fb1417 | 316 | // TODO: i18n |
4f9478dc | 317 | error("File not supported: " + filename, "Import error"); |
e2d017a3 NR |
318 | } |
319 | } catch (IOException e) { | |
e0fb1417 | 320 | // TODO: i18n |
4f9478dc NR |
321 | error("Fail to import file" |
322 | + (filename == null ? "" : ": " + filename), | |
323 | "Import error", e); | |
e2d017a3 | 324 | } |
4f66bfa8 NR |
325 | return true; |
326 | case MENU_LIBRARY: | |
e0fb1417 | 327 | showMain(); |
e2d017a3 NR |
328 | return true; |
329 | } | |
330 | ||
331 | return super.onMenu(menu); | |
332 | } | |
333 | ||
4f9478dc NR |
334 | /** |
335 | * Import the given url. | |
336 | * <p> | |
337 | * Can fail if the host is not supported. | |
338 | * | |
339 | * @param url | |
340 | * | |
341 | * @return TRUE in case of success, FALSE if the host is not supported | |
342 | * | |
343 | * @throws IOException | |
344 | * in case of I/O error | |
345 | */ | |
346 | private boolean imprt(String url) throws IOException { | |
e2d017a3 NR |
347 | try { |
348 | reader.getLibrary().imprt(BasicReader.getUrl(url), null); | |
349 | main.refreshStories(); | |
350 | return true; | |
4f9478dc | 351 | } catch (UnknownHostException e) { |
e2d017a3 NR |
352 | return false; |
353 | } | |
354 | } | |
16a81ef7 NR |
355 | |
356 | @Override | |
350bc060 NR |
357 | public void openExternal(BasicLibrary lib, String luid, boolean sync) |
358 | throws IOException { | |
359 | reader.openExternal(lib, luid, sync); | |
16a81ef7 | 360 | } |
cf9c5ed1 | 361 | |
4f9478dc NR |
362 | /** |
363 | * Display an error message and log it. | |
364 | * | |
365 | * @param message | |
366 | * the message | |
367 | * @param title | |
368 | * the title of the error message | |
369 | */ | |
370 | private void error(String message, String title) { | |
371 | error(message, title, null); | |
372 | } | |
373 | ||
374 | /** | |
375 | * Display an error message and log it, including the linked | |
376 | * {@link Exception}. | |
377 | * | |
378 | * @param message | |
379 | * the message | |
380 | * @param title | |
381 | * the title of the error message | |
382 | * @param e | |
383 | * the exception to log if any (can be NULL) | |
384 | */ | |
385 | private void error(String message, String title, Exception e) { | |
386 | Instance.getTraceHandler().error(title + ": " + message); | |
387 | if (e != null) { | |
388 | Instance.getTraceHandler().error(e); | |
389 | } | |
390 | ||
391 | if (e != null) { | |
392 | messageBox(title, message // | |
393 | + "\n" + e.getMessage()); | |
394 | } else { | |
395 | messageBox(title, message); | |
396 | } | |
397 | } | |
398 | ||
cf9c5ed1 NR |
399 | /** |
400 | * Ask the user and, if confirmed, close the {@link TApplication} this | |
401 | * {@link TWidget} is running on. | |
402 | * <p> | |
403 | * This should result in the program terminating. | |
404 | * | |
405 | * @param widget | |
406 | * the {@link TWidget} | |
407 | */ | |
408 | static public void close(TWidget widget) { | |
409 | close(widget.getApplication()); | |
410 | } | |
411 | ||
412 | /** | |
413 | * Ask the user and, if confirmed, close the {@link TApplication}. | |
414 | * <p> | |
415 | * This should result in the program terminating. | |
416 | * | |
417 | * @param app | |
418 | * the {@link TApplication} | |
419 | */ | |
420 | static void close(TApplication app) { | |
421 | // TODO: i18n | |
422 | if (app.messageBox("Confirmation", "(TODO: i18n) Exit application?", | |
423 | TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) { | |
424 | app.exit(); | |
425 | } | |
426 | } | |
c1873e56 | 427 | } |