Code cleanup 2 (a third one is pending)
[fanfix.git] / src / be / nikiroo / fanfix / reader / BasicReader.java
1 package be.nikiroo.fanfix.reader;
2
3 import java.awt.Desktop;
4 import java.io.File;
5 import java.io.IOException;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8
9 import be.nikiroo.fanfix.Instance;
10 import be.nikiroo.fanfix.bundles.Config;
11 import be.nikiroo.fanfix.bundles.UiConfig;
12 import be.nikiroo.fanfix.data.MetaData;
13 import be.nikiroo.fanfix.data.Story;
14 import be.nikiroo.fanfix.library.BasicLibrary;
15 import be.nikiroo.fanfix.library.LocalLibrary;
16 import be.nikiroo.fanfix.supported.BasicSupport;
17 import be.nikiroo.utils.Progress;
18 import be.nikiroo.utils.serial.SerialUtils;
19
20 /**
21 * The class that handles the different {@link Story} readers you can use.
22 * <p>
23 * All the readers should be accessed via {@link BasicReader#getReader()}.
24 *
25 * @author niki
26 */
27 public abstract class BasicReader implements Reader {
28 private static BasicLibrary defaultLibrary = Instance.getLibrary();
29 private static ReaderType defaultType = ReaderType.GUI;
30
31 private BasicLibrary lib;
32 private Story story;
33
34 /**
35 * Take the default reader type configuration from the config file.
36 */
37 static {
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 }
47 }
48
49 public Story getStory() {
50 return story;
51 }
52
53 public BasicLibrary getLibrary() {
54 if (lib == null) {
55 lib = defaultLibrary;
56 }
57
58 return lib;
59 }
60
61 public void setLibrary(LocalLibrary lib) {
62 this.lib = lib;
63 }
64
65 public void setStory(String luid, Progress pg) throws IOException {
66 story = getLibrary().getStory(luid, pg);
67 if (story == null) {
68 throw new IOException("Cannot retrieve story from library: " + luid);
69 }
70 }
71
72 public void setStory(URL source, Progress pg) throws IOException {
73 BasicSupport support = BasicSupport.getSupport(source);
74 if (support == null) {
75 throw new IOException("URL not supported: " + source.toString());
76 }
77
78 story = support.process(source, pg);
79 if (story == null) {
80 throw new IOException(
81 "Cannot retrieve story from external source: "
82 + source.toString());
83
84 }
85 }
86
87 public void read() throws IOException {
88 read(-1);
89 }
90
91 /**
92 * Return a new {@link BasicReader} ready for use if one is configured.
93 * <p>
94 * Can return NULL if none are configured.
95 *
96 * @return a {@link BasicReader}, or NULL if none configured
97 */
98 public static Reader getReader() {
99 try {
100 if (defaultType != null) {
101 return (Reader) SerialUtils.createObject(defaultType
102 .getTypeName());
103 }
104 } catch (Exception e) {
105 Instance.syserr(new Exception("Cannot create a reader of type: "
106 + defaultType + " (Not compiled in?)", e));
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 }
132
133 /**
134 * Change the default {@link LocalLibrary} to open with the
135 * {@link BasicReader}s.
136 *
137 * @param lib
138 * the new {@link LocalLibrary}
139 */
140 public static void setDefaultLibrary(BasicLibrary lib) {
141 BasicReader.defaultLibrary = lib;
142 }
143
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 }
171
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 */
184 public static void openExternal(BasicLibrary lib, String luid)
185 throws IOException {
186 MetaData meta = lib.getInfo(luid);
187 File target = lib.getFile(luid);
188
189 openExternal(meta, target);
190 }
191
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 */
204 protected static void openExternal(MetaData meta, File target)
205 throws IOException {
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 }
232 }