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
.BasicLibrary
;
10 import be
.nikiroo
.fanfix
.Instance
;
11 import be
.nikiroo
.fanfix
.LocalLibrary
;
12 import be
.nikiroo
.fanfix
.bundles
.Config
;
13 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
14 import be
.nikiroo
.fanfix
.data
.MetaData
;
15 import be
.nikiroo
.fanfix
.data
.Story
;
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
{
28 public enum ReaderType
{
29 /** Simple reader that outputs everything on the console */
31 /** Reader that starts local programs to handle the stories */
33 /** A text (UTF-8) reader with menu and text windows */
38 public String
getTypeName() {
39 String pkg
= "be.nikiroo.fanfix.reader.";
42 return pkg
+ "CliReader";
44 return pkg
+ "TuiReader";
46 return pkg
+ "LocalReader";
53 private static BasicLibrary defaultLibrary
= Instance
.getLibrary();
54 private static ReaderType defaultType
= ReaderType
.GUI
;
56 private BasicLibrary lib
;
58 private ReaderType type
;
61 * Take the default reader type configuration from the config file.
64 String typeString
= Instance
.getConfig().getString(Config
.READER_TYPE
);
65 if (typeString
!= null && !typeString
.isEmpty()) {
67 ReaderType type
= ReaderType
.valueOf(typeString
.toUpperCase());
69 } catch (IllegalArgumentException e
) {
76 * The type of this reader.
80 public ReaderType
getType() {
85 * The type of this reader.
90 protected BasicReader
setType(ReaderType type
) {
96 * Return the current {@link Story}.
98 * @return the {@link Story}
100 public Story
getStory() {
105 * The {@link LocalLibrary} to load the stories from (by default, takes the
106 * default {@link LocalLibrary}).
108 * @return the {@link LocalLibrary}
110 public BasicLibrary
getLibrary() {
112 lib
= defaultLibrary
;
119 * Change the {@link LocalLibrary} that will be managed by this
120 * {@link BasicReader}.
123 * the new {@link LocalLibrary}
125 public void setLibrary(LocalLibrary lib
) {
130 * Create a new {@link BasicReader} for a {@link Story} in the
131 * {@link LocalLibrary}.
134 * the {@link Story} ID
136 * the optional progress reporter
138 * @throws IOException
139 * in case of I/O error
141 public void setStory(String luid
, Progress pg
) throws IOException
{
142 story
= lib
.getStory(luid
, pg
);
144 throw new IOException("Cannot retrieve story from library: " + luid
);
149 * Create a new {@link BasicReader} for an external {@link Story}.
152 * the {@link Story} {@link URL}
154 * the optional progress reporter
156 * @throws IOException
157 * in case of I/O error
159 public void setStory(URL source
, Progress pg
) throws IOException
{
160 BasicSupport support
= BasicSupport
.getSupport(source
);
161 if (support
== null) {
162 throw new IOException("URL not supported: " + source
.toString());
165 story
= support
.process(source
, pg
);
167 throw new IOException(
168 "Cannot retrieve story from external source: "
169 + source
.toString());
175 * Start the {@link Story} Reading.
177 * @throws IOException
178 * in case of I/O error or if the {@link Story} was not
181 public abstract void read() throws IOException
;
184 * Read the selected chapter (starting at 1).
189 * @throws IOException
190 * in case of I/O error or if the {@link Story} was not
193 public abstract void read(int chapter
) throws IOException
;
196 * Start the reader in browse mode for the given source (or pass NULL for
200 * the library to browse
203 * the type of {@link Story} to take into account, or NULL for
206 public abstract void browse(String source
);
209 * Return a new {@link BasicReader} ready for use if one is configured.
211 * Can return NULL if none are configured.
213 * @return a {@link BasicReader}, or NULL if none configured
215 public static BasicReader
getReader() {
217 if (defaultType
!= null) {
218 return ((BasicReader
) SerialUtils
.createObject(defaultType
219 .getTypeName())).setType(defaultType
);
221 } catch (Exception e
) {
222 Instance
.syserr(new Exception("Cannot create a reader of type: "
223 + defaultType
+ " (Not compiled in?)", e
));
230 * The default {@link ReaderType} used when calling
231 * {@link BasicReader#getReader()}.
233 * @return the default type
235 public static ReaderType
getDefaultReaderType() {
240 * The default {@link ReaderType} used when calling
241 * {@link BasicReader#getReader()}.
244 * the new default type
246 public static void setDefaultReaderType(ReaderType defaultType
) {
247 BasicReader
.defaultType
= defaultType
;
251 * Change the default {@link LocalLibrary} to open with the
252 * {@link BasicReader}s.
255 * the new {@link LocalLibrary}
257 public static void setDefaultLibrary(BasicLibrary lib
) {
258 BasicReader
.defaultLibrary
= lib
;
262 * Return an {@link URL} from this {@link String}, be it a file path or an
263 * actual {@link URL}.
265 * @param sourceString
268 * @return the corresponding {@link URL}
270 * @throws MalformedURLException
271 * if this is neither a file nor a conventional {@link URL}
273 public static URL
getUrl(String sourceString
) throws MalformedURLException
{
274 if (sourceString
== null || sourceString
.isEmpty()) {
275 throw new MalformedURLException("Empty url");
280 source
= new URL(sourceString
);
281 } catch (MalformedURLException e
) {
282 File sourceFile
= new File(sourceString
);
283 source
= sourceFile
.toURI().toURL();
289 // open with external player the related file
290 public static void open(BasicLibrary lib
, String luid
) throws IOException
{
291 MetaData meta
= lib
.getInfo(luid
);
292 File target
= lib
.getFile(luid
);
297 // open with external player the related file
298 protected static void open(MetaData meta
, File target
) throws IOException
{
299 String program
= null;
300 if (meta
.isImageDocument()) {
301 program
= Instance
.getUiConfig().getString(
302 UiConfig
.IMAGES_DOCUMENT_READER
);
304 program
= Instance
.getUiConfig().getString(
305 UiConfig
.NON_IMAGES_DOCUMENT_READER
);
308 if (program
!= null && program
.trim().isEmpty()) {
312 if (program
== null) {
314 Desktop
.getDesktop().browse(target
.toURI());
315 } catch (UnsupportedOperationException e
) {
316 Runtime
.getRuntime().exec(
317 new String
[] { "xdg-open", target
.getAbsolutePath() });
321 Runtime
.getRuntime().exec(
322 new String
[] { program
, target
.getAbsolutePath() });