# Fanfix
+# Version WIP
+
+- Bug fixes
+- Remote server/client improvements
+
## Version 1.6.2
- Better progress bars
return cache.load(uniqueID, true, true);
}
+ /**
+ * Remove the given resource from the cache.
+ *
+ * @param uniqueID
+ * a unique ID used to locate the cached resource
+ *
+ * @return TRUE if it was removed
+ */
+ public boolean removeFromCache(String uniqueID) {
+ return cache.remove(uniqueID);
+ }
+
/**
* Clean the cache (delete the cached items).
*
if (type != null) {
story = BasicSupport.getSupport(type).process(url,
pgProcess);
+ story.setMeta(meta);
} else {
throw new IOException("Unknown type: " + meta.getType());
}
try {
cacheLib.imprt(lib, luid, pgImport);
pgImport.done();
- Story story = cacheLib.getStory(luid, pgGet);
- metas.remove(story.getMeta());
- metas.add(story.getMeta());
+ clearCache();
} catch (IOException e) {
Instance.syserr(e);
}
@Override
public BufferedImage getCover(final String luid) {
- // Retrieve it from the cache if possible:
if (isCached(luid)) {
return cacheLib.getCover(luid);
}
+ // We could update the cache here, but it's not easy
return lib.getCover(luid);
}
+ @Override
+ public BufferedImage getSourceCover(String source) {
+ // no cache for the source cover
+ return lib.getSourceCover(source);
+ }
+
+ @Override
+ public void setSourceCover(String source, String luid) {
+ lib.setSourceCover(source, luid);
+ cacheLib.setSourceCover(source, getSourceCover(source));
+ }
+
@Override
protected void clearCache() {
metas = null;
@Override
public synchronized void delete(String luid) throws IOException {
- cacheLib.delete(luid);
+ if (isCached(luid)) {
+ cacheLib.delete(luid);
+ }
lib.delete(luid);
clearCache();
}
- @Override
- public void setSourceCover(String source, String luid) {
- cacheLib.setSourceCover(source, luid);
- lib.setSourceCover(source, luid);
- }
-
@Override
public synchronized void changeSource(String luid, String newSource,
Progress pg) throws IOException {
* The directory (full path) where the new {@link Story} related to this
* {@link MetaData} should be located on disk.
*
- * @param type
+ * @param source
* the type (source)
*
* @return the target directory
*/
- private File getExpectedDir(String type) {
- String source = type.replaceAll("[^a-zA-Z0-9._+-]", "_");
- return new File(baseDir, source);
+ private File getExpectedDir(String source) {
+ String sanitizedSource = source.replaceAll("[^a-zA-Z0-9._+-]", "_");
+ return new File(baseDir, sanitizedSource);
}
/**
return stories;
}
+
+ /**
+ * Fix the source cover to the given story cover.
+ *
+ * @param source
+ * the source to change
+ * @param coverImage
+ * the cover image
+ */
+ void setSourceCover(String source, BufferedImage coverImage) {
+ sourceCovers.put(source, coverImage);
+ File cover = new File(getExpectedDir(source), ".cover.png");
+ try {
+ ImageIO.write(sourceCovers.get(source), "png", cover);
+ } catch (IOException e) {
+ Instance.syserr(e);
+ sourceCovers.remove(source);
+ }
+ }
}
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[] { "GET_METADATA", "*" });
if (fromNetwork != null) {
for (MetaData meta : fromNetwork) {
@Override
public BufferedImage getCover(final String luid) {
- return this.<BufferedImage> getRemoteObject("GET_COVER " + luid);
+ return this.<BufferedImage> getRemoteObject( //
+ new Object[] { "GET_COVER", luid });
+ }
+
+ @Override
+ public BufferedImage getSourceCover(final String source) {
+ return this.<BufferedImage> getRemoteObject( //
+ new Object[] { "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[] { "GET_STORY", luid });
}
@Override
@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[] { "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[] { "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[] { "SET_SOURCE_COVER", source, luid });
}
@Override
* @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) {
package be.nikiroo.fanfix.library;
import java.io.IOException;
+import java.security.InvalidParameterException;
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.Version;
import be.nikiroo.utils.serial.ConnectActionServer;
import be.nikiroo.utils.serial.Server;
/**
* Create a new remote server that will listen for order on the given port.
* <p>
- * The available commands are:
+ * The available commands are given as String arrays (first item is the command,
+ * the rest are the arguments):
* <ul>
- * <li>GET_METADATA *: will get the metadata of all the stories in the library</li>
+ * <li>GET_METADATA *: will return the metadata of all the stories in the
+ * library</li>
* <li>GET_STORY [luid]: will return the given story if it exists (or NULL if
* not)</li>
+ * <li>SAVE_STORY [story] [luid]: save the story with the given LUID</li>
+ * <li>DELETE_STORY [luid]: delete the story of LUID luid</li>
+ * <li>GET_COVER [luid]: return the cover of the story</li>
+ * <li>GET_SOURCE_COVER [source]: return the cover for this source</li>
+ * <li>SET_SOURCE_COVER [source], [luid]: set the default cover for the given
+ * source to the cover of the story denoted by luid</li>
* </ul>
*
* @author niki
@Override
protected Object onRequest(ConnectActionServer action,
Version clientVersion, Object data) throws Exception {
- String command = null;
- String args = null;
- if (data instanceof String) {
- command = (String) data;
- int pos = command.indexOf(" ");
- if (pos >= 0) {
- args = command.substring(pos + 1);
- command = command.substring(0, pos);
+
+ String command = "";
+ Object[] args = new Object[0];
+ if (data instanceof Object[]) {
+ args = (Object[]) data;
+ if (args.length > 0) {
+ command = "" + args[0];
}
}
- System.out.println(String.format("COMMAND: [%s], ARGS: [%s]", command,
- args));
+ System.out.print("COMMAND: ");
+ for (Object arg : args) {
+ System.out.print(arg + " ");
+ }
+ System.out.println("");
// TODO: progress (+send name + %age info back to client)
if ("GET_METADATA".equals(command)) {
- if (args != null && args.equals("*")) {
+ if (args[1].equals("*")) {
List<MetaData> metas = Instance.getLibrary().getMetas(null);
return metas.toArray(new MetaData[] {});
}
+ throw new InvalidParameterException(
+ "only * is valid here, but you passed: " + args[1]);
} else if ("GET_STORY".equals(command)) {
- if (args != null) {
- return Instance.getLibrary().getStory(args, null);
- }
+ return Instance.getLibrary().getStory("" + args[1], null);
+ } else if ("SAVE_STORY".equals(command)) {
+ Instance.getLibrary().save((Story) args[1], "" + args[2], null);
+ } else if ("DELETE_STORY".equals(command)) {
+ Instance.getLibrary().delete("" + args[1]);
} else if ("GET_COVER".equals(command)) {
- if (args != null) {
- return Instance.getLibrary().getCover(args);
- }
+ return Instance.getLibrary().getCover("" + args[1]);
+ } else if ("GET_SOURCE_COVER".equals(command)) {
+ return Instance.getLibrary().getSourceCover("" + args[1]);
+ } else if ("SET_SOURCE_COVER".equals(command)) {
+ Instance.getLibrary().setSourceCover("" + args[1], "" + args[2]);
}
return null;
optSecondary = "";
}
- icon = new JLabel(generateCoverIcon(meta));
+ icon = new JLabel(generateCoverIcon());
title = new JLabel(
String.format(
"<html>"
/**
* Generate a cover icon based upon the given {@link MetaData}.
*
- * @param meta
- * the {@link MetaData} about the target {@link Story} or source
- * (if no LUID)
- *
* @return the icon
*/
- private ImageIcon generateCoverIcon(MetaData meta) {
+ private ImageIcon generateCoverIcon() {
BufferedImage resizedImage = null;
- String id = null;
-
- String key = meta.getUuid();
- if (key == null) {
- // a fake meta (a source)
- key = "source_" + meta.getSource();
- }
+ String id = getIconId(meta);
- id = key + ".thumb_" + SPINE_WIDTH + "x" + COVER_WIDTH + "+"
- + SPINE_HEIGHT + "+" + COVER_HEIGHT + "@" + HOFFSET;
InputStream in = Instance.getCache().getFromCache(id);
if (in != null) {
try {
return new ImageIcon(resizedImage);
}
+
+ /**
+ * Manually clear the icon set for this item.
+ *
+ * @param meta
+ * the meta of the story or source (if luid is null)
+ */
+ public static void clearIcon(MetaData meta) {
+ String id = getIconId(meta);
+ Instance.getCache().removeFromCache(id);
+ }
+
+ /**
+ * Get a unique ID from this meta (note that if the luid is null, it is
+ * considered a source and not a {@link Story}).
+ *
+ * @param meta
+ * the meta
+ * @return the unique ID
+ */
+ private static String getIconId(MetaData meta) {
+ String id = null;
+
+ String key = meta.getUuid();
+ if (meta.getLuid() == null) {
+ // a fake meta (== a source)
+ key = "source_" + meta.getSource();
+ }
+
+ id = key + ".thumb_" + SPINE_WIDTH + "x" + COVER_WIDTH + "+"
+ + SPINE_HEIGHT + "+" + COVER_HEIGHT + "@" + HOFFSET;
+
+ return id;
+ }
}
reader.clearLocalReaderCache(selectedBook.getMeta()
.getLuid());
selectedBook.setCached(false);
+ GuiReaderBook.clearIcon(selectedBook.getMeta());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
reader.getLibrary().setSourceCover(
selectedBook.getMeta().getSource(),
selectedBook.getMeta().getLuid());
+ MetaData source = selectedBook.getMeta().clone();
+ source.setLuid(null);
+ GuiReaderBook.clearIcon(source);
}
}
});