update nikiroo-utils, remote lib errors need some work
[fanfix.git] / src / be / nikiroo / fanfix / library / BasicLibrary.java
index 8ec4e5620655ad148fb6b4b47b2c16d0f4284ee8..f96506d9f43bc42feef62460947543487430861f 100644 (file)
@@ -44,7 +44,7 @@ abstract public class BasicLibrary {
                /** The library is invalid (not correctly set up). */
                INVALID,
                /** You are not allowed to access this library. */
-               UNAUTORIZED,
+               UNAUTHORIZED,
                /** The library is currently out of commission. */
                UNAVAILABLE,
        }
@@ -119,6 +119,31 @@ abstract public class BasicLibrary {
                return null;
        }
 
+       /**
+        * Return the cover image associated to this author.
+        * <p>
+        * By default, return the custom cover if any, and if not, return the cover
+        * of the first story with this author.
+        * 
+        * @param author
+        *            the author
+        * 
+        * @return the cover image or NULL
+        */
+       public Image getAuthorCover(String author) {
+               Image custom = getCustomAuthorCover(author);
+               if (custom != null) {
+                       return custom;
+               }
+
+               List<MetaData> metas = getListByAuthor(author);
+               if (metas.size() > 0) {
+                       return getCover(metas.get(0).getLuid());
+               }
+
+               return null;
+       }
+
        /**
         * Return the custom cover image associated to this source.
         * <p>
@@ -134,7 +159,21 @@ abstract public class BasicLibrary {
        }
 
        /**
-        * Fix the source cover to the given story cover.
+        * Return the custom cover image associated to this author.
+        * <p>
+        * By default, return NULL.
+        * 
+        * @param author
+        *            the author to look for
+        * 
+        * @return the custom cover or NULL if none
+        */
+       public Image getCustomAuthorCover(@SuppressWarnings("unused") String author) {
+               return null;
+       }
+
+       /**
+        * Set the source cover to the given story cover.
         * 
         * @param source
         *            the source to change
@@ -143,6 +182,16 @@ abstract public class BasicLibrary {
         */
        public abstract void setSourceCover(String source, String luid);
 
+       /**
+        * Set the author cover to the given story cover.
+        * 
+        * @param author
+        *            the author to change
+        * @param luid
+        *            the story LUID
+        */
+       public abstract void setAuthorCover(String author, String luid);
+
        /**
         * Return the list of stories (represented by their {@link MetaData}, which
         * <b>MAY</b> not have the cover included).
@@ -516,6 +565,43 @@ abstract public class BasicLibrary {
         * @return the corresponding {@link Story} or NULL if not found
         */
        public synchronized Story getStory(String luid, Progress pg) {
+               Progress pgMetas = new Progress();
+               Progress pgStory = new Progress();
+               if (pg != null) {
+                       pg.setMinMax(0, 100);
+                       pg.addProgress(pgMetas, 10);
+                       pg.addProgress(pgStory, 90);
+               }
+
+               MetaData meta = null;
+               for (MetaData oneMeta : getMetas(pgMetas)) {
+                       if (oneMeta.getLuid().equals(luid)) {
+                               meta = oneMeta;
+                               break;
+                       }
+               }
+
+               pgMetas.done();
+
+               Story story = getStory(luid, meta, pgStory);
+               pgStory.done();
+
+               return story;
+       }
+
+       /**
+        * Retrieve a specific {@link Story}.
+        * 
+        * @param luid
+        *            the meta of the story
+        * @param pg
+        *            the optional progress reporter
+        * 
+        * @return the corresponding {@link Story} or NULL if not found
+        */
+       public synchronized Story getStory(String luid,
+                       @SuppressWarnings("javadoc") MetaData meta, Progress pg) {
+
                if (pg == null) {
                        pg = new Progress();
                }
@@ -528,39 +614,33 @@ abstract public class BasicLibrary {
                pg.addProgress(pgProcess, 1);
 
                Story story = null;
-               for (MetaData meta : getMetas(null)) {
-                       if (meta.getLuid().equals(luid)) {
-                               File file = getFile(luid, pgGet);
-                               pgGet.done();
-                               try {
-                                       SupportType type = SupportType.valueOfAllOkUC(meta
-                                                       .getType());
-                                       URL url = file.toURI().toURL();
-                                       if (type != null) {
-                                               story = BasicSupport.getSupport(type, url) //
-                                                               .process(pgProcess);
-
-                                               // Because we do not want to clear the meta cache:
-                                               meta.setCover(story.getMeta().getCover());
-                                               meta.setResume(story.getMeta().getResume());
-                                               story.setMeta(meta);
-                                               //
-                                       } else {
-                                               throw new IOException("Unknown type: " + meta.getType());
-                                       }
-                               } catch (IOException e) {
-                                       // We should not have not-supported files in the
-                                       // library
-                                       Instance.getTraceHandler().error(
-                                                       new IOException("Cannot load file from library: "
-                                                                       + file, e));
-                               } finally {
-                                       pgProcess.done();
-                                       pg.done();
-                               }
-
-                               break;
+               File file = getFile(luid, pgGet);
+               pgGet.done();
+               try {
+                       SupportType type = SupportType.valueOfAllOkUC(meta.getType());
+                       URL url = file.toURI().toURL();
+                       if (type != null) {
+                               story = BasicSupport.getSupport(type, url) //
+                                               .process(pgProcess);
+
+                               // Because we do not want to clear the meta cache:
+                               meta.setCover(story.getMeta().getCover());
+                               meta.setResume(story.getMeta().getResume());
+                               story.setMeta(meta);
+                               //
+                       } else {
+                               throw new IOException("Unknown type: " + meta.getType());
                        }
+               } catch (IOException e) {
+                       // We should not have not-supported files in the
+                       // library
+                       Instance.getTraceHandler().error(
+                                       new IOException(String.format(
+                                                       "Cannot load file of type '%s' from library: %s",
+                                                       meta.getType(), file), e));
+               } finally {
+                       pgProcess.done();
+                       pg.done();
                }
 
                return story;
@@ -863,6 +943,8 @@ abstract public class BasicLibrary {
                meta.setTitle(newTitle);
                meta.setAuthor(newAuthor);
                saveMeta(meta, pg);
+
+               invalidateInfo(luid);
        }
 
        /**