Some fixes: output types, libraries, remote
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / RemoteLibrary.java
index 511d1c1580cbf607069ab4c1b6fb34b6a1602843..63082ce94fd0a954790865d4a4ddc19fbf6c5ebb 100644 (file)
@@ -9,14 +9,13 @@ import java.util.List;
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
-import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.Version;
 import be.nikiroo.utils.serial.ConnectActionClient;
 
 /**
  * This {@link BasicLibrary} will access a remote server to list the available
- * stories, and download the one you try to load to the local directory
+ * stories, and download the ones you try to load to the local directory
  * specified in the configuration.
  * 
  * @author niki
@@ -44,8 +43,12 @@ public class RemoteLibrary extends BasicLibrary {
                this.baseDir = Instance.getRemoteDir(host);
                this.baseDir.mkdirs();
 
-               this.lib = new LocalLibrary(baseDir, OutputType.INFO_TEXT,
-                               OutputType.CBZ);
+               this.lib = new LocalLibrary(baseDir);
+       }
+
+       @Override
+       public String getLibraryName() {
+               return host + ":" + port;
        }
 
        @Override
@@ -56,7 +59,7 @@ public class RemoteLibrary extends BasicLibrary {
                        metas = new ArrayList<MetaData>();
 
                        try {
-                               new ConnectActionClient(host, port, true, null) {
+                               new ConnectActionClient(host, port, true) {
                                        @Override
                                        public void action(Version serverVersion) throws Exception {
                                                try {
@@ -72,6 +75,14 @@ public class RemoteLibrary extends BasicLibrary {
                        } catch (IOException e) {
                                Instance.syserr(e);
                        }
+
+                       List<String> test = new ArrayList<String>();
+                       for (MetaData meta : metas) {
+                               if (test.contains(meta.getLuid())) {
+                                       throw new RuntimeException("wwops");
+                               }
+                               test.add(meta.getLuid());
+                       }
                }
 
                return metas;
@@ -83,7 +94,7 @@ public class RemoteLibrary extends BasicLibrary {
                if (file == null) {
                        final File[] tmp = new File[1];
                        try {
-                               new ConnectActionClient(host, port, true, null) {
+                               new ConnectActionClient(host, port, true) {
                                        @Override
                                        public void action(Version serverVersion) throws Exception {
                                                try {
@@ -114,13 +125,30 @@ public class RemoteLibrary extends BasicLibrary {
        }
 
        @Override
-       public BufferedImage getCover(String luid) {
-               // Retrieve it from the network if needed:
-               if (lib.getInfo(luid) == null) {
-                       getFile(luid);
+       public BufferedImage getCover(final String luid) {
+               // Retrieve it from the cache if possible:
+               if (lib.getInfo(luid) != null) {
+                       return lib.getCover(luid);
                }
 
-               return lib.getCover(luid);
+               final BufferedImage[] result = new BufferedImage[1];
+               try {
+                       new ConnectActionClient(host, port, true) {
+                               @Override
+                               public void action(Version serverVersion) throws Exception {
+                                       try {
+                                               Object rep = send("GET_COVER " + luid);
+                                               result[0] = (BufferedImage) rep;
+                                       } catch (Exception e) {
+                                               Instance.syserr(e);
+                                       }
+                               }
+                       }.connect();
+               } catch (IOException e) {
+                       Instance.syserr(e);
+               }
+
+               return result[0];
        }
 
        @Override
@@ -137,20 +165,41 @@ public class RemoteLibrary extends BasicLibrary {
        }
 
        @Override
-       protected int getNextId() {
+       public synchronized void delete(String luid) throws IOException {
                throw new java.lang.InternalError(
                                "No write support allowed on remote Libraries");
        }
 
        @Override
-       protected void doDelete(String luid) throws IOException {
+       public void setSourceCover(String source, String luid) {
                throw new java.lang.InternalError(
                                "No write support allowed on remote Libraries");
        }
 
+       // All the following methods are only used by Save and Delete in
+       // BasicLibrary:
+
+       @Override
+       protected int getNextId() {
+               throw new java.lang.InternalError("Should not have been called");
+       }
+
+       @Override
+       protected void doDelete(String luid) throws IOException {
+               throw new java.lang.InternalError("Should not have been called");
+       }
+
        @Override
        protected Story doSave(Story story, Progress pg) throws IOException {
-               throw new java.lang.InternalError(
-                               "No write support allowed on remote Libraries");
+               throw new java.lang.InternalError("Should not have been called");
+       }
+
+       /**
+        * Return the backing local library.
+        * 
+        * @return the library
+        */
+       LocalLibrary getLocalLibrary() {
+               return lib;
        }
 }