time in log from remote server
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderBookInfo.java
CommitLineData
79a99506
NR
1package be.nikiroo.fanfix.reader.ui;
2
5bc9573b 3import be.nikiroo.fanfix.bundles.StringIdGui;
79a99506 4import be.nikiroo.fanfix.data.MetaData;
5bc9573b 5import be.nikiroo.fanfix.data.Story;
79a99506
NR
6import be.nikiroo.fanfix.library.BasicLibrary;
7import be.nikiroo.utils.Image;
596ed3d6 8import be.nikiroo.utils.StringUtils;
79a99506 9
5bc9573b
NR
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 */
79a99506
NR
17public 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
5bc9573b
NR
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 */
79a99506
NR
49 private GuiReaderBookInfo(Type type, String id, String value) {
50 this.type = type;
51 this.id = id;
52 this.value = value;
53 }
54
5bc9573b
NR
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 */
79a99506 65 public String getMainInfo() {
c349fd48
NR
66 if (meta != null) {
67 return meta.getTitle();
68 }
69
79a99506
NR
70 return value;
71 }
72
5bc9573b
NR
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 */
79a99506
NR
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
5bc9573b
NR
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 */
79a99506
NR
111 public MetaData getMeta() {
112 return meta;
113 }
114
5bc9573b
NR
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 */
79a99506
NR
127 public Image getBaseImage(BasicLibrary lib) {
128 switch (type) {
129 case STORY:
d4a992bf
NR
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;
79a99506
NR
139 case SOURCE:
140 return lib.getSourceCover(value);
141 case AUTHOR:
142 return lib.getAuthorCover(value);
143 }
144
145 return null;
146 }
147
5bc9573b
NR
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 */
79a99506
NR
156 static public GuiReaderBookInfo fromMeta(MetaData meta) {
157 String uid = meta.getUuid();
158 if (uid == null || uid.trim().isEmpty()) {
159 uid = meta.getLuid();
160 }
d4a992bf
NR
161 if (uid == null || uid.trim().isEmpty()) {
162 uid = meta.getUrl();
163 }
79a99506
NR
164
165 GuiReaderBookInfo info = new GuiReaderBookInfo(Type.STORY, uid,
166 meta.getTitle());
167
168 info.meta = meta;
b4b7ac5c 169 info.count = StringUtils.formatNumber(meta.getWords());
5bc9573b
NR
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 }
79a99506
NR
175
176 return info;
177 }
178
5bc9573b
NR
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 */
79a99506
NR
190 static public GuiReaderBookInfo fromSource(BasicLibrary lib, String source) {
191 GuiReaderBookInfo info = new GuiReaderBookInfo(Type.SOURCE, "source_"
192 + source, source);
193
b4b7ac5c
NR
194 info.count = StringUtils.formatNumber(lib.getListBySource(source)
195 .size());
5bc9573b
NR
196 if (!info.count.isEmpty()) {
197 info.count = GuiReader.trans(StringIdGui.BOOK_COUNT_STORIES,
198 info.count);
199 }
79a99506
NR
200
201 return info;
202 }
203
5bc9573b
NR
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 */
79a99506
NR
215 static public GuiReaderBookInfo fromAuthor(BasicLibrary lib, String author) {
216 GuiReaderBookInfo info = new GuiReaderBookInfo(Type.AUTHOR, "author_"
217 + author, author);
218
b4b7ac5c
NR
219 info.count = StringUtils.formatNumber(lib.getListByAuthor(author)
220 .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 }
79a99506 228}