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