fix remote default covers
[fanfix.git] / src / be / nikiroo / fanfix / library / CacheLibrary.java
index 26204156b4d587cb7353851de1a8ea8af7e12b84..a550740bafa7a9105d271ef9dd7df53515651d6e 100644 (file)
@@ -83,8 +83,8 @@ public class CacheLibrary extends BasicLibrary {
                if (!isCached(luid)) {
                        try {
                                cacheLib.imprt(lib, luid, pgImport);
+                               updateInfo(cacheLib.getInfo(luid));
                                pgImport.done();
-                               invalidateInfo(luid);
                        } catch (IOException e) {
                                Instance.getTraceHandler().error(e);
                        }
@@ -112,42 +112,66 @@ public class CacheLibrary extends BasicLibrary {
 
        @Override
        public Image getSourceCover(String source) {
-               // no cache for the source cover
+               Image custom = getCustomSourceCover(source);
+               if (custom != null) {
+                       return custom;
+               }
+
+               Image cached = cacheLib.getSourceCover(source);
+               if (cached != null) {
+                       return cached;
+               }
+
                return lib.getSourceCover(source);
        }
 
+       @Override
+       public Image getCustomSourceCover(String source) {
+               Image custom = cacheLib.getCustomSourceCover(source);
+               if (custom == null) {
+                       custom = lib.getCustomSourceCover(source);
+                       if (custom != null) {
+                               cacheLib.setSourceCover(source, custom);
+                       }
+               }
+
+               return custom;
+       }
+
        @Override
        public void setSourceCover(String source, String luid) {
                lib.setSourceCover(source, luid);
-               cacheLib.setSourceCover(source, getSourceCover(source));
+               cacheLib.setSourceCover(source, getCover(luid));
        }
 
        @Override
-       protected void invalidateInfo(String luid) {
-               List<MetaData> metas = this.metas;
+       protected void updateInfo(MetaData meta) {
+               if (meta != null && metas != null) {
+                       for (int i = 0; i < metas.size(); i++) {
+                               if (metas.get(i).getLuid().equals(meta.getLuid())) {
+                                       metas.set(i, meta);
+                               }
+                       }
+               }
+
+               cacheLib.updateInfo(meta);
+               lib.updateInfo(meta);
+       }
 
+       @Override
+       protected void deleteInfo(String luid) {
                if (luid == null) {
-                       this.metas = null;
+                       metas = null;
                } else if (metas != null) {
-                       MetaData meta = lib.getInfo(luid);
                        for (int i = 0; i < metas.size(); i++) {
                                if (metas.get(i).getLuid().equals(luid)) {
-                                       if (meta != null) {
-                                               metas.set(i, meta);
-                                               meta = null;
-                                       } else {
-                                               metas.remove(i--);
-                                       }
+                                       metas.remove(i--);
                                }
                        }
-
-                       if (meta != null) {
-                               metas.add(meta);
-                       }
                }
 
-               cacheLib.invalidateInfo(luid);
-               lib.invalidateInfo(luid);
+               cacheLib.deleteInfo(luid);
+               lib.deleteInfo(luid);
        }
 
        @Override
@@ -167,7 +191,7 @@ public class CacheLibrary extends BasicLibrary {
                story = lib.save(story, luid, pgLib);
                story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib);
 
-               invalidateInfo(story.getMeta().getLuid());
+               updateInfo(story.getMeta());
 
                return story;
        }
@@ -179,13 +203,9 @@ public class CacheLibrary extends BasicLibrary {
                }
                lib.delete(luid);
 
-               List<MetaData> metas = this.metas;
-               if (metas != null) {
-                       for (int i = 0; i < metas.size(); i++) {
-                               if (metas.get(i).getLuid().equals(luid)) {
-                                       metas.set(i, lib.getInfo(luid));
-                               }
-                       }
+               MetaData meta = getInfo(luid);
+               if (meta != null) {
+                       metas.remove(meta);
                }
        }
 
@@ -234,6 +254,9 @@ public class CacheLibrary extends BasicLibrary {
 
        /**
         * Clear the {@link Story} from the cache.
+        * <p>
+        * The next time we try to retrieve the {@link Story}, it may be required to
+        * cache it again.
         * 
         * @param luid
         *            the story to clear
@@ -244,7 +267,6 @@ public class CacheLibrary extends BasicLibrary {
        public void clearFromCache(String luid) throws IOException {
                if (isCached(luid)) {
                        cacheLib.delete(luid);
-                       invalidateInfo(luid);
                }
        }
 
@@ -263,6 +285,8 @@ public class CacheLibrary extends BasicLibrary {
                Story story = lib.imprt(url, pgImprt);
                cacheLib.save(story, story.getMeta().getLuid(), pgCache);
 
+               updateInfo(story.getMeta());
+
                pg.done();
                return story;
        }