Dependency fix + Local/Remote Library support
[fanfix.git] / src / be / nikiroo / fanfix / reader / LocalReader.java
index 29a840f3300d77c49ece90df927b23cc3d596a0f..593f58b2122685034d3ad7b34e4c230467e1ed0f 100644 (file)
@@ -13,19 +13,26 @@ import javax.swing.event.HyperlinkEvent;
 import javax.swing.event.HyperlinkListener;
 
 import be.nikiroo.fanfix.Instance;
-import be.nikiroo.fanfix.Library;
+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.utils.Progress;
 import be.nikiroo.utils.Version;
+import be.nikiroo.utils.ui.UIUtils;
 
 class LocalReader extends BasicReader {
-       private Library lib;
+       static private boolean nativeLookLoaded;
+
+       private LocalLibrary localLibrary;
 
        public LocalReader() throws IOException {
+               if (!nativeLookLoaded) {
+                       UIUtils.setLookAndFeel();
+                       nativeLookLoaded = true;
+               }
+
                File dir = Instance.getReaderDir();
                dir.mkdirs();
                if (!dir.exists()) {
@@ -59,7 +66,7 @@ class LocalReader extends BasicReader {
                                                        key, value), e);
                }
 
-               lib = new Library(dir, text, images);
+               localLibrary = new LocalLibrary(dir, text, images);
        }
 
        @Override
@@ -100,7 +107,7 @@ class LocalReader extends BasicReader {
                try {
                        Story story = Instance.getLibrary().getStory(luid, pgGetStory);
                        if (story != null) {
-                               story = lib.save(story, luid, pgSave);
+                               story = localLibrary.save(story, luid, pgSave);
                        } else {
                                throw new IOException("Cannot find story in Library: " + luid);
                        }
@@ -111,29 +118,6 @@ class LocalReader extends BasicReader {
                }
        }
 
-       /**
-        * 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) {
-                       imprt(luid, pg);
-                       file = lib.getFile(luid);
-               }
-
-               return file;
-       }
-
        /**
         * Check if the {@link Story} denoted by this Library UID is present in the
         * {@link LocalReader} cache.
@@ -144,11 +128,11 @@ class LocalReader extends BasicReader {
         * @return TRUE if it is
         */
        public boolean isCached(String luid) {
-               return lib.getInfo(luid) != null;
+               return localLibrary.getInfo(luid) != null;
        }
 
        @Override
-       public void start(String type) {
+       public void browse(String type) {
                // TODO: improve presentation of update message
                final VersionCheck updates = VersionCheck.check();
                StringBuilder builder = new StringBuilder();
@@ -213,45 +197,40 @@ class LocalReader extends BasicReader {
 
        // delete from local reader library
        void clearLocalReaderCache(String luid) {
-               lib.delete(luid);
+               try {
+                       localLibrary.delete(luid);
+               } catch (IOException e) {
+                       Instance.syserr(e);
+               }
        }
 
        // delete from main library
        void delete(String luid) {
-               lib.delete(luid);
-               Instance.getLibrary().delete(luid);
+               try {
+                       localLibrary.delete(luid);
+                       Instance.getLibrary().delete(luid);
+               } catch (IOException e) {
+                       Instance.syserr(e);
+               }
        }
 
        // open the given book
        void open(String luid, Progress pg) throws IOException {
-               MetaData meta = Instance.getLibrary().getInfo(luid);
-               File target = getTarget(luid, pg);
-
-               String program = null;
-               if (meta.isImageDocument()) {
-                       program = Instance.getUiConfig().getString(
-                                       UiConfig.IMAGES_DOCUMENT_READER);
-               } else {
-                       program = Instance.getUiConfig().getString(
-                                       UiConfig.NON_IMAGES_DOCUMENT_READER);
-               }
-
-               if (program != null && program.trim().isEmpty()) {
-                       program = null;
+               File file = localLibrary.getFile(luid);
+               if (file == null) {
+                       imprt(luid, pg);
+                       file = localLibrary.getFile(luid);
                }
 
-               if (program == null) {
-                       try {
-                               Desktop.getDesktop().browse(target.toURI());
-                       } catch (UnsupportedOperationException e) {
-                               Runtime.getRuntime().exec(
-                                               new String[] { "xdg-open", target.getAbsolutePath() });
-
-                       }
-               } else {
-                       Runtime.getRuntime().exec(
-                                       new String[] { program, target.getAbsolutePath() });
+               open(getLibrary().getInfo(luid), file);
+       }
 
+       void changeType(String luid, String newType) {
+               try {
+                       localLibrary.changeSource(luid, newType, null);
+                       Instance.getLibrary().changeSource(luid, newType, null);
+               } catch (IOException e) {
+                       Instance.syserr(e);
                }
        }
 }