X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FRemoteLibrary.java;h=75f98be8bd62b18c9ab66801eb372ba6a0003389;hb=00f6344a8389698f3332767d445b8345b050a763;hp=06370320fb94a6709f543e859841d4e176e4f8de;hpb=468b960ba378ae71230f475d3e97521e133ec019;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/library/RemoteLibrary.java b/src/be/nikiroo/fanfix/library/RemoteLibrary.java index 0637032..75f98be 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibrary.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibrary.java @@ -1,8 +1,8 @@ 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; @@ -10,6 +10,7 @@ import java.util.List; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; +import be.nikiroo.utils.Image; import be.nikiroo.utils.Progress; import be.nikiroo.utils.StringUtils; import be.nikiroo.utils.Version; @@ -94,15 +95,15 @@ public class RemoteLibrary extends BasicLibrary { } @Override - public BufferedImage getCover(final String luid) { - final BufferedImage[] result = new BufferedImage[1]; + public Image getCover(final String luid) { + final Image[] result = new Image[1]; try { new ConnectActionClientObject(host, port, true) { @Override public void action(Version serverVersion) throws Exception { Object rep = send(new Object[] { md5, "GET_COVER", luid }); - result[0] = (BufferedImage) rep; + result[0] = (Image) rep; } @Override @@ -118,16 +119,16 @@ public class RemoteLibrary extends BasicLibrary { } @Override - public BufferedImage getSourceCover(final String source) { - final BufferedImage[] result = new BufferedImage[1]; + public Image getCustomSourceCover(final String source) { + final Image[] result = new Image[1]; try { new ConnectActionClientObject(host, port, true) { @Override public void action(Version serverVersion) throws Exception { - Object rep = send(new Object[] { md5, "GET_SOURCE_COVER", - source }); - result[0] = (BufferedImage) rep; + Object rep = send(new Object[] { md5, + "GET_CUSTOM_SOURCE_COVER", source }); + result[0] = (Image) rep; } @Override @@ -191,16 +192,23 @@ public class RemoteLibrary extends BasicLibrary { @Override public synchronized Story save(final Story story, final String luid, Progress pg) throws IOException { - final Progress pgF = pg; + final String[] luidSaved = new String[1]; + Progress pgSave = new Progress(); + Progress pgRefresh = new Progress(); + if (pg == null) { + pg = new Progress(); + } + + pg.setMinMax(0, 10); + pg.addProgress(pgSave, 9); + pg.addProgress(pgRefresh, 1); + + final Progress pgF = pgSave; new ConnectActionClientObject(host, port, true) { @Override public void action(Version serverVersion) throws Exception { Progress pg = pgF; - if (pg == null) { - pg = new Progress(); - } - if (story.getMeta().getWords() <= Integer.MAX_VALUE) { pg.setMinMax(0, (int) story.getMeta().getWords()); } @@ -213,7 +221,8 @@ public class RemoteLibrary extends BasicLibrary { pg.add(1); } - send(null); + luidSaved[0] = (String) send(null); + pg.done(); } @@ -224,8 +233,17 @@ public class RemoteLibrary extends BasicLibrary { }.connect(); // because the meta changed: - clearCache(); - story.setMeta(getInfo(luid)); + MetaData meta = getInfo(luidSaved[0]); + if (story.getMeta().getClass() != null) { + // If already available locally: + meta.setCover(story.getMeta().getCover()); + } else { + // If required: + meta.setCover(getCover(meta.getLuid())); + } + story.setMeta(meta); + + pg.done(); return story; } @@ -265,20 +283,47 @@ 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 { + // Import the file locally if it is actually a file + if (url == null || url.getProtocol().equalsIgnoreCase("file")) { + return super.imprt(url, pg); + } + + // Import it remotely if it is an URL + + 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 @@ -289,24 +334,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 getMetas(Progress pg) { - final Progress pgF = pg; - final List metas = new ArrayList(); + // 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; @@ -314,10 +367,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 @@ -325,15 +401,34 @@ 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 metas = getMetasList(luid, null); + if (!metas.isEmpty()) { + return metas.get(0); + } + + return null; } @Override - protected void clearCache() { + protected List getMetas(Progress pg) { + return getMetasList("*", pg); + } + + @Override + protected void updateInfo(MetaData meta) { + // Will be taken care of directly server side + } + + @Override + protected void deleteInfo(String luid) { + // Will be taken care of directly server side } // The following methods are only used by Save and Delete in BasicLibrary: @@ -352,4 +447,64 @@ 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 "*". + *

+ * Will not get the covers. + * + * @param luid + * the luid of the story or * + * @param pg + * the optional progress + * + * + * @return the metas + */ + private List getMetasList(final String luid, Progress pg) { + final Progress pgF = pg; + final List metas = new ArrayList(); + + 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; + } }