Some fixes: output types, libraries, remote
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / GuiReader.java
index cdf80d0c9fda7b3d02b0d4b7d045a011f877b551..410e05e56f0cd3a05338c8d37f6633e488cbb075 100644 (file)
@@ -13,11 +13,11 @@ import javax.swing.event.HyperlinkEvent;
 import javax.swing.event.HyperlinkListener;
 
 import be.nikiroo.fanfix.Instance;
-import be.nikiroo.fanfix.LocalLibrary;
 import be.nikiroo.fanfix.VersionCheck;
 import be.nikiroo.fanfix.bundles.UiConfig;
+import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
-import be.nikiroo.fanfix.output.BasicOutput.OutputType;
+import be.nikiroo.fanfix.library.LocalLibrary;
 import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.Version;
 import be.nikiroo.utils.ui.UIUtils;
@@ -40,48 +40,20 @@ class GuiReader extends BasicReader {
                                        "Cannote create cache directory for local reader: " + dir);
                }
 
-               OutputType text = null;
-               OutputType images = null;
-
-               try {
-                       text = OutputType.valueOfNullOkUC(Instance.getUiConfig().getString(
-                                       UiConfig.NON_IMAGES_DOCUMENT_TYPE));
-                       if (text == null) {
-                               text = OutputType.HTML;
-                       }
-
-                       images = OutputType.valueOfNullOkUC(Instance.getUiConfig()
-                                       .getString(UiConfig.IMAGES_DOCUMENT_TYPE));
-                       if (images == null) {
-                               images = OutputType.CBZ;
-                       }
-               } catch (Exception e) {
-                       UiConfig key = (text == null) ? UiConfig.NON_IMAGES_DOCUMENT_TYPE
-                                       : UiConfig.IMAGES_DOCUMENT_TYPE;
-                       String value = Instance.getUiConfig().getString(key);
-
-                       throw new IOException(
-                                       String.format(
-                                                       "The configuration option %s is not valid: %s",
-                                                       key, value), e);
-               }
-
-               localLibrary = new LocalLibrary(dir, text, images);
+               localLibrary = new LocalLibrary(dir, Instance.getUiConfig().getString(
+                               UiConfig.GUI_NON_IMAGES_DOCUMENT_TYPE), Instance.getUiConfig()
+                               .getString(UiConfig.GUI_IMAGES_DOCUMENT_TYPE), true);
        }
 
        @Override
        public void read() throws IOException {
-               if (getStory() == null) {
+               MetaData meta = getMeta();
+
+               if (meta == null) {
                        throw new IOException("No story to read");
                }
 
-               open(getStory().getMeta().getLuid(), null);
-       }
-
-       @Override
-       public void read(int chapter) throws IOException {
-               // TODO: show a special page?
-               read();
+               read(meta.getLuid(), null);
        }
 
        /**
@@ -96,21 +68,8 @@ class GuiReader 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, pgGetStory);
-                       if (story != null) {
-                               story = localLibrary.save(story, luid, pgSave);
-                       } else {
-                               throw new IOException("Cannot find story in Library: " + luid);
-                       }
+                       localLibrary.imprt(getLibrary(), luid, pg);
                } catch (IOException e) {
                        throw new IOException(
                                        "Cannot import story from library to LocalReader library: "
@@ -159,6 +118,7 @@ class GuiReader extends BasicReader {
 
                        // handle link events
                        updateMessage.addHyperlinkListener(new HyperlinkListener() {
+                               @Override
                                public void hyperlinkUpdate(HyperlinkEvent e) {
                                        if (e.getEventType().equals(
                                                        HyperlinkEvent.EventType.ACTIVATED))
@@ -177,6 +137,7 @@ class GuiReader extends BasicReader {
 
                final String typeFinal = type;
                EventQueue.invokeLater(new Runnable() {
+                       @Override
                        public void run() {
                                if (updates.isNewVersionAvailable()) {
                                        int rep = JOptionPane.showConfirmDialog(null,
@@ -189,8 +150,7 @@ class GuiReader extends BasicReader {
                                        }
                                }
 
-                               new GuiReaderFrame(GuiReader.this, typeFinal)
-                                               .setVisible(true);
+                               new GuiReaderFrame(GuiReader.this, typeFinal).setVisible(true);
                        }
                });
        }
@@ -207,28 +167,33 @@ class GuiReader extends BasicReader {
        // delete from main library
        void delete(String luid) {
                try {
-                       localLibrary.delete(luid);
-                       Instance.getLibrary().delete(luid);
+                       if (localLibrary.getInfo(luid) != null) {
+                               localLibrary.delete(luid);
+                       }
+                       getLibrary().delete(luid);
                } catch (IOException e) {
                        Instance.syserr(e);
                }
        }
 
        // open the given book
-       void open(String luid, Progress pg) throws IOException {
+       void read(String luid, Progress pg) throws IOException {
                File file = localLibrary.getFile(luid);
                if (file == null) {
                        imprt(luid, pg);
                        file = localLibrary.getFile(luid);
                }
 
-               open(getLibrary().getInfo(luid), file);
+               // TODO: show a special page for the chapter?
+               openExternal(getLibrary().getInfo(luid), file);
        }
 
-       void changeType(String luid, String newType) {
+       void changeType(String luid, String newSource) {
                try {
-                       localLibrary.changeSource(luid, newType, null);
-                       Instance.getLibrary().changeSource(luid, newType, null);
+                       if (localLibrary.getInfo(luid) != null) {
+                               localLibrary.changeSource(luid, newSource, null);
+                       }
+                       getLibrary().changeSource(luid, newSource, null);
                } catch (IOException e) {
                        Instance.syserr(e);
                }