6fa969b50da925f0ffe1994fa0f25dbc76faec8f
[fanfix.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_LIBRARY = 1029;
38 public static final int MENU_EXIT = 1030;
39
40 private Reader reader;
41 private TuiReaderMainWindow main;
42
43 private MetaData meta;
44 private String source;
45 private boolean useMeta;
46
47 // start reading if meta present
48 public TuiReaderApplication(Reader reader, BackendType backend)
49 throws Exception {
50 super(backend);
51 init(reader);
52
53 MetaData meta = getMeta();
54
55 showMain(meta, null, true);
56 }
57
58 public TuiReaderApplication(Reader reader, String source,
59 TApplication.BackendType backend) throws Exception {
60 super(backend);
61 init(reader);
62
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 }
89 }
90
91 @Override
92 public void read() throws IOException {
93 MetaData meta = getMeta();
94
95 if (meta == null) {
96 throw new IOException("No story to read");
97 }
98
99 // TODO: open in editor + external option
100 if (!meta.isImageDocument()) {
101 @SuppressWarnings("unused")
102 Object discard = new TuiReaderStoryWindow(this, getLibrary(), meta,
103 getChapter());
104 } else {
105 try {
106 openExternal(getLibrary(), meta.getLuid());
107 } catch (IOException e) {
108 messageBox("Error when trying to open the story",
109 e.getMessage(), TMessageBox.Type.OK);
110 }
111 }
112 }
113
114 @Override
115 public MetaData getMeta() {
116 return reader.getMeta();
117 }
118
119 @Override
120 public Story getStory(Progress pg) {
121 return reader.getStory(pg);
122 }
123
124 @Override
125 public BasicLibrary getLibrary() {
126 return reader.getLibrary();
127 }
128
129 @Override
130 public void setLibrary(BasicLibrary lib) {
131 reader.setLibrary(lib);
132 }
133
134 @Override
135 public void setMeta(MetaData meta) throws IOException {
136 reader.setMeta(meta);
137 }
138
139 @Override
140 public void setMeta(String luid) throws IOException {
141 reader.setMeta(luid);
142 }
143
144 @Override
145 public void setMeta(URL source, Progress pg) throws IOException {
146 reader.setMeta(source, pg);
147 }
148
149 @Override
150 public void browse(String source) {
151 reader.browse(source);
152 }
153
154 @Override
155 public int getChapter() {
156 return reader.getChapter();
157 }
158
159 @Override
160 public void setChapter(int chapter) {
161 reader.setChapter(chapter);
162 }
163
164 private void init(Reader reader) {
165 this.reader = reader;
166
167 // TODO: traces/errors?
168 Instance.setTraceHandler(null);
169
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();
179 fileMenu.addItem(MENU_LIBRARY, "Lib&rary");
180 fileMenu.addSeparator();
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
192 addWindowMenu();
193
194 getBackend().setTitle("Fanfix");
195 }
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) {
204 exit(); // TODO: exit(false) for "no confirm box"
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
241 return true;
242 case MENU_LIBRARY:
243 try {
244 showMain(meta, source, useMeta);
245 } catch (IOException e) {
246 e.printStackTrace();
247 }
248
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 }
264
265 @Override
266 public void openExternal(BasicLibrary lib, String luid) throws IOException {
267 reader.openExternal(lib, luid);
268 }
269 }