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
= getLibrary().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: "
87 public void read() throws IOException
{
92 * Return a new {@link BasicReader} ready for use if one is configured.
94 * Can return NULL if none are configured.
96 * @return a {@link BasicReader}, or NULL if none configured
98 public static Reader
getReader() {
100 if (defaultType
!= null) {
101 return (Reader
) SerialUtils
.createObject(defaultType
104 } catch (Exception e
) {
105 Instance
.syserr(new Exception("Cannot create a reader of type: "
106 + defaultType
+ " (Not compiled in?)", e
));
113 * The default {@link ReaderType} used when calling
114 * {@link BasicReader#getReader()}.
116 * @return the default type
118 public static ReaderType
getDefaultReaderType() {
123 * The default {@link ReaderType} used when calling
124 * {@link BasicReader#getReader()}.
127 * the new default type
129 public static void setDefaultReaderType(ReaderType defaultType
) {
130 BasicReader
.defaultType
= defaultType
;
134 * Change the default {@link LocalLibrary} to open with the
135 * {@link BasicReader}s.
138 * the new {@link LocalLibrary}
140 public static void setDefaultLibrary(BasicLibrary lib
) {
141 BasicReader
.defaultLibrary
= lib
;
145 * Return an {@link URL} from this {@link String}, be it a file path or an
146 * actual {@link URL}.
148 * @param sourceString
151 * @return the corresponding {@link URL}
153 * @throws MalformedURLException
154 * if this is neither a file nor a conventional {@link URL}
156 public static URL
getUrl(String sourceString
) throws MalformedURLException
{
157 if (sourceString
== null || sourceString
.isEmpty()) {
158 throw new MalformedURLException("Empty url");
163 source
= new URL(sourceString
);
164 } catch (MalformedURLException e
) {
165 File sourceFile
= new File(sourceString
);
166 source
= sourceFile
.toURI().toURL();
173 * Open the {@link Story} with an external reader (the program will be
174 * passed the main file associated with this {@link Story}).
177 * the {@link BasicLibrary} to select the {@link Story} from
179 * the {@link Story} LUID
181 * @throws IOException
182 * in case of I/O error
184 public static void openExternal(BasicLibrary lib
, String luid
)
186 MetaData meta
= lib
.getInfo(luid
);
187 File target
= lib
.getFile(luid
);
189 openExternal(meta
, target
);
193 * Open the {@link Story} with an external reader (the program will be
194 * passed the given target file).
197 * the {@link Story} to load
199 * the target {@link File}
201 * @throws IOException
202 * in case of I/O error
204 protected static void openExternal(MetaData meta
, File target
)
206 String program
= null;
207 if (meta
.isImageDocument()) {
208 program
= Instance
.getUiConfig().getString(
209 UiConfig
.IMAGES_DOCUMENT_READER
);
211 program
= Instance
.getUiConfig().getString(
212 UiConfig
.NON_IMAGES_DOCUMENT_READER
);
215 if (program
!= null && program
.trim().isEmpty()) {
219 if (program
== null) {
221 Desktop
.getDesktop().browse(target
.toURI());
222 } catch (UnsupportedOperationException e
) {
223 Runtime
.getRuntime().exec(
224 new String
[] { "xdg-open", target
.getAbsolutePath() });
228 Runtime
.getRuntime().exec(
229 new String
[] { program
, target
.getAbsolutePath() });