Commit | Line | Data |
---|---|---|
89cb07a6 NR |
1 | package be.nikiroo.fanfix.reader; |
2 | ||
3b2b638f | 3 | import java.io.File; |
89cb07a6 | 4 | import java.io.IOException; |
3b2b638f | 5 | import java.net.MalformedURLException; |
89cb07a6 | 6 | import java.net.URL; |
df6e2d88 NR |
7 | import java.text.ParseException; |
8 | import java.text.SimpleDateFormat; | |
df6e2d88 | 9 | import java.util.Date; |
a6c830bb NR |
10 | import java.util.Map; |
11 | import java.util.TreeMap; | |
89cb07a6 NR |
12 | |
13 | import be.nikiroo.fanfix.Instance; | |
d0114000 | 14 | import be.nikiroo.fanfix.bundles.Config; |
c1873e56 NR |
15 | import be.nikiroo.fanfix.bundles.UiConfig; |
16 | import be.nikiroo.fanfix.data.MetaData; | |
89cb07a6 | 17 | import be.nikiroo.fanfix.data.Story; |
e42573a0 NR |
18 | import be.nikiroo.fanfix.library.BasicLibrary; |
19 | import be.nikiroo.fanfix.library.LocalLibrary; | |
89cb07a6 | 20 | import be.nikiroo.fanfix.supported.BasicSupport; |
3b2b638f | 21 | import be.nikiroo.utils.Progress; |
df6e2d88 | 22 | import be.nikiroo.utils.StringUtils; |
9119671d | 23 | import be.nikiroo.utils.serial.SerialUtils; |
89cb07a6 NR |
24 | |
25 | /** | |
dd56a893 | 26 | * The class that handles the different {@link Story} readers you can use. |
89cb07a6 | 27 | * <p> |
dd56a893 | 28 | * All the readers should be accessed via {@link BasicReader#getReader()}. |
89cb07a6 NR |
29 | * |
30 | * @author niki | |
31 | */ | |
e42573a0 | 32 | public abstract class BasicReader implements Reader { |
68e2c6d2 | 33 | private static BasicLibrary defaultLibrary = Instance.getLibrary(); |
c1873e56 | 34 | private static ReaderType defaultType = ReaderType.GUI; |
b0e88ebd | 35 | |
68e2c6d2 | 36 | private BasicLibrary lib; |
bc2ea776 | 37 | private MetaData meta; |
89cb07a6 | 38 | private Story story; |
bc2ea776 | 39 | private int chapter; |
3727aae2 | 40 | |
d0114000 NR |
41 | /** |
42 | * Take the default reader type configuration from the config file. | |
43 | */ | |
3727aae2 | 44 | static { |
d0114000 NR |
45 | String typeString = Instance.getConfig().getString(Config.READER_TYPE); |
46 | if (typeString != null && !typeString.isEmpty()) { | |
47 | try { | |
48 | ReaderType type = ReaderType.valueOf(typeString.toUpperCase()); | |
49 | defaultType = type; | |
50 | } catch (IllegalArgumentException e) { | |
51 | // Do nothing | |
52 | } | |
53 | } | |
3727aae2 NR |
54 | } |
55 | ||
211f7ddb | 56 | @Override |
bc2ea776 NR |
57 | public synchronized Story getStory(Progress pg) { |
58 | if (story == null) { | |
59 | story = getLibrary().getStory(meta.getLuid(), pg); | |
60 | } | |
61 | ||
89cb07a6 NR |
62 | return story; |
63 | } | |
64 | ||
211f7ddb | 65 | @Override |
68e2c6d2 | 66 | public BasicLibrary getLibrary() { |
b0e88ebd NR |
67 | if (lib == null) { |
68 | lib = defaultLibrary; | |
69 | } | |
70 | ||
71 | return lib; | |
72 | } | |
73 | ||
211f7ddb | 74 | @Override |
bc2ea776 | 75 | public void setLibrary(BasicLibrary lib) { |
b0e88ebd NR |
76 | this.lib = lib; |
77 | } | |
78 | ||
211f7ddb | 79 | @Override |
9fe3f177 | 80 | public synchronized MetaData getMeta() { |
bc2ea776 NR |
81 | return meta; |
82 | } | |
83 | ||
211f7ddb | 84 | @Override |
bc2ea776 NR |
85 | public synchronized void setMeta(MetaData meta) throws IOException { |
86 | setMeta(meta == null ? null : meta.getLuid()); // must check the library | |
87 | } | |
88 | ||
211f7ddb | 89 | @Override |
bc2ea776 NR |
90 | public synchronized void setMeta(String luid) throws IOException { |
91 | story = null; | |
92 | meta = getLibrary().getInfo(luid); | |
93 | ||
94 | if (meta == null) { | |
92fb0719 | 95 | throw new IOException("Cannot retrieve story from library: " + luid); |
89cb07a6 NR |
96 | } |
97 | } | |
98 | ||
211f7ddb | 99 | @Override |
350bc060 | 100 | public synchronized void setMeta(URL url, Progress pg) throws IOException { |
e0fb1417 | 101 | BasicSupport support = BasicSupport.getSupport(url); |
89cb07a6 | 102 | if (support == null) { |
e0fb1417 | 103 | throw new IOException("URL not supported: " + url.toString()); |
89cb07a6 NR |
104 | } |
105 | ||
0ffa4754 | 106 | story = support.process(pg); |
89cb07a6 NR |
107 | if (story == null) { |
108 | throw new IOException( | |
109 | "Cannot retrieve story from external source: " | |
e0fb1417 | 110 | + url.toString()); |
89cb07a6 | 111 | } |
bc2ea776 NR |
112 | |
113 | meta = story.getMeta(); | |
114 | } | |
115 | ||
211f7ddb | 116 | @Override |
bc2ea776 NR |
117 | public int getChapter() { |
118 | return chapter; | |
89cb07a6 NR |
119 | } |
120 | ||
211f7ddb | 121 | @Override |
bc2ea776 NR |
122 | public void setChapter(int chapter) { |
123 | this.chapter = chapter; | |
6322ab64 NR |
124 | } |
125 | ||
3727aae2 | 126 | /** |
d0114000 NR |
127 | * Return a new {@link BasicReader} ready for use if one is configured. |
128 | * <p> | |
129 | * Can return NULL if none are configured. | |
3727aae2 | 130 | * |
d0114000 | 131 | * @return a {@link BasicReader}, or NULL if none configured |
3727aae2 | 132 | */ |
e42573a0 | 133 | public static Reader getReader() { |
333f0e7b NR |
134 | try { |
135 | if (defaultType != null) { | |
e42573a0 NR |
136 | return (Reader) SerialUtils.createObject(defaultType |
137 | .getTypeName()); | |
d0114000 | 138 | } |
9119671d | 139 | } catch (Exception e) { |
16a81ef7 NR |
140 | Instance.getTraceHandler().error( |
141 | new Exception("Cannot create a reader of type: " | |
142 | + defaultType + " (Not compiled in?)", e)); | |
3727aae2 NR |
143 | } |
144 | ||
145 | return null; | |
146 | } | |
147 | ||
148 | /** | |
bc2ea776 | 149 | * The default {@link Reader.ReaderType} used when calling |
3727aae2 NR |
150 | * {@link BasicReader#getReader()}. |
151 | * | |
152 | * @return the default type | |
153 | */ | |
154 | public static ReaderType getDefaultReaderType() { | |
155 | return defaultType; | |
156 | } | |
157 | ||
158 | /** | |
bc2ea776 | 159 | * The default {@link Reader.ReaderType} used when calling |
3727aae2 NR |
160 | * {@link BasicReader#getReader()}. |
161 | * | |
162 | * @param defaultType | |
163 | * the new default type | |
164 | */ | |
165 | public static void setDefaultReaderType(ReaderType defaultType) { | |
166 | BasicReader.defaultType = defaultType; | |
167 | } | |
3b2b638f | 168 | |
b0e88ebd | 169 | /** |
68e2c6d2 NR |
170 | * Change the default {@link LocalLibrary} to open with the |
171 | * {@link BasicReader}s. | |
b0e88ebd NR |
172 | * |
173 | * @param lib | |
68e2c6d2 | 174 | * the new {@link LocalLibrary} |
b0e88ebd | 175 | */ |
68e2c6d2 | 176 | public static void setDefaultLibrary(BasicLibrary lib) { |
b0e88ebd NR |
177 | BasicReader.defaultLibrary = lib; |
178 | } | |
179 | ||
3b2b638f NR |
180 | /** |
181 | * Return an {@link URL} from this {@link String}, be it a file path or an | |
182 | * actual {@link URL}. | |
183 | * | |
184 | * @param sourceString | |
185 | * the source | |
186 | * | |
187 | * @return the corresponding {@link URL} | |
188 | * | |
189 | * @throws MalformedURLException | |
190 | * if this is neither a file nor a conventional {@link URL} | |
191 | */ | |
192 | public static URL getUrl(String sourceString) throws MalformedURLException { | |
193 | if (sourceString == null || sourceString.isEmpty()) { | |
194 | throw new MalformedURLException("Empty url"); | |
195 | } | |
196 | ||
197 | URL source = null; | |
198 | try { | |
199 | source = new URL(sourceString); | |
200 | } catch (MalformedURLException e) { | |
201 | File sourceFile = new File(sourceString); | |
202 | source = sourceFile.toURI().toURL(); | |
203 | } | |
204 | ||
205 | return source; | |
206 | } | |
c1873e56 | 207 | |
df6e2d88 NR |
208 | /** |
209 | * Describe a {@link Story} from its {@link MetaData} and return a list of | |
210 | * title/value that represent this {@link Story}. | |
211 | * | |
212 | * @param meta | |
213 | * the {@link MetaData} to represent | |
214 | * | |
215 | * @return the information | |
216 | */ | |
a6c830bb NR |
217 | public static Map<String, String> getMetaDesc(MetaData meta) { |
218 | Map<String, String> metaDesc = new TreeMap<String, String>(); | |
df6e2d88 NR |
219 | |
220 | // TODO: i18n | |
221 | ||
222 | StringBuilder tags = new StringBuilder(); | |
223 | for (String tag : meta.getTags()) { | |
224 | if (tags.length() > 0) { | |
225 | tags.append(", "); | |
226 | } | |
227 | tags.append(tag); | |
228 | } | |
229 | ||
d16065ec | 230 | // TODO: i18n |
a6c830bb NR |
231 | metaDesc.put("Author", meta.getAuthor()); |
232 | metaDesc.put("Publication date", formatDate(meta.getDate())); | |
233 | metaDesc.put("Published on", meta.getPublisher()); | |
234 | metaDesc.put("URL", meta.getUrl()); | |
d16065ec NR |
235 | String count = ""; |
236 | if (meta.getWords() > 0) { | |
237 | count = StringUtils.formatNumber(meta.getWords()); | |
238 | } | |
a98e49ee | 239 | if (meta.isImageDocument()) { |
d16065ec | 240 | metaDesc.put("Number of images", count); |
a98e49ee | 241 | } else { |
d16065ec | 242 | metaDesc.put("Number of words", count); |
a98e49ee | 243 | } |
a6c830bb NR |
244 | metaDesc.put("Source", meta.getSource()); |
245 | metaDesc.put("Subject", meta.getSubject()); | |
246 | metaDesc.put("Language", meta.getLang()); | |
247 | metaDesc.put("Tags", tags.toString()); | |
df6e2d88 NR |
248 | |
249 | return metaDesc; | |
250 | } | |
251 | ||
5dd985cf NR |
252 | /** |
253 | * Open the {@link Story} with an external reader (the program will be | |
254 | * passed the main file associated with this {@link Story}). | |
255 | * | |
256 | * @param lib | |
257 | * the {@link BasicLibrary} to select the {@link Story} from | |
258 | * @param luid | |
259 | * the {@link Story} LUID | |
350bc060 NR |
260 | * @param sync |
261 | * execute the process synchronously (wait until it is terminated | |
262 | * before returning) | |
5dd985cf NR |
263 | * |
264 | * @throws IOException | |
265 | * in case of I/O error | |
266 | */ | |
16a81ef7 | 267 | @Override |
350bc060 NR |
268 | public void openExternal(BasicLibrary lib, String luid, boolean sync) |
269 | throws IOException { | |
b0e88ebd | 270 | MetaData meta = lib.getInfo(luid); |
ff05b828 | 271 | File target = lib.getFile(luid, null); |
c1873e56 | 272 | |
350bc060 | 273 | openExternal(meta, target, sync); |
c1873e56 NR |
274 | } |
275 | ||
5dd985cf NR |
276 | /** |
277 | * Open the {@link Story} with an external reader (the program will be | |
278 | * passed the given target file). | |
279 | * | |
280 | * @param meta | |
281 | * the {@link Story} to load | |
282 | * @param target | |
283 | * the target {@link File} | |
350bc060 NR |
284 | * @param sync |
285 | * execute the process synchronously (wait until it is terminated | |
286 | * before returning) | |
5dd985cf NR |
287 | * |
288 | * @throws IOException | |
289 | * in case of I/O error | |
290 | */ | |
350bc060 NR |
291 | protected void openExternal(MetaData meta, File target, boolean sync) |
292 | throws IOException { | |
c1873e56 NR |
293 | String program = null; |
294 | if (meta.isImageDocument()) { | |
295 | program = Instance.getUiConfig().getString( | |
296 | UiConfig.IMAGES_DOCUMENT_READER); | |
297 | } else { | |
298 | program = Instance.getUiConfig().getString( | |
299 | UiConfig.NON_IMAGES_DOCUMENT_READER); | |
300 | } | |
301 | ||
302 | if (program != null && program.trim().isEmpty()) { | |
303 | program = null; | |
304 | } | |
305 | ||
350bc060 | 306 | start(target, program, sync); |
16a81ef7 | 307 | } |
c1873e56 | 308 | |
16a81ef7 NR |
309 | /** |
310 | * Start a file and open it with the given program if given or the first | |
311 | * default system starter we can find. | |
312 | * | |
313 | * @param target | |
314 | * the target to open | |
315 | * @param program | |
316 | * the program to use or NULL for the default system starter | |
350bc060 NR |
317 | * @param sync |
318 | * execute the process synchronously (wait until it is terminated | |
319 | * before returning) | |
16a81ef7 NR |
320 | * |
321 | * @throws IOException | |
322 | * in case of I/O error | |
323 | */ | |
350bc060 NR |
324 | protected void start(File target, String program, boolean sync) |
325 | throws IOException { | |
326 | ||
327 | Process proc = null; | |
16a81ef7 NR |
328 | if (program == null) { |
329 | boolean ok = false; | |
b4f9071c NR |
330 | for (String starter : new String[] { "xdg-open", "open", "see", |
331 | "start", "run" }) { | |
16a81ef7 | 332 | try { |
9e2fad36 NR |
333 | Instance.getTraceHandler().trace( |
334 | "starting external program"); | |
350bc060 | 335 | proc = Runtime.getRuntime().exec( |
16a81ef7 NR |
336 | new String[] { starter, target.getAbsolutePath() }); |
337 | ok = true; | |
338 | break; | |
339 | } catch (IOException e) { | |
340 | } | |
341 | } | |
342 | if (!ok) { | |
343 | throw new IOException("Cannot find a program to start the file"); | |
c1873e56 NR |
344 | } |
345 | } else { | |
9e2fad36 | 346 | Instance.getTraceHandler().trace("starting external program"); |
350bc060 | 347 | proc = Runtime.getRuntime().exec( |
c1873e56 NR |
348 | new String[] { program, target.getAbsolutePath() }); |
349 | } | |
350bc060 NR |
350 | |
351 | if (proc != null && sync) { | |
9ce8d884 N |
352 | try { |
353 | proc.waitFor(); | |
354 | } catch (InterruptedException e) { | |
350bc060 NR |
355 | } |
356 | } | |
c1873e56 | 357 | } |
df6e2d88 | 358 | |
df6e2d88 NR |
359 | static private String formatDate(String date) { |
360 | long ms = 0; | |
361 | ||
d16065ec | 362 | if (date != null && !date.isEmpty()) { |
df6e2d88 | 363 | try { |
d16065ec | 364 | ms = StringUtils.toTime(date); |
df6e2d88 NR |
365 | } catch (ParseException e) { |
366 | } | |
d16065ec NR |
367 | |
368 | if (ms <= 0) { | |
369 | SimpleDateFormat sdf = new SimpleDateFormat( | |
370 | "yyyy-MM-dd'T'HH:mm:ssSSS"); | |
371 | try { | |
372 | ms = sdf.parse(date).getTime(); | |
373 | } catch (ParseException e) { | |
374 | } | |
375 | } | |
376 | ||
377 | if (ms > 0) { | |
378 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | |
379 | return sdf.format(new Date(ms)); | |
380 | } | |
df6e2d88 NR |
381 | } |
382 | ||
d16065ec NR |
383 | if (date == null) { |
384 | date = ""; | |
df6e2d88 NR |
385 | } |
386 | ||
387 | // :( | |
388 | return date; | |
389 | } | |
89cb07a6 | 390 | } |