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 url
, Progress pg
) throws IOException
{
95 BasicSupport support
= BasicSupport
.getSupport(url
);
96 if (support
== null) {
97 throw new IOException("URL not supported: " + url
.toString());
100 story
= support
.process(pg
);
102 throw new IOException(
103 "Cannot retrieve story from external source: "
107 meta
= story
.getMeta();
111 public int getChapter() {
116 public void setChapter(int chapter
) {
117 this.chapter
= chapter
;
121 * Return a new {@link BasicReader} ready for use if one is configured.
123 * Can return NULL if none are configured.
125 * @return a {@link BasicReader}, or NULL if none configured
127 public static Reader
getReader() {
129 if (defaultType
!= null) {
130 return (Reader
) SerialUtils
.createObject(defaultType
133 } catch (Exception e
) {
134 Instance
.getTraceHandler().error(
135 new Exception("Cannot create a reader of type: "
136 + defaultType
+ " (Not compiled in?)", e
));
143 * The default {@link Reader.ReaderType} used when calling
144 * {@link BasicReader#getReader()}.
146 * @return the default type
148 public static ReaderType
getDefaultReaderType() {
153 * The default {@link Reader.ReaderType} used when calling
154 * {@link BasicReader#getReader()}.
157 * the new default type
159 public static void setDefaultReaderType(ReaderType defaultType
) {
160 BasicReader
.defaultType
= defaultType
;
164 * Change the default {@link LocalLibrary} to open with the
165 * {@link BasicReader}s.
168 * the new {@link LocalLibrary}
170 public static void setDefaultLibrary(BasicLibrary lib
) {
171 BasicReader
.defaultLibrary
= lib
;
175 * Return an {@link URL} from this {@link String}, be it a file path or an
176 * actual {@link URL}.
178 * @param sourceString
181 * @return the corresponding {@link URL}
183 * @throws MalformedURLException
184 * if this is neither a file nor a conventional {@link URL}
186 public static URL
getUrl(String sourceString
) throws MalformedURLException
{
187 if (sourceString
== null || sourceString
.isEmpty()) {
188 throw new MalformedURLException("Empty url");
193 source
= new URL(sourceString
);
194 } catch (MalformedURLException e
) {
195 File sourceFile
= new File(sourceString
);
196 source
= sourceFile
.toURI().toURL();
203 * Open the {@link Story} with an external reader (the program will be
204 * passed the main file associated with this {@link Story}).
207 * the {@link BasicLibrary} to select the {@link Story} from
209 * the {@link Story} LUID
211 * execute the process synchronously (wait until it is terminated
214 * @throws IOException
215 * in case of I/O error
218 public void openExternal(BasicLibrary lib
, String luid
, boolean sync
)
220 MetaData meta
= lib
.getInfo(luid
);
221 File target
= lib
.getFile(luid
, null);
223 openExternal(meta
, target
, sync
);
227 * Open the {@link Story} with an external reader (the program will be
228 * passed the given target file).
231 * the {@link Story} to load
233 * the target {@link File}
235 * execute the process synchronously (wait until it is terminated
238 * @throws IOException
239 * in case of I/O error
241 protected void openExternal(MetaData meta
, File target
, boolean sync
)
243 String program
= null;
244 if (meta
.isImageDocument()) {
245 program
= Instance
.getUiConfig().getString(
246 UiConfig
.IMAGES_DOCUMENT_READER
);
248 program
= Instance
.getUiConfig().getString(
249 UiConfig
.NON_IMAGES_DOCUMENT_READER
);
252 if (program
!= null && program
.trim().isEmpty()) {
256 start(target
, program
, sync
);
260 * Start a file and open it with the given program if given or the first
261 * default system starter we can find.
266 * the program to use or NULL for the default system starter
268 * execute the process synchronously (wait until it is terminated
271 * @throws IOException
272 * in case of I/O error
274 protected void start(File target
, String program
, boolean sync
)
278 if (program
== null) {
280 for (String starter
: new String
[] { "xdg-open", "open", "see",
283 Instance
.getTraceHandler().trace(
284 "starting external program");
285 proc
= Runtime
.getRuntime().exec(
286 new String
[] { starter
, target
.getAbsolutePath() });
289 } catch (IOException e
) {
293 throw new IOException("Cannot find a program to start the file");
296 Instance
.getTraceHandler().trace("starting external program");
297 proc
= Runtime
.getRuntime().exec(
298 new String
[] { program
, target
.getAbsolutePath() });
301 if (proc
!= null && sync
) {
302 while (proc
.isAlive()) {
305 } catch (InterruptedException e
) {