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
.Library
;
11 import be
.nikiroo
.fanfix
.bundles
.Config
;
12 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
13 import be
.nikiroo
.fanfix
.data
.MetaData
;
14 import be
.nikiroo
.fanfix
.data
.Story
;
15 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
16 import be
.nikiroo
.utils
.Progress
;
17 import be
.nikiroo
.utils
.ui
.UIUtils
;
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.";
41 case CLI
: return pkg
+ "CliReader";
42 case TUI
: return pkg
+ "TuiReader";
43 case GUI
: return pkg
+ "LocalReader";
50 private static ReaderType defaultType
= ReaderType
.GUI
;
52 private ReaderType type
;
55 * Take the default reader type configuration from the config file.
58 String typeString
= Instance
.getConfig().getString(Config
.READER_TYPE
);
59 if (typeString
!= null && !typeString
.isEmpty()) {
61 ReaderType type
= ReaderType
.valueOf(typeString
.toUpperCase());
63 } catch (IllegalArgumentException e
) {
70 * The type of this reader.
74 public ReaderType
getType() {
79 * The type of this reader.
84 protected BasicReader
setType(ReaderType type
) {
90 * Return the current {@link Story}.
92 * @return the {@link Story}
94 public Story
getStory() {
99 * Create a new {@link BasicReader} for a {@link Story} in the
103 * the {@link Story} ID
105 * the optional progress reporter
107 * @throws IOException
108 * in case of I/O error
110 public void setStory(String luid
, Progress pg
) throws IOException
{
111 story
= Instance
.getLibrary().getStory(luid
, pg
);
113 throw new IOException("Cannot retrieve story from library: " + luid
);
118 * Create a new {@link BasicReader} for an external {@link Story}.
121 * the {@link Story} {@link URL}
123 * the optional progress reporter
125 * @throws IOException
126 * in case of I/O error
128 public void setStory(URL source
, Progress pg
) throws IOException
{
129 BasicSupport support
= BasicSupport
.getSupport(source
);
130 if (support
== null) {
131 throw new IOException("URL not supported: " + source
.toString());
134 story
= support
.process(source
, pg
);
136 throw new IOException(
137 "Cannot retrieve story from external source: "
138 + source
.toString());
144 * Start the {@link Story} Reading.
146 * @throws IOException
147 * in case of I/O error or if the {@link Story} was not
150 public abstract void read() throws IOException
;
153 * Read the selected chapter (starting at 1).
158 * @throws IOException
159 * in case of I/O error or if the {@link Story} was not
162 public abstract void read(int chapter
) throws IOException
;
165 * Start the reader in browse mode for the given type (or pass NULL for all
169 * the type of {@link Story} to take into account, or NULL for
172 public abstract void start(String type
);
175 * Return a new {@link BasicReader} ready for use if one is configured.
177 * Can return NULL if none are configured.
179 * @return a {@link BasicReader}, or NULL if none configured
181 public static BasicReader
getReader() {
183 if (defaultType
!= null) {
184 return ((BasicReader
)SerialUtils
.createObject
185 (defaultType
.getTypeName())).setType(defaultType
);
187 } catch (Exception e
) {
188 Instance
.syserr(new Exception("Cannot create a reader of type: "
189 + defaultType
+ " (Not compiled in?)", e
));
196 * The default {@link ReaderType} used when calling
197 * {@link BasicReader#getReader()}.
199 * @return the default type
201 public static ReaderType
getDefaultReaderType() {
206 * The default {@link ReaderType} used when calling
207 * {@link BasicReader#getReader()}.
210 * the new default type
212 public static void setDefaultReaderType(ReaderType defaultType
) {
213 BasicReader
.defaultType
= defaultType
;
217 * Return an {@link URL} from this {@link String}, be it a file path or an
218 * actual {@link URL}.
220 * @param sourceString
223 * @return the corresponding {@link URL}
225 * @throws MalformedURLException
226 * if this is neither a file nor a conventional {@link URL}
228 public static URL
getUrl(String sourceString
) throws MalformedURLException
{
229 if (sourceString
== null || sourceString
.isEmpty()) {
230 throw new MalformedURLException("Empty url");
235 source
= new URL(sourceString
);
236 } catch (MalformedURLException e
) {
237 File sourceFile
= new File(sourceString
);
238 source
= sourceFile
.toURI().toURL();
244 // open with external player the related file
245 public static void open(String luid
) throws IOException
{
246 MetaData meta
= Instance
.getLibrary().getInfo(luid
);
247 File target
= Instance
.getLibrary().getFile(luid
);
252 // open with external player the related file
253 protected static void open(MetaData meta
, File target
) throws IOException
{
254 String program
= null;
255 if (meta
.isImageDocument()) {
256 program
= Instance
.getUiConfig().getString(
257 UiConfig
.IMAGES_DOCUMENT_READER
);
259 program
= Instance
.getUiConfig().getString(
260 UiConfig
.NON_IMAGES_DOCUMENT_READER
);
263 if (program
!= null && program
.trim().isEmpty()) {
267 if (program
== null) {
269 Desktop
.getDesktop().browse(target
.toURI());
270 } catch (UnsupportedOperationException e
) {
271 Runtime
.getRuntime().exec(
272 new String
[] { "xdg-open", target
.getAbsolutePath() });
276 Runtime
.getRuntime().exec(
277 new String
[] { program
, target
.getAbsolutePath() });