+ /**
+ * The facility to use temporary files in this program.
+ * <p>
+ * <b>MUST</b> be closed at end of program.
+ *
+ * @return the facility
+ */
+ public static TempFiles getTempFiles() {
+ return tempFiles;
+ }
+
+ /**
+ * The configuration directory (will check, in order of preference, the
+ * system properties, the environment and then defaults to
+ * {@link Instance#getHome()}/.fanfix).
+ *
+ * @return the config directory
+ */
+ private static String getConfigDir() {
+ String configDir = System.getProperty("CONFIG_DIR");
+
+ if (configDir == null) {
+ configDir = System.getenv("CONFIG_DIR");
+ }
+
+ if (configDir == null) {
+ configDir = new File(getHome(), ".fanfix").getPath();
+ }
+
+ return configDir;
+ }
+
+ /**
+ * Create the config variables ({@link Instance#config},
+ * {@link Instance#uiconfig}, {@link Instance#trans} and
+ * {@link Instance#transGui}).
+ *
+ * @param configDir
+ * the directory where to find the configuration files
+ * @param refresh
+ * TRUE to reset the configuration files from the default
+ * included ones
+ */
+ private static void createConfigs(String configDir, boolean refresh) {
+ if (!refresh) {
+ Bundles.setDirectory(configDir);
+ }
+
+ try {
+ config = new ConfigBundle();
+ config.updateFile(configDir);
+ } catch (IOException e) {
+ tracer.error(e);
+ }
+
+ try {
+ uiconfig = new UiConfigBundle();
+ uiconfig.updateFile(configDir);
+ } catch (IOException e) {
+ tracer.error(e);
+ }
+
+ // No updateFile for this one! (we do not want the user to have custom
+ // translations that won't accept updates from newer versions)
+ trans = new StringIdBundle(getLang());
+ transGui = new StringIdGuiBundle(getLang());
+
+ // Fix an old bug (we used to store custom translation files by
+ // default):
+ if (trans.getString(StringId.INPUT_DESC_CBZ) == null) {
+ trans.deleteFile(configDir);
+ }
+
+ Boolean noutf = checkEnv("NOUTF");
+ if (noutf != null && noutf) {
+ trans.setUnicode(false);
+ transGui.setUnicode(false);
+ }
+
+ Bundles.setDirectory(configDir);
+ }
+
+ /**
+ * Create the default library as specified by the config.
+ *
+ * @param remoteDir
+ * the base remote directory if needed
+ *
+ * @return the default {@link BasicLibrary}
+ */
+ private static BasicLibrary createDefaultLibrary(File remoteDir) {
+ BasicLibrary lib = null;
+
+ String remoteLib = config.getString(Config.DEFAULT_LIBRARY);
+ if (remoteLib == null || remoteLib.trim().isEmpty()) {
+ 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();
+ }
+ }
+ try {
+ lib = new LocalLibrary(getFile(libDir));
+ } catch (Exception e) {
+ tracer.error(new IOException(
+ "Cannot create library for directory: "
+ + getFile(libDir), e));
+ }
+ } else {
+ Exception ex = null;
+ 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("Selecting remote library " + host + ":"
+ + port);
+ lib = new RemoteLibrary(key, host,
+ Integer.parseInt(port));
+ lib = new CacheLibrary(getRemoteDir(remoteDir, host),
+ lib);
+
+ } catch (Exception e) {
+ ex = e;
+ }
+ }
+ }
+
+ if (lib == null) {
+ tracer.error(new IOException(
+ "Cannot create remote library for: " + remoteLib, ex));
+ }
+ }
+
+ return lib;
+ }
+