remove reader ui/tui
[nikiroo-utils.git] / src / be / nikiroo / fanfix / Instance.java
index f48d05b7d4cd84994eeb7e5e238018e99317afbd..c3c086fc9cd498ac3c61c84eebcf9e9bc831da1a 100644 (file)
@@ -48,8 +48,19 @@ public class Instance {
        /**
         * Initialise the instance -- if already initialised, nothing will happen.
         * <p>
-        * Before calling this method, you may call {@link Bundles#setDirectory(String)}
-        * if wanted.
+        * Before calling this method, you may call
+        * {@link Bundles#setDirectory(String)} if wanted.
+        * <p>
+        * Note that this method will honour some environment variables, the 3 most
+        * important ones probably being:
+        * <ul>
+        * <li><tt>DEBUG</tt>: will enable DEBUG output if set to 1 (or Y or TRUE or
+        * ON, case insensitive)</li>
+        * <li><tt>CONFIG_DIR</tt>: will use this directory as configuration
+        * directory (supports $HOME notation, defaults to $HOME/.fanfix</li>
+        * <li><tt>BOOKS_DIR</tt>: will use this directory as library directory
+        * (supports $HOME notation, defaults to $HOME/Books</li>
+        * </ul>
         */
        static public void init() {
                init(false);
@@ -136,10 +147,7 @@ public class Instance {
                lib = createDefaultLibrary(remoteDir);
 
                // create cache and TMP
-               File tmp = getFile(Config.CACHE_DIR, new File(configDir, "tmp"));
-               if (!tmp.isAbsolute()) {
-                       tmp = new File(configDir, tmp.getPath());
-               }
+               File tmp = getFile(Config.CACHE_DIR, configDir, "tmp");
                Image.setTemporaryFilesRoot(new File(tmp.getParent(), "tmp.images"));
 
                String ua = config.getString(Config.NETWORK_USER_AGENT, "");
@@ -155,9 +163,8 @@ public class Instance {
                cache.setTraceHandler(tracer);
 
                // readerTmp / coverDir
-               readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER, new File(configDir, "tmp-reader"));
-
-               coverDir = getFile(Config.DEFAULT_COVERS_DIR, new File(configDir, "covers"));
+               readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER, configDir, "tmp-reader");
+               coverDir = getFile(Config.DEFAULT_COVERS_DIR, configDir, "covers");
                coverDir.mkdirs();
 
                try {
@@ -277,9 +284,9 @@ public class Instance {
        }
 
        /**
-        * Get the (unique) {@link LocalLibrary} for the program.
+        * Get the (unique) {@link BasicLibrary} for the program.
         * 
-        * @return the {@link LocalLibrary}
+        * @return the {@link BasicLibrary}
         */
        public BasicLibrary getLibrary() {
                if (lib == null) {
@@ -289,6 +296,18 @@ public class Instance {
                return lib;
        }
 
+       /**
+        * Change the default {@link BasicLibrary} for this program.
+        * <p>
+        * Be careful.
+        * 
+        * @param lib
+        *            the new {@link BasicLibrary}
+        */
+       public void setLibrary(BasicLibrary lib) {
+               this.lib = lib;
+       }
+
        /**
         * Return the directory where to look for default cover pages.
         * 
@@ -465,7 +484,6 @@ public class Instance {
                BasicLibrary lib = null;
 
                boolean useRemote = config.getBoolean(Config.REMOTE_LIBRARY_ENABLED, false);
-
                if (useRemote) {
                        String host = null;
                        int port = -1;
@@ -483,15 +501,12 @@ public class Instance {
                } else {
                        String libDir = System.getenv("BOOKS_DIR");
                        if (libDir == null || libDir.isEmpty()) {
-                               libDir = config.getString(Config.LIBRARY_DIR, "$HOME/Books");
-                               if (!getFile(libDir).isAbsolute()) {
-                                       libDir = new File(configDir, libDir).getPath();
-                               }
+                               libDir = getFile(Config.LIBRARY_DIR, configDir, "$HOME/Books").getPath();
                        }
                        try {
-                               lib = new LocalLibrary(getFile(libDir), config);
+                               lib = new LocalLibrary(new File(libDir), config);
                        } catch (Exception e) {
-                               tracer.error(new IOException("Cannot create library for directory: " + getFile(libDir), e));
+                               tracer.error(new IOException("Cannot create library for directory: " + libDir, e));
                        }
                }
 
@@ -502,38 +517,43 @@ public class Instance {
         * Return a path, but support the special $HOME variable.
         * 
         * @param id  the key for the path, which may contain "$HOME"
-        * @param def the default value if none
+        * @param configDir the directory to use as base if not absolute
+        * @param def the default value if none (will be configDir-rooted if needed)
         * @return the path, with expanded "$HOME" if needed
         */
-       protected File getFile(Config id, File def) {
-               String path = config.getString(id, def.getPath());
-               return getFile(path);
+       protected File getFile(Config id, String configDir, String def) {
+               String path = config.getString(id, def);
+               return getFile(path, configDir);
        }
 
        /**
         * Return a path, but support the special $HOME variable.
         * 
         * @param id  the key for the path, which may contain "$HOME"
-        * @param def the default value if none
+        * @param configDir the directory to use as base if not absolute
+        * @param def the default value if none (will be configDir-rooted if needed)
         * @return the path, with expanded "$HOME" if needed
         */
-       protected File getFile(UiConfig id, File def) {
-               String path = uiconfig.getString(id, def.getPath());
-               return getFile(path);
+       protected File getFile(UiConfig id, String configDir, String def) {
+               String path = uiconfig.getString(id, def);
+               return getFile(path, configDir);
        }
 
        /**
         * Return a path, but support the special $HOME variable.
         * 
         * @param path the path, which may contain "$HOME"
+        * @param configDir the directory to use as base if not absolute
         * @return the path, with expanded "$HOME" if needed
         */
-       protected File getFile(String path) {
+       protected File getFile(String path, String configDir) {
                File file = null;
                if (path != null && !path.isEmpty()) {
                        path = path.replace('/', File.separatorChar);
                        if (path.contains("$HOME")) {
                                path = path.replace("$HOME", getHome());
+                       } else if (!path.startsWith("/")) {
+                               path = new File(configDir, path).getPath();
                        }
 
                        file = new File(path);