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