Progress handling in BasicOutput
[fanfix.git] / src / be / nikiroo / fanfix / reader / LocalReader.java
index 26f48f5a21210fe4d6026b44d0e2bdae3cf3e2c1..a28d4feadeeeb4502da949661923619acc16d241 100644 (file)
@@ -6,11 +6,10 @@ import java.io.IOException;
 
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.Library;
-import be.nikiroo.fanfix.bundles.Config;
-import be.nikiroo.fanfix.data.MetaData;
+import be.nikiroo.fanfix.bundles.UiConfig;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
-import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
+import be.nikiroo.utils.ui.Progress;
 
 class LocalReader extends BasicReader {
        private Library lib;
@@ -24,14 +23,14 @@ class LocalReader extends BasicReader {
                }
 
                // TODO: can throw an exception, manage that (convert to IOEx ?)
-               OutputType text = OutputType.valueOfNullOkUC(Instance.getConfig()
-                               .getString(Config.LOCAL_READER_NON_IMAGES_DOCUMENT_TYPE));
+               OutputType text = OutputType.valueOfNullOkUC(Instance.getUiConfig()
+                               .getString(UiConfig.LOCAL_READER_NON_IMAGES_DOCUMENT_TYPE));
                if (text == null) {
                        text = OutputType.HTML;
                }
 
-               OutputType images = OutputType.valueOfNullOkUC(Instance.getConfig()
-                               .getString(Config.LOCAL_READER_IMAGES_DOCUMENT_TYPE));
+               OutputType images = OutputType.valueOfNullOkUC(Instance.getUiConfig()
+                               .getString(UiConfig.LOCAL_READER_IMAGES_DOCUMENT_TYPE));
                if (images == null) {
                        images = OutputType.CBZ;
                }
@@ -48,36 +47,66 @@ class LocalReader extends BasicReader {
        public void read(int chapter) {
        }
 
-       // return new luid
-       public String imprt(String luid) {
+       /**
+        * Import the story into the local reader library, and keep the same LUID.
+        * 
+        * @param luid
+        *            the Library UID
+        * @param pg
+        *            the optional progress reporter
+        * 
+        * @throws IOException
+        *             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);
-                       story = lib.save(story);
-                       return story.getMeta().getLuid();
+                       Story story = Instance.getLibrary().getStory(luid, pgGetStory);
+                       if (story != null) {
+                               story = lib.save(story, luid, pgSave);
+                       } else {
+                               throw new IOException("Cannot find story in Library: " + luid);
+                       }
                } catch (IOException e) {
-                       Instance.syserr(new IOException(
+                       throw new IOException(
                                        "Cannot import story from library to LocalReader library: "
-                                                       + luid, e));
+                                                       + luid, e);
                }
-
-               return null;
        }
 
-       public File getTarget(String luid) {
-               MetaData meta = lib.getInfo(luid);
+       /**
+        * Get the target file related to this {@link Story}.
+        * 
+        * @param luid
+        *            the LUID of the {@link Story}
+        * @param pg
+        *            the optional progress reporter
+        * 
+        * @return the target file
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public File getTarget(String luid, Progress pg) throws IOException {
                File file = lib.getFile(luid);
                if (file == null) {
-                       luid = imprt(luid);
+                       imprt(luid, pg);
                        file = lib.getFile(luid);
-                       meta = lib.getInfo(luid);
                }
 
                return file;
        }
 
        @Override
-       public void start(SupportType type) {
-               final SupportType typeFinal = type;
+       public void start(String type) {
+               final String typeFinal = type;
                EventQueue.invokeLater(new Runnable() {
                        public void run() {
                                new LocalReaderFrame(LocalReader.this, typeFinal)
@@ -85,8 +114,4 @@ class LocalReader extends BasicReader {
                        }
                });
        }
-
-       public static void main(String[] args) throws IOException {
-               new LocalReader().start(null);
-       }
 }