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
) throws IOException
{
131 reader
.search(searchOn
, keywords
, page
, item
);
135 public void searchTag(SupportType searchOn
, int page
, int item
,
136 Integer
... tags
) throws IOException
{
137 reader
.searchTag(searchOn
, page
, item
, tags
);
141 * Open the given {@link Story} for reading. This may or may not start an
142 * external program to read said {@link Story}.
145 * the {@link Story} to read
147 * execute the process synchronously (wait until it is terminated
150 * @throws IOException
151 * in case of I/O errors
153 public void read(Story story
, boolean sync
) throws IOException
{
155 throw new IOException("No story to read");
158 // TODO: open in editor + external option
159 if (!story
.getMeta().isImageDocument()) {
160 TWindow window
= new TuiReaderStoryWindow(this, story
, getChapter());
164 openExternal(getLibrary(), story
.getMeta().getLuid(), sync
);
165 } catch (IOException e
) {
166 messageBox("Error when trying to open the story",
167 e
.getMessage(), TMessageBox
.Type
.OK
);
173 * Set the default status bar when this window appear.
175 * Some shortcuts are always visible, and will be put here.
177 * Note that shortcuts placed this way on menu won't work unless the menu
178 * also implement them.
181 * the new window or menu on screen
183 * the description to show on the status ba
185 public TStatusBar
setStatusBar(TWindow window
, String description
) {
186 TStatusBar statusBar
= window
.newStatusBar(description
);
187 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, CMD_EXIT
, "Exit");
192 private void showMain() {
193 if (main
!= null && main
.isVisible()) {
199 main
= new TuiReaderMainWindow(this);
204 private void init(Reader reader
) {
205 this.reader
= reader
;
207 // TODO: traces/errors?
208 Instance
.setTraceHandler(null);
210 // Add the menus TODO: i18n
211 TMenu fileMenu
= addMenu("&File");
212 fileMenu
.addItem(MENU_OPEN
, "&Open...");
213 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
214 fileMenu
.addItem(MENU_DELETE
, "&Delete...");
216 fileMenu
.addSeparator();
217 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
218 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
219 fileMenu
.addSeparator();
220 fileMenu
.addItem(MENU_LIBRARY
, "Lib&rary");
221 fileMenu
.addSeparator();
222 fileMenu
.addItem(MENU_EXIT
, "E&xit");
224 setStatusBar(fileMenu
, "File-management "
225 + "commands (Open, Save, Print, etc.)");
227 // TODO: Edit: re-download, delete
233 getBackend().setTitle("Fanfix");
237 protected boolean onCommand(TCommandEvent command
) {
238 if (command
.getCmd().equals(TuiReaderMainWindow
.CMD_SEARCH
)) {
239 messageBox("title", "caption");
242 return super.onCommand(command
);
246 protected boolean onMenu(TMenuEvent menu
) {
248 switch (menu
.getId()) {
253 String openfile
= null;
255 openfile
= fileOpenBox(".");
256 reader
.setMeta(BasicReader
.getUrl(openfile
), null);
258 } catch (IOException e
) {
260 error("Fail to open file"
261 + (openfile
== null ?
"" : ": " + openfile
),
269 MetaData meta
= null;
271 meta
= main
.getSelectedMeta();
274 luid
= meta
.getLuid();
275 story
= luid
+ ": " + meta
.getTitle();
279 TMessageBox mbox
= messageBox("Delete story", "Delete story \""
280 + story
+ "\"", Type
.OKCANCEL
);
281 if (mbox
.getResult() == Result
.OK
) {
283 reader
.getLibrary().delete(luid
);
285 main
.refreshStories();
287 } catch (IOException e
) {
289 error("Fail to delete the story: \"" + story
+ "\"",
295 case MENU_IMPORT_URL
:
296 String clipboard
= "";
298 clipboard
= ("" + Toolkit
.getDefaultToolkit()
299 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
301 } catch (Exception e
) {
302 // No data will be handled
305 if (clipboard
== null || !clipboard
.startsWith("http")) {
309 String url
= inputBox("Import story", "URL to import", clipboard
)
315 error("URK not supported: " + url
, "Import error");
317 } catch (IOException e
) {
319 error("Fail to import URL: " + url
, "Import error", e
);
323 case MENU_IMPORT_FILE
:
324 String filename
= null;
326 filename
= fileOpenBox(".");
327 if (!imprt(filename
)) {
329 error("File not supported: " + filename
, "Import error");
331 } catch (IOException e
) {
333 error("Fail to import file"
334 + (filename
== null ?
"" : ": " + filename
),
343 return super.onMenu(menu
);
347 * Import the given url.
349 * Can fail if the host is not supported.
353 * @return TRUE in case of success, FALSE if the host is not supported
355 * @throws IOException
356 * in case of I/O error
358 private boolean imprt(String url
) throws IOException
{
360 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
361 main
.refreshStories();
363 } catch (UnknownHostException e
) {
369 public void openExternal(BasicLibrary lib
, String luid
, boolean sync
)
371 reader
.openExternal(lib
, luid
, sync
);
375 * Display an error message and log it.
380 * the title of the error message
382 private void error(String message
, String title
) {
383 error(message
, title
, null);
387 * Display an error message and log it, including the linked
393 * the title of the error message
395 * the exception to log if any (can be NULL)
397 private void error(String message
, String title
, Exception e
) {
398 Instance
.getTraceHandler().error(title
+ ": " + message
);
400 Instance
.getTraceHandler().error(e
);
404 messageBox(title
, message
//
405 + "\n" + e
.getMessage());
407 messageBox(title
, message
);
412 * Ask the user and, if confirmed, close the {@link TApplication} this
413 * {@link TWidget} is running on.
415 * This should result in the program terminating.
418 * the {@link TWidget}
420 static public void close(TWidget widget
) {
421 close(widget
.getApplication());
425 * Ask the user and, if confirmed, close the {@link TApplication}.
427 * This should result in the program terminating.
430 * the {@link TApplication}
432 static void close(TApplication app
) {
434 if (app
.messageBox("Confirmation", "(TODO: i18n) Exit application?",
435 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {