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_FILE_OPEN
= 1025;
41 public static final int MENU_FILE_IMPORT_URL
= 1026;
42 public static final int MENU_FILE_IMPORT_FILE
= 1027;
43 public static final int MENU_FILE_EXPORT
= 1028;
44 public static final int MENU_FILE_DELETE
= 1029;
45 public static final int MENU_FILE_LIBRARY
= 1030;
46 public static final int MENU_FILE_EXIT
= 1031;
48 public static final TCommand CMD_EXIT
= new TCommand(MENU_FILE_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
) throws IOException
{
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
) {
117 reader
.browse(source
);
118 } catch (IOException e
) {
119 Instance
.getTraceHandler().error(e
);
124 public int getChapter() {
125 return reader
.getChapter();
129 public void setChapter(int chapter
) {
130 reader
.setChapter(chapter
);
134 public void search(boolean sync
) throws IOException
{
139 public void search(SupportType searchOn
, String keywords
, int page
,
140 int item
, boolean sync
) throws IOException
{
141 reader
.search(searchOn
, keywords
, page
, item
, sync
);
145 public void searchTag(SupportType searchOn
, int page
, int item
,
146 boolean sync
, Integer
... tags
) throws IOException
{
147 reader
.searchTag(searchOn
, page
, item
, sync
, tags
);
151 * Open the given {@link Story} for reading. This may or may not start an
152 * external program to read said {@link Story}.
155 * the {@link Story} to read
157 * execute the process synchronously (wait until it is terminated
160 * @throws IOException
161 * in case of I/O errors
163 public void read(Story story
, boolean sync
) throws IOException
{
165 throw new IOException("No story to read");
168 // TODO: open in editor + external option
169 if (!story
.getMeta().isImageDocument()) {
170 TWindow window
= new TuiReaderStoryWindow(this, story
, getChapter());
174 openExternal(getLibrary(), story
.getMeta().getLuid(), sync
);
175 } catch (IOException e
) {
176 messageBox("Error when trying to open the story",
177 e
.getMessage(), TMessageBox
.Type
.OK
);
183 * Set the default status bar when this window appear.
185 * Some shortcuts are always visible, and will be put here.
187 * Note that shortcuts placed this way on menu won't work unless the menu
188 * also implement them.
191 * the new window or menu on screen
193 * the description to show on the status ba
195 public TStatusBar
setStatusBar(TWindow window
, String description
) {
196 TStatusBar statusBar
= window
.newStatusBar(description
);
197 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, CMD_EXIT
, "Exit");
202 private void showMain() {
203 if (main
!= null && main
.isVisible()) {
209 main
= new TuiReaderMainWindow(this);
214 private void init(Reader reader
) {
215 this.reader
= reader
;
217 // TODO: traces/errors?
218 Instance
.setTraceHandler(null);
220 // Add the menus TODO: i18n
221 TMenu fileMenu
= addMenu("&File");
222 fileMenu
.addItem(MENU_FILE_OPEN
, "&Open...");
223 fileMenu
.addItem(MENU_FILE_EXPORT
, "&Save as...");
224 fileMenu
.addItem(MENU_FILE_DELETE
, "&Delete...");
226 fileMenu
.addSeparator();
227 fileMenu
.addItem(MENU_FILE_IMPORT_URL
, "Import &URL...");
228 fileMenu
.addItem(MENU_FILE_IMPORT_FILE
, "Import &file...");
229 fileMenu
.addSeparator();
230 fileMenu
.addItem(MENU_FILE_LIBRARY
, "Lib&rary");
231 fileMenu
.addSeparator();
232 fileMenu
.addItem(MENU_FILE_EXIT
, "E&xit");
234 setStatusBar(fileMenu
, "File-management "
235 + "commands (Open, Save, Print, etc.)");
237 // TODO: Edit: re-download, delete
243 getBackend().setTitle("Fanfix");
247 protected boolean onCommand(TCommandEvent command
) {
248 if (command
.getCmd().equals(TuiReaderMainWindow
.CMD_SEARCH
)) {
249 messageBox("title", "caption");
252 return super.onCommand(command
);
256 protected boolean onMenu(TMenuEvent menu
) {
258 switch (menu
.getId()) {
263 String openfile
= null;
265 openfile
= fileOpenBox(".");
266 reader
.setMeta(BasicReader
.getUrl(openfile
), null);
268 } catch (IOException e
) {
270 error("Fail to open file"
271 + (openfile
== null ?
"" : ": " + openfile
),
276 case MENU_FILE_DELETE
:
279 MetaData meta
= null;
281 meta
= main
.getSelectedMeta();
284 luid
= meta
.getLuid();
285 story
= luid
+ ": " + meta
.getTitle();
289 TMessageBox mbox
= messageBox("Delete story", "Delete story \""
290 + story
+ "\"", Type
.OKCANCEL
);
291 if (mbox
.getResult() == Result
.OK
) {
293 reader
.getLibrary().delete(luid
);
295 main
.refreshStories();
297 } catch (IOException e
) {
299 error("Fail to delete the story: \"" + story
+ "\"",
305 case MENU_FILE_IMPORT_URL
:
306 String clipboard
= "";
308 clipboard
= ("" + Toolkit
.getDefaultToolkit()
309 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
311 } catch (Exception e
) {
312 // No data will be handled
315 if (clipboard
== null || !clipboard
.startsWith("http")) {
319 String url
= inputBox("Import story", "URL to import", clipboard
)
325 error("URK not supported: " + url
, "Import error");
327 } catch (IOException e
) {
329 error("Fail to import URL: " + url
, "Import error", e
);
333 case MENU_FILE_IMPORT_FILE
:
334 String filename
= null;
336 filename
= fileOpenBox(".");
337 if (!imprt(filename
)) {
339 error("File not supported: " + filename
, "Import error");
341 } catch (IOException e
) {
343 error("Fail to import file"
344 + (filename
== null ?
"" : ": " + filename
),
348 case MENU_FILE_LIBRARY
:
353 return super.onMenu(menu
);
357 * Import the given url.
359 * Can fail if the host is not supported.
363 * @return TRUE in case of success, FALSE if the host is not supported
365 * @throws IOException
366 * in case of I/O error
368 private boolean imprt(String url
) throws IOException
{
370 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
371 main
.refreshStories();
373 } catch (UnknownHostException e
) {
379 public void openExternal(BasicLibrary lib
, String luid
, boolean sync
)
381 reader
.openExternal(lib
, luid
, sync
);
385 * Display an error message and log it.
390 * the title of the error message
392 private void error(String message
, String title
) {
393 error(message
, title
, null);
397 * Display an error message and log it, including the linked
403 * the title of the error message
405 * the exception to log if any (can be NULL)
407 private void error(String message
, String title
, Exception e
) {
408 Instance
.getTraceHandler().error(title
+ ": " + message
);
410 Instance
.getTraceHandler().error(e
);
414 messageBox(title
, message
//
415 + "\n" + e
.getMessage());
417 messageBox(title
, message
);
422 * Ask the user and, if confirmed, close the {@link TApplication} this
423 * {@link TWidget} is running on.
425 * This should result in the program terminating.
428 * the {@link TWidget}
430 static public void close(TWidget widget
) {
431 close(widget
.getApplication());
435 * Ask the user and, if confirmed, close the {@link TApplication}.
437 * This should result in the program terminating.
440 * the {@link TApplication}
442 static void close(TApplication app
) {
444 if (app
.messageBox("Confirmation", "(TODO: i18n) Exit application?",
445 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {