Fix default remote lib not using cache
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / TuiReaderApplication.java
1 package be.nikiroo.fanfix.reader;
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.utils.Progress;
21
22 /**
23 * Manages the TUI general mode and links and manages the {@link TWindow}s.
24 * <p>
25 * It will also enclose a {@link Reader} and simply handle the reading part
26 * differently (it will create the required sub-windows and display them).
27 *
28 * @author niki
29 */
30 class TuiReaderApplication extends TApplication implements Reader {
31 public static final int MENU_OPEN = 1025;
32 public static final int MENU_IMPORT_URL = 1026;
33 public static final int MENU_IMPORT_FILE = 1027;
34 public static final int MENU_EXPORT = 1028;
35 public static final int MENU_EXIT = 1029;
36
37 private Reader reader;
38 private TuiReaderMainWindow main;
39
40 // start reading if meta present
41 public TuiReaderApplication(Reader reader, BackendType backend)
42 throws Exception {
43 super(backend);
44 init(reader);
45
46 MetaData meta = getMeta();
47
48 main = new TuiReaderMainWindow(this);
49 main.setMeta(meta);
50 if (meta != null) {
51 read();
52 }
53 }
54
55 public TuiReaderApplication(Reader reader, String source,
56 TApplication.BackendType backend) throws Exception {
57 super(backend);
58
59 init(reader);
60
61 main = new TuiReaderMainWindow(this);
62 main.setSource(source);
63 }
64
65 @Override
66 public void read() throws IOException {
67 MetaData meta = getMeta();
68
69 if (meta == null) {
70 throw new IOException("No story to read");
71 }
72
73 // TODO: open in editor + external option
74 if (!meta.isImageDocument()) {
75 @SuppressWarnings("unused")
76 Object discard = new TuiReaderStoryWindow(this, getLibrary(), meta,
77 getChapter());
78 } else {
79 try {
80 BasicReader.openExternal(getLibrary(), meta.getLuid());
81 } catch (IOException e) {
82 messageBox("Error when trying to open the story",
83 e.getMessage(), TMessageBox.Type.OK);
84 }
85 }
86 }
87
88 @Override
89 public MetaData getMeta() {
90 return reader.getMeta();
91 }
92
93 @Override
94 public Story getStory(Progress pg) {
95 return reader.getStory(pg);
96 }
97
98 @Override
99 public BasicLibrary getLibrary() {
100 return reader.getLibrary();
101 }
102
103 @Override
104 public void setLibrary(BasicLibrary lib) {
105 reader.setLibrary(lib);
106 }
107
108 @Override
109 public void setMeta(MetaData meta) throws IOException {
110 reader.setMeta(meta);
111 }
112
113 @Override
114 public void setMeta(String luid) throws IOException {
115 reader.setMeta(luid);
116 }
117
118 @Override
119 public void setMeta(URL source, Progress pg) throws IOException {
120 reader.setMeta(source, pg);
121 }
122
123 @Override
124 public void browse(String source) {
125 reader.browse(source);
126 }
127
128 @Override
129 public int getChapter() {
130 return reader.getChapter();
131 }
132
133 @Override
134 public void setChapter(int chapter) {
135 reader.setChapter(chapter);
136 }
137
138 private void init(Reader reader) {
139 this.reader = reader;
140
141 // TODO: traces/errors?
142 Instance.setTraceHandler(null);
143
144 // Add the menus TODO: i18n
145 TMenu fileMenu = addMenu("&File");
146 fileMenu.addItem(MENU_OPEN, "&Open");
147 fileMenu.addItem(MENU_EXPORT, "&Save as...");
148 // TODO: Move to...
149 fileMenu.addSeparator();
150 fileMenu.addItem(MENU_IMPORT_URL, "Import &URL...");
151 fileMenu.addItem(MENU_IMPORT_FILE, "Import &file...");
152 fileMenu.addSeparator();
153 fileMenu.addItem(MENU_EXIT, "E&xit");
154
155 TStatusBar statusBar = fileMenu.newStatusBar("File-management "
156 + "commands (Open, Save, Print, etc.)");
157 // TODO: doesn't actually work:
158 statusBar.addShortcutKeypress(TKeypress.kbF10, TCommand.cmExit, "Exit");
159
160 // TODO: Edit: re-download, delete
161
162 //
163
164 addWindowMenu();
165
166 getBackend().setTitle("Fanfix");
167 }
168
169 @Override
170 protected boolean onMenu(TMenuEvent menu) {
171 // TODO: i18n
172 switch (menu.getId()) {
173 case MENU_EXIT:
174 if (messageBox("Confirmation", "(TODO: i18n) Exit application?",
175 TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) {
176 // exit(false);
177 }
178
179 return true;
180 case MENU_IMPORT_URL:
181 String clipboard = "";
182 try {
183 clipboard = ("" + Toolkit.getDefaultToolkit()
184 .getSystemClipboard().getData(DataFlavor.stringFlavor))
185 .trim();
186 } catch (Exception e) {
187 // No data will be handled
188 }
189
190 if (clipboard == null || !clipboard.startsWith("http")) {
191 clipboard = "";
192 }
193
194 String url = inputBox("Import story", "URL to import", clipboard)
195 .getText();
196
197 if (!imprt(url)) {
198 // TODO: bad import
199 }
200
201 return true;
202 case MENU_IMPORT_FILE:
203 try {
204 String filename = fileOpenBox(".");
205 if (!imprt(filename)) {
206 // TODO: bad import
207 }
208 } catch (IOException e) {
209 // TODO: bad file
210 e.printStackTrace();
211 }
212
213 return true;
214 }
215
216 return super.onMenu(menu);
217 }
218
219 private boolean imprt(String url) {
220 try {
221 reader.getLibrary().imprt(BasicReader.getUrl(url), null);
222 main.refreshStories();
223 return true;
224 } catch (IOException e) {
225 return false;
226 }
227 }
228 }