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
, int item
) {
131 reader
.search(searchOn
, keywords
, page
,item
);
135 public void searchTag(SupportType searchOn
, int page
, int item
, String
... tags
) {
136 reader
.searchTag(searchOn
, page
, item
, tags
);
140 * Open the given {@link Story} for reading. This may or may not start an
141 * external program to read said {@link Story}.
144 * the {@link Story} to read
146 * execute the process synchronously (wait until it is terminated
149 * @throws IOException
150 * in case of I/O errors
152 public void read(Story story
, boolean sync
) throws IOException
{
154 throw new IOException("No story to read");
157 // TODO: open in editor + external option
158 if (!story
.getMeta().isImageDocument()) {
159 TWindow window
= new TuiReaderStoryWindow(this, story
, getChapter());
163 openExternal(getLibrary(), story
.getMeta().getLuid(), sync
);
164 } catch (IOException e
) {
165 messageBox("Error when trying to open the story",
166 e
.getMessage(), TMessageBox
.Type
.OK
);
172 * Set the default status bar when this window appear.
174 * Some shortcuts are always visible, and will be put here.
176 * Note that shortcuts placed this way on menu won't work unless the menu
177 * also implement them.
180 * the new window or menu on screen
182 * the description to show on the status ba
184 public TStatusBar
setStatusBar(TWindow window
, String description
) {
185 TStatusBar statusBar
= window
.newStatusBar(description
);
186 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, CMD_EXIT
, "Exit");
191 private void showMain() {
192 if (main
!= null && main
.isVisible()) {
198 main
= new TuiReaderMainWindow(this);
203 private void init(Reader reader
) {
204 this.reader
= reader
;
206 // TODO: traces/errors?
207 Instance
.setTraceHandler(null);
209 // Add the menus TODO: i18n
210 TMenu fileMenu
= addMenu("&File");
211 fileMenu
.addItem(MENU_OPEN
, "&Open...");
212 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
213 fileMenu
.addItem(MENU_DELETE
, "&Delete...");
215 fileMenu
.addSeparator();
216 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
217 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
218 fileMenu
.addSeparator();
219 fileMenu
.addItem(MENU_LIBRARY
, "Lib&rary");
220 fileMenu
.addSeparator();
221 fileMenu
.addItem(MENU_EXIT
, "E&xit");
223 setStatusBar(fileMenu
, "File-management "
224 + "commands (Open, Save, Print, etc.)");
226 // TODO: Edit: re-download, delete
232 getBackend().setTitle("Fanfix");
236 protected boolean onCommand(TCommandEvent command
) {
237 if (command
.getCmd().equals(TuiReaderMainWindow
.CMD_SEARCH
)) {
238 messageBox("title", "caption");
241 return super.onCommand(command
);
245 protected boolean onMenu(TMenuEvent menu
) {
247 switch (menu
.getId()) {
252 String openfile
= null;
254 openfile
= fileOpenBox(".");
255 reader
.setMeta(BasicReader
.getUrl(openfile
), null);
257 } catch (IOException e
) {
259 error("Fail to open file"
260 + (openfile
== null ?
"" : ": " + openfile
),
268 MetaData meta
= null;
270 meta
= main
.getSelectedMeta();
273 luid
= meta
.getLuid();
274 story
= luid
+ ": " + meta
.getTitle();
278 TMessageBox mbox
= messageBox("Delete story", "Delete story \""
279 + story
+ "\"", Type
.OKCANCEL
);
280 if (mbox
.getResult() == Result
.OK
) {
282 reader
.getLibrary().delete(luid
);
284 main
.refreshStories();
286 } catch (IOException e
) {
288 error("Fail to delete the story: \"" + story
+ "\"",
294 case MENU_IMPORT_URL
:
295 String clipboard
= "";
297 clipboard
= ("" + Toolkit
.getDefaultToolkit()
298 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
300 } catch (Exception e
) {
301 // No data will be handled
304 if (clipboard
== null || !clipboard
.startsWith("http")) {
308 String url
= inputBox("Import story", "URL to import", clipboard
)
314 error("URK not supported: " + url
, "Import error");
316 } catch (IOException e
) {
318 error("Fail to import URL: " + url
, "Import error", e
);
322 case MENU_IMPORT_FILE
:
323 String filename
= null;
325 filename
= fileOpenBox(".");
326 if (!imprt(filename
)) {
328 error("File not supported: " + filename
, "Import error");
330 } catch (IOException e
) {
332 error("Fail to import file"
333 + (filename
== null ?
"" : ": " + filename
),
342 return super.onMenu(menu
);
346 * Import the given url.
348 * Can fail if the host is not supported.
352 * @return TRUE in case of success, FALSE if the host is not supported
354 * @throws IOException
355 * in case of I/O error
357 private boolean imprt(String url
) throws IOException
{
359 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
360 main
.refreshStories();
362 } catch (UnknownHostException e
) {
368 public void openExternal(BasicLibrary lib
, String luid
, boolean sync
)
370 reader
.openExternal(lib
, luid
, sync
);
374 * Display an error message and log it.
379 * the title of the error message
381 private void error(String message
, String title
) {
382 error(message
, title
, null);
386 * Display an error message and log it, including the linked
392 * the title of the error message
394 * the exception to log if any (can be NULL)
396 private void error(String message
, String title
, Exception e
) {
397 Instance
.getTraceHandler().error(title
+ ": " + message
);
399 Instance
.getTraceHandler().error(e
);
403 messageBox(title
, message
//
404 + "\n" + e
.getMessage());
406 messageBox(title
, message
);
411 * Ask the user and, if confirmed, close the {@link TApplication} this
412 * {@link TWidget} is running on.
414 * This should result in the program terminating.
417 * the {@link TWidget}
419 static public void close(TWidget widget
) {
420 close(widget
.getApplication());
424 * Ask the user and, if confirmed, close the {@link TApplication}.
426 * This should result in the program terminating.
429 * the {@link TApplication}
431 static void close(TApplication app
) {
433 if (app
.messageBox("Confirmation", "(TODO: i18n) Exit application?",
434 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {