Invalidate info for one luid instead of all
[fanfix.git] / src / be / nikiroo / fanfix / library / RemoteLibrary.java
index f3104135f2977b689a222ea68242c8bc5be75d27..326a488b1766a8fc9ddf5052dae8c8fdb32b11cc 100644 (file)
@@ -3,6 +3,7 @@ package be.nikiroo.fanfix.library;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
+import java.net.URL;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.List;
@@ -220,8 +221,7 @@ public class RemoteLibrary extends BasicLibrary {
                                        pg.add(1);
                                }
 
-                               send(null); // done sending the story
-                               luidSaved[0] = (String) send(null); // get LUID
+                               luidSaved[0] = (String) send(null);
 
                                pg.done();
                        }
@@ -233,9 +233,11 @@ public class RemoteLibrary extends BasicLibrary {
                }.connect();
 
                // because the meta changed:
-               clearCache();
-               refresh(pgRefresh);
-               story.setMeta(getInfo(luidSaved[0]));
+               invalidateInfo(luidSaved[0]);
+
+               MetaData meta = getInfo(luidSaved[0]);
+               meta.setCover(story.getMeta().getCover());
+               story.setMeta(meta);
 
                pg.done();
 
@@ -277,20 +279,40 @@ public class RemoteLibrary extends BasicLibrary {
        }
 
        @Override
