gui: allow 'all' and 'listing' for sources and authors
[fanfix.git] / src / be / nikiroo / fanfix / library / BasicLibrary.java
index 29a3cf97efc162fb86f7035585e853fbbc25ce0a..8ec4e5620655ad148fb6b4b47b2c16d0f4284ee8 100644 (file)
@@ -7,6 +7,8 @@ import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
 
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
@@ -17,6 +19,7 @@ import be.nikiroo.fanfix.supported.BasicSupport;
 import be.nikiroo.fanfix.supported.SupportType;
 import be.nikiroo.utils.Image;
 import be.nikiroo.utils.Progress;
+import be.nikiroo.utils.StringUtils;
 
 /**
  * Manage a library of Stories: import, export, list, modify.
@@ -155,8 +158,8 @@ abstract public class BasicLibrary {
         * 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);
        }
 
        /**
@@ -168,7 +171,7 @@ abstract public class BasicLibrary {
         *            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
@@ -243,6 +246,47 @@ abstract public class BasicLibrary {
                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.
         * 
@@ -261,10 +305,130 @@ abstract public class BasicLibrary {
                return list;
        }
 
+       /**
+        * Return the list of authors, grouped by starting letter(s) if needed.
+        * <p>
+        * If the number of author is not too high, only one group with an empty
+        * name and all the authors will be returned.
+        * <p>
+        * If not, the authors will be separated into groups:
+        * <ul>
+        * <li><tt>*</tt>: any author whose name doesn't contain letters nor numbers
+        * </li>
+        * <li><tt>0-9</tt>: any authors whose name starts with a number</li>
+        * <li><tt>A-C</tt> (for instance): any author whose name starts with
+        * <tt>A</tt>, <tt>B</tt> or <tt>C</tt></li>
+        * </ul>
+        * Note that the letters used in the groups can vary (except <tt>*</tt> and
+        * <tt>0-9</tt>, which may only be present or not).
+        * 
+        * @return the authors' names, grouped by letter(s)
+        */
+       public Map<String, List<String>> getAuthorsGrouped() {
+               int MAX = 20;
+
+               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.put("", authors);
+                       return groups;
+               }
+
+               // Create groups A to Z, which can be empty here
+               for (char car = 'A'; car <= 'Z'; car++) {
+                       groups.put(Character.toString(car), getAuthorsGroup(authors, car));
+               }
+
+               // 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 = keyNow.charAt(0) + "-"
+                                               + keyNext.charAt(keyNext.length() - 1);
+
+                               List<String> all = new ArrayList<String>();
+                               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)
+                       }
+               }
+
+               // 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);
+                       }
+               }
+
+               return groups;
+       }
+
+       /**
+        * Get all the authors that start with the given character:
+        * <ul>
+        * <li><tt>*</tt>: any author whose name doesn't contain letters nor numbers
+        * </li>
+        * <li><tt>0</tt>: any authors whose name starts with a number</li>
+        * <li><tt>A</tt> (any capital latin letter): any author whose name starts
+        * with <tt>A</tt></li>
+        * </ul>
+        * 
+        * @param authors
+        *            the full list of authors
+        * @param car
+        *            the starting character, <tt>*</tt>, <tt>0</tt> or a capital
+        *            letter
+        * @return the authors that fulfill the starting letter
+        */
+       private List<String> getAuthorsGroup(List<String> authors, char car) {
+               List<String> accepted = new ArrayList<String>();
+               for (String author : authors) {
+                       char first = '*';
+                       for (int i = 0; first == '*' && i < author.length(); i++) {
+                               String san = StringUtils.sanitize(author, true, true);
+                               char c = san.charAt(i);
+                               if (c >= '0' && c <= '9') {
+                                       first = '0';
+                               } else if (c >= 'a' && c <= 'z') {
+                                       first = (char) (c - 'a' + 'A');
+                               } else if (c >= 'A' && c <= 'Z') {
+                                       first = c;
+                               }
+                       }
+
+                       if (first == car) {
+                               accepted.add(author);
+                       }
+               }
+
+               return accepted;
+       }
+
        /**
         * List all the stories in the {@link BasicLibrary}.
         * <p>
-        * Cover images not included.
+        * Cover images <b>MAYBE</b> not included.
         * 
         * @return the stories
         */
@@ -594,7 +758,7 @@ abstract public class BasicLibrary {
                                this.getClass().getSimpleName() + ": deleting story " + luid);
 
                doDelete(luid);
-               deleteInfo(luid);
+               invalidateInfo(luid);
 
                Instance.getTraceHandler().trace(
                                this.getClass().getSimpleName() + ": story deleted (" + luid
@@ -621,7 +785,83 @@ abstract public class BasicLibrary {
                        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);
        }
 
@@ -632,7 +872,7 @@ abstract public class BasicLibrary {
         * 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)
@@ -658,8 +898,8 @@ abstract public class BasicLibrary {
                        throw new IOException("Story not found: " + meta.getLuid());
                }
 
+               // TODO: this is not safe!
                delete(meta.getLuid());
-
                story.setMeta(meta);
                save(story, meta.getLuid(), pgSet);