Improve remote, fix bugs, update nikiroo-utils
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / BasicLibrary.java
index de3dbb64890dd53df4bad285c198693e8f061348..d324008f0b4c2294e503d907a25b0edc226ed944 100644 (file)
@@ -29,6 +29,17 @@ import be.nikiroo.utils.Progress;
  * @author niki
  */
 abstract public class BasicLibrary {
+       /**
+        * Return a name for this library (the UI may display this).
+        * <p>
+        * Must not be NULL.
+        * 
+        * @return the name, or an empty {@link String} if none
+        */
+       public String getLibraryName() {
+               return "";
+       }
+
        /**
         * Retrieve the main {@link File} corresponding to the given {@link Story},
         * which can be passed to an external reader or instance.
@@ -37,10 +48,12 @@ abstract public class BasicLibrary {
         * 
         * @param luid
         *            the Library UID of the story
+        * @param pg
+        *            the optional {@link Progress}
         * 
         * @return the corresponding {@link Story}
         */
-       public abstract File getFile(String luid);
+       public abstract File getFile(String luid, Progress pg);
 
        /**
         * Return the cover image associated to this story.
@@ -52,6 +65,35 @@ abstract public class BasicLibrary {
         */
        public abstract BufferedImage getCover(String luid);
 
+       /**
+        * Return the cover image associated to this source.
+        * <p>
+        * By default, return the cover of the first story with this source.
+        * 
+        * @param source
+        *            the source
+        * 
+        * @return the cover image or NULL
+        */
+       public BufferedImage getSourceCover(String source) {
+               List<MetaData> metas = getListBySource(source);
+               if (metas.size() > 0) {
+                       return getCover(metas.get(0).getLuid());
+               }
+
+               return null;
+       }
+
+       /**
+        * Fix the source cover to the given story cover.
+        * 
+        * @param source
+        *            the source to change
+        * @param luid
+        *            the story LUID
+        */
+       public abstract void setSourceCover(String source, String luid);
+
        /**
         * Return the list of stories (represented by their {@link MetaData}, which
         * <b>MAY</b> not have the cover included).
@@ -254,21 +296,30 @@ abstract public class BasicLibrary {
         * @return the corresponding {@link Story} or NULL if not found
         */
        public synchronized Story getStory(String luid, Progress pg) {
-               // TODO: pg
                if (pg == null) {
                        pg = new Progress();
                }
 
+               Progress pgGet = new Progress();
+               Progress pgProcess = new Progress();
+
+               pg.setMinMax(0, 2);
+               pg.addProgress(pgGet, 1);
+               pg.addProgress(pgProcess, 1);
+
                Story story = null;
                for (MetaData meta : getMetas(null)) {
                        if (meta.getLuid().equals(luid)) {
-                               File file = getFile(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).process(url, pg);
+                                               story = BasicSupport.getSupport(type).process(url,
+                                                               pgProcess);
+                                               story.setMeta(meta);
                                        } else {
                                                throw new IOException("Unknown type: " + meta.getType());
                                        }
@@ -278,6 +329,7 @@ abstract public class BasicLibrary {
                                        Instance.syserr(new IOException(
                                                        "Cannot load file from library: " + file, e));
                                } finally {
+                                       pgProcess.done();
                                        pg.done();
                                }
 
@@ -311,6 +363,41 @@ abstract public class BasicLibrary {
                return save(support.process(url, pg), null);
        }
 
+       /**
+        * Import the story from one library to another, and keep the same LUID.
+        * 
+        * @param other
+        *            the other library to import from
+        * @param luid
+        *            the Library UID
+        * @param pg
+        *            the optional progress reporter
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public void imprt(BasicLibrary other, String luid, Progress pg)
+                       throws IOException {
+               Progress pgGetStory = new Progress();
+               Progress pgSave = new Progress();
+               if (pg == null) {
+                       pg = new Progress();
+               }
+
+               pg.setMinMax(0, 2);
+               pg.addProgress(pgGetStory, 1);
+               pg.addProgress(pgSave, 1);
+
+               Story story = other.getStory(luid, pgGetStory);
+               if (story != null) {
+                       story = this.save(story, luid, pgSave);
+                       pg.done();
+               } else {
+                       pg.done();
+                       throw new IOException("Cannot find story in Library: " + luid);
+               }
+       }
+
        /**
         * Export the {@link Story} to the given target in the given format.
         * 
@@ -401,7 +488,7 @@ abstract public class BasicLibrary {
                if (getInfo(luid) != null) {
                        delete(luid);
                }
-               
+
                doSave(story, pg);
 
                clearCache();