Version 1.1.0
[fanfix.git] / src / be / nikiroo / fanfix / supported / BasicSupport.java
index ed7c4db1455bbff75c39c2b1fa95369ba8ba3968..c47d05e608dd43de574aa69cc96ee677168eccee 100644 (file)
@@ -14,8 +14,6 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Scanner;
 
-import javax.imageio.ImageIO;
-
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.bundles.Config;
 import be.nikiroo.fanfix.bundles.StringId;
@@ -24,7 +22,9 @@ import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Paragraph;
 import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
 import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.utils.IOUtils;
 import be.nikiroo.utils.StringUtils;
+import be.nikiroo.utils.ui.Progress;
 
 /**
  * This class is the base class used by the other support classes. It can be
@@ -329,26 +329,42 @@ public abstract class BasicSupport {
         * 
         * @param url
         *            the story resource
+        * @param pg
+        *            the optional progress reporter
         * 
         * @return the {@link Story}
         * 
         * @throws IOException
         *             in case of I/O error
         */
-       public Story process(URL url) throws IOException {
+       public Story process(URL url, Progress pg) throws IOException {
+               if (pg == null) {
+                       pg = new Progress();
+               } else {
+                       pg.setMinMax(0, 100);
+               }
+
                setCurrentReferer(url);
 
+               pg.setProgress(1);
                try {
                        Story story = processMeta(url, false, true);
+                       pg.setProgress(10);
                        if (story == null) {
+                               pg.setProgress(100);
                                return null;
                        }
 
                        story.setChapters(new ArrayList<Chapter>());
 
                        List<Entry<String, URL>> chapters = getChapters(url, getInput());
+                       pg.setProgress(20);
+
                        int i = 1;
                        if (chapters != null) {
+                               Progress pgChaps = new Progress(0, chapters.size());
+                               pg.addProgress(pgChaps, 80);
+
                                for (Entry<String, URL> chap : chapters) {
                                        setCurrentReferer(chap.getValue());
                                        InputStream chapIn = Instance.getCache().open(
@@ -361,8 +377,11 @@ public abstract class BasicSupport {
                                                chapIn.close();
                                        }
 
+                                       pgChaps.setProgress(i);
                                        i++;
                                }
+                       } else {
+                               pg.setProgress(100);
                        }
 
                        return story;
@@ -466,7 +485,6 @@ public abstract class BasicSupport {
         */
        protected Chapter makeChapter(URL source, int number, String name,
                        String content) throws IOException {
-
                // Chapter name: process it correctly, then remove the possible
                // redundant "Chapter x: " in front of it
                String chapterName = processPara(name).getContent().trim();
@@ -514,7 +532,7 @@ public abstract class BasicSupport {
                                String line = scan.next().trim();
                                boolean image = false;
                                if (line.startsWith("[") && line.endsWith("]")) {
-                                       URL url = getImageUrl(source,
+                                       URL url = getImageUrl(this, source,
                                                        line.substring(1, line.length() - 1).trim());
                                        if (url != null) {
                                                paras.add(new Paragraph(url));
@@ -582,7 +600,7 @@ public abstract class BasicSupport {
                                && Instance.getCoverDir() != null) {
                        try {
                                File fileCover = new File(Instance.getCoverDir(), subject);
-                               return getImage(fileCover.toURI().toURL(), subject);
+                               return getImage(null, fileCover.toURI().toURL(), subject);
                        } catch (MalformedURLException e) {
                        }
                }
@@ -603,13 +621,13 @@ public abstract class BasicSupport {
                }
        }
 
-       static BufferedImage getImage(URL source, String line) {
-               URL url = getImageUrl(source, line);
+       static BufferedImage getImage(BasicSupport support, URL source, String line) {
+               URL url = getImageUrl(support, source, line);
                if (url != null) {
                        InputStream in = null;
                        try {
                                in = Instance.getCache().open(url, getSupport(url), true);
-                               return ImageIO.read(in);
+                               return IOUtils.toImage(in);
                        } catch (IOException e) {
                        } finally {
                                if (in != null) {
@@ -636,7 +654,7 @@ public abstract class BasicSupport {
         * @return the image URL if found, or NULL
         * 
         */
-       static URL getImageUrl(URL source, String line) {
+       static URL getImageUrl(BasicSupport support, URL source, String line) {
                URL url = null;
 
                if (line != null) {
@@ -645,11 +663,11 @@ public abstract class BasicSupport {
                        if (source != null) {
                                path = new File(source.getFile()).getParent();
                                try {
-                                       String urlBase = new File(new File(path), line.trim())
-                                                       .toURI().toURL().toString();
+                                       String basePath = new File(new File(path), line.trim())
+                                                       .getAbsolutePath();
                                        for (String ext : getImageExt(true)) {
-                                               if (new File(urlBase + ext).exists()) {
-                                                       url = new File(urlBase + ext).toURI().toURL();
+                                               if (new File(basePath + ext).exists()) {
+                                                       url = new File(basePath + ext).toURI().toURL();
                                                }
                                        }
                                } catch (Exception e) {
@@ -663,6 +681,7 @@ public abstract class BasicSupport {
                                        for (String ext : getImageExt(true)) {
                                                if (Instance.getCache().check(new URL(line + ext))) {
                                                        url = new URL(line + ext);
+                                                       break;
                                                }
                                        }
 
@@ -671,8 +690,7 @@ public abstract class BasicSupport {
                                                for (String ext : getImageExt(true)) {
                                                        try {
                                                                url = new URL(line + ext);
-                                                               Instance.getCache().refresh(url,
-                                                                               getSupport(url), true);
+                                                               Instance.getCache().refresh(url, support, true);
                                                                break;
                                                        } catch (IOException e) {
                                                                // no image with this ext
@@ -688,7 +706,7 @@ public abstract class BasicSupport {
                        // refresh the cached file
                        if (url != null) {
                                try {
-                                       Instance.getCache().refresh(url, getSupport(url), true);
+                                       Instance.getCache().refresh(url, support, true);
                                } catch (IOException e) {
                                        // woops, broken image
                                        url = null;
@@ -764,6 +782,22 @@ public abstract class BasicSupport {
                        boolean singleQ = line.startsWith("" + openQuote);
                        boolean doubleQ = line.startsWith("" + openDoubleQuote);
 
+                       // Do not try when more than one quote at a time
+                       // (some stories are not easily readable if we do)
+                       if (singleQ
+                                       && line.indexOf(closeQuote, 1) < line
+                                                       .lastIndexOf(closeQuote)) {
+                               newParas.add(para);
+                               return newParas;
+                       }
+                       if (doubleQ
+                                       && line.indexOf(closeDoubleQuote, 1) < line
+                                                       .lastIndexOf(closeDoubleQuote)) {
+                               newParas.add(para);
+                               return newParas;
+                       }
+                       //
+
                        if (!singleQ && !doubleQ) {
                                line = openDoubleQuote + line + closeDoubleQuote;
                                newParas.add(new Paragraph(ParagraphType.QUOTE, line));