Commit | Line | Data |
---|---|---|
89cb07a6 NR |
1 | package be.nikiroo.fanfix.reader; |
2 | ||
3 | import java.io.IOException; | |
4 | import java.net.URL; | |
5 | ||
6 | import be.nikiroo.fanfix.Instance; | |
7 | import be.nikiroo.fanfix.Library; | |
d0114000 | 8 | import be.nikiroo.fanfix.bundles.Config; |
89cb07a6 NR |
9 | import be.nikiroo.fanfix.data.Story; |
10 | import be.nikiroo.fanfix.supported.BasicSupport; | |
92fb0719 | 11 | import be.nikiroo.utils.ui.Progress; |
89cb07a6 NR |
12 | |
13 | /** | |
dd56a893 | 14 | * The class that handles the different {@link Story} readers you can use. |
89cb07a6 | 15 | * <p> |
dd56a893 | 16 | * All the readers should be accessed via {@link BasicReader#getReader()}. |
89cb07a6 NR |
17 | * |
18 | * @author niki | |
19 | */ | |
3727aae2 NR |
20 | public abstract class BasicReader { |
21 | public enum ReaderType { | |
22 | /** Simple reader that outputs everything on the console */ | |
23 | CLI, | |
24 | /** Reader that starts local programs to handle the stories */ | |
25 | LOCAL | |
26 | } | |
27 | ||
d0114000 | 28 | private static ReaderType defaultType = ReaderType.CLI; |
89cb07a6 | 29 | private Story story; |
3727aae2 NR |
30 | private ReaderType type; |
31 | ||
d0114000 NR |
32 | /** |
33 | * Take the default reader type configuration from the config file. | |
34 | */ | |
3727aae2 | 35 | static { |
d0114000 NR |
36 | String typeString = Instance.getConfig().getString(Config.READER_TYPE); |
37 | if (typeString != null && !typeString.isEmpty()) { | |
38 | try { | |
39 | ReaderType type = ReaderType.valueOf(typeString.toUpperCase()); | |
40 | defaultType = type; | |
41 | } catch (IllegalArgumentException e) { | |
42 | // Do nothing | |
43 | } | |
44 | } | |
3727aae2 NR |
45 | } |
46 | ||
47 | /** | |
48 | * The type of this reader. | |
49 | * | |
50 | * @return the type | |
51 | */ | |
52 | public ReaderType getType() { | |
53 | return type; | |
54 | } | |
55 | ||
56 | /** | |
57 | * The type of this reader. | |
58 | * | |
59 | * @param type | |
60 | * the new type | |
61 | */ | |
62 | protected BasicReader setType(ReaderType type) { | |
63 | this.type = type; | |
64 | return this; | |
65 | } | |
89cb07a6 NR |
66 | |
67 | /** | |
68 | * Return the current {@link Story}. | |
69 | * | |
70 | * @return the {@link Story} | |
71 | */ | |
72 | public Story getStory() { | |
73 | return story; | |
74 | } | |
75 | ||
76 | /** | |
3727aae2 | 77 | * Create a new {@link BasicReader} for a {@link Story} in the |
89cb07a6 NR |
78 | * {@link Library} . |
79 | * | |
80 | * @param luid | |
81 | * the {@link Story} ID | |
92fb0719 NR |
82 | * @param pg |
83 | * the optional progress reporter | |
84 | * | |
89cb07a6 NR |
85 | * @throws IOException |
86 | * in case of I/O error | |
87 | */ | |
92fb0719 NR |
88 | public void setStory(String luid, Progress pg) throws IOException { |
89 | story = Instance.getLibrary().getStory(luid, pg); | |
89cb07a6 | 90 | if (story == null) { |
92fb0719 | 91 | throw new IOException("Cannot retrieve story from library: " + luid); |
89cb07a6 NR |
92 | } |
93 | } | |
94 | ||
95 | /** | |
3727aae2 | 96 | * Create a new {@link BasicReader} for an external {@link Story}. |
89cb07a6 NR |
97 | * |
98 | * @param source | |
99 | * the {@link Story} {@link URL} | |
92fb0719 NR |
100 | * @param pg |
101 | * the optional progress reporter | |
102 | * | |
89cb07a6 NR |
103 | * @throws IOException |
104 | * in case of I/O error | |
105 | */ | |
92fb0719 | 106 | public void setStory(URL source, Progress pg) throws IOException { |
89cb07a6 NR |
107 | BasicSupport support = BasicSupport.getSupport(source); |
108 | if (support == null) { | |
109 | throw new IOException("URL not supported: " + source.toString()); | |
110 | } | |
111 | ||
92fb0719 | 112 | story = support.process(source, pg); |
89cb07a6 NR |
113 | if (story == null) { |
114 | throw new IOException( | |
115 | "Cannot retrieve story from external source: " | |
116 | + source.toString()); | |
117 | ||
118 | } | |
119 | } | |
120 | ||
121 | /** | |
122 | * Start the {@link Story} Reading. | |
123 | * | |
124 | * @throws IOException | |
125 | * in case of I/O error or if the {@link Story} was not | |
126 | * previously set | |
127 | */ | |
128 | public abstract void read() throws IOException; | |
129 | ||
130 | /** | |
131 | * Read the selected chapter (starting at 1). | |
132 | * | |
133 | * @param chapter | |
134 | * the chapter | |
135 | */ | |
136 | public abstract void read(int chapter); | |
137 | ||
138 | /** | |
139 | * Start the reader in browse mode for the given type (or pass NULL for all | |
140 | * types). | |
141 | * | |
142 | * @param type | |
143 | * the type of {@link Story} to take into account, or NULL for | |
144 | * all | |
145 | */ | |
333f0e7b | 146 | public abstract void start(String type); |
3727aae2 NR |
147 | |
148 | /** | |
d0114000 NR |
149 | * Return a new {@link BasicReader} ready for use if one is configured. |
150 | * <p> | |
151 | * Can return NULL if none are configured. | |
3727aae2 | 152 | * |
d0114000 | 153 | * @return a {@link BasicReader}, or NULL if none configured |
3727aae2 NR |
154 | */ |
155 | public static BasicReader getReader() { | |
333f0e7b NR |
156 | try { |
157 | if (defaultType != null) { | |
158 | switch (defaultType) { | |
159 | case LOCAL: | |
160 | return new LocalReader().setType(ReaderType.LOCAL); | |
161 | case CLI: | |
162 | return new CliReader().setType(ReaderType.CLI); | |
163 | } | |
d0114000 | 164 | } |
333f0e7b NR |
165 | } catch (IOException e) { |
166 | Instance.syserr(new Exception("Cannot create a reader of type: " | |
167 | + defaultType, e)); | |
3727aae2 NR |
168 | } |
169 | ||
170 | return null; | |
171 | } | |
172 | ||
173 | /** | |
174 | * The default {@link ReaderType} used when calling | |
175 | * {@link BasicReader#getReader()}. | |
176 | * | |
177 | * @return the default type | |
178 | */ | |
179 | public static ReaderType getDefaultReaderType() { | |
180 | return defaultType; | |
181 | } | |
182 | ||
183 | /** | |
184 | * The default {@link ReaderType} used when calling | |
185 | * {@link BasicReader#getReader()}. | |
186 | * | |
187 | * @param defaultType | |
188 | * the new default type | |
189 | */ | |
190 | public static void setDefaultReaderType(ReaderType defaultType) { | |
191 | BasicReader.defaultType = defaultType; | |
192 | } | |
89cb07a6 | 193 | } |