cover: allow custom author covers
[fanfix.git] / src / be / nikiroo / fanfix / library / CacheLibrary.java
CommitLineData
ff05b828
NR
1package be.nikiroo.fanfix.library;
2
ff05b828
NR
3import java.io.File;
4import java.io.IOException;
edf79e5e 5import java.net.URL;
ff05b828
NR
6import java.util.List;
7
8import be.nikiroo.fanfix.Instance;
9import be.nikiroo.fanfix.bundles.UiConfig;
10import be.nikiroo.fanfix.data.MetaData;
11import be.nikiroo.fanfix.data.Story;
16a81ef7 12import be.nikiroo.utils.Image;
ff05b828
NR
13import be.nikiroo.utils.Progress;
14
15/**
16 * This library will cache another pre-existing {@link BasicLibrary}.
17 *
18 * @author niki
19 */
20public class CacheLibrary extends BasicLibrary {
21 private List<MetaData> metas;
22 private BasicLibrary lib;
23 private LocalLibrary cacheLib;
24
25 /**
26 * Create a cache library around the given one.
27 * <p>
28 * It will return the same result, but those will be saved to disk at the
29 * same time to be fetched quicker the next time.
30 *
31 * @param cacheDir
32 * the cache directory where to save the files to disk
33 * @param lib
34 * the original library to wrap
35 */
36 public CacheLibrary(File cacheDir, BasicLibrary lib) {
37 this.cacheLib = new LocalLibrary(cacheDir, Instance.getUiConfig()
38 .getString(UiConfig.GUI_NON_IMAGES_DOCUMENT_TYPE), Instance
39 .getUiConfig().getString(UiConfig.GUI_IMAGES_DOCUMENT_TYPE),
40 true);
41 this.lib = lib;
42 }
43
44 @Override
45 public String getLibraryName() {
46 return lib.getLibraryName();
47 }
48
e6249b0f
NR
49 @Override
50 public Status getStatus() {
51 return lib.getStatus();
52 }
53
ff05b828
NR
54 @Override
55 protected List<MetaData> getMetas(Progress pg) {
56 if (pg == null) {
57 pg = new Progress();
58 }
59
60 if (metas == null) {
61 metas = lib.getMetas(pg);
62 }
63
64 pg.done();
65 return metas;
66 }
67
68 @Override
69 public synchronized File getFile(final String luid, Progress pg) {
70 if (pg == null) {
71 pg = new Progress();
72 }
73
74 Progress pgImport = new Progress();
75 Progress pgGet = new Progress();
76 Progress pgRecall = new Progress();
77
78 pg.setMinMax(0, 5);
79 pg.addProgress(pgImport, 3);
80 pg.addProgress(pgGet, 1);
81 pg.addProgress(pgRecall, 1);
82
83 if (!isCached(luid)) {
84 try {
85 cacheLib.imprt(lib, luid, pgImport);
efa3c511 86 updateInfo(cacheLib.getInfo(luid));
ff05b828 87 pgImport.done();
ff05b828 88 } catch (IOException e) {
62c63b07 89 Instance.getTraceHandler().error(e);
ff05b828
NR
90 }
91
92 pgImport.done();
93 pgGet.done();
94 }
95
96 File file = cacheLib.getFile(luid, pgRecall);
97 pgRecall.done();
98
99 pg.done();
100 return file;
101 }
102
103 @Override
16a81ef7 104 public Image getCover(final String luid) {
ff05b828
NR
105 if (isCached(luid)) {
106 return cacheLib.getCover(luid);
107 }
108
085a2f9a 109 // We could update the cache here, but it's not easy
ff05b828
NR
110 return lib.getCover(luid);
111 }
112
085a2f9a 113 @Override
16a81ef7 114 public Image getSourceCover(String source) {
e1de8087
NR
115 Image custom = getCustomSourceCover(source);
116 if (custom != null) {
117 return custom;
118 }
119
cf45a4c4
NR
120 Image cached = cacheLib.getSourceCover(source);
121 if (cached != null) {
122 return cached;
123 }
124
125 return lib.getSourceCover(source);
e1de8087
NR
126 }
127
3989dfc5
NR
128 @Override
129 public Image getAuthorCover(String author) {
130 Image custom = getCustomSourceCover(author);
131 if (custom != null) {
132 return custom;
133 }
134
135 Image cached = cacheLib.getSourceCover(author);
136 if (cached != null) {
137 return cached;
138 }
139
140 return lib.getSourceCover(author);
141
142 }
143
e1de8087
NR
144 @Override
145 public Image getCustomSourceCover(String source) {
146 Image custom = cacheLib.getCustomSourceCover(source);
147 if (custom == null) {
148 custom = lib.getCustomSourceCover(source);
149 if (custom != null) {
150 cacheLib.setSourceCover(source, custom);
151 }
152 }
153
154 return custom;
085a2f9a 155 }
3989dfc5
NR
156
157 @Override
158 public Image getCustomAuthorCover(String author) {
159 Image custom = cacheLib.getCustomAuthorCover(author);
160 if (custom == null) {
161 custom = lib.getCustomAuthorCover(author);
162 if (custom != null) {
163 cacheLib.setAuthorCover(author, custom);
164 }
165 }
166
167 return custom;
168 }
085a2f9a
NR
169
170 @Override
171 public void setSourceCover(String source, String luid) {
172 lib.setSourceCover(source, luid);
0dc195de 173 cacheLib.setSourceCover(source, getCover(luid));
085a2f9a
NR
174 }
175
3989dfc5
NR
176 @Override
177 public void setAuthorCover(String author, String luid) {
178 lib.setAuthorCover(author, luid);
179 cacheLib.setAuthorCover(author, getCover(luid));
180 }
181
ff05b828 182 @Override
efa3c511 183 protected void updateInfo(MetaData meta) {
b56c9d60 184 if (meta != null && metas != null) {
efa3c511
NR
185 for (int i = 0; i < metas.size(); i++) {
186 if (metas.get(i).getLuid().equals(meta.getLuid())) {
187 metas.set(i, meta);
188 }
189 }
190 }
191
192 cacheLib.updateInfo(meta);
193 lib.updateInfo(meta);
194 }
195
196 @Override
c8d48938 197 protected void invalidateInfo(String luid) {
e272f05f 198 if (luid == null) {
cbd62024 199 metas = null;
e272f05f 200 } else if (metas != null) {
e272f05f
NR
201 for (int i = 0; i < metas.size(); i++) {
202 if (metas.get(i).getLuid().equals(luid)) {
cbd62024 203 metas.remove(i--);
e272f05f
NR
204 }
205 }
e272f05f
NR
206 }
207
c8d48938
NR
208 cacheLib.invalidateInfo(luid);
209 lib.invalidateInfo(luid);
ff05b828
NR
210 }
211
212 @Override
213 public synchronized Story save(Story story, String luid, Progress pg)
214 throws IOException {
03c1cede
NR
215 Progress pgLib = new Progress();
216 Progress pgCacheLib = new Progress();
217
218 if (pg == null) {
219 pg = new Progress();
220 }
221
222 pg.setMinMax(0, 2);
223 pg.addProgress(pgLib, 1);
224 pg.addProgress(pgCacheLib, 1);
225
226 story = lib.save(story, luid, pgLib);
0fa0fe95 227 story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib);
03c1cede 228
efa3c511 229 updateInfo(story.getMeta());
03c1cede 230
ff05b828
NR
231 return story;
232 }
233
234 @Override
235 public synchronized void delete(String luid) throws IOException {
085a2f9a
NR
236 if (isCached(luid)) {
237 cacheLib.delete(luid);
238 }
ff05b828 239 lib.delete(luid);
e272f05f 240
cbd62024
NR
241 MetaData meta = getInfo(luid);
242 if (meta != null) {
243 metas.remove(meta);
e272f05f 244 }
ff05b828
NR
245 }
246
ff05b828 247 @Override
c8d48938
NR
248 protected synchronized void changeSTA(String luid, String newSource,
249 String newTitle, String newAuthor, Progress pg) throws IOException {
ff05b828
NR
250 if (pg == null) {
251 pg = new Progress();
252 }
253
254 Progress pgCache = new Progress();
255 Progress pgOrig = new Progress();
256 pg.setMinMax(0, 2);
257 pg.addProgress(pgCache, 1);
258 pg.addProgress(pgOrig, 1);
259
e272f05f
NR
260 MetaData meta = getInfo(luid);
261 if (meta == null) {
262 throw new IOException("Story not found: " + luid);
263 }
264
e06632ee 265 if (isCached(luid)) {
c8d48938 266 cacheLib.changeSTA(luid, newSource, newTitle, newAuthor, pgCache);
e06632ee 267 }
ff05b828 268 pgCache.done();
e272f05f 269
c8d48938 270 lib.changeSTA(luid, newSource, newTitle, newAuthor, pgOrig);
ff05b828
NR
271 pgOrig.done();
272
e272f05f 273 meta.setSource(newSource);
c8d48938
NR
274 meta.setTitle(newTitle);
275 meta.setAuthor(newAuthor);
ff05b828
NR
276 pg.done();
277 }
278
279 /**
280 * Check if the {@link Story} denoted by this Library UID is present in the
281 * cache.
282 *
283 * @param luid
284 * the Library UID
285 *
286 * @return TRUE if it is
287 */
288 public boolean isCached(String luid) {
289 return cacheLib.getInfo(luid) != null;
290 }
291
292 /**
293 * Clear the {@link Story} from the cache.
c3b229a1
NR
294 * <p>
295 * The next time we try to retrieve the {@link Story}, it may be required to
296 * cache it again.
ff05b828
NR
297 *
298 * @param luid
299 * the story to clear
300 *
301 * @throws IOException
302 * in case of I/O error
303 */
304 public void clearFromCache(String luid) throws IOException {
e06632ee
NR
305 if (isCached(luid)) {
306 cacheLib.delete(luid);
e06632ee 307 }
ff05b828
NR
308 }
309
edf79e5e
NR
310 @Override
311 public Story imprt(URL url, Progress pg) throws IOException {
312 if (pg == null) {
313 pg = new Progress();
314 }
315
316 Progress pgImprt = new Progress();
317 Progress pgCache = new Progress();
318 pg.setMinMax(0, 10);
319 pg.addProgress(pgImprt, 7);
320 pg.addProgress(pgCache, 3);
321
322 Story story = lib.imprt(url, pgImprt);
323 cacheLib.save(story, story.getMeta().getLuid(), pgCache);
324
efa3c511 325 updateInfo(story.getMeta());
cbd62024 326
edf79e5e
NR
327 pg.done();
328 return story;
329 }
330
ff05b828
NR
331 // All the following methods are only used by Save and Delete in
332 // BasicLibrary:
333
334 @Override
335 protected int getNextId() {
336 throw new java.lang.InternalError("Should not have been called");
337 }
338
339 @Override
340 protected void doDelete(String luid) throws IOException {
341 throw new java.lang.InternalError("Should not have been called");
342 }
343
344 @Override
345 protected Story doSave(Story story, Progress pg) throws IOException {
346 throw new java.lang.InternalError("Should not have been called");
347 }
348}