tui: fix meta/story use
[fanfix.git] / src / be / nikiroo / fanfix / reader / BasicReader.java
CommitLineData
89cb07a6
NR
1package be.nikiroo.fanfix.reader;
2
3b2b638f 3import java.io.File;
89cb07a6 4import java.io.IOException;
3b2b638f 5import java.net.MalformedURLException;
89cb07a6
NR
6import java.net.URL;
7
8import be.nikiroo.fanfix.Instance;
d0114000 9import be.nikiroo.fanfix.bundles.Config;
c1873e56
NR
10import be.nikiroo.fanfix.bundles.UiConfig;
11import be.nikiroo.fanfix.data.MetaData;
89cb07a6 12import be.nikiroo.fanfix.data.Story;
e42573a0
NR
13import be.nikiroo.fanfix.library.BasicLibrary;
14import be.nikiroo.fanfix.library.LocalLibrary;
89cb07a6 15import be.nikiroo.fanfix.supported.BasicSupport;
3b2b638f 16import be.nikiroo.utils.Progress;
9119671d 17import be.nikiroo.utils.serial.SerialUtils;
89cb07a6
NR
18
19/**
dd56a893 20 * The class that handles the different {@link Story} readers you can use.
89cb07a6 21 * <p>
dd56a893 22 * All the readers should be accessed via {@link BasicReader#getReader()}.
89cb07a6
NR
23 *
24 * @author niki
25 */
e42573a0 26public abstract class BasicReader implements Reader {
68e2c6d2 27 private static BasicLibrary defaultLibrary = Instance.getLibrary();
c1873e56 28 private static ReaderType defaultType = ReaderType.GUI;
b0e88ebd 29
68e2c6d2 30 private BasicLibrary lib;
bc2ea776 31 private MetaData meta;
89cb07a6 32 private Story story;
bc2ea776 33 private int chapter;
3727aae2 34
d0114000
NR
35 /**
36 * Take the default reader type configuration from the config file.
37 */
3727aae2 38 static {
d0114000
NR
39 String typeString = Instance.getConfig().getString(Config.READER_TYPE);
40 if (typeString != null && !typeString.isEmpty()) {
41 try {
42 ReaderType type = ReaderType.valueOf(typeString.toUpperCase());
43 defaultType = type;
44 } catch (IllegalArgumentException e) {
45 // Do nothing
46 }
47 }
3727aae2
NR
48 }
49
211f7ddb 50 @Override
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
211f7ddb 59 @Override
68e2c6d2 60 public BasicLibrary getLibrary() {
b0e88ebd
NR
61 if (lib == null) {
62 lib = defaultLibrary;
63 }
64
65 return lib;
66 }
67
211f7ddb 68 @Override
bc2ea776 69 public void setLibrary(BasicLibrary lib) {
b0e88ebd
NR
70 this.lib = lib;
71 }
72
211f7ddb 73 @Override
9fe3f177 74 public synchronized MetaData getMeta() {
bc2ea776
NR
75 return meta;
76 }
77
211f7ddb 78 @Override
bc2ea776
NR
79 public synchronized void setMeta(MetaData meta) throws IOException {
80 setMeta(meta == null ? null : meta.getLuid()); // must check the library
81 }
82
211f7ddb 83 @Override
bc2ea776
NR
84 public synchronized void setMeta(String luid) throws IOException {
85 story = null;
86 meta = getLibrary().getInfo(luid);
87
88 if (meta == null) {
92fb0719 89 throw new IOException("Cannot retrieve story from library: " + luid);
89cb07a6
NR
90 }
91 }
92
211f7ddb 93 @Override
e0fb1417 94 public synchronized void setMeta(URL url, Progress pg)
bc2ea776 95 throws IOException {
e0fb1417 96 BasicSupport support = BasicSupport.getSupport(url);
89cb07a6 97 if (support == null) {
e0fb1417 98 throw new IOException("URL not supported: " + url.toString());
89cb07a6
NR
99 }
100
0ffa4754 101 story = support.process(pg);
89cb07a6
NR
102 if (story == null) {
103 throw new IOException(
104 "Cannot retrieve story from external source: "
e0fb1417 105 + url.toString());
89cb07a6 106 }
bc2ea776
NR
107
108 meta = story.getMeta();
109 }
110
211f7ddb 111 @Override
bc2ea776
NR
112 public int getChapter() {
113 return chapter;
89cb07a6
NR
114 }
115
211f7ddb 116 @Override
bc2ea776
NR
117 public void setChapter(int chapter) {
118 this.chapter = chapter;
6322ab64
NR
119 }
120
3727aae2 121 /**
d0114000
NR
122 * Return a new {@link BasicReader} ready for use if one is configured.
123 * <p>
124 * Can return NULL if none are configured.
3727aae2 125 *
d0114000 126 * @return a {@link BasicReader}, or NULL if none configured
3727aae2 127 */
e42573a0 128 public static Reader getReader() {
333f0e7b
NR
129 try {
130 if (defaultType != null) {
e42573a0
NR
131 return (Reader) SerialUtils.createObject(defaultType
132 .getTypeName());
d0114000 133 }
9119671d 134 } catch (Exception e) {
16a81ef7
NR
135 Instance.getTraceHandler().error(
136 new Exception("Cannot create a reader of type: "
137 + defaultType + " (Not compiled in?)", e));
3727aae2
NR
138 }
139
140 return null;
141 }
142
143 /**
bc2ea776 144 * The default {@link Reader.ReaderType} used when calling
3727aae2
NR
145 * {@link BasicReader#getReader()}.
146 *
147 * @return the default type
148 */
149 public static ReaderType getDefaultReaderType() {
150 return defaultType;
151 }
152
153 /**
bc2ea776 154 * The default {@link Reader.ReaderType} used when calling
3727aae2
NR
155 * {@link BasicReader#getReader()}.
156 *
157 * @param defaultType
158 * the new default type
159 */
160 public static void setDefaultReaderType(ReaderType defaultType) {
161 BasicReader.defaultType = defaultType;
162 }
3b2b638f 163
b0e88ebd 164 /**
68e2c6d2
NR
165 * Change the default {@link LocalLibrary} to open with the
166 * {@link BasicReader}s.
b0e88ebd
NR
167 *
168 * @param lib
68e2c6d2 169 * the new {@link LocalLibrary}
b0e88ebd 170 */
68e2c6d2 171 public static void setDefaultLibrary(BasicLibrary lib) {
b0e88ebd
NR
172 BasicReader.defaultLibrary = lib;
173 }
174
3b2b638f
NR
175 /**
176 * Return an {@link URL} from this {@link String}, be it a file path or an
177 * actual {@link URL}.
178 *
179 * @param sourceString
180 * the source
181 *
182 * @return the corresponding {@link URL}
183 *
184 * @throws MalformedURLException
185 * if this is neither a file nor a conventional {@link URL}
186 */
187 public static URL getUrl(String sourceString) throws MalformedURLException {
188 if (sourceString == null || sourceString.isEmpty()) {
189 throw new MalformedURLException("Empty url");
190 }
191
192 URL source = null;
193 try {
194 source = new URL(sourceString);
195 } catch (MalformedURLException e) {
196 File sourceFile = new File(sourceString);
197 source = sourceFile.toURI().toURL();
198 }
199
200 return source;
201 }
c1873e56 202
5dd985cf
NR
203 /**
204 * Open the {@link Story} with an external reader (the program will be
205 * passed the main file associated with this {@link Story}).
206 *
207 * @param lib
208 * the {@link BasicLibrary} to select the {@link Story} from
209 * @param luid
210 * the {@link Story} LUID
211 *
212 * @throws IOException
213 * in case of I/O error
214 */
16a81ef7
NR
215 @Override
216 public void openExternal(BasicLibrary lib, String luid) throws IOException {
b0e88ebd 217 MetaData meta = lib.getInfo(luid);
ff05b828 218 File target = lib.getFile(luid, null);
c1873e56 219
6322ab64 220 openExternal(meta, target);
c1873e56
NR
221 }
222
5dd985cf
NR
223 /**
224 * Open the {@link Story} with an external reader (the program will be
225 * passed the given target file).
226 *
227 * @param meta
228 * the {@link Story} to load
229 * @param target
230 * the target {@link File}
231 *
232 * @throws IOException
233 * in case of I/O error
234 */
16a81ef7 235 protected void openExternal(MetaData meta, File target) throws IOException {
c1873e56
NR
236 String program = null;
237 if (meta.isImageDocument()) {
238 program = Instance.getUiConfig().getString(
239 UiConfig.IMAGES_DOCUMENT_READER);
240 } else {
241 program = Instance.getUiConfig().getString(
242 UiConfig.NON_IMAGES_DOCUMENT_READER);
243 }
244
245 if (program != null && program.trim().isEmpty()) {
246 program = null;
247 }
248
16a81ef7
NR
249 start(target, program);
250 }
c1873e56 251
16a81ef7
NR
252 /**
253 * Start a file and open it with the given program if given or the first
254 * default system starter we can find.
255 *
256 * @param target
257 * the target to open
258 * @param program
259 * the program to use or NULL for the default system starter
260 *
261 * @throws IOException
262 * in case of I/O error
263 */
264 protected void start(File target, String program) throws IOException {
265 if (program == null) {
266 boolean ok = false;
b4f9071c
NR
267 for (String starter : new String[] { "xdg-open", "open", "see",
268 "start", "run" }) {
16a81ef7 269 try {
9e2fad36
NR
270 Instance.getTraceHandler().trace(
271 "starting external program");
16a81ef7
NR
272 Runtime.getRuntime().exec(
273 new String[] { starter, target.getAbsolutePath() });
274 ok = true;
275 break;
276 } catch (IOException e) {
277 }
278 }
279 if (!ok) {
280 throw new IOException("Cannot find a program to start the file");
c1873e56
NR
281 }
282 } else {
9e2fad36 283 Instance.getTraceHandler().trace("starting external program");
c1873e56
NR
284 Runtime.getRuntime().exec(
285 new String[] { program, target.getAbsolutePath() });
286 }
287 }
89cb07a6 288}