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