1 package be
.nikiroo
.fanfix
.reader
;
4 import java
.io
.IOException
;
5 import java
.net
.MalformedURLException
;
8 import be
.nikiroo
.fanfix
.Instance
;
9 import be
.nikiroo
.fanfix
.bundles
.Config
;
10 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
11 import be
.nikiroo
.fanfix
.data
.MetaData
;
12 import be
.nikiroo
.fanfix
.data
.Story
;
13 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
14 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
15 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
16 import be
.nikiroo
.utils
.Progress
;
17 import be
.nikiroo
.utils
.serial
.SerialUtils
;
20 * The class that handles the different {@link Story} readers you can use.
22 * All the readers should be accessed via {@link BasicReader#getReader()}.
26 public abstract class BasicReader
implements Reader
{
27 private static BasicLibrary defaultLibrary
= Instance
.getLibrary();
28 private static ReaderType defaultType
= ReaderType
.GUI
;
30 private BasicLibrary lib
;
31 private MetaData meta
;
36 * Take the default reader type configuration from the config file.
39 String typeString
= Instance
.getConfig().getString(Config
.READER_TYPE
);
40 if (typeString
!= null && !typeString
.isEmpty()) {
42 ReaderType type
= ReaderType
.valueOf(typeString
.toUpperCase());
44 } catch (IllegalArgumentException e
) {
51 public synchronized Story
getStory(Progress pg
) {
53 story
= getLibrary().getStory(meta
.getLuid(), pg
);
60 public BasicLibrary
getLibrary() {
69 public void setLibrary(BasicLibrary lib
) {
74 public synchronized MetaData
getMeta() {
79 public synchronized void setMeta(MetaData meta
) throws IOException
{
80 setMeta(meta
== null ?
null : meta
.getLuid()); // must check the library
84 public synchronized void setMeta(String luid
) throws IOException
{
86 meta
= getLibrary().getInfo(luid
);
89 throw new IOException("Cannot retrieve story from library: " + luid
);
94 public synchronized void setMeta(URL source
, Progress pg
)
96 BasicSupport support
= BasicSupport
.getSupport(source
);
97 if (support
== null) {
98 throw new IOException("URL not supported: " + source
.toString());
101 story
= support
.process(pg
);
103 throw new IOException(
104 "Cannot retrieve story from external source: "
105 + source
.toString());
108 meta
= story
.getMeta();
112 public int getChapter() {
117 public void setChapter(int chapter
) {
118 this.chapter
= chapter
;
122 * Return a new {@link BasicReader} ready for use if one is configured.
124 * Can return NULL if none are configured.
126 * @return a {@link BasicReader}, or NULL if none configured
128 public static Reader
getReader() {
130 if (defaultType
!= null) {
131 return (Reader
) SerialUtils
.createObject(defaultType
134 } catch (Exception e
) {
135 Instance
.getTraceHandler().error(
136 new Exception("Cannot create a reader of type: "
137 + defaultType
+ " (Not compiled in?)", e
));
144 * The default {@link Reader.ReaderType} used when calling
145 * {@link BasicReader#getReader()}.
147 * @return the default type
149 public static ReaderType
getDefaultReaderType() {
154 * The default {@link Reader.ReaderType} used when calling
155 * {@link BasicReader#getReader()}.
158 * the new default type
160 public static void setDefaultReaderType(ReaderType defaultType
) {
161 BasicReader
.defaultType
= defaultType
;
165 * Change the default {@link LocalLibrary} to open with the
166 * {@link BasicReader}s.
169 * the new {@link LocalLibrary}
171 public static void setDefaultLibrary(BasicLibrary lib
) {
172 BasicReader
.defaultLibrary
= lib
;
176 * Return an {@link URL} from this {@link String}, be it a file path or an
177 * actual {@link URL}.
179 * @param sourceString
182 * @return the corresponding {@link URL}
184 * @throws MalformedURLException
185 * if this is neither a file nor a conventional {@link URL}
187 public static URL
getUrl(String sourceString
) throws MalformedURLException
{
188 if (sourceString
== null || sourceString
.isEmpty()) {
189 throw new MalformedURLException("Empty url");
194 source
= new URL(sourceString
);
195 } catch (MalformedURLException e
) {
196 File sourceFile
= new File(sourceString
);
197 source
= sourceFile
.toURI().toURL();
204 * Open the {@link Story} with an external reader (the program will be
205 * passed the main file associated with this {@link Story}).
208 * the {@link BasicLibrary} to select the {@link Story} from
210 * the {@link Story} LUID
212 * @throws IOException
213 * in case of I/O error
216 public void openExternal(BasicLibrary lib
, String luid
) throws IOException
{
217 MetaData meta
= lib
.getInfo(luid
);
218 File target
= lib
.getFile(luid
, null);
220 openExternal(meta
, target
);
224 * Open the {@link Story} with an external reader (the program will be
225 * passed the given target file).
228 * the {@link Story} to load
230 * the target {@link File}
232 * @throws IOException
233 * in case of I/O error
235 protected void openExternal(MetaData meta
, File target
) throws IOException
{
236 String program
= null;
237 if (meta
.isImageDocument()) {
238 program
= Instance
.getUiConfig().getString(
239 UiConfig
.IMAGES_DOCUMENT_READER
);
241 program
= Instance
.getUiConfig().getString(
242 UiConfig
.NON_IMAGES_DOCUMENT_READER
);
245 if (program
!= null && program
.trim().isEmpty()) {
249 start(target
, program
);
253 * Start a file and open it with the given program if given or the first
254 * default system starter we can find.
259 * the program to use or NULL for the default system starter
261 * @throws IOException
262 * in case of I/O error
264 protected void start(File target
, String program
) throws IOException
{
265 if (program
== null) {
267 for (String starter
: new String
[] { "xdg-open", "open", "see",
270 Runtime
.getRuntime().exec(
271 new String
[] { starter
, target
.getAbsolutePath() });
274 } catch (IOException e
) {
278 throw new IOException("Cannot find a program to start the file");
281 Runtime
.getRuntime().exec(
282 new String
[] { program
, target
.getAbsolutePath() });