manage remote and io exception in fanfix
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderBookInfo.java
index eb6dfbf36294a1f8711b173b394d25187222ba8a..f071be02e59d559991317a57c193ab8e04e2fb81 100644 (file)
@@ -1,5 +1,7 @@
 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;
@@ -123,11 +125,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:
@@ -150,12 +163,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
@@ -180,7 +196,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.getListBySource(source).size();
+               } catch (IOException e) {
+               }
+
+               info.count = StringUtils.formatNumber(size);
                if (!info.count.isEmpty()) {
                        info.count = GuiReader.trans(StringIdGui.BOOK_COUNT_STORIES,
                                        info.count);
@@ -204,7 +226,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.getListByAuthor(author).size();
+               } catch (IOException e) {
+               }
+
+               info.count = StringUtils.formatNumber(size);
                if (!info.count.isEmpty()) {
                        info.count = GuiReader.trans(StringIdGui.BOOK_COUNT_STORIES,
                                        info.count);
@@ -212,30 +240,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
-        * 
-        * @deprecated use {@link StringUtils} after update instead
-        */
-       @Deprecated
-       static private String formatNumber(long number) {
-               // TODO: replace with StringUtils after update
-               String displayNumber;
-               if (number >= 4000) {
-                       displayNumber = "" + (number / 1000) + "k";
-               } else if (number > 0) {
-                       displayNumber = "" + number;
-               } else {
-                       displayNumber = "";
-               }
-
-               return displayNumber;
-       }
 }