TUI: code cleanup + show story info
[fanfix.git] / src / be / nikiroo / fanfix / reader / tui / TuiReaderApplication.java
CommitLineData
16a81ef7 1package be.nikiroo.fanfix.reader.tui;
c1873e56 2
e2d017a3
NR
3import java.awt.Toolkit;
4import java.awt.datatransfer.DataFlavor;
c1873e56 5import java.io.IOException;
6322ab64 6import java.net.URL;
c1873e56
NR
7
8import jexer.TApplication;
e2d017a3
NR
9import jexer.TCommand;
10import jexer.TKeypress;
c1873e56 11import jexer.TMessageBox;
e2d017a3 12import jexer.TStatusBar;
6322ab64 13import jexer.TWindow;
e2d017a3
NR
14import jexer.event.TMenuEvent;
15import jexer.menu.TMenu;
a8209dd0 16import be.nikiroo.fanfix.Instance;
c1873e56 17import be.nikiroo.fanfix.data.MetaData;
6322ab64
NR
18import be.nikiroo.fanfix.data.Story;
19import be.nikiroo.fanfix.library.BasicLibrary;
16a81ef7
NR
20import be.nikiroo.fanfix.reader.BasicReader;
21import be.nikiroo.fanfix.reader.Reader;
6322ab64 22import be.nikiroo.utils.Progress;
c1873e56 23
6322ab64 24/**
bc2ea776
NR
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).
6322ab64
NR
29 *
30 * @author niki
31 */
32class TuiReaderApplication extends TApplication implements Reader {
e2d017a3
NR
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;
4f66bfa8
NR
37 public static final int MENU_LIBRARY = 1029;
38 public static final int MENU_EXIT = 1030;
e2d017a3 39
6322ab64 40 private Reader reader;
e2d017a3 41 private TuiReaderMainWindow main;
c1873e56 42
4f66bfa8
NR
43 private MetaData meta;
44 private String source;
45 private boolean useMeta;
46
bc2ea776
NR
47 // start reading if meta present
48 public TuiReaderApplication(Reader reader, BackendType backend)
49 throws Exception {
50 super(backend);
51 init(reader);
c1873e56 52
5b00c122 53 showMain(getMeta(), null, true);
6322ab64
NR
54 }
55
e2d017a3 56 public TuiReaderApplication(Reader reader, String source,
bc2ea776 57 TApplication.BackendType backend) throws Exception {
c1873e56 58 super(backend);
bc2ea776 59 init(reader);
c1873e56 60
4f66bfa8
NR
61 showMain(null, source, false);
62 }
63
211f7ddb 64 @Override
6322ab64 65 public void read() throws IOException {
bc2ea776 66 MetaData meta = getMeta();
c1873e56 67
bc2ea776
NR
68 if (meta == null) {
69 throw new IOException("No story to read");
70 }
6322ab64 71
c1873e56 72 // TODO: open in editor + external option
b0e88ebd 73 if (!meta.isImageDocument()) {
e2d017a3
NR
74 @SuppressWarnings("unused")
75 Object discard = new TuiReaderStoryWindow(this, getLibrary(), meta,
76 getChapter());
b0e88ebd
NR
77 } else {
78 try {
16a81ef7 79 openExternal(getLibrary(), meta.getLuid());
b0e88ebd 80 } catch (IOException e) {
c1873e56 81 messageBox("Error when trying to open the story",
b0e88ebd 82 e.getMessage(), TMessageBox.Type.OK);
c1873e56 83 }
c1873e56
NR
84 }
85 }
6322ab64 86
211f7ddb 87 @Override
bc2ea776
NR
88 public MetaData getMeta() {
89 return reader.getMeta();
90 }
91
211f7ddb 92 @Override
bc2ea776
NR
93 public Story getStory(Progress pg) {
94 return reader.getStory(pg);
6322ab64
NR
95 }
96
211f7ddb 97 @Override
6322ab64
NR
98 public BasicLibrary getLibrary() {
99 return reader.getLibrary();
100 }
101
211f7ddb 102 @Override
bc2ea776 103 public void setLibrary(BasicLibrary lib) {
6322ab64
NR
104 reader.setLibrary(lib);
105 }
106
211f7ddb 107 @Override
bc2ea776
NR
108 public void setMeta(MetaData meta) throws IOException {
109 reader.setMeta(meta);
110 }
111
211f7ddb 112 @Override
bc2ea776
NR
113 public void setMeta(String luid) throws IOException {
114 reader.setMeta(luid);
6322ab64
NR
115 }
116
211f7ddb 117 @Override
bc2ea776
NR
118 public void setMeta(URL source, Progress pg) throws IOException {
119 reader.setMeta(source, pg);
6322ab64
NR
120 }
121
211f7ddb 122 @Override
6322ab64
NR
123 public void browse(String source) {
124 reader.browse(source);
125 }
bc2ea776 126
211f7ddb 127 @Override
bc2ea776
NR
128 public int getChapter() {
129 return reader.getChapter();
130 }
131
211f7ddb 132 @Override
bc2ea776
NR
133 public void setChapter(int chapter) {
134 reader.setChapter(chapter);
135 }
136
5b00c122
NR
137 /**
138 * Set the default status bar when this window appear.
139 * <p>
140 * Some shortcuts are always visible, and will be put here.
141 * <p>
142 * Note that shortcuts placed this way on menu won't work unless the menu
143 * also implement them.
144 *
145 * @param window
146 * the new window or menu on screen
147 * @param description
148 * the description to show on the status ba
149 */
150 public TStatusBar setStatusBar(TWindow window, String description) {
151 TStatusBar statusBar = window.newStatusBar(description);
152 statusBar.addShortcutKeypress(TKeypress.kbF10, TCommand.cmExit, "Exit");
153 return statusBar;
154
155 }
156
157 private void showMain(MetaData meta, String source, boolean useMeta)
158 throws IOException {
159 // TODO: thread-safety
160 this.meta = meta;
161 this.source = source;
162 this.useMeta = useMeta;
163
164 if (main != null && main.isVisible()) {
165 main.activate();
166 } else {
167 if (main != null) {
168 main.close();
169 }
170 main = new TuiReaderMainWindow(this);
171 if (useMeta) {
172 main.setMeta(meta);
173 if (meta != null) {
174 read();
175 }
176 } else {
177 main.setSource(source);
178 }
179 }
180 }
181
bc2ea776
NR
182 private void init(Reader reader) {
183 this.reader = reader;
184
581d42c0
NR
185 // TODO: traces/errors?
186 Instance.setTraceHandler(null);
a8209dd0 187
e2d017a3
NR
188 // Add the menus TODO: i18n
189 TMenu fileMenu = addMenu("&File");
190 fileMenu.addItem(MENU_OPEN, "&Open");
191 fileMenu.addItem(MENU_EXPORT, "&Save as...");
192 // TODO: Move to...
193 fileMenu.addSeparator();
194 fileMenu.addItem(MENU_IMPORT_URL, "Import &URL...");
195 fileMenu.addItem(MENU_IMPORT_FILE, "Import &file...");
196 fileMenu.addSeparator();
4f66bfa8
NR
197 fileMenu.addItem(MENU_LIBRARY, "Lib&rary");
198 fileMenu.addSeparator();
e2d017a3
NR
199 fileMenu.addItem(MENU_EXIT, "E&xit");
200
5b00c122 201 setStatusBar(fileMenu, "File-management "
e2d017a3 202 + "commands (Open, Save, Print, etc.)");
e2d017a3
NR
203
204 // TODO: Edit: re-download, delete
205
206 //
207
bc2ea776 208 addWindowMenu();
bc2ea776
NR
209
210 getBackend().setTitle("Fanfix");
211 }
e2d017a3
NR
212
213 @Override
214 protected boolean onMenu(TMenuEvent menu) {
215 // TODO: i18n
216 switch (menu.getId()) {
217 case MENU_EXIT:
218 if (messageBox("Confirmation", "(TODO: i18n) Exit application?",
219 TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) {
4f66bfa8 220 exit(); // TODO: exit(false) for "no confirm box"
e2d017a3
NR
221 }
222
223 return true;
224 case MENU_IMPORT_URL:
225 String clipboard = "";
226 try {
227 clipboard = ("" + Toolkit.getDefaultToolkit()
228 .getSystemClipboard().getData(DataFlavor.stringFlavor))
229 .trim();
230 } catch (Exception e) {
231 // No data will be handled
232 }
233
234 if (clipboard == null || !clipboard.startsWith("http")) {
235 clipboard = "";
236 }
237
238 String url = inputBox("Import story", "URL to import", clipboard)
239 .getText();
240
241 if (!imprt(url)) {
242 // TODO: bad import
243 }
244
245 return true;
246 case MENU_IMPORT_FILE:
247 try {
248 String filename = fileOpenBox(".");
249 if (!imprt(filename)) {
250 // TODO: bad import
251 }
252 } catch (IOException e) {
253 // TODO: bad file
254 e.printStackTrace();
255 }
256
4f66bfa8
NR
257 return true;
258 case MENU_LIBRARY:
259 try {
260 showMain(meta, source, useMeta);
261 } catch (IOException e) {
262 e.printStackTrace();
263 }
264
e2d017a3
NR
265 return true;
266 }
267
268 return super.onMenu(menu);
269 }
270
271 private boolean imprt(String url) {
272 try {
273 reader.getLibrary().imprt(BasicReader.getUrl(url), null);
274 main.refreshStories();
275 return true;
276 } catch (IOException e) {
277 return false;
278 }
279 }
16a81ef7
NR
280
281 @Override
282 public void openExternal(BasicLibrary lib, String luid) throws IOException {
283 reader.openExternal(lib, luid);
284 }
c1873e56 285}