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
) {
51 public synchronized Story
getStory(Progress pg
) {
53 story
= getLibrary().getStory(meta
.getLuid(), pg
);
59 public BasicLibrary
getLibrary() {
67 public void setLibrary(BasicLibrary lib
) {
71 public MetaData
getMeta() {
75 public synchronized void setMeta(MetaData meta
) throws IOException
{
76 setMeta(meta
== null ?
null : meta
.getLuid()); // must check the library
79 public synchronized void setMeta(String luid
) throws IOException
{
81 meta
= getLibrary().getInfo(luid
);
84 throw new IOException("Cannot retrieve story from library: " + luid
);
88 public synchronized void setMeta(URL source
, Progress pg
)
90 BasicSupport support
= BasicSupport
.getSupport(source
);
91 if (support
== null) {
92 throw new IOException("URL not supported: " + source
.toString());
95 story
= support
.process(source
, pg
);
97 throw new IOException(
98 "Cannot retrieve story from external source: "
102 meta
= story
.getMeta();
105 public int getChapter() {
109 public void setChapter(int chapter
) {
110 this.chapter
= chapter
;
114 * Return a new {@link BasicReader} ready for use if one is configured.
116 * Can return NULL if none are configured.
118 * @return a {@link BasicReader}, or NULL if none configured
120 public static Reader
getReader() {
122 if (defaultType
!= null) {
123 return (Reader
) SerialUtils
.createObject(defaultType
126 } catch (Exception e
) {
127 Instance
.syserr(new Exception("Cannot create a reader of type: "
128 + defaultType
+ " (Not compiled in?)", e
));
135 * The default {@link Reader.ReaderType} used when calling
136 * {@link BasicReader#getReader()}.
138 * @return the default type
140 public static ReaderType
getDefaultReaderType() {
145 * The default {@link Reader.ReaderType} used when calling
146 * {@link BasicReader#getReader()}.
149 * the new default type
151 public static void setDefaultReaderType(ReaderType defaultType
) {
152 BasicReader
.defaultType
= defaultType
;
156 * Change the default {@link LocalLibrary} to open with the
157 * {@link BasicReader}s.
160 * the new {@link LocalLibrary}
162 public static void setDefaultLibrary(BasicLibrary lib
) {
163 BasicReader
.defaultLibrary
= lib
;
167 * Return an {@link URL} from this {@link String}, be it a file path or an
168 * actual {@link URL}.
170 * @param sourceString
173 * @return the corresponding {@link URL}
175 * @throws MalformedURLException
176 * if this is neither a file nor a conventional {@link URL}
178 public static URL
getUrl(String sourceString
) throws MalformedURLException
{
179 if (sourceString
== null || sourceString
.isEmpty()) {
180 throw new MalformedURLException("Empty url");
185 source
= new URL(sourceString
);
186 } catch (MalformedURLException e
) {
187 File sourceFile
= new File(sourceString
);
188 source
= sourceFile
.toURI().toURL();
195 * Open the {@link Story} with an external reader (the program will be
196 * passed the main file associated with this {@link Story}).
199 * the {@link BasicLibrary} to select the {@link Story} from
201 * the {@link Story} LUID
203 * @throws IOException
204 * in case of I/O error
206 public static void openExternal(BasicLibrary lib
, String luid
)
208 MetaData meta
= lib
.getInfo(luid
);
209 File target
= lib
.getFile(luid
);
211 openExternal(meta
, target
);
215 * Open the {@link Story} with an external reader (the program will be
216 * passed the given target file).
219 * the {@link Story} to load
221 * the target {@link File}
223 * @throws IOException
224 * in case of I/O error
226 protected static void openExternal(MetaData meta
, File target
)
228 String program
= null;
229 if (meta
.isImageDocument()) {
230 program
= Instance
.getUiConfig().getString(
231 UiConfig
.IMAGES_DOCUMENT_READER
);
233 program
= Instance
.getUiConfig().getString(
234 UiConfig
.NON_IMAGES_DOCUMENT_READER
);
237 if (program
!= null && program
.trim().isEmpty()) {
241 if (program
== null) {
243 Desktop
.getDesktop().browse(target
.toURI());
244 } catch (UnsupportedOperationException e
) {
245 Runtime
.getRuntime().exec(
246 new String
[] { "xdg-open", target
.getAbsolutePath() });
250 Runtime
.getRuntime().exec(
251 new String
[] { program
, target
.getAbsolutePath() });