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(boolean sync
) throws IOException
{
135 public void search(SupportType searchOn
, String keywords
, int page
,
136 int item
, boolean sync
) throws IOException
{
137 reader
.search(searchOn
, keywords
, page
, item
, sync
);
141 public void searchTag(SupportType searchOn
, int page
, int item
,
142 boolean sync
, Integer
... tags
) throws IOException
{
143 reader
.searchTag(searchOn
, page
, item
, sync
, tags
);
147 * Open the given {@link Story} for reading. This may or may not start an
148 * external program to read said {@link Story}.
151 * the {@link Story} to read
153 * execute the process synchronously (wait until it is terminated
156 * @throws IOException
157 * in case of I/O errors
159 public void read(Story story
, boolean sync
) throws IOException
{
161 throw new IOException("No story to read");
164 // TODO: open in editor + external option
165 if (!story
.getMeta().isImageDocument()) {
166 TWindow window
= new TuiReaderStoryWindow(this, story
, getChapter());
170 openExternal(getLibrary(), story
.getMeta().getLuid(), sync
);
171 } catch (IOException e
) {
172 messageBox("Error when trying to open the story",
173 e
.getMessage(), TMessageBox
.Type
.OK
);
179 * Set the default status bar when this window appear.
181 * Some shortcuts are always visible, and will be put here.
183 * Note that shortcuts placed this way on menu won't work unless the menu
184 * also implement them.
187 * the new window or menu on screen
189 * the description to show on the status ba
191 public TStatusBar
setStatusBar(TWindow window
, String description
) {
192 TStatusBar statusBar
= window
.newStatusBar(description
);
193 statusBar
.addShortcutKeypress(TKeypress
.kbF10
, CMD_EXIT
, "Exit");
198 private void showMain() {
199 if (main
!= null && main
.isVisible()) {
205 main
= new TuiReaderMainWindow(this);
210 private void init(Reader reader
) {
211 this.reader
= reader
;
213 // TODO: traces/errors?
214 Instance
.setTraceHandler(null);
216 // Add the menus TODO: i18n
217 TMenu fileMenu
= addMenu("&File");
218 fileMenu
.addItem(MENU_OPEN
, "&Open...");
219 fileMenu
.addItem(MENU_EXPORT
, "&Save as...");
220 fileMenu
.addItem(MENU_DELETE
, "&Delete...");
222 fileMenu
.addSeparator();
223 fileMenu
.addItem(MENU_IMPORT_URL
, "Import &URL...");
224 fileMenu
.addItem(MENU_IMPORT_FILE
, "Import &file...");
225 fileMenu
.addSeparator();
226 fileMenu
.addItem(MENU_LIBRARY
, "Lib&rary");
227 fileMenu
.addSeparator();
228 fileMenu
.addItem(MENU_EXIT
, "E&xit");
230 setStatusBar(fileMenu
, "File-management "
231 + "commands (Open, Save, Print, etc.)");
233 // TODO: Edit: re-download, delete
239 getBackend().setTitle("Fanfix");
243 protected boolean onCommand(TCommandEvent command
) {
244 if (command
.getCmd().equals(TuiReaderMainWindow
.CMD_SEARCH
)) {
245 messageBox("title", "caption");
248 return super.onCommand(command
);
252 protected boolean onMenu(TMenuEvent menu
) {
254 switch (menu
.getId()) {
259 String openfile
= null;
261 openfile
= fileOpenBox(".");
262 reader
.setMeta(BasicReader
.getUrl(openfile
), null);
264 } catch (IOException e
) {
266 error("Fail to open file"
267 + (openfile
== null ?
"" : ": " + openfile
),
275 MetaData meta
= null;
277 meta
= main
.getSelectedMeta();
280 luid
= meta
.getLuid();
281 story
= luid
+ ": " + meta
.getTitle();
285 TMessageBox mbox
= messageBox("Delete story", "Delete story \""
286 + story
+ "\"", Type
.OKCANCEL
);
287 if (mbox
.getResult() == Result
.OK
) {
289 reader
.getLibrary().delete(luid
);
291 main
.refreshStories();
293 } catch (IOException e
) {
295 error("Fail to delete the story: \"" + story
+ "\"",
301 case MENU_IMPORT_URL
:
302 String clipboard
= "";
304 clipboard
= ("" + Toolkit
.getDefaultToolkit()
305 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
307 } catch (Exception e
) {
308 // No data will be handled
311 if (clipboard
== null || !clipboard
.startsWith("http")) {
315 String url
= inputBox("Import story", "URL to import", clipboard
)
321 error("URK not supported: " + url
, "Import error");
323 } catch (IOException e
) {
325 error("Fail to import URL: " + url
, "Import error", e
);
329 case MENU_IMPORT_FILE
:
330 String filename
= null;
332 filename
= fileOpenBox(".");
333 if (!imprt(filename
)) {
335 error("File not supported: " + filename
, "Import error");
337 } catch (IOException e
) {
339 error("Fail to import file"
340 + (filename
== null ?
"" : ": " + filename
),
349 return super.onMenu(menu
);
353 * Import the given url.
355 * Can fail if the host is not supported.
359 * @return TRUE in case of success, FALSE if the host is not supported
361 * @throws IOException
362 * in case of I/O error
364 private boolean imprt(String url
) throws IOException
{
366 reader
.getLibrary().imprt(BasicReader
.getUrl(url
), null);
367 main
.refreshStories();
369 } catch (UnknownHostException e
) {
375 public void openExternal(BasicLibrary lib
, String luid
, boolean sync
)
377 reader
.openExternal(lib
, luid
, sync
);
381 * Display an error message and log it.
386 * the title of the error message
388 private void error(String message
, String title
) {
389 error(message
, title
, null);
393 * Display an error message and log it, including the linked
399 * the title of the error message
401 * the exception to log if any (can be NULL)
403 private void error(String message
, String title
, Exception e
) {
404 Instance
.getTraceHandler().error(title
+ ": " + message
);
406 Instance
.getTraceHandler().error(e
);
410 messageBox(title
, message
//
411 + "\n" + e
.getMessage());
413 messageBox(title
, message
);
418 * Ask the user and, if confirmed, close the {@link TApplication} this
419 * {@link TWidget} is running on.
421 * This should result in the program terminating.
424 * the {@link TWidget}
426 static public void close(TWidget widget
) {
427 close(widget
.getApplication());
431 * Ask the user and, if confirmed, close the {@link TApplication}.
433 * This should result in the program terminating.
436 * the {@link TApplication}
438 static void close(TApplication app
) {
440 if (app
.messageBox("Confirmation", "(TODO: i18n) Exit application?",
441 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {