From bee7dffea9ef8d3312eee90993a9faaeac6c1127 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sat, 18 Feb 2017 20:10:35 +0100 Subject: [PATCH] Progress handling in BasicOutput --- src/be/nikiroo/fanfix/Library.java | 24 +++++++--- src/be/nikiroo/fanfix/Main.java | 11 ++++- src/be/nikiroo/fanfix/output/BasicOutput.java | 46 +++++++++++++++++-- src/be/nikiroo/fanfix/reader/LocalReader.java | 12 ++++- 4 files changed, 79 insertions(+), 14 deletions(-) diff --git a/src/be/nikiroo/fanfix/Library.java b/src/be/nikiroo/fanfix/Library.java index 03b584c..3b39743 100644 --- a/src/be/nikiroo/fanfix/Library.java +++ b/src/be/nikiroo/fanfix/Library.java @@ -217,17 +217,25 @@ public class Library { */ public File export(String luid, OutputType type, String target, Progress pg) throws IOException { + Progress pgGetStory = new Progress(); + Progress pgOut = new Progress(); + if (pg != null) { + pg.setMax(2); + pg.addProgress(pgGetStory, 1); + pg.addProgress(pgOut, 1); + } + BasicOutput out = BasicOutput.getOutput(type, true); if (out == null) { throw new IOException("Output type not supported: " + type); } - Story story = getStory(luid, pg); + Story story = getStory(luid, pgGetStory); if (story == null) { throw new IOException("Cannot find story to export: " + luid); } - return out.process(story, target); + return out.process(story, target, pgOut); } /** @@ -235,14 +243,16 @@ public class Library { * * @param story * the {@link Story} to save + * @param pg + * the optional progress reporter * * @return the same {@link Story}, whose LUID may have changed * * @throws IOException * in case of I/O error */ - public Story save(Story story) throws IOException { - return save(story, null); + public Story save(Story story, Progress pg) throws IOException { + return save(story, null, pg); } /** @@ -253,13 +263,15 @@ public class Library { * the {@link Story} to save * @param luid * the correct LUID or NULL to get the next free one + * @param pg + * the optional progress reporter * * @return the same {@link Story}, whose LUID may have changed * * @throws IOException * in case of I/O error */ - public Story save(Story story, String luid) throws IOException { + public Story save(Story story, String luid, Progress pg) throws IOException { // Do not change the original metadata, but change the original story MetaData key = story.getMeta().clone(); story.setMeta(key); @@ -284,7 +296,7 @@ public class Library { } BasicOutput it = BasicOutput.getOutput(out, true); - File file = it.process(story, getFile(key).getPath()); + File file = it.process(story, getFile(key).getPath(), pg); getStories().put(story.getMeta(), file); return story; diff --git a/src/be/nikiroo/fanfix/Main.java b/src/be/nikiroo/fanfix/Main.java index e6f584f..09635da 100644 --- a/src/be/nikiroo/fanfix/Main.java +++ b/src/be/nikiroo/fanfix/Main.java @@ -411,12 +411,19 @@ public class Main { BasicSupport support = BasicSupport.getSupport(source); if (support != null) { - Story story = support.process(source, pg); + Progress pgIn = new Progress(); + Progress pgOut = new Progress(); + if (pg != null) { + pg.setMax(2); + pg.addProgress(pgIn, 1); + pg.addProgress(pgOut, 1); + } + Story story = support.process(source, pgIn); try { target = new File(target).getAbsolutePath(); BasicOutput.getOutput(type, infoCover).process( - story, target); + story, target, pgOut); } catch (IOException e) { Instance.syserr(new IOException(trans( StringId.ERR_SAVING, target), e)); diff --git a/src/be/nikiroo/fanfix/output/BasicOutput.java b/src/be/nikiroo/fanfix/output/BasicOutput.java index 33a42c5..1342eb0 100644 --- a/src/be/nikiroo/fanfix/output/BasicOutput.java +++ b/src/be/nikiroo/fanfix/output/BasicOutput.java @@ -2,13 +2,16 @@ package be.nikiroo.fanfix.output; import java.io.File; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.bundles.StringId; import be.nikiroo.fanfix.data.Chapter; import be.nikiroo.fanfix.data.Paragraph; -import be.nikiroo.fanfix.data.Story; import be.nikiroo.fanfix.data.Paragraph.ParagraphType; +import be.nikiroo.fanfix.data.Story; +import be.nikiroo.utils.ui.Progress; /** * This class is the base class used by the other output classes. It can be used @@ -135,6 +138,8 @@ public abstract class BasicOutput { private OutputType type; private boolean writeCover; private boolean writeInfo; + private Progress storyPg; + private Progress chapPg; /** * Process the {@link Story} into the given target. @@ -144,6 +149,8 @@ public abstract class BasicOutput { * @param target * the target where to save to (will not necessary be taken as is * by the processor, for instance an extension can be added) + * @param pg + * the optional progress reporter * * @return the actual main target saved, which can be slightly different * that the input one @@ -151,7 +158,10 @@ public abstract class BasicOutput { * @throws IOException * in case of I/O error */ - public File process(Story story, String target) throws IOException { + public File process(Story story, String target, Progress pg) + throws IOException { + storyPg = pg; + target = new File(target).getAbsolutePath(); File targetDir = new File(target).getParentFile(); String targetName = new File(target).getName(); @@ -180,6 +190,7 @@ public abstract class BasicOutput { * the target filename (will not necessary be taken as is by the * processor, for instance an extension can be added) * + * * @return the actual main target saved, which can be slightly different * that the input one * @@ -253,6 +264,12 @@ public abstract class BasicOutput { } protected void writeStory(Story story) throws IOException { + if (storyPg == null) { + storyPg = new Progress(0, story.getChapters().size() + 2); + } else { + storyPg.setMinMax(0, story.getChapters().size() + 2); + } + String chapterNameNum = String.format("%03d", 0); String paragraphNumber = String.format("%04d", 0); imageName = paragraphNumber + "_" + chapterNameNum + ".png"; @@ -268,11 +285,28 @@ public abstract class BasicOutput { InfoCover.writeInfo(targetDir, targetName, story.getMeta()); } - writeStoryHeader(story); + storyPg.setProgress(1); + + List chapPgs = new ArrayList(story.getChapters() + .size()); for (Chapter chap : story) { - writeChapter(chap); + chapPg = new Progress(0, chap.getParagraphs().size()); + storyPg.addProgress(chapPg, 1); + chapPgs.add(chapPg); + chapPg = null; + } + + writeStoryHeader(story); + for (int i = 0; i < story.getChapters().size(); i++) { + chapPg = chapPgs.get(i); + writeChapter(story.getChapters().get(i)); + chapPg.setProgress(chapPg.getMax()); + chapPg = null; } writeStoryFooter(story); + + storyPg.setProgress(storyPg.getMax()); + storyPg = null; } protected void writeChapter(Chapter chap) throws IOException { @@ -289,10 +323,14 @@ public abstract class BasicOutput { imageName = chapterNameNum + "_" + paragraphNumber + ".png"; writeChapterHeader(chap); + int i = 1; for (Paragraph para : chap) { paragraphNumber = String.format("%04d", num++); imageName = chapterNameNum + "_" + paragraphNumber + ".png"; writeParagraph(para); + if (chapPg != null) { + chapPg.setProgress(i++); + } } writeChapterFooter(chap); } diff --git a/src/be/nikiroo/fanfix/reader/LocalReader.java b/src/be/nikiroo/fanfix/reader/LocalReader.java index 88ffc5d..a28d4fe 100644 --- a/src/be/nikiroo/fanfix/reader/LocalReader.java +++ b/src/be/nikiroo/fanfix/reader/LocalReader.java @@ -59,10 +59,18 @@ class LocalReader extends BasicReader { * in case of I/O error */ public void imprt(String luid, Progress pg) throws IOException { + Progress pgGetStory = new Progress(); + Progress pgSave = new Progress(); + if (pg != null) { + pg.setMax(2); + pg.addProgress(pgGetStory, 1); + pg.addProgress(pgSave, 1); + } + try { - Story story = Instance.getLibrary().getStory(luid, pg); + Story story = Instance.getLibrary().getStory(luid, pgGetStory); if (story != null) { - story = lib.save(story, luid); + story = lib.save(story, luid, pgSave); } else { throw new IOException("Cannot find story in Library: " + luid); } -- 2.27.0