-       public synchronized File getFile(final String luid, Progress pg) {
-               throw new java.lang.InternalError(
-                               "Operation not supportorted on remote Libraries");
-       }
+       // Could work (more slowly) without it
+       public Story imprt(final URL url, Progress pg) throws IOException {
+               if (pg == null) {
+                       pg = new Progress();
+               }
+
+               pg.setMinMax(0, 2);
+               Progress pgImprt = new Progress();
+               Progress pgGet = new Progress();
+               pg.addProgress(pgImprt, 1);
+               pg.addProgress(pgGet, 1);
+
+               final Progress pgF = pgImprt;
+               final String[] luid = new String[1];
 
-       /**
-        * Stop the server.
-        */
-       public void exit() {
                try {
                        new ConnectActionClientObject(host, port, true) {
                                @Override
                                public void action(Version serverVersion) throws Exception {
-                                       send(new Object[] { md5, "EXIT" });
+                                       Progress pg = pgF;
+
+                                       Object rep = send(new Object[] { md5, "IMPORT",
+                                                       url.toString() });
+
+                                       while (true) {
+                                               if (!RemoteLibraryServer.updateProgress(pg, rep)) {
+                                                       break;
+                                               }
+
+                                               rep = send(null);
+                                       }
+
+                                       pg.done();
+                                       luid[0] = (String) rep;
                                }
 
                                @Override
@@ -301,24 +323,32 @@ public class RemoteLibrary extends BasicLibrary {
                } catch (IOException e) {
                        Instance.getTraceHandler().error(e);
                }
+
+               if (luid[0] == null) {
+                       throw new IOException("Remote failure");
+               }
+
+               Story story = getStory(luid[0], pgGet);
+               pgGet.done();
+
+               pg.done();
+               return story;
        }
 
        @Override
-       protected List<MetaData> getMetas(Progress pg) {
-               final Progress pgF = pg;
-               final List<MetaData> metas = new ArrayList<MetaData>();
+       // Could work (more slowly) without it
+       public synchronized void changeSource(final String luid,
+                       final String newSource, Progress pg) throws IOException {
+               final Progress pgF = pg == null ? new Progress() : pg;
 
                try {
                        new ConnectActionClientObject(host, port, true) {
                                @Override
                                public void action(Version serverVersion) throws Exception {
                                        Progress pg = pgF;
-                                       if (pg == null) {
-                                               pg = new Progress();
-                                       }
-
-                                       Object rep = send(new Object[] { md5, "GET_METADATA", "*" });
 
+                                       Object rep = send(new Object[] { md5, "CHANGE_SOURCE",
+                                                       luid, newSource });
                                        while (true) {
                                                if (!RemoteLibraryServer.updateProgress(pg, rep)) {
                                                        break;
@@ -326,10 +356,33 @@ public class RemoteLibrary extends BasicLibrary {
 
                                                rep = send(null);
                                        }
+                               }
 
-                                       for (MetaData meta : (MetaData[]) rep) {
-                                               metas.add(meta);
-                                       }
+                               @Override
+                               protected void onError(Exception e) {
+                                       Instance.getTraceHandler().error(e);
+                               }
+                       }.connect();
+               } catch (IOException e) {
+                       Instance.getTraceHandler().error(e);
+               }
+       }
+
+       @Override
+       public synchronized File getFile(final String luid, Progress pg) {
+               throw new java.lang.InternalError(
+                               "Operation not supportorted on remote Libraries");
+       }
+
+       /**
+        * Stop the server.
+        */
+       public void exit() {
+               try {
+                       new ConnectActionClientObject(host, port, true) {
+                               @Override
+                               public void action(Version serverVersion) throws Exception {
+                                       send(new Object[] { md5, "EXIT" });
                                }
 
                                @Override
@@ -337,15 +390,28 @@ public class RemoteLibrary extends BasicLibrary {
                                        Instance.getTraceHandler().error(e);
                                }
                        }.connect();
-               } catch (Exception e) {
+               } catch (IOException e) {
                        Instance.getTraceHandler().error(e);
                }
+       }
 
-               return metas;
+       @Override
+       public synchronized MetaData getInfo(String luid) {
+               List<MetaData> metas = getMetasList(luid, null);
+               if (!metas.isEmpty()) {
+                       return metas.get(0);
+               }
+
+               return null;
+       }
+
+       @Override
+       protected List<MetaData> getMetas(Progress pg) {
+               return getMetasList("*", pg);
        }
 
        @Override
-       protected void clearCache() {
+       protected void invalidateInfo(String luid) {
        }
 
        // The following methods are only used by Save and Delete in BasicLibrary:
@@ -364,4 +430,62 @@ public class RemoteLibrary extends BasicLibrary {
        protected Story doSave(Story story, Progress pg) throws IOException {
                throw new java.lang.InternalError("Should not have been called");
        }
+
+       //
+
+       /**
+        * Return the meta of the given story or a list of all known metas if the
+        * luid is "*".
+        * 
+        * @param luid
+        *            the luid of the story or *
+        * @param pg
+        *            the optional progress
+        * 
+        * 
+        * @return the metas
+        */
+       private List<MetaData> getMetasList(final String luid, Progress pg) {
+               final Progress pgF = pg;
+               final List<MetaData> metas = new ArrayList<MetaData>();
+
+               try {
+                       new ConnectActionClientObject(host, port, true) {
+                               @Override
+                               public void action(Version serverVersion) throws Exception {
+                                       Progress pg = pgF;
+                                       if (pg == null) {
+                                               pg = new Progress();
+                                       }
+
+                                       Object rep = send(new Object[] { md5, "GET_METADATA", luid });
+
+                                       while (true) {
+                                               if (!RemoteLibraryServer.updateProgress(pg, rep)) {
+                                                       break;
+                                               }
+
+                                               rep = send(null);
+                                       }
+
+                                       if (rep instanceof MetaData[]) {
+                                               for (MetaData meta : (MetaData[]) rep) {
+                                                       metas.add(meta);
+                                               }
+                                       } else if (rep != null) {
+                                               metas.add((MetaData) rep);
+                                       }
+                               }
+
+                               @Override
+                               protected void onError(Exception e) {
+                                       Instance.getTraceHandler().error(e);
+                               }
+                       }.connect();
+               } catch (Exception e) {
+                       Instance.getTraceHandler().error(e);
+               }
+
+               return metas;
+       }
 }