Remote: now with a pin code
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / RemoteLibrary.java
index 27a0280459a6d6786b2172e0777888af0c942cdd..0a747bdc11adc3d208f4011c74f4deff6be25f16 100644 (file)
@@ -23,16 +23,21 @@ import be.nikiroo.utils.serial.ConnectActionClient;
 public class RemoteLibrary extends BasicLibrary {
        private String host;
        private int port;
+       private final String key;
 
        /**
         * Create a {@link RemoteLibrary} linked to the given server.
         * 
+        * @param key
+        *            the key that will allow us to exchange information with the
+        *            server
         * @param host
         *            the host to contact or NULL for localhost
         * @param port
         *            the port to contact it on
         */
-       public RemoteLibrary(String host, int port) {
+       public RemoteLibrary(String key, String host, int port) {
+               this.key = key;
                this.host = host;
                this.port = port;
        }
@@ -46,8 +51,8 @@ public class RemoteLibrary extends BasicLibrary {
        protected List<MetaData> getMetas(Progress pg) {
                // TODO: progress
                final List<MetaData> metas = new ArrayList<MetaData>();
-               MetaData[] fromNetwork = this
-                               .<MetaData[]> getRemoteObject("GET_METADATA *");
+               MetaData[] fromNetwork = this.<MetaData[]> getRemoteObject( //
+                               new Object[] { key, "GET_METADATA", "*" });
 
                if (fromNetwork != null) {
                        for (MetaData meta : fromNetwork) {
@@ -60,12 +65,20 @@ public class RemoteLibrary extends BasicLibrary {
 
        @Override
        public BufferedImage getCover(final String luid) {
-               return this.<BufferedImage> getRemoteObject("GET_COVER " + luid);
+               return this.<BufferedImage> getRemoteObject( //
+                               new Object[] { key, "GET_COVER", luid });
+       }
+
+       @Override
+       public BufferedImage getSourceCover(final String source) {
+               return this.<BufferedImage> getRemoteObject( //
+                               new Object[] { key, "GET_SOURCE_COVER", source });
        }
 
        @Override
        public synchronized Story getStory(final String luid, Progress pg) {
-               return this.<Story> getRemoteObject("GET_STORY " + luid);
+               return this.<Story> getRemoteObject( //
+                               new Object[] { key, "GET_STORY", luid });
        }
 
        @Override
@@ -75,24 +88,28 @@ public class RemoteLibrary extends BasicLibrary {
        @Override
        public synchronized Story save(Story story, String luid, Progress pg)
                        throws IOException {
-               throw new java.lang.InternalError(
-                               "No write support allowed on remote Libraries");
+               getRemoteObject(new Object[] { key, "SAVE_STORY", story, luid });
+
+               // because the meta changed:
+               clearCache();
+               story.setMeta(getInfo(luid));
+
+               return story;
        }
 
        @Override
        public synchronized void delete(String luid) throws IOException {
-               throw new java.lang.InternalError(
-                               "No write support allowed on remote Libraries");
+               getRemoteObject(new Object[] { key, "DELETE_STORY", luid });
        }
 
        @Override
        public void setSourceCover(String source, String luid) {
-               throw new java.lang.InternalError(
-                               "No write support allowed on remote Libraries");
+               this.<BufferedImage> getRemoteObject( //
+               new Object[] { key, "SET_SOURCE_COVER", source, luid });
        }
 
        @Override
-       public synchronized File getFile(final String luid) {
+       public synchronized File getFile(final String luid, Progress pg) {
                throw new java.lang.InternalError(
                                "Operation not supportorted on remote Libraries");
        }
@@ -125,7 +142,7 @@ public class RemoteLibrary extends BasicLibrary {
         * @return the object or NULL
         */
        @SuppressWarnings("unchecked")
-       private <T> T getRemoteObject(final String command) {
+       private <T> T getRemoteObject(final Object[] command) {
                final Object[] result = new Object[1];
                try {
                        new ConnectActionClient(host, port, true) {