1 package be
.nikiroo
.fanfix
;
4 import java
.io
.IOException
;
7 import be
.nikiroo
.fanfix
.bundles
.Config
;
8 import be
.nikiroo
.fanfix
.bundles
.ConfigBundle
;
9 import be
.nikiroo
.fanfix
.bundles
.StringId
;
10 import be
.nikiroo
.fanfix
.bundles
.StringIdBundle
;
11 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
12 import be
.nikiroo
.fanfix
.bundles
.UiConfigBundle
;
13 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
14 import be
.nikiroo
.fanfix
.library
.CacheLibrary
;
15 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
16 import be
.nikiroo
.fanfix
.library
.RemoteLibrary
;
17 import be
.nikiroo
.utils
.Cache
;
18 import be
.nikiroo
.utils
.IOUtils
;
19 import be
.nikiroo
.utils
.TempFiles
;
20 import be
.nikiroo
.utils
.TraceHandler
;
21 import be
.nikiroo
.utils
.resources
.Bundles
;
24 * Global state for the program (services and singletons).
28 public class Instance
{
29 private static ConfigBundle config
;
30 private static UiConfigBundle uiconfig
;
31 private static StringIdBundle trans
;
32 private static DataLoader cache
;
33 private static BasicLibrary lib
;
34 private static File coverDir
;
35 private static File readerTmp
;
36 private static File remoteDir
;
37 private static String configDir
;
38 private static TraceHandler tracer
;
39 private static TempFiles tempFiles
;
42 // Before we can configure it:
43 tracer
= new TraceHandler(true, checkEnv("DEBUG"), checkEnv("DEBUG"));
46 configDir
= getConfigDir();
47 if (!new File(configDir
).exists()) {
48 new File(configDir
).mkdirs();
51 // Most of the rest is dependent upon this:
52 createConfigs(configDir
, false);
55 boolean debug
= Instance
.getConfig()
56 .getBoolean(Config
.DEBUG_ERR
, false);
57 boolean trace
= Instance
.getConfig().getBoolean(Config
.DEBUG_TRACE
,
60 if (checkEnv("DEBUG")) {
65 tracer
= new TraceHandler(true, debug
, trace
);
68 remoteDir
= new File(configDir
, "remote");
69 lib
= createDefaultLibrary(remoteDir
);
72 File tmp
= getFile(Config
.CACHE_DIR
);
74 // Could have used: System.getProperty("java.io.tmpdir")
75 tmp
= new File(configDir
, "tmp");
77 String ua
= config
.getString(Config
.USER_AGENT
);
79 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
80 int hoursLarge
= config
81 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
82 cache
= new DataLoader(tmp
, ua
, hours
, hoursLarge
);
83 } catch (IOException e
) {
84 tracer
.error(new IOException(
85 "Cannot create cache (will continue without cache)", e
));
86 cache
= new DataLoader(ua
);
89 cache
.setTraceHandler(tracer
);
91 // readerTmp / coverDir
92 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
93 if (readerTmp
== null) {
94 readerTmp
= new File(configDir
, "tmp-reader");
97 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
98 if (coverDir
!= null && !coverDir
.exists()) {
99 tracer
.error(new IOException(
100 "The 'default covers' directory does not exists: "
106 tempFiles
= new TempFiles("fanfix");
107 } catch (IOException e
) {
108 tracer
.error(new IOException("Cannot create temporary directory", e
));
113 * The traces handler for this {@link Cache}.
117 * @return the traces handler (never NULL)
119 public static TraceHandler
getTraceHandler() {
124 * The traces handler for this {@link Cache}.
127 * the new traces handler or NULL
129 public static void setTraceHandler(TraceHandler tracer
) {
130 if (tracer
== null) {
131 tracer
= new TraceHandler(false, false, false);
134 Instance
.tracer
= tracer
;
135 cache
.setTraceHandler(tracer
);
139 * Get the (unique) configuration service for the program.
141 * @return the configuration service
143 public static ConfigBundle
getConfig() {
148 * Get the (unique) UI configuration service for the program.
150 * @return the configuration service
152 public static UiConfigBundle
getUiConfig() {
157 * Reset the configuration.
160 * also reset the translation files
162 public static void resetConfig(boolean resetTrans
) {
163 String dir
= Bundles
.getDirectory();
164 Bundles
.setDirectory(null);
167 ConfigBundle config
= new ConfigBundle();
168 config
.updateFile(configDir
);
169 } catch (IOException e
) {
173 UiConfigBundle uiconfig
= new UiConfigBundle();
174 uiconfig
.updateFile(configDir
);
175 } catch (IOException e
) {
181 StringIdBundle trans
= new StringIdBundle(null);
182 trans
.updateFile(configDir
);
183 } catch (IOException e
) {
188 Bundles
.setDirectory(dir
);
193 * Get the (unique) {@link DataLoader} for the program.
195 * @return the {@link DataLoader}
197 public static DataLoader
getCache() {
202 * Get the (unique) {link StringIdBundle} for the program.
204 * @return the {link StringIdBundle}
206 public static StringIdBundle
getTrans() {
211 * Get the (unique) {@link LocalLibrary} for the program.
213 * @return the {@link LocalLibrary}
215 public static BasicLibrary
getLibrary() {
217 throw new NullPointerException("We don't have a library to return");
224 * Return the directory where to look for default cover pages.
226 * @return the default covers directory
228 public static File
getCoverDir() {
233 * Return the directory where to store temporary files for the local reader.
235 * @return the directory
237 public static File
getReaderDir() {
242 * Return the directory where to store temporary files for the remote
243 * {@link LocalLibrary}.
246 * the remote for this host
248 * @return the directory
250 public static File
getRemoteDir(String host
) {
251 return getRemoteDir(remoteDir
, host
);
255 * Return the directory where to store temporary files for the remote
256 * {@link LocalLibrary}.
259 * the base remote directory
261 * the remote for this host
263 * @return the directory
265 private static File
getRemoteDir(File remoteDir
, String host
) {
269 return new File(remoteDir
, host
);
276 * Check if we need to check that a new version of Fanfix is available.
278 * @return TRUE if we need to
280 public static boolean isVersionCheckNeeded() {
282 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 1) * 24 * 60
285 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
287 long delay
= new Date().getTime()
288 - Long
.parseLong(lastUpString
);
295 } catch (Exception e
) {
296 // No file or bad file:
304 * Notify that we checked for a new version of Fanfix.
306 public static void setVersionChecked() {
308 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
309 Long
.toString(new Date().getTime()));
310 } catch (IOException e
) {
316 * The facility to use temporary files in this program.
318 * <b>MUST</b> be closed at end of program.
320 * @return the facility
322 public static TempFiles
getTempFiles() {
327 * The configuration directory (will check, in order of preference,
328 * {@link Bundles#getDirectory()}, the system properties, the environment
329 * and then defaults to $HOME/.fanfix).
331 * @return the config directory
333 private static String
getConfigDir() {
334 String configDir
= Bundles
.getDirectory();
336 if (configDir
== null) {
337 configDir
= System
.getProperty("CONFIG_DIR");
340 if (configDir
== null) {
341 configDir
= System
.getenv("CONFIG_DIR");
344 if (configDir
== null) {
345 configDir
= new File(getHome(), ".fanfix").getPath();
352 * Create the config variables ({@link Instance#config},
353 * {@link Instance#uiconfig} and {@link Instance#trans}).
356 * the directory where to find the configuration files
358 * TRUE to reset the configuration files from the default
361 private static void createConfigs(String configDir
, boolean refresh
) {
363 Bundles
.setDirectory(configDir
);
367 config
= new ConfigBundle();
368 config
.updateFile(configDir
);
369 } catch (IOException e
) {
374 uiconfig
= new UiConfigBundle();
375 uiconfig
.updateFile(configDir
);
376 } catch (IOException e
) {
380 // No updateFile for this one! (we do not want the user to have custom
381 // translations that won't accept updates from newer versions)
382 trans
= new StringIdBundle(getLang());
384 // Fix an old bug (we used to store custom translation files by
386 if (trans
.getString(StringId
.INPUT_DESC_CBZ
) == null) {
387 trans
.deleteFile(configDir
);
390 if (checkEnv("NOUTF")) {
391 trans
.setUnicode(false);
394 Bundles
.setDirectory(configDir
);
398 * Create the default library as specified by the config.
401 * the base remote directory if needed
403 * @return the default {@link BasicLibrary}
405 private static BasicLibrary
createDefaultLibrary(File remoteDir
) {
406 BasicLibrary lib
= null;
408 String remoteLib
= config
.getString(Config
.DEFAULT_LIBRARY
);
409 if (remoteLib
== null || remoteLib
.trim().isEmpty()) {
410 String libDir
= System
.getProperty("fanfix.libdir");
411 if (libDir
== null || libDir
.isEmpty()) {
412 libDir
= config
.getString(Config
.LIBRARY_DIR
);
415 lib
= new LocalLibrary(getFile(libDir
));
416 } catch (Exception e
) {
417 tracer
.error(new IOException(
418 "Cannot create library for directory: "
419 + getFile(libDir
), e
));
422 int pos
= remoteLib
.lastIndexOf(":");
424 String port
= remoteLib
.substring(pos
+ 1).trim();
425 remoteLib
= remoteLib
.substring(0, pos
);
426 pos
= remoteLib
.lastIndexOf(":");
428 String host
= remoteLib
.substring(pos
+ 1).trim();
429 String key
= remoteLib
.substring(0, pos
).trim();
432 tracer
.trace("Selecting remote library " + host
+ ":"
434 lib
= new RemoteLibrary(key
, host
,
435 Integer
.parseInt(port
));
436 lib
= new CacheLibrary(getRemoteDir(remoteDir
, host
),
439 } catch (Exception e
) {
445 tracer
.error(new IOException(
446 "Cannot create remote library for: " + remoteLib
));
454 * Return a path, but support the special $HOME variable.
458 private static File
getFile(Config id
) {
459 return getFile(config
.getString(id
));
463 * Return a path, but support the special $HOME variable.
467 private static File
getFile(UiConfig id
) {
468 return getFile(uiconfig
.getString(id
));
472 * Return a path, but support the special $HOME variable.
476 private static File
getFile(String path
) {
478 if (path
!= null && !path
.isEmpty()) {
479 path
= path
.replace('/', File
.separatorChar
);
480 if (path
.contains("$HOME")) {
481 path
= path
.replace("$HOME", getHome());
484 file
= new File(path
);
491 * Return the home directory from the system properties.
495 private static String
getHome() {
496 String home
= System
.getProperty("fanfix.home");
497 if (home
!= null && new File(home
).isFile()) {
501 if (home
== null || home
.trim().isEmpty()) {
502 home
= System
.getProperty("user.home");
503 if (!new File(home
).isDirectory()) {
508 if (home
== null || home
.trim().isEmpty()) {
509 home
= System
.getProperty("java.io.tmpdir");
510 if (!new File(home
).isDirectory()) {
523 * The language to use for the application (NULL = default system language).
525 * @return the language
527 private static String
getLang() {
528 String lang
= config
.getString(Config
.LANG
);
530 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
531 lang
= System
.getenv("LANG");
534 if (lang
!= null && lang
.isEmpty()) {
542 * Check that the given environment variable is "enabled".
545 * the variable to check
547 * @return TRUE if it is
549 private static boolean checkEnv(String key
) {
550 String value
= System
.getenv(key
);
552 value
= value
.trim().toLowerCase();
553 if ("yes".equals(value
) || "true".equals(value
)
554 || "on".equals(value
) || "1".equals(value
)
555 || "y".equals(value
)) {