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.Entry;
+import java.util.Map;
+import java.util.TreeMap;
import be.nikiroo.fanfix.Instance;
import be.nikiroo.fanfix.data.MetaData;
return null;
}
+ /**
+ * Return the cover image associated to this author.
+ * <p>
+ * 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<MetaData> metas = getListByAuthor(author);
+ if (metas.size() > 0) {
+ return getCover(metas.get(0).getLuid());
+ }
+
+ return null;
+ }
+
/**
* Return the custom cover image associated to this source.
* <p>
}
/**
- * Fix the source cover to the given story cover.
+ * Return the custom cover image associated to this author.
+ * <p>
+ * 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
*/
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
* <b>MAY</b> not have the cover included).
* Invalidate the {@link Story} cache (when the content should be re-read
* because it was changed).
*/
- protected void deleteInfo() {
- deleteInfo(null);
+ protected void invalidateInfo() {
+ invalidateInfo(null);
}
/**
* the LUID of the {@link Story} to clear from the cache, or NULL
* for all stories
*/
- protected abstract void deleteInfo(String luid);
+ protected abstract void invalidateInfo(String luid);
/**
* Invalidate the {@link Story} cache (when the content has changed, but we
return list;
}
+ /**
+ * List all the known types (sources) of stories, grouped by directory
+ * ("Source_1/a" and "Source_1/b" will be grouped into "Source_1").
+ * <p>
+ * Note that an empty item in the list means a non-grouped source (type) --
+ * e.g., you could have for Source_1:
+ * <ul>
+ * <li><tt></tt>: empty, so source is "Source_1"</li>
+ * <li><tt>a</tt>: empty, so source is "Source_1/a"</li>
+ * <li><tt>b</tt>: empty, so source is "Source_1/b"</li>
+ * </ul>
+ *
+ * @return the grouped list
+ */
+ public synchronized Map<String, List<String>> getSourcesGrouped() {
+ Map<String, List<String>> map = new TreeMap<String, List<String>>();
+ for (String source : getSources()) {
+ String name;
+ String subname;
+
+ int pos = source.indexOf('/');
+ if (pos > 0 && pos < source.length() - 1) {
+ name = source.substring(0, pos);
+ subname = source.substring(pos + 1);
+
+ } else {
+ name = source;
+ subname = "";
+ }
+
+ List<String> list = map.get(name);
+ if (list == null) {
+ list = new ArrayList<String>();
+ map.put(name, list);
+ }
+ list.add(subname);
+ }
+
+ return map;
+ }
+
/**
* List all the known authors of stories.
*
*
* @return the authors' names, grouped by letter(s)
*/
- public List<Entry<String, List<String>>> getAuthorsGrouped() {
+ public Map<String, List<String>> getAuthorsGrouped() {
int MAX = 20;
- List<Entry<String, List<String>>> groups = new ArrayList<Entry<String, List<String>>>();
+ Map<String, List<String>> groups = new TreeMap<String, List<String>>();
List<String> authors = getAuthors();
+ // If all authors fit the max, just report them as is
if (authors.size() <= MAX) {
- groups.add(new SimpleEntry<String, List<String>>("", authors));
+ groups.put("", authors);
return groups;
}
- groups.add(new SimpleEntry<String, List<String>>("*", getAuthorsGroup(
- authors, '*')));
- groups.add(new SimpleEntry<String, List<String>>("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<String, List<String>>(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<String, List<String>> now = groups.get(i);
- Entry<String, List<String>> next = groups.get(i + 1);
- int currentTotal = now.getValue().size() + next.getValue().size();
+ // Collapse them
+ List<String> keys = new ArrayList<String>(groups.keySet());
+ for (int i = 0; i + 1 < keys.size(); i++) {
+ String keyNow = keys.get(i);
+ String keyNext = keys.get(i + 1);
+
+ List<String> now = groups.get(keyNow);
+ List<String> 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<String> all = new ArrayList<String>();
- all.addAll(now.getValue());
- all.addAll(next.getValue());
- groups.set(i, new SimpleEntry<String, List<String>>(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<String>(groups.keySet());
+ for (String key : keys) {
+ if (groups.get(key).isEmpty()) {
+ groups.remove(key);
}
}
/**
* List all the stories in the {@link BasicLibrary}.
* <p>
- * Cover images not included.
+ * Cover images <b>MAYBE</b> not included.
*
* @return the stories
*/
* @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();
}
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;
this.getClass().getSimpleName() + ": deleting story " + luid);
doDelete(luid);
- deleteInfo(luid);
+ invalidateInfo(luid);
Instance.getTraceHandler().trace(
this.getClass().getSimpleName() + ": story deleted (" + luid
throw new IOException("Story not found: " + luid);
}
+ changeSTA(luid, newSource, meta.getTitle(), meta.getAuthor(), pg);
+ }
+
+ /**
+ * Change the title (name) of the given {@link Story}.
+ *
+ * @param luid
+ * the {@link Story} LUID
+ * @param newTitle
+ * the new title
+ * @param pg
+ * the optional progress reporter
+ *
+ * @throws IOException
+ * in case of I/O error or if the {@link Story} was not found
+ */
+ public synchronized void changeTitle(String luid, String newTitle,
+ Progress pg) throws IOException {
+ MetaData meta = getInfo(luid);
+ if (meta == null) {
+ throw new IOException("Story not found: " + luid);
+ }
+
+ changeSTA(luid, meta.getSource(), newTitle, meta.getAuthor(), pg);
+ }
+
+ /**
+ * Change the author of the given {@link Story}.
+ *
+ * @param luid
+ * the {@link Story} LUID
+ * @param newAuthor
+ * the new author
+ * @param pg
+ * the optional progress reporter
+ *
+ * @throws IOException
+ * in case of I/O error or if the {@link Story} was not found
+ */
+ public synchronized void changeAuthor(String luid, String newAuthor,
+ Progress pg) throws IOException {
+ MetaData meta = getInfo(luid);
+ if (meta == null) {
+ throw new IOException("Story not found: " + luid);
+ }
+
+ changeSTA(luid, meta.getSource(), meta.getTitle(), newAuthor, pg);
+ }
+
+ /**
+ * Change the Source, Title and Author of the {@link Story} in one single
+ * go.
+ *
+ * @param luid
+ * the {@link Story} LUID
+ * @param newSource
+ * the new source
+ * @param newTitle
+ * the new title
+ * @param newAuthor
+ * the new author
+ * @param pg
+ * the optional progress reporter
+ *
+ * @throws IOException
+ * in case of I/O error or if the {@link Story} was not found
+ */
+ protected synchronized void changeSTA(String luid, String newSource,
+ String newTitle, String newAuthor, Progress pg) throws IOException {
+ MetaData meta = getInfo(luid);
+ if (meta == null) {
+ throw new IOException("Story not found: " + luid);
+ }
+
meta.setSource(newSource);
+ meta.setTitle(newTitle);
+ meta.setAuthor(newAuthor);
saveMeta(meta, pg);
}
* By default, delete the old {@link Story} then recreate a new
* {@link Story}.
* <p>
- * Note that this behaviour can lead to data loss.
+ * Note that this behaviour can lead to data loss in case of problems!
*
* @param meta
* the new {@link MetaData} (LUID <b>MUST NOT</b> change)
throw new IOException("Story not found: " + meta.getLuid());
}
+ // TODO: this is not safe!
delete(meta.getLuid());
-
story.setMeta(meta);
save(story, meta.getLuid(), pgSet);