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