remote: fix 30s timeout bug
[nikiroo-utils.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;
37 public static final int MENU_EXIT = 1029;
38
6322ab64 39 private Reader reader;
e2d017a3 40 private TuiReaderMainWindow main;
c1873e56 41
bc2ea776
NR
42 // start reading if meta present
43 public TuiReaderApplication(Reader reader, BackendType backend)
44 throws Exception {
45 super(backend);
46 init(reader);
c1873e56 47
bc2ea776 48 MetaData meta = getMeta();
c1873e56 49
e2d017a3
NR
50 main = new TuiReaderMainWindow(this);
51 main.setMeta(meta);
bc2ea776
NR
52 if (meta != null) {
53 read();
54 }
6322ab64
NR
55 }
56
e2d017a3 57 public TuiReaderApplication(Reader reader, String source,
bc2ea776 58 TApplication.BackendType backend) throws Exception {
c1873e56 59 super(backend);
e2d017a3 60
bc2ea776 61 init(reader);
c1873e56 62
e2d017a3
NR
63 main = new TuiReaderMainWindow(this);
64 main.setSource(source);
6322ab64 65 }
c1873e56 66
211f7ddb 67 @Override
6322ab64 68 public void read() throws IOException {
bc2ea776 69 MetaData meta = getMeta();
c1873e56 70
bc2ea776
NR
71 if (meta == null) {
72 throw new IOException("No story to read");
73 }
6322ab64 74
c1873e56 75 // TODO: open in editor + external option
b0e88ebd 76 if (!meta.isImageDocument()) {
e2d017a3
NR
77 @SuppressWarnings("unused")
78 Object discard = new TuiReaderStoryWindow(this, getLibrary(), meta,
79 getChapter());
b0e88ebd
NR
80 } else {
81 try {
16a81ef7 82 openExternal(getLibrary(), meta.getLuid());
b0e88ebd 83 } catch (IOException e) {
c1873e56 84 messageBox("Error when trying to open the story",
b0e88ebd 85 e.getMessage(), TMessageBox.Type.OK);
c1873e56 86 }
c1873e56
NR
87 }
88 }
6322ab64 89
211f7ddb 90 @Override
bc2ea776
NR
91 public MetaData getMeta() {
92 return reader.getMeta();
93 }
94
211f7ddb 95 @Override
bc2ea776
NR
96 public Story getStory(Progress pg) {
97 return reader.getStory(pg);
6322ab64
NR
98 }
99
211f7ddb 100 @Override
6322ab64
NR
101 public BasicLibrary getLibrary() {
102 return reader.getLibrary();
103 }
104
211f7ddb 105 @Override
bc2ea776 106 public void setLibrary(BasicLibrary lib) {
6322ab64
NR
107 reader.setLibrary(lib);
108 }
109
211f7ddb 110 @Override
bc2ea776
NR
111 public void setMeta(MetaData meta) throws IOException {
112 reader.setMeta(meta);
113 }
114
211f7ddb 115 @Override
bc2ea776
NR
116 public void setMeta(String luid) throws IOException {
117 reader.setMeta(luid);
6322ab64
NR
118 }
119
211f7ddb 120 @Override
bc2ea776
NR
121 public void setMeta(URL source, Progress pg) throws IOException {
122 reader.setMeta(source, pg);
6322ab64
NR
123 }
124
211f7ddb 125 @Override
6322ab64
NR
126 public void browse(String source) {
127 reader.browse(source);
128 }
bc2ea776 129
211f7ddb 130 @Override
bc2ea776
NR
131 public int getChapter() {
132 return reader.getChapter();
133 }
134
211f7ddb 135 @Override
bc2ea776
NR
136 public void setChapter(int chapter) {
137 reader.setChapter(chapter);
138 }
139
140 private void init(Reader reader) {
141 this.reader = reader;
142
581d42c0
NR
143 // TODO: traces/errors?
144 Instance.setTraceHandler(null);
a8209dd0 145
e2d017a3
NR
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
bc2ea776 166 addWindowMenu();
bc2ea776
NR
167
168 getBackend().setTitle("Fanfix");
169 }
e2d017a3
NR
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) {
581d42c0 178 // exit(false);
e2d017a3
NR
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 }
16a81ef7
NR
230
231 @Override
232 public void openExternal(BasicLibrary lib, String luid) throws IOException {
233 reader.openExternal(lib, luid);
234 }
c1873e56 235}