Commit | Line | Data |
---|---|---|
89cb07a6 NR |
1 | package be.nikiroo.fanfix.reader; |
2 | ||
c1873e56 | 3 | import java.awt.Desktop; |
3b2b638f | 4 | import java.io.File; |
89cb07a6 | 5 | import java.io.IOException; |
3b2b638f | 6 | import java.net.MalformedURLException; |
89cb07a6 NR |
7 | import java.net.URL; |
8 | ||
68e2c6d2 | 9 | import be.nikiroo.fanfix.BasicLibrary; |
89cb07a6 | 10 | import be.nikiroo.fanfix.Instance; |
68e2c6d2 | 11 | import be.nikiroo.fanfix.LocalLibrary; |
d0114000 | 12 | import be.nikiroo.fanfix.bundles.Config; |
c1873e56 NR |
13 | import be.nikiroo.fanfix.bundles.UiConfig; |
14 | import be.nikiroo.fanfix.data.MetaData; | |
89cb07a6 NR |
15 | import be.nikiroo.fanfix.data.Story; |
16 | import be.nikiroo.fanfix.supported.BasicSupport; | |
3b2b638f | 17 | import be.nikiroo.utils.Progress; |
9119671d | 18 | import be.nikiroo.utils.serial.SerialUtils; |
89cb07a6 NR |
19 | |
20 | /** | |
dd56a893 | 21 | * The class that handles the different {@link Story} readers you can use. |
89cb07a6 | 22 | * <p> |
dd56a893 | 23 | * All the readers should be accessed via {@link BasicReader#getReader()}. |
89cb07a6 NR |
24 | * |
25 | * @author niki | |
26 | */ | |
3727aae2 | 27 | public abstract class BasicReader { |
5dd985cf NR |
28 | /** |
29 | * A type of {@link BasicReader}. | |
30 | * | |
31 | * @author niki | |
32 | */ | |
3727aae2 NR |
33 | public enum ReaderType { |
34 | /** Simple reader that outputs everything on the console */ | |
35 | CLI, | |
36 | /** Reader that starts local programs to handle the stories */ | |
c1873e56 NR |
37 | GUI, |
38 | /** A text (UTF-8) reader with menu and text windows */ | |
39 | TUI, | |
b0e88ebd | 40 | |
9119671d | 41 | ; |
b0e88ebd | 42 | |
5dd985cf NR |
43 | /** |
44 | * Return the full class name of a type that implements said | |
45 | * {@link ReaderType}. | |
46 | * | |
47 | * @return the class name | |
48 | */ | |
9119671d NR |
49 | public String getTypeName() { |
50 | String pkg = "be.nikiroo.fanfix.reader."; | |
51 | switch (this) { | |
b0e88ebd NR |
52 | case CLI: |
53 | return pkg + "CliReader"; | |
54 | case TUI: | |
55 | return pkg + "TuiReader"; | |
56 | case GUI: | |
5dd985cf | 57 | return pkg + "GuiReader"; |
9119671d | 58 | } |
b0e88ebd | 59 | |
9119671d NR |
60 | return null; |
61 | } | |
3727aae2 NR |
62 | } |
63 | ||
68e2c6d2 | 64 | private static BasicLibrary defaultLibrary = Instance.getLibrary(); |
c1873e56 | 65 | private static ReaderType defaultType = ReaderType.GUI; |
b0e88ebd | 66 | |
68e2c6d2 | 67 | private BasicLibrary lib; |
89cb07a6 | 68 | private Story story; |
3727aae2 NR |
69 | private ReaderType type; |
70 | ||
d0114000 NR |
71 | /** |
72 | * Take the default reader type configuration from the config file. | |
73 | */ | |
3727aae2 | 74 | static { |
d0114000 NR |
75 | String typeString = Instance.getConfig().getString(Config.READER_TYPE); |
76 | if (typeString != null && !typeString.isEmpty()) { | |
77 | try { | |
78 | ReaderType type = ReaderType.valueOf(typeString.toUpperCase()); | |
79 | defaultType = type; | |
80 | } catch (IllegalArgumentException e) { | |
81 | // Do nothing | |
82 | } | |
83 | } | |
3727aae2 NR |
84 | } |
85 | ||
86 | /** | |
87 | * The type of this reader. | |
88 | * | |
89 | * @return the type | |
90 | */ | |
91 | public ReaderType getType() { | |
92 | return type; | |
93 | } | |
94 | ||
95 | /** | |
96 | * The type of this reader. | |
97 | * | |
98 | * @param type | |
99 | * the new type | |
5dd985cf NR |
100 | * |
101 | * @return the type | |
3727aae2 NR |
102 | */ |
103 | protected BasicReader setType(ReaderType type) { | |
104 | this.type = type; | |
105 | return this; | |
106 | } | |
89cb07a6 NR |
107 | |
108 | /** | |
109 | * Return the current {@link Story}. | |
110 | * | |
111 | * @return the {@link Story} | |
112 | */ | |
113 | public Story getStory() { | |
114 | return story; | |
115 | } | |
116 | ||
b0e88ebd | 117 | /** |
68e2c6d2 NR |
118 | * The {@link LocalLibrary} to load the stories from (by default, takes the |
119 | * default {@link LocalLibrary}). | |
b0e88ebd | 120 | * |
68e2c6d2 | 121 | * @return the {@link LocalLibrary} |
b0e88ebd | 122 | */ |
68e2c6d2 | 123 | public BasicLibrary getLibrary() { |
b0e88ebd NR |
124 | if (lib == null) { |
125 | lib = defaultLibrary; | |
126 | } | |
127 | ||
128 | return lib; | |
129 | } | |
130 | ||
131 | /** | |
68e2c6d2 | 132 | * Change the {@link LocalLibrary} that will be managed by this |
b0e88ebd NR |
133 | * {@link BasicReader}. |
134 | * | |
135 | * @param lib | |
68e2c6d2 | 136 | * the new {@link LocalLibrary} |
b0e88ebd | 137 | */ |
68e2c6d2 | 138 | public void setLibrary(LocalLibrary lib) { |
b0e88ebd NR |
139 | this.lib = lib; |
140 | } | |
141 | ||
89cb07a6 | 142 | /** |
3727aae2 | 143 | * Create a new {@link BasicReader} for a {@link Story} in the |
68e2c6d2 | 144 | * {@link LocalLibrary}. |
89cb07a6 NR |
145 | * |
146 | * @param luid | |
147 | * the {@link Story} ID | |
92fb0719 NR |
148 | * @param pg |
149 | * the optional progress reporter | |
150 | * | |
89cb07a6 NR |
151 | * @throws IOException |
152 | * in case of I/O error | |
153 | */ | |
92fb0719 | 154 | public void setStory(String luid, Progress pg) throws IOException { |
b0e88ebd | 155 | story = lib.getStory(luid, pg); |
89cb07a6 | 156 | if (story == null) { |
92fb0719 | 157 | throw new IOException("Cannot retrieve story from library: " + luid); |
89cb07a6 NR |
158 | } |
159 | } | |
160 | ||
161 | /** | |
3727aae2 | 162 | * Create a new {@link BasicReader} for an external {@link Story}. |
89cb07a6 NR |
163 | * |
164 | * @param source | |
165 | * the {@link Story} {@link URL} | |
92fb0719 NR |
166 | * @param pg |
167 | * the optional progress reporter | |
168 | * | |
89cb07a6 NR |
169 | * @throws IOException |
170 | * in case of I/O error | |
171 | */ | |
92fb0719 | 172 | public void setStory(URL source, Progress pg) throws IOException { |
89cb07a6 NR |
173 | BasicSupport support = BasicSupport.getSupport(source); |
174 | if (support == null) { | |
175 | throw new IOException("URL not supported: " + source.toString()); | |
176 | } | |
177 | ||
92fb0719 | 178 | story = support.process(source, pg); |
89cb07a6 NR |
179 | if (story == null) { |
180 | throw new IOException( | |
181 | "Cannot retrieve story from external source: " | |
182 | + source.toString()); | |
183 | ||
184 | } | |
185 | } | |
186 | ||
187 | /** | |
188 | * Start the {@link Story} Reading. | |
189 | * | |
190 | * @throws IOException | |
191 | * in case of I/O error or if the {@link Story} was not | |
192 | * previously set | |
193 | */ | |
194 | public abstract void read() throws IOException; | |
195 | ||
196 | /** | |
197 | * Read the selected chapter (starting at 1). | |
198 | * | |
199 | * @param chapter | |
200 | * the chapter | |
edd46289 NR |
201 | * |
202 | * @throws IOException | |
203 | * in case of I/O error or if the {@link Story} was not | |
204 | * previously set | |
89cb07a6 | 205 | */ |
edd46289 | 206 | public abstract void read(int chapter) throws IOException; |
89cb07a6 NR |
207 | |
208 | /** | |
b0e88ebd NR |
209 | * Start the reader in browse mode for the given source (or pass NULL for |
210 | * all sources). | |
89cb07a6 | 211 | * |
b0e88ebd | 212 | * @param source |
89cb07a6 NR |
213 | * the type of {@link Story} to take into account, or NULL for |
214 | * all | |
215 | */ | |
b0e88ebd | 216 | public abstract void browse(String source); |
3727aae2 NR |
217 | |
218 | /** | |
d0114000 NR |
219 | * Return a new {@link BasicReader} ready for use if one is configured. |
220 | * <p> | |
221 | * Can return NULL if none are configured. | |
3727aae2 | 222 | * |
d0114000 | 223 | * @return a {@link BasicReader}, or NULL if none configured |
3727aae2 NR |
224 | */ |
225 | public static BasicReader getReader() { | |
333f0e7b NR |
226 | try { |
227 | if (defaultType != null) { | |
b0e88ebd NR |
228 | return ((BasicReader) SerialUtils.createObject(defaultType |
229 | .getTypeName())).setType(defaultType); | |
d0114000 | 230 | } |
9119671d | 231 | } catch (Exception e) { |
333f0e7b | 232 | Instance.syserr(new Exception("Cannot create a reader of type: " |
9119671d | 233 | + defaultType + " (Not compiled in?)", e)); |
3727aae2 NR |
234 | } |
235 | ||
236 | return null; | |
237 | } | |
238 | ||
239 | /** | |
240 | * The default {@link ReaderType} used when calling | |
241 | * {@link BasicReader#getReader()}. | |
242 | * | |
243 | * @return the default type | |
244 | */ | |
245 | public static ReaderType getDefaultReaderType() { | |
246 | return defaultType; | |
247 | } | |
248 | ||
249 | /** | |
250 | * The default {@link ReaderType} used when calling | |
251 | * {@link BasicReader#getReader()}. | |
252 | * | |
253 | * @param defaultType | |
254 | * the new default type | |
255 | */ | |
256 | public static void setDefaultReaderType(ReaderType defaultType) { | |
257 | BasicReader.defaultType = defaultType; | |
258 | } | |
3b2b638f | 259 | |
b0e88ebd | 260 | /** |
68e2c6d2 NR |
261 | * Change the default {@link LocalLibrary} to open with the |
262 | * {@link BasicReader}s. | |
b0e88ebd NR |
263 | * |
264 | * @param lib | |
68e2c6d2 | 265 | * the new {@link LocalLibrary} |
b0e88ebd | 266 | */ |
68e2c6d2 | 267 | public static void setDefaultLibrary(BasicLibrary lib) { |
b0e88ebd NR |
268 | BasicReader.defaultLibrary = lib; |
269 | } | |
270 | ||
3b2b638f NR |
271 | /** |
272 | * Return an {@link URL} from this {@link String}, be it a file path or an | |
273 | * actual {@link URL}. | |
274 | * | |
275 | * @param sourceString | |
276 | * the source | |
277 | * | |
278 | * @return the corresponding {@link URL} | |
279 | * | |
280 | * @throws MalformedURLException | |
281 | * if this is neither a file nor a conventional {@link URL} | |
282 | */ | |
283 | public static URL getUrl(String sourceString) throws MalformedURLException { | |
284 | if (sourceString == null || sourceString.isEmpty()) { | |
285 | throw new MalformedURLException("Empty url"); | |
286 | } | |
287 | ||
288 | URL source = null; | |
289 | try { | |
290 | source = new URL(sourceString); | |
291 | } catch (MalformedURLException e) { | |
292 | File sourceFile = new File(sourceString); | |
293 | source = sourceFile.toURI().toURL(); | |
294 | } | |
295 | ||
296 | return source; | |
297 | } | |
c1873e56 | 298 | |
5dd985cf NR |
299 | /** |
300 | * Open the {@link Story} with an external reader (the program will be | |
301 | * passed the main file associated with this {@link Story}). | |
302 | * | |
303 | * @param lib | |
304 | * the {@link BasicLibrary} to select the {@link Story} from | |
305 | * @param luid | |
306 | * the {@link Story} LUID | |
307 | * | |
308 | * @throws IOException | |
309 | * in case of I/O error | |
310 | */ | |
68e2c6d2 | 311 | public static void open(BasicLibrary lib, String luid) throws IOException { |
b0e88ebd NR |
312 | MetaData meta = lib.getInfo(luid); |
313 | File target = lib.getFile(luid); | |
c1873e56 NR |
314 | |
315 | open(meta, target); | |
316 | } | |
317 | ||
5dd985cf NR |
318 | /** |
319 | * Open the {@link Story} with an external reader (the program will be | |
320 | * passed the given target file). | |
321 | * | |
322 | * @param meta | |
323 | * the {@link Story} to load | |
324 | * @param target | |
325 | * the target {@link File} | |
326 | * | |
327 | * @throws IOException | |
328 | * in case of I/O error | |
329 | */ | |
c1873e56 NR |
330 | protected static void open(MetaData meta, File target) throws IOException { |
331 | String program = null; | |
332 | if (meta.isImageDocument()) { | |
333 | program = Instance.getUiConfig().getString( | |
334 | UiConfig.IMAGES_DOCUMENT_READER); | |
335 | } else { | |
336 | program = Instance.getUiConfig().getString( | |
337 | UiConfig.NON_IMAGES_DOCUMENT_READER); | |
338 | } | |
339 | ||
340 | if (program != null && program.trim().isEmpty()) { | |
341 | program = null; | |
342 | } | |
343 | ||
344 | if (program == null) { | |
345 | try { | |
346 | Desktop.getDesktop().browse(target.toURI()); | |
347 | } catch (UnsupportedOperationException e) { | |
348 | Runtime.getRuntime().exec( | |
349 | new String[] { "xdg-open", target.getAbsolutePath() }); | |
350 | ||
351 | } | |
352 | } else { | |
353 | Runtime.getRuntime().exec( | |
354 | new String[] { program, target.getAbsolutePath() }); | |
355 | } | |
356 | } | |
89cb07a6 | 357 | } |