X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FBasicLibrary.java;h=7f7a09dd528d63ce0322150de77c1c4bf683c4a8;hb=aaeabf3a939ce517e3044a0a8ee782f19f3f930d;hp=b3b49fdbce50ec5aa6b4877af9c42b2a949e39df;hpb=c1b93db3213970c86b766c92a0b0d546c9d1c2ae;p=fanfix.git
diff --git a/src/be/nikiroo/fanfix/library/BasicLibrary.java b/src/be/nikiroo/fanfix/library/BasicLibrary.java
index b3b49fd..7f7a09d 100644
--- a/src/be/nikiroo/fanfix/library/BasicLibrary.java
+++ b/src/be/nikiroo/fanfix/library/BasicLibrary.java
@@ -4,12 +4,10 @@ import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.UnknownHostException;
-import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
-import java.util.Map.Entry;
import java.util.TreeMap;
import be.nikiroo.fanfix.Instance;
@@ -121,6 +119,31 @@ abstract public class BasicLibrary {
return null;
}
+ /**
+ * Return the cover image associated to this author.
+ *
+ * By default, return the custom cover if any, and if not, return the cover
+ * of the first story with this author.
+ *
+ * @param author
+ * the author
+ *
+ * @return the cover image or NULL
+ */
+ public Image getAuthorCover(String author) {
+ Image custom = getCustomAuthorCover(author);
+ if (custom != null) {
+ return custom;
+ }
+
+ List metas = getListByAuthor(author);
+ if (metas.size() > 0) {
+ return getCover(metas.get(0).getLuid());
+ }
+
+ return null;
+ }
+
/**
* Return the custom cover image associated to this source.
*
@@ -136,7 +159,21 @@ abstract public class BasicLibrary {
}
/**
- * Fix the source cover to the given story cover.
+ * Return the custom cover image associated to this author.
+ *
+ * By default, return NULL.
+ *
+ * @param author
+ * the author to look for
+ *
+ * @return the custom cover or NULL if none
+ */
+ public Image getCustomAuthorCover(@SuppressWarnings("unused") String author) {
+ return null;
+ }
+
+ /**
+ * Set the source cover to the given story cover.
*
* @param source
* the source to change
@@ -145,6 +182,16 @@ abstract public class BasicLibrary {
*/
public abstract void setSourceCover(String source, String luid);
+ /**
+ * Set the author cover to the given story cover.
+ *
+ * @param author
+ * the author to change
+ * @param luid
+ * the story LUID
+ */
+ public abstract void setAuthorCover(String author, String luid);
+
/**
* Return the list of stories (represented by their {@link MetaData}, which
* MAY not have the cover included).
@@ -326,48 +373,60 @@ abstract public class BasicLibrary {
*
* @return the authors' names, grouped by letter(s)
*/
- public List>> getAuthorsGrouped() {
+ public Map> getAuthorsGrouped() {
int MAX = 20;
- List>> groups = new ArrayList>>();
+ Map> groups = new TreeMap>();
List authors = getAuthors();
+ // If all authors fit the max, just report them as is
if (authors.size() <= MAX) {
- groups.add(new SimpleEntry>("", authors));
+ groups.put("", authors);
return groups;
}
- groups.add(new SimpleEntry>("*", getAuthorsGroup(
- authors, '*')));
- groups.add(new SimpleEntry>("0-9",
- getAuthorsGroup(authors, '0')));
-
+ // Create groups A to Z, which can be empty here
for (char car = 'A'; car <= 'Z'; car++) {
- groups.add(new SimpleEntry>(Character
- .toString(car), getAuthorsGroup(authors, car)));
+ groups.put(Character.toString(car), getAuthorsGroup(authors, car));
}
- // do NOT collapse * and [0-9] with the rest
- for (int i = 2; i + 1 < groups.size(); i++) {
- Entry> now = groups.get(i);
- Entry> next = groups.get(i + 1);
- int currentTotal = now.getValue().size() + next.getValue().size();
+ // Collapse them
+ List keys = new ArrayList(groups.keySet());
+ for (int i = 0; i + 1 < keys.size(); i++) {
+ String keyNow = keys.get(i);
+ String keyNext = keys.get(i + 1);
+
+ List now = groups.get(keyNow);
+ List next = groups.get(keyNext);
+
+ int currentTotal = now.size() + next.size();
if (currentTotal <= MAX) {
- String key = now.getKey().charAt(0) + "-"
- + next.getKey().charAt(next.getKey().length() - 1);
+ String key = keyNow.charAt(0) + "-"
+ + keyNext.charAt(keyNext.length() - 1);
+
List all = new ArrayList();
- all.addAll(now.getValue());
- all.addAll(next.getValue());
- groups.set(i, new SimpleEntry>(key, all));
- groups.remove(i + 1);
- i--;
+ all.addAll(now);
+ all.addAll(next);
+
+ groups.remove(keyNow);
+ groups.remove(keyNext);
+ groups.put(key, all);
+
+ keys.set(i, key); // set the new key instead of key(i)
+ keys.remove(i + 1); // remove the next, consumed key
+ i--; // restart at key(i)
}
}
- for (int i = 0; i < groups.size(); i++) {
- if (groups.get(i).getValue().size() == 0) {
- groups.remove(i);
- i--;
+ // Add "special" groups
+ groups.put("*", getAuthorsGroup(authors, '*'));
+ groups.put("0-9", getAuthorsGroup(authors, '0'));
+
+ // Prune empty groups
+ keys = new ArrayList(groups.keySet());
+ for (String key : keys) {
+ if (groups.get(key).isEmpty()) {
+ groups.remove(key);
}
}
@@ -506,6 +565,43 @@ abstract public class BasicLibrary {
* @return the corresponding {@link Story} or NULL if not found
*/
public synchronized Story getStory(String luid, Progress pg) {
+ Progress pgMetas = new Progress();
+ Progress pgStory = new Progress();
+ if (pg != null) {
+ pg.setMinMax(0, 100);
+ pg.addProgress(pgMetas, 10);
+ pg.addProgress(pgStory, 90);
+ }
+
+ MetaData meta = null;
+ for (MetaData oneMeta : getMetas(pgMetas)) {
+ if (oneMeta.getLuid().equals(luid)) {
+ meta = oneMeta;
+ break;
+ }
+ }
+
+ pgMetas.done();
+
+ Story story = getStory(luid, meta, pgStory);
+ pgStory.done();
+
+ return story;
+ }
+
+ /**
+ * Retrieve a specific {@link Story}.
+ *
+ * @param luid
+ * the meta of the story
+ * @param pg
+ * the optional progress reporter
+ *
+ * @return the corresponding {@link Story} or NULL if not found
+ */
+ public synchronized Story getStory(String luid,
+ @SuppressWarnings("javadoc") MetaData meta, Progress pg) {
+
if (pg == null) {
pg = new Progress();
}
@@ -518,39 +614,33 @@ abstract public class BasicLibrary {
pg.addProgress(pgProcess, 1);
Story story = null;
- for (MetaData meta : getMetas(null)) {
- if (meta.getLuid().equals(luid)) {
- File file = getFile(luid, pgGet);
- pgGet.done();
- try {
- SupportType type = SupportType.valueOfAllOkUC(meta
- .getType());
- URL url = file.toURI().toURL();
- if (type != null) {
- story = BasicSupport.getSupport(type, url) //
- .process(pgProcess);
-
- // Because we do not want to clear the meta cache:
- meta.setCover(story.getMeta().getCover());
- meta.setResume(story.getMeta().getResume());
- story.setMeta(meta);
- //
- } else {
- throw new IOException("Unknown type: " + meta.getType());
- }
- } catch (IOException e) {
- // We should not have not-supported files in the
- // library
- Instance.getTraceHandler().error(
- new IOException("Cannot load file from library: "
- + file, e));
- } finally {
- pgProcess.done();
- pg.done();
- }
-
- break;
+ File file = getFile(luid, pgGet);
+ pgGet.done();
+ try {
+ SupportType type = SupportType.valueOfAllOkUC(meta.getType());
+ URL url = file.toURI().toURL();
+ if (type != null) {
+ story = BasicSupport.getSupport(type, url) //
+ .process(pgProcess);
+
+ // Because we do not want to clear the meta cache:
+ meta.setCover(story.getMeta().getCover());
+ meta.setResume(story.getMeta().getResume());
+ story.setMeta(meta);
+ //
+ } else {
+ throw new IOException("Unknown type: " + meta.getType());
}
+ } catch (IOException e) {
+ // We should not have not-supported files in the
+ // library
+ Instance.getTraceHandler().error(
+ new IOException(String.format(
+ "Cannot load file of type '%s' from library: %s",
+ meta.getType(), file), e));
+ } finally {
+ pgProcess.done();
+ pg.done();
}
return story;
@@ -853,6 +943,8 @@ abstract public class BasicLibrary {
meta.setTitle(newTitle);
meta.setAuthor(newAuthor);
saveMeta(meta, pg);
+
+ invalidateInfo(luid);
}
/**