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
.StringIdGuiBundle
;
12 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
13 import be
.nikiroo
.fanfix
.bundles
.UiConfigBundle
;
14 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
15 import be
.nikiroo
.fanfix
.library
.CacheLibrary
;
16 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
17 import be
.nikiroo
.fanfix
.library
.RemoteLibrary
;
18 import be
.nikiroo
.utils
.Cache
;
19 import be
.nikiroo
.utils
.IOUtils
;
20 import be
.nikiroo
.utils
.TempFiles
;
21 import be
.nikiroo
.utils
.TraceHandler
;
22 import be
.nikiroo
.utils
.resources
.Bundles
;
25 * Global state for the program (services and singletons).
29 public class Instance
{
30 private static ConfigBundle config
;
31 private static UiConfigBundle uiconfig
;
32 private static StringIdBundle trans
;
33 private static DataLoader cache
;
34 private static StringIdGuiBundle transGui
;
35 private static BasicLibrary lib
;
36 private static File coverDir
;
37 private static File readerTmp
;
38 private static File remoteDir
;
39 private static String configDir
;
40 private static TraceHandler tracer
;
41 private static TempFiles tempFiles
;
44 // Before we can configure it:
45 tracer
= new TraceHandler(true, checkEnv("DEBUG"), checkEnv("DEBUG"));
48 configDir
= getConfigDir();
49 if (!new File(configDir
).exists()) {
50 new File(configDir
).mkdirs();
53 // Most of the rest is dependent upon this:
54 createConfigs(configDir
, false);
57 // TODO: include new nikiroo-utils version
58 // Proxy.use(Instance.getConfig().getString(Config.USE_PROXY));
61 boolean debug
= Instance
.getConfig()
62 .getBoolean(Config
.DEBUG_ERR
, false);
63 boolean trace
= Instance
.getConfig().getBoolean(Config
.DEBUG_TRACE
,
66 if (checkEnv("DEBUG")) {
71 tracer
= new TraceHandler(true, debug
, trace
);
74 remoteDir
= new File(configDir
, "remote");
75 lib
= createDefaultLibrary(remoteDir
);
78 File tmp
= getFile(Config
.CACHE_DIR
);
80 // Could have used: System.getProperty("java.io.tmpdir")
81 tmp
= new File(configDir
, "tmp");
83 String ua
= config
.getString(Config
.USER_AGENT
);
85 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
86 int hoursLarge
= config
87 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
88 cache
= new DataLoader(tmp
, ua
, hours
, hoursLarge
);
89 } catch (IOException e
) {
90 tracer
.error(new IOException(
91 "Cannot create cache (will continue without cache)", e
));
92 cache
= new DataLoader(ua
);
95 cache
.setTraceHandler(tracer
);
97 // readerTmp / coverDir
98 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
99 if (readerTmp
== null) {
100 readerTmp
= new File(configDir
, "tmp-reader");
103 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
104 if (coverDir
!= null && !coverDir
.exists()) {
105 tracer
.error(new IOException(
106 "The 'default covers' directory does not exists: "
112 tempFiles
= new TempFiles("fanfix");
113 } catch (IOException e
) {
114 tracer
.error(new IOException("Cannot create temporary directory", e
));
119 * The traces handler for this {@link Cache}.
123 * @return the traces handler (never NULL)
125 public static TraceHandler
getTraceHandler() {
130 * The traces handler for this {@link Cache}.
133 * the new traces handler or NULL
135 public static void setTraceHandler(TraceHandler tracer
) {
136 if (tracer
== null) {
137 tracer
= new TraceHandler(false, false, false);
140 Instance
.tracer
= tracer
;
141 cache
.setTraceHandler(tracer
);
145 * Get the (unique) configuration service for the program.
147 * @return the configuration service
149 public static ConfigBundle
getConfig() {
154 * Get the (unique) UI configuration service for the program.
156 * @return the configuration service
158 public static UiConfigBundle
getUiConfig() {
163 * Reset the configuration.
166 * also reset the translation files
168 public static void resetConfig(boolean resetTrans
) {
169 String dir
= Bundles
.getDirectory();
170 Bundles
.setDirectory(null);
173 ConfigBundle config
= new ConfigBundle();
174 config
.updateFile(configDir
);
175 } catch (IOException e
) {
179 UiConfigBundle uiconfig
= new UiConfigBundle();
180 uiconfig
.updateFile(configDir
);
181 } catch (IOException e
) {
187 StringIdBundle trans
= new StringIdBundle(null);
188 trans
.updateFile(configDir
);
189 } catch (IOException e
) {
194 Bundles
.setDirectory(dir
);
199 * Get the (unique) {@link DataLoader} for the program.
201 * @return the {@link DataLoader}
203 public static DataLoader
getCache() {
208 * Get the (unique) {link StringIdBundle} for the program.
210 * This is used for the translations of the core parts of Fanfix.
212 * @return the {link StringIdBundle}
214 public static StringIdBundle
getTrans() {
219 * Get the (unique) {link StringIdGuiBundle} for the program.
221 * This is used for the translations of the GUI parts of Fanfix.
223 * @return the {link StringIdGuiBundle}
225 public static StringIdGuiBundle
getTransGui() {
230 * Get the (unique) {@link LocalLibrary} for the program.
232 * @return the {@link LocalLibrary}
234 public static BasicLibrary
getLibrary() {
236 throw new NullPointerException("We don't have a library to return");
243 * Return the directory where to look for default cover pages.
245 * @return the default covers directory
247 public static File
getCoverDir() {
252 * Return the directory where to store temporary files for the local reader.
254 * @return the directory
256 public static File
getReaderDir() {
261 * Return the directory where to store temporary files for the remote
262 * {@link LocalLibrary}.
265 * the remote for this host
267 * @return the directory
269 public static File
getRemoteDir(String host
) {
270 return getRemoteDir(remoteDir
, host
);
274 * Return the directory where to store temporary files for the remote
275 * {@link LocalLibrary}.
278 * the base remote directory
280 * the remote for this host
282 * @return the directory
284 private static File
getRemoteDir(File remoteDir
, String host
) {
288 return new File(remoteDir
, host
);
295 * Check if we need to check that a new version of Fanfix is available.
297 * @return TRUE if we need to
299 public static boolean isVersionCheckNeeded() {
301 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 1) * 24 * 60
304 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
306 long delay
= new Date().getTime()
307 - Long
.parseLong(lastUpString
);
314 } catch (Exception e
) {
315 // No file or bad file:
323 * Notify that we checked for a new version of Fanfix.
325 public static void setVersionChecked() {
327 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
328 Long
.toString(new Date().getTime()));
329 } catch (IOException e
) {
335 * The facility to use temporary files in this program.
337 * <b>MUST</b> be closed at end of program.
339 * @return the facility
341 public static TempFiles
getTempFiles() {
346 * The configuration directory (will check, in order of preference,
347 * {@link Bundles#getDirectory()}, the system properties, the environment
348 * and then defaults to $HOME/.fanfix).
350 * @return the config directory
352 private static String
getConfigDir() {
353 String configDir
= Bundles
.getDirectory();
355 if (configDir
== null) {
356 configDir
= System
.getProperty("CONFIG_DIR");
359 if (configDir
== null) {
360 configDir
= System
.getenv("CONFIG_DIR");
363 if (configDir
== null) {
364 configDir
= new File(getHome(), ".fanfix").getPath();
371 * Create the config variables ({@link Instance#config},
372 * {@link Instance#uiconfig}, {@link Instance#trans} and
373 * {@link Instance#transGui}).
376 * the directory where to find the configuration files
378 * TRUE to reset the configuration files from the default
381 private static void createConfigs(String configDir
, boolean refresh
) {
383 Bundles
.setDirectory(configDir
);
387 config
= new ConfigBundle();
388 config
.updateFile(configDir
);
389 } catch (IOException e
) {
394 uiconfig
= new UiConfigBundle();
395 uiconfig
.updateFile(configDir
);
396 } catch (IOException e
) {
400 // No updateFile for this one! (we do not want the user to have custom
401 // translations that won't accept updates from newer versions)
402 trans
= new StringIdBundle(getLang());
403 transGui
= new StringIdGuiBundle(getLang());
405 // Fix an old bug (we used to store custom translation files by
407 if (trans
.getString(StringId
.INPUT_DESC_CBZ
) == null) {
408 trans
.deleteFile(configDir
);
411 if (checkEnv("NOUTF")) {
412 trans
.setUnicode(false);
413 transGui
.setUnicode(false);
416 Bundles
.setDirectory(configDir
);
420 * Create the default library as specified by the config.
423 * the base remote directory if needed
425 * @return the default {@link BasicLibrary}
427 private static BasicLibrary
createDefaultLibrary(File remoteDir
) {
428 BasicLibrary lib
= null;
430 String remoteLib
= config
.getString(Config
.DEFAULT_LIBRARY
);
431 if (remoteLib
== null || remoteLib
.trim().isEmpty()) {
432 String libDir
= System
.getProperty("fanfix.libdir");
433 if (libDir
== null || libDir
.isEmpty()) {
434 libDir
= config
.getString(Config
.LIBRARY_DIR
);
437 lib
= new LocalLibrary(getFile(libDir
));
438 } catch (Exception e
) {
439 tracer
.error(new IOException(
440 "Cannot create library for directory: "
441 + getFile(libDir
), e
));
444 int pos
= remoteLib
.lastIndexOf(":");
446 String port
= remoteLib
.substring(pos
+ 1).trim();
447 remoteLib
= remoteLib
.substring(0, pos
);
448 pos
= remoteLib
.lastIndexOf(":");
450 String host
= remoteLib
.substring(pos
+ 1).trim();
451 String key
= remoteLib
.substring(0, pos
).trim();
454 tracer
.trace("Selecting remote library " + host
+ ":"
456 lib
= new RemoteLibrary(key
, host
,
457 Integer
.parseInt(port
));
458 lib
= new CacheLibrary(getRemoteDir(remoteDir
, host
),
461 } catch (Exception e
) {
467 tracer
.error(new IOException(
468 "Cannot create remote library for: " + remoteLib
));
476 * Return a path, but support the special $HOME variable.
480 private static File
getFile(Config id
) {
481 return getFile(config
.getString(id
));
485 * Return a path, but support the special $HOME variable.
489 private static File
getFile(UiConfig id
) {
490 return getFile(uiconfig
.getString(id
));
494 * Return a path, but support the special $HOME variable.
498 private static File
getFile(String path
) {
500 if (path
!= null && !path
.isEmpty()) {
501 path
= path
.replace('/', File
.separatorChar
);
502 if (path
.contains("$HOME")) {
503 path
= path
.replace("$HOME", getHome());
506 file
= new File(path
);
513 * Return the home directory from the system properties.
517 private static String
getHome() {
518 String home
= System
.getProperty("fanfix.home");
519 if (home
!= null && new File(home
).isFile()) {
523 if (home
== null || home
.trim().isEmpty()) {
524 home
= System
.getProperty("user.home");
525 if (!new File(home
).isDirectory()) {
530 if (home
== null || home
.trim().isEmpty()) {
531 home
= System
.getProperty("java.io.tmpdir");
532 if (!new File(home
).isDirectory()) {
545 * The language to use for the application (NULL = default system language).
547 * @return the language
549 private static String
getLang() {
550 String lang
= config
.getString(Config
.LANG
);
552 if (lang
== null | lang
.isEmpty()) {
553 if (System
.getenv("LANG") != null
554 && !System
.getenv("LANG").isEmpty()) {
555 lang
= System
.getenv("LANG");
559 if (lang
!= null && lang
.isEmpty()) {
567 * Check that the given environment variable is "enabled".
570 * the variable to check
572 * @return TRUE if it is
574 private static boolean checkEnv(String key
) {
575 String value
= System
.getenv(key
);
577 value
= value
.trim().toLowerCase();
578 if ("yes".equals(value
) || "true".equals(value
)
579 || "on".equals(value
) || "1".equals(value
)
580 || "y".equals(value
)) {