1 package be
.nikiroo
.fanfix
.reader
.tui
;
3 import java
.awt
.Toolkit
;
4 import java
.awt
.datatransfer
.DataFlavor
;
5 import java
.io
.IOException
;
7 import java
.net
.UnknownHostException
;
9 import jexer
.TApplication
;
10 import jexer
.TCommand
;
11 import jexer
.TKeypress
;
12 import jexer
.TMessageBox
;
13 import jexer
.TMessageBox
.Result
;
14 import jexer
.TMessageBox
.Type
;
15 import jexer
.TStatusBar
;
18 import jexer
.event
.TCommandEvent
;
19 import jexer
.event
.TMenuEvent
;
20 import jexer
.menu
.TMenu
;
21 import be
.nikiroo
.fanfix
.Instance
;
22 import be
.nikiroo
.fanfix
.data
.MetaData
;
23 import be
.nikiroo
.fanfix
.data
.Story
;
24 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
25 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
26 import be
.nikiroo
.fanfix
.reader
.Reader
;
27 import be
.nikiroo
.fanfix
.reader
.tui
.TuiReaderMainWindow
.Mode
;
28 import be
.nikiroo
.fanfix
.supported
.SupportType
;
29 import be
.nikiroo
.utils
.Progress
;
32 * Manages the TUI general mode and links and manages the {@link TWindow}s.
34 * It will also enclose a {@link Reader} and simply handle the reading part
35 * differently (it will create the required sub-windows and display them).
39 class TuiReaderApplication
extends TApplication
implements Reader
{
40 public static final int MENU_OPEN
= 1025;
41 public static final int MENU_IMPORT_URL
= 1026;
42 public static final int MENU_IMPORT_FILE
= 1027;
43 public static final int MENU_EXPORT
= 1028;
44 public static final int MENU_DELETE
= 1029;
45 public static final int MENU_LIBRARY
= 1030;
46 public static final int MENU_EXIT
= 1031;
48 public static final TCommand CMD_EXIT
= new TCommand(MENU_EXIT
) {
51 private Reader reader
;
52 private TuiReaderMainWindow main
;
54 // start reading if meta present
55 public TuiReaderApplication(Reader reader
, BackendType backend
)
60 if (getMeta() != null) {
65 public TuiReaderApplication(Reader reader
, String source
,
66 TApplication
.BackendType backend
) throws Exception
{
71 main
.setMode(Mode
.SOURCE
, source
);
75 public void read(boolean sync
) throws IOException
{
76 read(getStory(null), sync
);
80 public MetaData
getMeta() {
81 return reader
.getMeta();
85 public Story
getStory(Progress pg
) {
86 return reader
.getStory(pg
);
90 public BasicLibrary
getLibrary() {
91 return reader
.getLibrary();
95 public void setLibrary(BasicLibrary lib
) {
96 reader
.setLibrary(lib
);
100 public void setMeta(MetaData meta
) throws IOException
{
101 reader
.setMeta(meta
);
105 public void setMeta(String luid
) throws IOException
{
106 reader
.setMeta(luid
);
110 public void setMeta(URL source
, Progress pg
) throws IOException
{
111 reader
.setMeta(source
, pg
);
115 public void browse(String source
) {
116 reader
.browse(source
);
120 public int getChapter() {
121 return reader
.getChapter();
125 public void setChapter(int chapter
) {
126 reader
.setChapter(chapter
);
130 public void search(SupportType searchOn
, String keywords
, int page
,
131 int item
, boolean sync
) throws IOException
{
132 reader
.search(searchOn
, keywords
, page
, item
, sync
);
136 public void searchTag(SupportType searchOn
, int page
, int item
,
137 boolean sync
, Integer
... tags
) throws IOException
{
138 reader
.searchTag(searchOn
, page
, item
, sync
, tags
);
142 * Open the given {@link Story} for reading. This may or may not start an
143 * external program to read said {@link Story}.
146 * the {@link Story} to read
148 * execute the process synchronously (wait until it is terminated
151 * @throws IOException
152 * in case of I/O errors
154 public void read(Story story
, boolean sync
) throws IOException
{
156 throw new IOException("No story to read");
159 // TODO: open in editor + external option
160 if (!story
.getMeta().isImageDocument()) {
161 TWindow window
= new TuiReaderStoryWindow(this, story
, getChapter());
165 openExternal(getLibrary(), story
.getMeta().getLuid(), sync
);
166 } catch (IOException e
) {
167 messageBox("Error when trying to open the story",
168 e
.getMessage(), TMessageBox
.Type
.OK
);
174 * Set the default status bar when this window appear.
176 * Some shortcuts are always visible, and will be put here.
178 * Note that shortcuts placed this way on menu won't work unless the menu
179 * also implement them.
182 * the new window or menu on screen
184 * the description to show on the status ba
186 public TStatusBar
setStatusBar(TWindow window
, String description
) {
187 TStatusBar statusBar
= window
.newStatusBar(description
);
188 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, CMD_EXIT
, "Exit");
193 private void showMain() {
194 if (main
!= null && main
.isVisible()) {
200 main
= new TuiReaderMainWindow(this);
205 private void init(Reader reader
) {
206 this.reader
= reader
;
208 // TODO: traces/errors?
209 Instance
.setTraceHandler(null);
211 // Add the menus TODO: i18n
212 TMenu fileMenu
= addMenu("&File");
213 fileMenu
.addItem(MENU_OPEN
, "&Open...");
214 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
215 fileMenu
.addItem(MENU_DELETE
, "&Delete...");
217 fileMenu
.addSeparator();
218 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
219 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
220 fileMenu
.addSeparator();
221 fileMenu
.addItem(MENU_LIBRARY
, "Lib&rary");
222 fileMenu
.addSeparator();
223 fileMenu
.addItem(MENU_EXIT
, "E&xit");
225 setStatusBar(fileMenu
, "File-management "
226 + "commands (Open, Save, Print, etc.)");
228 // TODO: Edit: re-download, delete
234 getBackend().setTitle("Fanfix");
238 protected boolean onCommand(TCommandEvent command
) {
239 if (command
.getCmd().equals(TuiReaderMainWindow
.CMD_SEARCH
)) {
240 messageBox("title", "caption");
243 return super.onCommand(command
);
247 protected boolean onMenu(TMenuEvent menu
) {
249 switch (menu
.getId()) {
254 String openfile
= null;
256 openfile
= fileOpenBox(".");
257 reader
.setMeta(BasicReader
.getUrl(openfile
), null);
259 } catch (IOException e
) {
261 error("Fail to open file"
262 + (openfile
== null ?
"" : ": " + openfile
),
270 MetaData meta
= null;
272 meta
= main
.getSelectedMeta();
275 luid
= meta
.getLuid();
276 story
= luid
+ ": " + meta
.getTitle();
280 TMessageBox mbox
= messageBox("Delete story", "Delete story \""
281 + story
+ "\"", Type
.OKCANCEL
);
282 if (mbox
.getResult() == Result
.OK
) {
284 reader
.getLibrary().delete(luid
);
286 main
.refreshStories();
288 } catch (IOException e
) {
290 error("Fail to delete the story: \"" + story
+ "\"",
296 case MENU_IMPORT_URL
:
297 String clipboard
= "";
299 clipboard
= ("" + Toolkit
.getDefaultToolkit()
300 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
302 } catch (Exception e
) {
303 // No data will be handled
306 if (clipboard
== null || !clipboard
.startsWith("http")) {
310 String url
= inputBox("Import story", "URL to import", clipboard
)
316 error("URK not supported: " + url
, "Import error");
318 } catch (IOException e
) {
320 error("Fail to import URL: " + url
, "Import error", e
);
324 case MENU_IMPORT_FILE
:
325 String filename
= null;
327 filename
= fileOpenBox(".");
328 if (!imprt(filename
)) {
330 error("File not supported: " + filename
, "Import error");
332 } catch (IOException e
) {
334 error("Fail to import file"
335 + (filename
== null ?
"" : ": " + filename
),
344 return super.onMenu(menu
);
348 * Import the given url.
350 * Can fail if the host is not supported.
354 * @return TRUE in case of success, FALSE if the host is not supported
356 * @throws IOException
357 * in case of I/O error
359 private boolean imprt(String url
) throws IOException
{
361 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
362 main
.refreshStories();
364 } catch (UnknownHostException e
) {
370 public void openExternal(BasicLibrary lib
, String luid
, boolean sync
)
372 reader
.openExternal(lib
, luid
, sync
);
376 * Display an error message and log it.
381 * the title of the error message
383 private void error(String message
, String title
) {
384 error(message
, title
, null);
388 * Display an error message and log it, including the linked
394 * the title of the error message
396 * the exception to log if any (can be NULL)
398 private void error(String message
, String title
, Exception e
) {
399 Instance
.getTraceHandler().error(title
+ ": " + message
);
401 Instance
.getTraceHandler().error(e
);
405 messageBox(title
, message
//
406 + "\n" + e
.getMessage());
408 messageBox(title
, message
);
413 * Ask the user and, if confirmed, close the {@link TApplication} this
414 * {@link TWidget} is running on.
416 * This should result in the program terminating.
419 * the {@link TWidget}
421 static public void close(TWidget widget
) {
422 close(widget
.getApplication());
426 * Ask the user and, if confirmed, close the {@link TApplication}.
428 * This should result in the program terminating.
431 * the {@link TApplication}
433 static void close(TApplication app
) {
435 if (app
.messageBox("Confirmation", "(TODO: i18n) Exit application?",
436 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {