TUI: better display of stories
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / tui / TuiReaderApplication.java
1 package be.nikiroo.fanfix.reader.tui;
2
3 import java.awt.Toolkit;
4 import java.awt.datatransfer.DataFlavor;
5 import java.io.IOException;
6 import java.net.URL;
7
8 import jexer.TApplication;
9 import jexer.TCommand;
10 import jexer.TKeypress;
11 import jexer.TMessageBox;
12 import jexer.TStatusBar;
13 import jexer.TWindow;
14 import jexer.event.TMenuEvent;
15 import jexer.menu.TMenu;
16 import be.nikiroo.fanfix.Instance;
17 import be.nikiroo.fanfix.data.MetaData;
18 import be.nikiroo.fanfix.data.Story;
19 import be.nikiroo.fanfix.library.BasicLibrary;
20 import be.nikiroo.fanfix.reader.BasicReader;
21 import be.nikiroo.fanfix.reader.Reader;
22 import be.nikiroo.utils.Progress;
23
24 /**
25 * Manages the TUI general mode and links and manages the {@link TWindow}s.
26 * <p>
27 * It will also enclose a {@link Reader} and simply handle the reading part
28 * differently (it will create the required sub-windows and display them).
29 *
30 * @author niki
31 */
32 class TuiReaderApplication extends TApplication implements Reader {
33 public static final int MENU_OPEN = 1025;
34 public static final int MENU_IMPORT_URL = 1026;
35 public static final int MENU_IMPORT_FILE = 1027;
36 public static final int MENU_EXPORT = 1028;
37 public static final int MENU_EXIT = 1029;
38
39 private Reader reader;
40 private TuiReaderMainWindow main;
41
42 // start reading if meta present
43 public TuiReaderApplication(Reader reader, BackendType backend)
44 throws Exception {
45 super(backend);
46 init(reader);
47
48 MetaData meta = getMeta();
49
50 main = new TuiReaderMainWindow(this);
51 main.setMeta(meta);
52 if (meta != null) {
53 read();
54 }
55 }
56
57 public TuiReaderApplication(Reader reader, String source,
58 TApplication.BackendType backend) throws Exception {
59 super(backend);
60 init(reader);
61
62 main = new TuiReaderMainWindow(this);
63 main.setSource(source);
64 }
65
66 @Override
67 public void read() throws IOException {
68 MetaData meta = getMeta();
69
70 if (meta == null) {
71 throw new IOException("No story to read");
72 }
73
74 // TODO: open in editor + external option
75 if (!meta.isImageDocument()) {
76 @SuppressWarnings("unused")
77 Object discard = new TuiReaderStoryWindow(this, getLibrary(), meta,
78 getChapter());
79 } else {
80 try {
81 openExternal(getLibrary(), meta.getLuid());
82 } catch (IOException e) {
83 messageBox("Error when trying to open the story",
84 e.getMessage(), TMessageBox.Type.OK);
85 }
86 }
87 }
88
89 @Override
90 public MetaData getMeta() {
91 return reader.getMeta();
92 }
93
94 @Override
95 public Story getStory(Progress pg) {
96 return reader.getStory(pg);
97 }
98
99 @Override
100 public BasicLibrary getLibrary() {
101 return reader.getLibrary();
102 }
103
104 @Override
105 public void setLibrary(BasicLibrary lib) {
106 reader.setLibrary(lib);
107 }
108
109 @Override
110 public void setMeta(MetaData meta) throws IOException {
111 reader.setMeta(meta);
112 }
113
114 @Override
115 public void setMeta(String luid) throws IOException {
116 reader.setMeta(luid);
117 }
118
119 @Override
120 public void setMeta(URL source, Progress pg) throws IOException {
121 reader.setMeta(source, pg);
122 }
123
124 @Override
125 public void browse(String source) {
126 reader.browse(source);
127 }
128
129 @Override
130 public int getChapter() {
131 return reader.getChapter();
132 }
133
134 @Override
135 public void setChapter(int chapter) {
136 reader.setChapter(chapter);
137 }
138
139 private void init(Reader reader) {
140 this.reader = reader;
141
142 // TODO: traces/errors?
143 Instance.setTraceHandler(null);
144
145 // Add the menus TODO: i18n
146 TMenu fileMenu = addMenu("&File");
147 fileMenu.addItem(MENU_OPEN, "&Open");
148 fileMenu.addItem(MENU_EXPORT, "&Save as...");
149 // TODO: Move to...
150 fileMenu.addSeparator();
151 fileMenu.addItem(MENU_IMPORT_URL, "Import &URL...");
152 fileMenu.addItem(MENU_IMPORT_FILE, "Import &file...");
153 fileMenu.addSeparator();
154 fileMenu.addItem(MENU_EXIT, "E&xit");
155
156 TStatusBar statusBar = fileMenu.newStatusBar("File-management "
157 + "commands (Open, Save, Print, etc.)");
158 // TODO: doesn't actually work:
159 statusBar.addShortcutKeypress(TKeypress.kbF10, TCommand.cmExit, "Exit");
160
161 // TODO: Edit: re-download, delete
162
163 //
164
165 addWindowMenu();
166
167 getBackend().setTitle("Fanfix");
168 }
169
170 @Override
171 protected boolean onMenu(TMenuEvent menu) {
172 // TODO: i18n
173 switch (menu.getId()) {
174 case MENU_EXIT:
175 if (messageBox("Confirmation", "(TODO: i18n) Exit application?",
176 TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) {
177 // exit(false);
178 }
179
180 return true;
181 case MENU_IMPORT_URL:
182 String clipboard = "";
183 try {
184 clipboard = ("" + Toolkit.getDefaultToolkit()
185 .getSystemClipboard().getData(DataFlavor.stringFlavor))
186 .trim();
187 } catch (Exception e) {
188 // No data will be handled
189 }
190
191 if (clipboard == null || !clipboard.startsWith("http")) {
192 clipboard = "";
193 }
194
195 String url = inputBox("Import story", "URL to import", clipboard)
196 .getText();
197
198 if (!imprt(url)) {
199 // TODO: bad import
200 }
201
202 return true;
203 case MENU_IMPORT_FILE:
204 try {
205 String filename = fileOpenBox(".");
206 if (!imprt(filename)) {
207 // TODO: bad import
208 }
209 } catch (IOException e) {
210 // TODO: bad file
211 e.printStackTrace();
212 }
213
214 return true;
215 }
216
217 return super.onMenu(menu);
218 }
219
220 private boolean imprt(String url) {
221 try {
222 reader.getLibrary().imprt(BasicReader.getUrl(url), null);
223 main.refreshStories();
224 return true;
225 } catch (IOException e) {
226 return false;
227 }
228 }
229
230 @Override
231 public void openExternal(BasicLibrary lib, String luid) throws IOException {
232 reader.openExternal(lib, luid);
233 }
234 }