X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2FRemoteLibrary.java;fp=src%2Fbe%2Fnikiroo%2Ffanfix%2FRemoteLibrary.java;h=cc78be906004d3adb76960427173054a86289d2c;hb=b0e88ebd20f8b2950c382694e936da76ac3596b6;hp=0000000000000000000000000000000000000000;hpb=0d781e306746cf460f1f41338eb437f32cf9db33;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/RemoteLibrary.java b/src/be/nikiroo/fanfix/RemoteLibrary.java new file mode 100644 index 0000000..cc78be9 --- /dev/null +++ b/src/be/nikiroo/fanfix/RemoteLibrary.java @@ -0,0 +1,109 @@ +package be.nikiroo.fanfix; + +import java.io.File; +import java.io.IOException; +import java.util.Map; + +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; + +public class RemoteLibrary extends Library { + private String host; + private int port; + + private Library lib; + + public RemoteLibrary(String host, int port) throws IOException { + this.host = host; + this.port = port; + + this.localSpeed = false; + this.baseDir = Instance.getRemoteDir(host); + this.baseDir.mkdirs(); + + this.lib = new Library(baseDir, OutputType.INFO_TEXT, OutputType.CBZ); + } + + @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"); + } + + @Override + public synchronized boolean delete(String luid) { + throw new java.lang.InternalError( + "No write support allowed on remote Libraries"); + } + + @Override + public synchronized boolean changeType(String luid, String newType) { + throw new java.lang.InternalError( + "No write support allowed on remote Libraries"); + } + + @Override + protected synchronized Map getStories(Progress pg) { + // TODO: progress + if (stories.isEmpty()) { + try { + new ConnectActionClient(host, port, true, null) { + public void action(Version serverVersion) throws Exception { + try { + Object rep = send("GET_METADATA *"); + for (MetaData meta : (MetaData[]) rep) { + stories.put(meta, null); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + }.connect(); + } catch (IOException e) { + Instance.syserr(e); + } + } + + return stories; + } + + @Override + public synchronized File getFile(final String luid) { + File file = lib.getFile(luid); + if (file == null) { + final File[] tmp = new File[1]; + try { + new ConnectActionClient(host, port, true, null) { + public void action(Version serverVersion) throws Exception { + try { + Object rep = send("GET_STORY " + luid); + Story story = (Story) rep; + if (story != null) { + lib.save(story, luid, null); + tmp[0] = lib.getFile(luid); + } + } catch (Exception e) { + Instance.syserr(e); + } + } + }.connect(); + } catch (IOException e) { + Instance.syserr(e); + } + + file = tmp[0]; + } + + if (file != null) { + MetaData meta = getInfo(luid); + stories.put(meta, file); + } + + return file; + } +}