Improve temporary cache system
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / GuiReader.java
index 2c30ab6740ad49a46a070585fbe581326b75fc1f..8ff9d92072e76f6a29da2a7599decb0736e4c75b 100644 (file)
@@ -14,10 +14,11 @@ import javax.swing.event.HyperlinkListener;
 
 import be.nikiroo.fanfix.Instance;
 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.library.BasicLibrary;
+import be.nikiroo.fanfix.library.CacheLibrary;
 import be.nikiroo.fanfix.library.LocalLibrary;
-import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.Version;
 import be.nikiroo.utils.ui.UIUtils;
@@ -25,7 +26,9 @@ import be.nikiroo.utils.ui.UIUtils;
 class GuiReader extends BasicReader {
        static private boolean nativeLookLoaded;
 
-       private LocalLibrary localLibrary;
+       private CacheLibrary cacheLib;
+
+       private File cacheDir;
 
        public GuiReader() throws IOException {
                if (!nativeLookLoaded) {
@@ -33,87 +36,38 @@ class GuiReader extends BasicReader {
                        nativeLookLoaded = true;
                }
 
-               File dir = Instance.getReaderDir();
-               dir.mkdirs();
-               if (!dir.exists()) {
+               cacheDir = Instance.getReaderDir();
+               cacheDir.mkdirs();
+               if (!cacheDir.exists()) {
                        throw new IOException(
-                                       "Cannote create cache directory for local reader: " + dir);
+                                       "Cannote create cache directory for local reader: "
+                                                       + cacheDir);
                }
+       }
 
-               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;
+       @Override
+       public synchronized BasicLibrary getLibrary() {
+               if (cacheLib == null) {
+                       BasicLibrary lib = super.getLibrary();
+                       if (lib instanceof CacheLibrary) {
+                               cacheLib = (CacheLibrary) lib;
+                       } else {
+                               cacheLib = new CacheLibrary(cacheDir, lib);
                        }
-               } 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);
+               return cacheLib;
        }
 
+       @Override
        public void read() throws IOException {
-               if (getStory() == null) {
-                       throw new IOException("No story to read");
-               }
-
-               open(getStory().getMeta().getLuid(), null);
-       }
-
-       public void read(int chapter) throws IOException {
-               // TODO: show a special page?
-               read();
-       }
+               MetaData meta = getMeta();
 
-       /**
-        * 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);
+               if (meta == null) {
+                       throw new IOException("No story to read");
                }
 
-               try {
-                       Story story = getLibrary().getStory(luid, pgGetStory);
-                       if (story != null) {
-                               story = localLibrary.save(story, luid, pgSave);
-                       } else {
-                               throw new IOException("Cannot find story in Library: " + luid);
-                       }
-               } catch (IOException e) {
-                       throw new IOException(
-                                       "Cannot import story from library to LocalReader library: "
-                                                       + luid, e);
-               }
+               read(meta.getLuid(), null);
        }
 
        /**
@@ -126,9 +80,10 @@ class GuiReader extends BasicReader {
         * @return TRUE if it is
         */
        public boolean isCached(String luid) {
-               return localLibrary.getInfo(luid) != null;
+               return cacheLib.isCached(luid);
        }
 
+       @Override
        public void browse(String type) {
                // TODO: improve presentation of update message
                final VersionCheck updates = VersionCheck.check();
@@ -156,6 +111,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))
@@ -174,6 +130,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,
@@ -194,7 +151,7 @@ class GuiReader extends BasicReader {
        // delete from local reader library
        void clearLocalReaderCache(String luid) {
                try {
-                       localLibrary.delete(luid);
+                       cacheLib.clearFromCache(luid);
                } catch (IOException e) {
                        Instance.syserr(e);
                }
@@ -203,28 +160,23 @@ class GuiReader extends BasicReader {
        // delete from main library
        void delete(String luid) {
                try {
-                       localLibrary.delete(luid);
-                       getLibrary().delete(luid);
+                       cacheLib.delete(luid);
                } catch (IOException e) {
                        Instance.syserr(e);
                }
        }
 
        // open the given book
-       void open(String luid, Progress pg) throws IOException {
-               File file = localLibrary.getFile(luid);
-               if (file == null) {
-                       imprt(luid, pg);
-                       file = localLibrary.getFile(luid);
-               }
+       void read(String luid, Progress pg) throws IOException {
+               File file = cacheLib.getFile(luid, pg);
 
+               // 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);
-                       getLibrary().changeSource(luid, newType, null);
+                       cacheLib.changeSource(luid, newSource, null);
                } catch (IOException e) {
                        Instance.syserr(e);
                }