Merge branch 'subtree'
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderBookInfo.java
index 23d4c3160169e0244a1070720d376fc0aba06ab6..3cef8cfddcd48232875e64be1ee5315abfac9616 100644 (file)
@@ -1,10 +1,13 @@
 package be.nikiroo.fanfix.reader.ui;
 
+import java.io.IOException;
+
 import be.nikiroo.fanfix.bundles.StringIdGui;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.library.BasicLibrary;
 import be.nikiroo.utils.Image;
+import be.nikiroo.utils.StringUtils;
 
 /**
  * Some meta information related to a "book" (which can either be a
@@ -14,6 +17,12 @@ import be.nikiroo.utils.Image;
  * @author niki
  */
 public class GuiReaderBookInfo {
+       /**
+        * The type of {@link GuiReaderBook} (i.e., related to a story or to something else that
+        * can encompass stories).
+        * 
+        * @author niki
+        */
        public enum Type {
                /** A normal story, which can be "read". */
                STORY,
@@ -50,6 +59,15 @@ public class GuiReaderBookInfo {
                this.id = id;
                this.value = value;
        }
+       
+       /**
+        * The type of {@link GuiReaderBookInfo}.
+        * 
+        * @return the type
+        */
+       public Type getType() {
+               return type;
+       }
 
        /**
         * Get the main info to display for this book (a title, an author, a
@@ -122,11 +140,22 @@ public class GuiReaderBookInfo {
         *            the {@link BasicLibrary} to use to fetch the image
         * 
         * @return the base image
+        * 
+        * @throws IOException
+        *             in case of I/O error
         */
-       public Image getBaseImage(BasicLibrary lib) {
+       public Image getBaseImage(BasicLibrary lib) throws IOException {
                switch (type) {
                case STORY:
-                       return lib.getCover(meta.getLuid());
+                       if (meta.getCover() != null) {
+                               return meta.getCover();
+                       }
+
+                       if (meta.getLuid() != null) {
+                               return lib.getCover(meta.getLuid());
+                       }
+
+                       return null;
                case SOURCE:
                        return lib.getSourceCover(value);
                case AUTHOR:
@@ -149,12 +178,15 @@ public class GuiReaderBookInfo {
                if (uid == null || uid.trim().isEmpty()) {
                        uid = meta.getLuid();
                }
+               if (uid == null || uid.trim().isEmpty()) {
+                       uid = meta.getUrl();
+               }
 
                GuiReaderBookInfo info = new GuiReaderBookInfo(Type.STORY, uid,
                                meta.getTitle());
 
                info.meta = meta;
-               info.count = formatNumber(meta.getWords());
+               info.count = StringUtils.formatNumber(meta.getWords());
                if (!info.count.isEmpty()) {
                        info.count = GuiReader.trans(
                                        meta.isImageDocument() ? StringIdGui.BOOK_COUNT_IMAGES
@@ -179,7 +211,13 @@ public class GuiReaderBookInfo {
                GuiReaderBookInfo info = new GuiReaderBookInfo(Type.SOURCE, "source_"
                                + source, source);
 
-               info.count = formatNumber(lib.getListBySource(source).size());
+               int size = 0;
+               try {
+                       size = lib.getList().filter(source, null, null).size();
+               } catch (IOException e) {
+               }
+
+               info.count = StringUtils.formatNumber(size);
                if (!info.count.isEmpty()) {
                        info.count = GuiReader.trans(StringIdGui.BOOK_COUNT_STORIES,
                                        info.count);
@@ -203,7 +241,13 @@ public class GuiReaderBookInfo {
                GuiReaderBookInfo info = new GuiReaderBookInfo(Type.AUTHOR, "author_"
                                + author, author);
 
-               info.count = formatNumber(lib.getListByAuthor(author).size());
+               int size = 0;
+               try {
+                       size = lib.getList().filter(null, author, null).size();
+               } catch (IOException e) {
+               }
+
+               info.count = StringUtils.formatNumber(size);
                if (!info.count.isEmpty()) {
                        info.count = GuiReader.trans(StringIdGui.BOOK_COUNT_STORIES,
                                        info.count);
@@ -211,26 +255,4 @@ public class GuiReaderBookInfo {
 
                return info;
        }
-
-       /**
-        * Format a number for display (use the "k" notation if higher or equal to
-        * 4000).
-        * 
-        * @param number
-        *            the number to parse
-        * 
-        * @return the displayable version of the number
-        */
-       static private String formatNumber(long number) {
-               String displayNumber;
-               if (number >= 4000) {
-                       displayNumber = "" + (number / 1000) + "k";
-               } else if (number > 0) {
-                       displayNumber = "" + number;
-               } else {
-                       displayNumber = "";
-               }
-
-               return displayNumber;
-       }
 }