Version 1.1.0
[fanfix.git] / src / be / nikiroo / fanfix / reader / BasicReader.java
... / ...
CommitLineData
1package be.nikiroo.fanfix.reader;
2
3import java.io.IOException;
4import java.net.URL;
5
6import be.nikiroo.fanfix.Instance;
7import be.nikiroo.fanfix.Library;
8import be.nikiroo.fanfix.bundles.Config;
9import be.nikiroo.fanfix.data.Story;
10import be.nikiroo.fanfix.supported.BasicSupport;
11import be.nikiroo.utils.ui.Progress;
12
13/**
14 * The class that handles the different {@link Story} readers you can use.
15 * <p>
16 * All the readers should be accessed via {@link BasicReader#getReader()}.
17 *
18 * @author niki
19 */
20public 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
28 private static ReaderType defaultType = ReaderType.CLI;
29 private Story story;
30 private ReaderType type;
31
32 /**
33 * Take the default reader type configuration from the config file.
34 */
35 static {
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 }
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 }
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 /**
77 * Create a new {@link BasicReader} for a {@link Story} in the
78 * {@link Library} .
79 *
80 * @param luid
81 * the {@link Story} ID
82 * @param pg
83 * the optional progress reporter
84 *
85 * @throws IOException
86 * in case of I/O error
87 */
88 public void setStory(String luid, Progress pg) throws IOException {
89 story = Instance.getLibrary().getStory(luid, pg);
90 if (story == null) {
91 throw new IOException("Cannot retrieve story from library: " + luid);
92 }
93 }
94
95 /**
96 * Create a new {@link BasicReader} for an external {@link Story}.
97 *
98 * @param source
99 * the {@link Story} {@link URL}
100 * @param pg
101 * the optional progress reporter
102 *
103 * @throws IOException
104 * in case of I/O error
105 */
106 public void setStory(URL source, Progress pg) throws IOException {
107 BasicSupport support = BasicSupport.getSupport(source);
108 if (support == null) {
109 throw new IOException("URL not supported: " + source.toString());
110 }
111
112 story = support.process(source, pg);
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 */
146 public abstract void start(String type);
147
148 /**
149 * Return a new {@link BasicReader} ready for use if one is configured.
150 * <p>
151 * Can return NULL if none are configured.
152 *
153 * @return a {@link BasicReader}, or NULL if none configured
154 */
155 public static BasicReader getReader() {
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 }
164 }
165 } catch (IOException e) {
166 Instance.syserr(new Exception("Cannot create a reader of type: "
167 + defaultType, e));
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 }
193}