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