update to more upstream-y jexer lib, fix library
[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
bc2ea776 53 MetaData meta = getMeta();
c1873e56 54
4f66bfa8 55 showMain(meta, null, true);
6322ab64
NR
56 }
57
e2d017a3 58 public TuiReaderApplication(Reader reader, String source,
bc2ea776 59 TApplication.BackendType backend) throws Exception {
c1873e56 60 super(backend);
bc2ea776 61 init(reader);
c1873e56 62
4f66bfa8
NR
63 showMain(null, source, false);
64 }
65
66 private void showMain(MetaData meta, String source, boolean useMeta)
67 throws IOException {
68 // TODO: thread-safety
69 this.meta = meta;
70 this.source = source;
71 this.useMeta = useMeta;
72
73 if (main != null && main.isVisible()) {
74 main.activate();
75 } else {
76 if (main != null) {
77 main.close();
78 }
79 main = new TuiReaderMainWindow(this);
80 if (useMeta) {
81 main.setMeta(meta);
82 if (meta != null) {
83 read();
84 }
85 } else {
86 main.setSource(source);
87 }
88 }
6322ab64 89 }
c1873e56 90
211f7ddb 91 @Override
6322ab64 92 public void read() throws IOException {
bc2ea776 93 MetaData meta = getMeta();
c1873e56 94
bc2ea776
NR
95 if (meta == null) {
96 throw new IOException("No story to read");
97 }
6322ab64 98
c1873e56 99 // TODO: open in editor + external option
b0e88ebd 100 if (!meta.isImageDocument()) {
e2d017a3
NR
101 @SuppressWarnings("unused")
102 Object discard = new TuiReaderStoryWindow(this, getLibrary(), meta,
103 getChapter());
b0e88ebd
NR
104 } else {
105 try {
16a81ef7 106 openExternal(getLibrary(), meta.getLuid());
b0e88ebd 107 } catch (IOException e) {
c1873e56 108 messageBox("Error when trying to open the story",
b0e88ebd 109 e.getMessage(), TMessageBox.Type.OK);
c1873e56 110 }
c1873e56
NR
111 }
112 }
6322ab64 113
211f7ddb 114 @Override
bc2ea776
NR
115 public MetaData getMeta() {
116 return reader.getMeta();
117 }
118
211f7ddb 119 @Override
bc2ea776
NR
120 public Story getStory(Progress pg) {
121 return reader.getStory(pg);
6322ab64
NR
122 }
123
211f7ddb 124 @Override
6322ab64
NR
125 public BasicLibrary getLibrary() {
126 return reader.getLibrary();
127 }
128
211f7ddb 129 @Override
bc2ea776 130 public void setLibrary(BasicLibrary lib) {
6322ab64
NR
131 reader.setLibrary(lib);
132 }
133
211f7ddb 134 @Override
bc2ea776
NR
135 public void setMeta(MetaData meta) throws IOException {
136 reader.setMeta(meta);
137 }
138
211f7ddb 139 @Override
bc2ea776
NR
140 public void setMeta(String luid) throws IOException {
141 reader.setMeta(luid);
6322ab64
NR
142 }
143
211f7ddb 144 @Override
bc2ea776
NR
145 public void setMeta(URL source, Progress pg) throws IOException {
146 reader.setMeta(source, pg);
6322ab64
NR
147 }
148
211f7ddb 149 @Override
6322ab64
NR
150 public void browse(String source) {
151 reader.browse(source);
152 }
bc2ea776 153
211f7ddb 154 @Override
bc2ea776
NR
155 public int getChapter() {
156 return reader.getChapter();
157 }
158
211f7ddb 159 @Override
bc2ea776
NR
160 public void setChapter(int chapter) {
161 reader.setChapter(chapter);
162 }
163
164 private void init(Reader reader) {
165 this.reader = reader;
166
581d42c0
NR
167 // TODO: traces/errors?
168 Instance.setTraceHandler(null);
a8209dd0 169
e2d017a3
NR
170 // Add the menus TODO: i18n
171 TMenu fileMenu = addMenu("&File");
172 fileMenu.addItem(MENU_OPEN, "&Open");
173 fileMenu.addItem(MENU_EXPORT, "&Save as...");
174 // TODO: Move to...
175 fileMenu.addSeparator();
176 fileMenu.addItem(MENU_IMPORT_URL, "Import &URL...");
177 fileMenu.addItem(MENU_IMPORT_FILE, "Import &file...");
178 fileMenu.addSeparator();
4f66bfa8
NR
179 fileMenu.addItem(MENU_LIBRARY, "Lib&rary");
180 fileMenu.addSeparator();
e2d017a3
NR
181 fileMenu.addItem(MENU_EXIT, "E&xit");
182
183 TStatusBar statusBar = fileMenu.newStatusBar("File-management "
184 + "commands (Open, Save, Print, etc.)");
185 // TODO: doesn't actually work:
186 statusBar.addShortcutKeypress(TKeypress.kbF10, TCommand.cmExit, "Exit");
187
188 // TODO: Edit: re-download, delete
189
190 //
191
bc2ea776 192 addWindowMenu();
bc2ea776
NR
193
194 getBackend().setTitle("Fanfix");
195 }
e2d017a3
NR
196
197 @Override
198 protected boolean onMenu(TMenuEvent menu) {
199 // TODO: i18n
200 switch (menu.getId()) {
201 case MENU_EXIT:
202 if (messageBox("Confirmation", "(TODO: i18n) Exit application?",
203 TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) {
4f66bfa8 204 exit(); // TODO: exit(false) for "no confirm box"
e2d017a3
NR
205 }
206
207 return true;
208 case MENU_IMPORT_URL:
209 String clipboard = "";
210 try {
211 clipboard = ("" + Toolkit.getDefaultToolkit()
212 .getSystemClipboard().getData(DataFlavor.stringFlavor))
213 .trim();
214 } catch (Exception e) {
215 // No data will be handled
216 }
217
218 if (clipboard == null || !clipboard.startsWith("http")) {
219 clipboard = "";
220 }
221
222 String url = inputBox("Import story", "URL to import", clipboard)
223 .getText();
224
225 if (!imprt(url)) {
226 // TODO: bad import
227 }
228
229 return true;
230 case MENU_IMPORT_FILE:
231 try {
232 String filename = fileOpenBox(".");
233 if (!imprt(filename)) {
234 // TODO: bad import
235 }
236 } catch (IOException e) {
237 // TODO: bad file
238 e.printStackTrace();
239 }
240
4f66bfa8
NR
241 return true;
242 case MENU_LIBRARY:
243 try {
244 showMain(meta, source, useMeta);
245 } catch (IOException e) {
246 e.printStackTrace();
247 }
248
e2d017a3
NR
249 return true;
250 }
251
252 return super.onMenu(menu);
253 }
254
255 private boolean imprt(String url) {
256 try {
257 reader.getLibrary().imprt(BasicReader.getUrl(url), null);
258 main.refreshStories();
259 return true;
260 } catch (IOException e) {
261 return false;
262 }
263 }
16a81ef7
NR
264
265 @Override
266 public void openExternal(BasicLibrary lib, String luid) throws IOException {
267 reader.openExternal(lib, luid);
268 }
c1873e56 269}