1 package be
.nikiroo
.fanfix
.reader
;
3 import java
.awt
.Desktop
;
5 import java
.io
.IOException
;
6 import java
.net
.MalformedURLException
;
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
;
21 * The class that handles the different {@link Story} readers you can use.
23 * All the readers should be accessed via {@link BasicReader#getReader()}.
27 public abstract class BasicReader
implements Reader
{
28 private static BasicLibrary defaultLibrary
= Instance
.getLibrary();
29 private static ReaderType defaultType
= ReaderType
.GUI
;
31 private BasicLibrary lib
;
35 * Take the default reader type configuration from the config file.
38 String typeString
= Instance
.getConfig().getString(Config
.READER_TYPE
);
39 if (typeString
!= null && !typeString
.isEmpty()) {
41 ReaderType type
= ReaderType
.valueOf(typeString
.toUpperCase());
43 } catch (IllegalArgumentException e
) {
49 public Story
getStory() {
53 public BasicLibrary
getLibrary() {
61 public void setLibrary(LocalLibrary lib
) {
65 public void setStory(String luid
, Progress pg
) throws IOException
{
66 story
= lib
.getStory(luid
, pg
);
68 throw new IOException("Cannot retrieve story from library: " + luid
);
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());
78 story
= support
.process(source
, pg
);
80 throw new IOException(
81 "Cannot retrieve story from external source: "
88 * Return a new {@link BasicReader} ready for use if one is configured.
90 * Can return NULL if none are configured.
92 * @return a {@link BasicReader}, or NULL if none configured
94 public static Reader
getReader() {
96 if (defaultType
!= null) {
97 return (Reader
) SerialUtils
.createObject(defaultType
100 } catch (Exception e
) {
101 Instance
.syserr(new Exception("Cannot create a reader of type: "
102 + defaultType
+ " (Not compiled in?)", e
));
109 * The default {@link ReaderType} used when calling
110 * {@link BasicReader#getReader()}.
112 * @return the default type
114 public static ReaderType
getDefaultReaderType() {
119 * The default {@link ReaderType} used when calling
120 * {@link BasicReader#getReader()}.
123 * the new default type
125 public static void setDefaultReaderType(ReaderType defaultType
) {
126 BasicReader
.defaultType
= defaultType
;
130 * Change the default {@link LocalLibrary} to open with the
131 * {@link BasicReader}s.
134 * the new {@link LocalLibrary}
136 public static void setDefaultLibrary(BasicLibrary lib
) {
137 BasicReader
.defaultLibrary
= lib
;
141 * Return an {@link URL} from this {@link String}, be it a file path or an
142 * actual {@link URL}.
144 * @param sourceString
147 * @return the corresponding {@link URL}
149 * @throws MalformedURLException
150 * if this is neither a file nor a conventional {@link URL}
152 public static URL
getUrl(String sourceString
) throws MalformedURLException
{
153 if (sourceString
== null || sourceString
.isEmpty()) {
154 throw new MalformedURLException("Empty url");
159 source
= new URL(sourceString
);
160 } catch (MalformedURLException e
) {
161 File sourceFile
= new File(sourceString
);
162 source
= sourceFile
.toURI().toURL();
169 * Open the {@link Story} with an external reader (the program will be
170 * passed the main file associated with this {@link Story}).
173 * the {@link BasicLibrary} to select the {@link Story} from
175 * the {@link Story} LUID
177 * @throws IOException
178 * in case of I/O error
180 public static void open(BasicLibrary lib
, String luid
) throws IOException
{
181 MetaData meta
= lib
.getInfo(luid
);
182 File target
= lib
.getFile(luid
);
188 * Open the {@link Story} with an external reader (the program will be
189 * passed the given target file).
192 * the {@link Story} to load
194 * the target {@link File}
196 * @throws IOException
197 * in case of I/O error
199 protected static void open(MetaData meta
, File target
) throws IOException
{
200 String program
= null;
201 if (meta
.isImageDocument()) {
202 program
= Instance
.getUiConfig().getString(
203 UiConfig
.IMAGES_DOCUMENT_READER
);
205 program
= Instance
.getUiConfig().getString(
206 UiConfig
.NON_IMAGES_DOCUMENT_READER
);
209 if (program
!= null && program
.trim().isEmpty()) {
213 if (program
== null) {
215 Desktop
.getDesktop().browse(target
.toURI());
216 } catch (UnsupportedOperationException e
) {
217 Runtime
.getRuntime().exec(
218 new String
[] { "xdg-open", target
.getAbsolutePath() });
222 Runtime
.getRuntime().exec(
223 new String
[] { program
, target
.getAbsolutePath() });