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
;
32 private MetaData meta
;
37 * Take the default reader type configuration from the config file.
40 String typeString
= Instance
.getConfig().getString(Config
.READER_TYPE
);
41 if (typeString
!= null && !typeString
.isEmpty()) {
43 ReaderType type
= ReaderType
.valueOf(typeString
.toUpperCase());
45 } catch (IllegalArgumentException e
) {
52 public synchronized Story
getStory(Progress pg
) {
54 story
= getLibrary().getStory(meta
.getLuid(), pg
);
61 public BasicLibrary
getLibrary() {
70 public void setLibrary(BasicLibrary lib
) {
75 public MetaData
getMeta() {
80 public synchronized void setMeta(MetaData meta
) throws IOException
{
81 setMeta(meta
== null ?
null : meta
.getLuid()); // must check the library
85 public synchronized void setMeta(String luid
) throws IOException
{
87 meta
= getLibrary().getInfo(luid
);
90 throw new IOException("Cannot retrieve story from library: " + luid
);
95 public synchronized void setMeta(URL source
, Progress pg
)
97 BasicSupport support
= BasicSupport
.getSupport(source
);
98 if (support
== null) {
99 throw new IOException("URL not supported: " + source
.toString());
102 story
= support
.process(source
, pg
);
104 throw new IOException(
105 "Cannot retrieve story from external source: "
106 + source
.toString());
109 meta
= story
.getMeta();
113 public int getChapter() {
118 public void setChapter(int chapter
) {
119 this.chapter
= chapter
;
123 * Return a new {@link BasicReader} ready for use if one is configured.
125 * Can return NULL if none are configured.
127 * @return a {@link BasicReader}, or NULL if none configured
129 public static Reader
getReader() {
131 if (defaultType
!= null) {
132 return (Reader
) SerialUtils
.createObject(defaultType
135 } catch (Exception e
) {
136 Instance
.syserr(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
215 public static void openExternal(BasicLibrary lib
, String luid
)
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 static void openExternal(MetaData meta
, File target
)
237 String program
= null;
238 if (meta
.isImageDocument()) {
239 program
= Instance
.getUiConfig().getString(
240 UiConfig
.IMAGES_DOCUMENT_READER
);
242 program
= Instance
.getUiConfig().getString(
243 UiConfig
.NON_IMAGES_DOCUMENT_READER
);
246 if (program
!= null && program
.trim().isEmpty()) {
250 if (program
== null) {
252 Desktop
.getDesktop().browse(target
.toURI());
253 } catch (UnsupportedOperationException e
) {
254 Runtime
.getRuntime().exec(
255 new String
[] { "xdg-open", target
.getAbsolutePath() });
259 Runtime
.getRuntime().exec(
260 new String
[] { program
, target
.getAbsolutePath() });