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 | ||
9 | import be.nikiroo.fanfix.Instance; | |
d0114000 | 10 | import be.nikiroo.fanfix.bundles.Config; |
c1873e56 NR |
11 | import be.nikiroo.fanfix.bundles.UiConfig; |
12 | import be.nikiroo.fanfix.data.MetaData; | |
89cb07a6 | 13 | import be.nikiroo.fanfix.data.Story; |
e42573a0 NR |
14 | import be.nikiroo.fanfix.library.BasicLibrary; |
15 | import be.nikiroo.fanfix.library.LocalLibrary; | |
89cb07a6 | 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 | */ | |
e42573a0 | 27 | public abstract class BasicReader implements Reader { |
68e2c6d2 | 28 | private static BasicLibrary defaultLibrary = Instance.getLibrary(); |
c1873e56 | 29 | private static ReaderType defaultType = ReaderType.GUI; |
b0e88ebd | 30 | |
68e2c6d2 | 31 | private BasicLibrary lib; |
bc2ea776 | 32 | private MetaData meta; |
89cb07a6 | 33 | private Story story; |
bc2ea776 | 34 | private int chapter; |
3727aae2 | 35 | |
d0114000 NR |
36 | /** |
37 | * Take the default reader type configuration from the config file. | |
38 | */ | |
3727aae2 | 39 | static { |
d0114000 NR |
40 | String typeString = Instance.getConfig().getString(Config.READER_TYPE); |
41 | if (typeString != null && !typeString.isEmpty()) { | |
42 | try { | |
43 | ReaderType type = ReaderType.valueOf(typeString.toUpperCase()); | |
44 | defaultType = type; | |
45 | } catch (IllegalArgumentException e) { | |
46 | // Do nothing | |
47 | } | |
48 | } | |
3727aae2 NR |
49 | } |
50 | ||
bc2ea776 NR |
51 | public synchronized Story getStory(Progress pg) { |
52 | if (story == null) { | |
53 | story = getLibrary().getStory(meta.getLuid(), pg); | |
54 | } | |
55 | ||
89cb07a6 NR |
56 | return story; |
57 | } | |
58 | ||
68e2c6d2 | 59 | public BasicLibrary getLibrary() { |
b0e88ebd NR |
60 | if (lib == null) { |
61 | lib = defaultLibrary; | |
62 | } | |
63 | ||
64 | return lib; | |
65 | } | |
66 | ||
bc2ea776 | 67 | public void setLibrary(BasicLibrary lib) { |
b0e88ebd NR |
68 | this.lib = lib; |
69 | } | |
70 | ||
bc2ea776 NR |
71 | public MetaData getMeta() { |
72 | return meta; | |
73 | } | |
74 | ||
75 | public synchronized void setMeta(MetaData meta) throws IOException { | |
76 | setMeta(meta == null ? null : meta.getLuid()); // must check the library | |
77 | } | |
78 | ||
79 | public synchronized void setMeta(String luid) throws IOException { | |
80 | story = null; | |
81 | meta = getLibrary().getInfo(luid); | |
82 | ||
83 | if (meta == null) { | |
92fb0719 | 84 | throw new IOException("Cannot retrieve story from library: " + luid); |
89cb07a6 NR |
85 | } |
86 | } | |
87 | ||
bc2ea776 NR |
88 | public synchronized void setMeta(URL source, Progress pg) |
89 | throws IOException { | |
89cb07a6 NR |
90 | BasicSupport support = BasicSupport.getSupport(source); |
91 | if (support == null) { | |
92 | throw new IOException("URL not supported: " + source.toString()); | |
93 | } | |
94 | ||
92fb0719 | 95 | story = support.process(source, pg); |
89cb07a6 NR |
96 | if (story == null) { |
97 | throw new IOException( | |
98 | "Cannot retrieve story from external source: " | |
99 | + source.toString()); | |
89cb07a6 | 100 | } |
bc2ea776 NR |
101 | |
102 | meta = story.getMeta(); | |
103 | } | |
104 | ||
105 | public int getChapter() { | |
106 | return chapter; | |
89cb07a6 NR |
107 | } |
108 | ||
bc2ea776 NR |
109 | public void setChapter(int chapter) { |
110 | this.chapter = chapter; | |
6322ab64 NR |
111 | } |
112 | ||
3727aae2 | 113 | /** |
d0114000 NR |
114 | * Return a new {@link BasicReader} ready for use if one is configured. |
115 | * <p> | |
116 | * Can return NULL if none are configured. | |
3727aae2 | 117 | * |
d0114000 | 118 | * @return a {@link BasicReader}, or NULL if none configured |
3727aae2 | 119 | */ |
e42573a0 | 120 | public static Reader getReader() { |
333f0e7b NR |
121 | try { |
122 | if (defaultType != null) { | |
e42573a0 NR |
123 | return (Reader) SerialUtils.createObject(defaultType |
124 | .getTypeName()); | |
d0114000 | 125 | } |
9119671d | 126 | } catch (Exception e) { |
333f0e7b | 127 | Instance.syserr(new Exception("Cannot create a reader of type: " |
9119671d | 128 | + defaultType + " (Not compiled in?)", e)); |
3727aae2 NR |
129 | } |
130 | ||
131 | return null; | |
132 | } | |
133 | ||
134 | /** | |
bc2ea776 | 135 | * The default {@link Reader.ReaderType} used when calling |
3727aae2 NR |
136 | * {@link BasicReader#getReader()}. |
137 | * | |
138 | * @return the default type | |
139 | */ | |
140 | public static ReaderType getDefaultReaderType() { | |
141 | return defaultType; | |
142 | } | |
143 | ||
144 | /** | |
bc2ea776 | 145 | * The default {@link Reader.ReaderType} used when calling |
3727aae2 NR |
146 | * {@link BasicReader#getReader()}. |
147 | * | |
148 | * @param defaultType | |
149 | * the new default type | |
150 | */ | |
151 | public static void setDefaultReaderType(ReaderType defaultType) { | |
152 | BasicReader.defaultType = defaultType; | |
153 | } | |
3b2b638f | 154 | |
b0e88ebd | 155 | /** |
68e2c6d2 NR |
156 | * Change the default {@link LocalLibrary} to open with the |
157 | * {@link BasicReader}s. | |
b0e88ebd NR |
158 | * |
159 | * @param lib | |
68e2c6d2 | 160 | * the new {@link LocalLibrary} |
b0e88ebd | 161 | */ |
68e2c6d2 | 162 | public static void setDefaultLibrary(BasicLibrary lib) { |
b0e88ebd NR |
163 | BasicReader.defaultLibrary = lib; |
164 | } | |
165 | ||
3b2b638f NR |
166 | /** |
167 | * Return an {@link URL} from this {@link String}, be it a file path or an | |
168 | * actual {@link URL}. | |
169 | * | |
170 | * @param sourceString | |
171 | * the source | |
172 | * | |
173 | * @return the corresponding {@link URL} | |
174 | * | |
175 | * @throws MalformedURLException | |
176 | * if this is neither a file nor a conventional {@link URL} | |
177 | */ | |
178 | public static URL getUrl(String sourceString) throws MalformedURLException { | |
179 | if (sourceString == null || sourceString.isEmpty()) { | |
180 | throw new MalformedURLException("Empty url"); | |
181 | } | |
182 | ||
183 | URL source = null; | |
184 | try { | |
185 | source = new URL(sourceString); | |
186 | } catch (MalformedURLException e) { | |
187 | File sourceFile = new File(sourceString); | |
188 | source = sourceFile.toURI().toURL(); | |
189 | } | |
190 | ||
191 | return source; | |
192 | } | |
c1873e56 | 193 | |
5dd985cf NR |
194 | /** |
195 | * Open the {@link Story} with an external reader (the program will be | |
196 | * passed the main file associated with this {@link Story}). | |
197 | * | |
198 | * @param lib | |
199 | * the {@link BasicLibrary} to select the {@link Story} from | |
200 | * @param luid | |
201 | * the {@link Story} LUID | |
202 | * | |
203 | * @throws IOException | |
204 | * in case of I/O error | |
205 | */ | |
6322ab64 NR |
206 | public static void openExternal(BasicLibrary lib, String luid) |
207 | throws IOException { | |
b0e88ebd NR |
208 | MetaData meta = lib.getInfo(luid); |
209 | File target = lib.getFile(luid); | |
c1873e56 | 210 | |
6322ab64 | 211 | openExternal(meta, target); |
c1873e56 NR |
212 | } |
213 | ||
5dd985cf NR |
214 | /** |
215 | * Open the {@link Story} with an external reader (the program will be | |
216 | * passed the given target file). | |
217 | * | |
218 | * @param meta | |
219 | * the {@link Story} to load | |
220 | * @param target | |
221 | * the target {@link File} | |
222 | * | |
223 | * @throws IOException | |
224 | * in case of I/O error | |
225 | */ | |
6322ab64 NR |
226 | protected static void openExternal(MetaData meta, File target) |
227 | throws IOException { | |
c1873e56 NR |
228 | String program = null; |
229 | if (meta.isImageDocument()) { | |
230 | program = Instance.getUiConfig().getString( | |
231 | UiConfig.IMAGES_DOCUMENT_READER); | |
232 | } else { | |
233 | program = Instance.getUiConfig().getString( | |
234 | UiConfig.NON_IMAGES_DOCUMENT_READER); | |
235 | } | |
236 | ||
237 | if (program != null && program.trim().isEmpty()) { | |
238 | program = null; | |
239 | } | |
240 | ||
241 | if (program == null) { | |
242 | try { | |
243 | Desktop.getDesktop().browse(target.toURI()); | |
244 | } catch (UnsupportedOperationException e) { | |
245 | Runtime.getRuntime().exec( | |
246 | new String[] { "xdg-open", target.getAbsolutePath() }); | |
247 | ||
248 | } | |
249 | } else { | |
250 | Runtime.getRuntime().exec( | |
251 | new String[] { program, target.getAbsolutePath() }); | |
252 | } | |
253 | } | |
89cb07a6 | 254 | } |