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