New option: default library (for remote by default)
authorNiki Roo <niki@nikiroo.be>
Sat, 2 Dec 2017 15:14:44 +0000 (16:14 +0100)
committerNiki Roo <niki@nikiroo.be>
Sat, 2 Dec 2017 15:14:44 +0000 (16:14 +0100)
src/be/nikiroo/fanfix/Instance.java
src/be/nikiroo/fanfix/bundles/Config.java
src/be/nikiroo/fanfix/bundles/config.properties

index ef0ba15ec6ac4d1766e82a4265567f10a231253f..7ed5e25940ad94954c4d8abe9a906667fc4900d7 100644 (file)
@@ -12,6 +12,7 @@ import be.nikiroo.fanfix.bundles.UiConfig;
 import be.nikiroo.fanfix.bundles.UiConfigBundle;
 import be.nikiroo.fanfix.library.BasicLibrary;
 import be.nikiroo.fanfix.library.LocalLibrary;
+import be.nikiroo.fanfix.library.RemoteLibrary;
 import be.nikiroo.utils.Cache;
 import be.nikiroo.utils.IOUtils;
 import be.nikiroo.utils.TraceHandler;
@@ -27,7 +28,7 @@ public class Instance {
        private static UiConfigBundle uiconfig;
        private static StringIdBundle trans;
        private static DataLoader cache;
-       private static LocalLibrary lib;
+       private static BasicLibrary lib;
        private static File coverDir;
        private static File readerTmp;
        private static File remoteDir;
@@ -105,12 +106,40 @@ public class Instance {
 
                tracer = new TraceHandler(true, debug, trace);
 
-               try {
-                       lib = new LocalLibrary(getFile(Config.LIBRARY_DIR));
-               } catch (Exception e) {
-                       tracer.error(new IOException(
-                                       "Cannot create library for directory: "
-                                                       + getFile(Config.LIBRARY_DIR), e));
+               String remoteLib = config.getString(Config.DEFAULT_LIBRARY);
+               if (remoteLib == null || remoteLib.trim().isEmpty()) {
+                       try {
+                               lib = new LocalLibrary(getFile(Config.LIBRARY_DIR));
+                       } catch (Exception e) {
+                               tracer.error(new IOException(
+                                               "Cannot create library for directory: "
+                                                               + getFile(Config.LIBRARY_DIR), e));
+                       }
+               } else {
+                       int pos = remoteLib.lastIndexOf(":");
+                       if (pos >= 0) {
+                               String port = remoteLib.substring(pos + 1).trim();
+                               remoteLib = remoteLib.substring(0, pos);
+                               pos = remoteLib.lastIndexOf(":");
+                               if (pos >= 0) {
+                                       String host = remoteLib.substring(pos + 1).trim();
+                                       String key = remoteLib.substring(0, pos).trim();
+
+                                       try {
+                                               tracer.trace("Contacting remote library " + host + ":"
+                                                               + port);
+                                               lib = new RemoteLibrary(key, host,
+                                                               Integer.parseInt(port));
+                                       } catch (Exception e) {
+                                       }
+                               }
+                       }
+
+                       if (lib == null) {
+                               tracer.error(new IOException(
+                                               "Cannot create remote library for: "
+                                                               + getFile(Config.DEFAULT_LIBRARY)));
+                       }
                }
 
                // Could have used: System.getProperty("java.io.tmpdir")
index d2d189e57fafb46e4377799c29d1e62c5be8ffa4..e3283acbda3b8ec5b29c02f8b3628d0e143dcc8a 100644 (file)
@@ -30,6 +30,8 @@ public enum Config {
        USER_AGENT, //
        @Meta(description = "absolute path, $HOME variable supported, / is always accepted as dir separator", format = Format.DIRECTORY, info = "The directory where to get the default story covers")
        DEFAULT_COVERS_DIR, //
+       @Meta(description = "string", info = "The default library to use (KEY:SERVER:PORT), or empty for the local library")
+       DEFAULT_LIBRARY, //
        @Meta(description = "absolute path, $HOME variable supported, / is always accepted as dir separator", format = Format.DIRECTORY, info = "The directory where to store the library")
        LIBRARY_DIR, //
        @Meta(description = "boolean", format = Format.BOOLEAN, info = "Show debug information on errors")
index b6d8fa5f594146268277a0ae3dd221f7ced07eca..a30e7352c0adbb1457f217149bacbc6dfd621300 100644 (file)
@@ -31,6 +31,8 @@ USER_AGENT = Mozilla/5.0 (X11; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.
 # absolute path, $HOME variable supported, / is always accepted as dir separator
 # (FORMAT: DIRECTORY) The directory where to get the default story covers
 DEFAULT_COVERS_DIR = $HOME/bin/epub/
+# string (FORMAT: STRING) The default library to use (KEY:SERVER:PORT), or empty for the local library
+DEFAULT_LIBRARY = 
 # absolute path, $HOME variable supported, / is always accepted as dir separator
 # (FORMAT: DIRECTORY) The directory where to store the library
 LIBRARY_DIR = $HOME/Books