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
;
20 * The class that handles the different {@link Story} readers you can use.
22 * All the readers should be accessed via {@link BasicReader#getReader()}.
26 public abstract class BasicReader
{
27 public enum ReaderType
{
28 /** Simple reader that outputs everything on the console */
30 /** Reader that starts local programs to handle the stories */
32 /** A text (UTF-8) reader with menu and text windows */
36 private static ReaderType defaultType
= ReaderType
.GUI
;
38 private ReaderType type
;
41 * Take the default reader type configuration from the config file.
44 String typeString
= Instance
.getConfig().getString(Config
.READER_TYPE
);
45 if (typeString
!= null && !typeString
.isEmpty()) {
47 ReaderType type
= ReaderType
.valueOf(typeString
.toUpperCase());
49 } catch (IllegalArgumentException e
) {
56 * The type of this reader.
60 public ReaderType
getType() {
65 * The type of this reader.
70 protected BasicReader
setType(ReaderType type
) {
76 * Return the current {@link Story}.
78 * @return the {@link Story}
80 public Story
getStory() {
85 * Create a new {@link BasicReader} for a {@link Story} in the
89 * the {@link Story} ID
91 * the optional progress reporter
94 * in case of I/O error
96 public void setStory(String luid
, Progress pg
) throws IOException
{
97 story
= Instance
.getLibrary().getStory(luid
, pg
);
99 throw new IOException("Cannot retrieve story from library: " + luid
);
104 * Create a new {@link BasicReader} for an external {@link Story}.
107 * the {@link Story} {@link URL}
109 * the optional progress reporter
111 * @throws IOException
112 * in case of I/O error
114 public void setStory(URL source
, Progress pg
) throws IOException
{
115 BasicSupport support
= BasicSupport
.getSupport(source
);
116 if (support
== null) {
117 throw new IOException("URL not supported: " + source
.toString());
120 story
= support
.process(source
, pg
);
122 throw new IOException(
123 "Cannot retrieve story from external source: "
124 + source
.toString());
130 * Start the {@link Story} Reading.
132 * @throws IOException
133 * in case of I/O error or if the {@link Story} was not
136 public abstract void read() throws IOException
;
139 * Read the selected chapter (starting at 1).
144 * @throws IOException
145 * in case of I/O error or if the {@link Story} was not
148 public abstract void read(int chapter
) throws IOException
;
151 * Start the reader in browse mode for the given type (or pass NULL for all
155 * the type of {@link Story} to take into account, or NULL for
158 public abstract void start(String type
);
161 * Return a new {@link BasicReader} ready for use if one is configured.
163 * Can return NULL if none are configured.
165 * @return a {@link BasicReader}, or NULL if none configured
167 public static BasicReader
getReader() {
169 if (defaultType
!= null) {
170 switch (defaultType
) {
172 UIUtils
.setLookAndFeel();
173 return new LocalReader().setType(ReaderType
.GUI
);
175 return new CliReader().setType(ReaderType
.CLI
);
177 return new TuiReader().setType(ReaderType
.TUI
);
180 } catch (IOException e
) {
181 Instance
.syserr(new Exception("Cannot create a reader of type: "
189 * The default {@link ReaderType} used when calling
190 * {@link BasicReader#getReader()}.
192 * @return the default type
194 public static ReaderType
getDefaultReaderType() {
199 * The default {@link ReaderType} used when calling
200 * {@link BasicReader#getReader()}.
203 * the new default type
205 public static void setDefaultReaderType(ReaderType defaultType
) {
206 BasicReader
.defaultType
= defaultType
;
210 * Return an {@link URL} from this {@link String}, be it a file path or an
211 * actual {@link URL}.
213 * @param sourceString
216 * @return the corresponding {@link URL}
218 * @throws MalformedURLException
219 * if this is neither a file nor a conventional {@link URL}
221 public static URL
getUrl(String sourceString
) throws MalformedURLException
{
222 if (sourceString
== null || sourceString
.isEmpty()) {
223 throw new MalformedURLException("Empty url");
228 source
= new URL(sourceString
);
229 } catch (MalformedURLException e
) {
230 File sourceFile
= new File(sourceString
);
231 source
= sourceFile
.toURI().toURL();
237 // open with external player the related file
238 public static void open(String luid
) throws IOException
{
239 MetaData meta
= Instance
.getLibrary().getInfo(luid
);
240 File target
= Instance
.getLibrary().getFile(luid
);
245 // open with external player the related file
246 protected static void open(MetaData meta
, File target
) throws IOException
{
247 String program
= null;
248 if (meta
.isImageDocument()) {
249 program
= Instance
.getUiConfig().getString(
250 UiConfig
.IMAGES_DOCUMENT_READER
);
252 program
= Instance
.getUiConfig().getString(
253 UiConfig
.NON_IMAGES_DOCUMENT_READER
);
256 if (program
!= null && program
.trim().isEmpty()) {
260 if (program
== null) {
262 Desktop
.getDesktop().browse(target
.toURI());
263 } catch (UnsupportedOperationException e
) {
264 Runtime
.getRuntime().exec(
265 new String
[] { "xdg-open", target
.getAbsolutePath() });
269 Runtime
.getRuntime().exec(
270 new String
[] { program
, target
.getAbsolutePath() });