Commit | Line | Data |
---|---|---|
79a99506 NR |
1 | package be.nikiroo.fanfix.reader.ui; |
2 | ||
0bb51c9c NR |
3 | import java.io.IOException; |
4 | ||
5bc9573b | 5 | import be.nikiroo.fanfix.bundles.StringIdGui; |
79a99506 | 6 | import be.nikiroo.fanfix.data.MetaData; |
5bc9573b | 7 | import be.nikiroo.fanfix.data.Story; |
79a99506 NR |
8 | import be.nikiroo.fanfix.library.BasicLibrary; |
9 | import be.nikiroo.utils.Image; | |
596ed3d6 | 10 | import be.nikiroo.utils.StringUtils; |
79a99506 | 11 | |
5bc9573b NR |
12 | /** |
13 | * Some meta information related to a "book" (which can either be a | |
14 | * {@link Story}, a fake-story grouping some authors or a fake-story grouping | |
15 | * some sources/types). | |
16 | * | |
17 | * @author niki | |
18 | */ | |
79a99506 | 19 | public class GuiReaderBookInfo { |
ef98466f NR |
20 | /** |
21 | * The type of {@link GuiReaderBook} (i.e., related to a story or to something else that | |
22 | * can encompass stories). | |
23 | * | |
24 | * @author niki | |
25 | */ | |
79a99506 NR |
26 | public enum Type { |
27 | /** A normal story, which can be "read". */ | |
28 | STORY, | |
29 | /** | |
30 | * A special, empty story that represents a source/type common to one or | |
31 | * more normal stories. | |
32 | */ | |
33 | SOURCE, | |
34 | /** A special, empty story that represents an author. */ | |
35 | AUTHOR | |
36 | } | |
37 | ||
38 | private Type type; | |
39 | private String id; | |
40 | private String value; | |
41 | private String count; | |
42 | ||
43 | private MetaData meta; | |
44 | ||
5bc9573b NR |
45 | /** |
46 | * For private use, see the "fromXXX" constructors instead for public use. | |
47 | * | |
48 | * @param type | |
49 | * the type of book | |
50 | * @param id | |
51 | * the main id, which must uniquely identify this book and will | |
52 | * be used as a unique ID later on | |
53 | * @param value | |
54 | * the main value to show (see | |
55 | * {@link GuiReaderBookInfo#getMainInfo()}) | |
56 | */ | |
79a99506 NR |
57 | private GuiReaderBookInfo(Type type, String id, String value) { |
58 | this.type = type; | |
59 | this.id = id; | |
60 | this.value = value; | |
61 | } | |
ca7d4e2f NR |
62 | |
63 | /** | |
64 | * The type of {@link GuiReaderBookInfo}. | |
65 | * | |
66 | * @return the type | |
67 | */ | |
68 | public Type getType() { | |
69 | return type; | |
70 | } | |
79a99506 | 71 | |
5bc9573b NR |
72 | /** |
73 | * Get the main info to display for this book (a title, an author, a | |
74 | * source/type name...). | |
75 | * <p> | |
76 | * Note that when {@link MetaData} about the book are present, the title | |
77 | * inside is returned instead of the actual value (that way, we can update | |
78 | * the {@link MetaData} and see the changes here). | |
79 | * | |
80 | * @return the main info, usually the title | |
81 | */ | |
79a99506 | 82 | public String getMainInfo() { |
c349fd48 NR |
83 | if (meta != null) { |
84 | return meta.getTitle(); | |
85 | } | |
86 | ||
79a99506 NR |
87 | return value; |
88 | } | |
89 | ||
5bc9573b NR |
90 | /** |
91 | * Get the secondary info, of the given type. | |
92 | * | |
93 | * @param seeCount | |
94 | * TRUE for word/image/story count, FALSE for author name | |
95 | * | |
96 | * @return the secondary info | |
97 | */ | |
79a99506 NR |
98 | public String getSecondaryInfo(boolean seeCount) { |
99 | String author = meta == null ? null : meta.getAuthor(); | |
100 | String secondaryInfo = seeCount ? count : author; | |
101 | ||
102 | if (secondaryInfo != null && !secondaryInfo.trim().isEmpty()) { | |
103 | secondaryInfo = "(" + secondaryInfo + ")"; | |
104 | } else { | |
105 | secondaryInfo = ""; | |
106 | } | |
107 | ||
108 | return secondaryInfo; | |
109 | } | |
110 | ||
111 | /** | |
112 | * A unique ID for this {@link GuiReaderBookInfo}. | |
113 | * | |
114 | * @return the unique ID | |
115 | */ | |
116 | public String getId() { | |
117 | return id; | |
118 | } | |
119 | ||
5bc9573b NR |
120 | /** |
121 | * The {@link MetaData} associated with this book, if this book is a | |
122 | * {@link Story}. | |
123 | * <p> | |
124 | * Can be NULL for non-story books (authors or sources/types). | |
125 | * | |
126 | * @return the {@link MetaData} or NULL | |
127 | */ | |
79a99506 NR |
128 | public MetaData getMeta() { |
129 | return meta; | |
130 | } | |
131 | ||
5bc9573b NR |
132 | /** |
133 | * Get the base image to use to represent this book. | |
134 | * <p> | |
135 | * The image is <b>NOT</b> resized in any way, this is the original version. | |
136 | * <p> | |
137 | * It can be NULL if no image can be found for this book. | |
138 | * | |
139 | * @param lib | |
140 | * the {@link BasicLibrary} to use to fetch the image | |
141 | * | |
142 | * @return the base image | |
0bb51c9c NR |
143 | * |
144 | * @throws IOException | |
145 | * in case of I/O error | |
5bc9573b | 146 | */ |
0bb51c9c | 147 | public Image getBaseImage(BasicLibrary lib) throws IOException { |
79a99506 NR |
148 | switch (type) { |
149 | case STORY: | |
d4a992bf NR |
150 | if (meta.getCover() != null) { |
151 | return meta.getCover(); | |
152 | } | |
153 | ||
154 | if (meta.getLuid() != null) { | |
155 | return lib.getCover(meta.getLuid()); | |
156 | } | |
157 | ||
158 | return null; | |
79a99506 NR |
159 | case SOURCE: |
160 | return lib.getSourceCover(value); | |
161 | case AUTHOR: | |
162 | return lib.getAuthorCover(value); | |
163 | } | |
164 | ||
165 | return null; | |
166 | } | |
167 | ||
5bc9573b NR |
168 | /** |
169 | * Create a new book describing the given {@link Story}. | |
170 | * | |
171 | * @param meta | |
172 | * the {@link MetaData} representing the {@link Story} | |
173 | * | |
174 | * @return the book | |
175 | */ | |
79a99506 NR |
176 | static public GuiReaderBookInfo fromMeta(MetaData meta) { |
177 | String uid = meta.getUuid(); | |
178 | if (uid == null || uid.trim().isEmpty()) { | |
179 | uid = meta.getLuid(); | |
180 | } | |
d4a992bf NR |
181 | if (uid == null || uid.trim().isEmpty()) { |
182 | uid = meta.getUrl(); | |
183 | } | |
79a99506 NR |
184 | |
185 | GuiReaderBookInfo info = new GuiReaderBookInfo(Type.STORY, uid, | |
186 | meta.getTitle()); | |
187 | ||
188 | info.meta = meta; | |
b4b7ac5c | 189 | info.count = StringUtils.formatNumber(meta.getWords()); |
5bc9573b NR |
190 | if (!info.count.isEmpty()) { |
191 | info.count = GuiReader.trans( | |
192 | meta.isImageDocument() ? StringIdGui.BOOK_COUNT_IMAGES | |
193 | : StringIdGui.BOOK_COUNT_WORDS, info.count); | |
194 | } | |
79a99506 NR |
195 | |
196 | return info; | |
197 | } | |
198 | ||
5bc9573b NR |
199 | /** |
200 | * Create a new book describing the given source/type. | |
201 | * | |
202 | * @param lib | |
203 | * the {@link BasicLibrary} to use to retrieve some more | |
204 | * information about the source | |
205 | * @param source | |
206 | * the source name | |
207 | * | |
208 | * @return the book | |
209 | */ | |
79a99506 NR |
210 | static public GuiReaderBookInfo fromSource(BasicLibrary lib, String source) { |
211 | GuiReaderBookInfo info = new GuiReaderBookInfo(Type.SOURCE, "source_" | |
212 | + source, source); | |
213 | ||
0bb51c9c NR |
214 | int size = 0; |
215 | try { | |
ef98466f | 216 | size = lib.getList().filter(source, null, null).size(); |
0bb51c9c NR |
217 | } catch (IOException e) { |
218 | } | |
219 | ||
220 | info.count = StringUtils.formatNumber(size); | |
5bc9573b NR |
221 | if (!info.count.isEmpty()) { |
222 | info.count = GuiReader.trans(StringIdGui.BOOK_COUNT_STORIES, | |
223 | info.count); | |
224 | } | |
79a99506 NR |
225 | |
226 | return info; | |
227 | } | |
228 | ||
5bc9573b NR |
229 | /** |
230 | * Create a new book describing the given author. | |
231 | * | |
232 | * @param lib | |
233 | * the {@link BasicLibrary} to use to retrieve some more | |
234 | * information about the author | |
235 | * @param author | |
236 | * the author name | |
237 | * | |
238 | * @return the book | |
239 | */ | |
79a99506 NR |
240 | static public GuiReaderBookInfo fromAuthor(BasicLibrary lib, String author) { |
241 | GuiReaderBookInfo info = new GuiReaderBookInfo(Type.AUTHOR, "author_" | |
242 | + author, author); | |
243 | ||
0bb51c9c NR |
244 | int size = 0; |
245 | try { | |
ef98466f | 246 | size = lib.getList().filter(null, author, null).size(); |
0bb51c9c NR |
247 | } catch (IOException e) { |
248 | } | |
249 | ||
250 | info.count = StringUtils.formatNumber(size); | |
5bc9573b NR |
251 | if (!info.count.isEmpty()) { |
252 | info.count = GuiReader.trans(StringIdGui.BOOK_COUNT_STORIES, | |
253 | info.count); | |
254 | } | |
79a99506 NR |
255 | |
256 | return info; | |
257 | } | |
79a99506 | 258 | } |