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
.Proxy
;
21 import be
.nikiroo
.utils
.TempFiles
;
22 import be
.nikiroo
.utils
.TraceHandler
;
23 import be
.nikiroo
.utils
.resources
.Bundles
;
26 * Global state for the program (services and singletons).
30 public class Instance
{
31 private static ConfigBundle config
;
32 private static UiConfigBundle uiconfig
;
33 private static StringIdBundle trans
;
34 private static DataLoader cache
;
35 private static StringIdGuiBundle transGui
;
36 private static BasicLibrary lib
;
37 private static File coverDir
;
38 private static File readerTmp
;
39 private static File remoteDir
;
40 private static String configDir
;
41 private static TraceHandler tracer
;
42 private static TempFiles tempFiles
;
45 // Before we can configure it:
46 Boolean debug
= checkEnv("DEBUG");
47 boolean trace
= debug
!= null && debug
;
48 tracer
= new TraceHandler(true, trace
, trace
);
51 configDir
= getConfigDir();
52 if (!new File(configDir
).exists()) {
53 new File(configDir
).mkdirs();
56 // Most of the rest is dependent upon this:
57 createConfigs(configDir
, false);
60 Proxy
.use(Instance
.getConfig().getString(Config
.USE_PROXY
));
64 debug
= Instance
.getConfig().getBoolean(Config
.DEBUG_ERR
, false);
65 trace
= Instance
.getConfig().getBoolean(Config
.DEBUG_TRACE
, false);
68 tracer
= new TraceHandler(true, debug
, trace
);
71 remoteDir
= new File(configDir
, "remote");
72 lib
= createDefaultLibrary(remoteDir
);
75 File tmp
= getFile(Config
.CACHE_DIR
);
77 // Could have used: System.getProperty("java.io.tmpdir")
78 tmp
= new File(configDir
, "tmp");
80 String ua
= config
.getString(Config
.USER_AGENT
);
82 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
83 int hoursLarge
= config
84 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
85 cache
= new DataLoader(tmp
, ua
, hours
, hoursLarge
);
86 } catch (IOException e
) {
87 tracer
.error(new IOException(
88 "Cannot create cache (will continue without cache)", e
));
89 cache
= new DataLoader(ua
);
92 cache
.setTraceHandler(tracer
);
94 // readerTmp / coverDir
95 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
96 if (readerTmp
== null) {
97 readerTmp
= new File(configDir
, "tmp-reader");
100 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
101 if (coverDir
!= null && !coverDir
.exists()) {
102 tracer
.error(new IOException(
103 "The 'default covers' directory does not exists: "
109 tempFiles
= new TempFiles("fanfix");
110 } catch (IOException e
) {
111 tracer
.error(new IOException("Cannot create temporary directory", e
));
116 * The traces handler for this {@link Cache}.
120 * @return the traces handler (never NULL)
122 public static TraceHandler
getTraceHandler() {
127 * The traces handler for this {@link Cache}.
130 * the new traces handler or NULL
132 public static void setTraceHandler(TraceHandler tracer
) {
133 if (tracer
== null) {
134 tracer
= new TraceHandler(false, false, false);
137 Instance
.tracer
= tracer
;
138 cache
.setTraceHandler(tracer
);
142 * Get the (unique) configuration service for the program.
144 * @return the configuration service
146 public static ConfigBundle
getConfig() {
151 * Get the (unique) UI configuration service for the program.
153 * @return the configuration service
155 public static UiConfigBundle
getUiConfig() {
160 * Reset the configuration.
163 * also reset the translation files
165 public static void resetConfig(boolean resetTrans
) {
166 String dir
= Bundles
.getDirectory();
167 Bundles
.setDirectory(null);
170 ConfigBundle config
= new ConfigBundle();
171 config
.updateFile(configDir
);
172 } catch (IOException e
) {
176 UiConfigBundle uiconfig
= new UiConfigBundle();
177 uiconfig
.updateFile(configDir
);
178 } catch (IOException e
) {
184 StringIdBundle trans
= new StringIdBundle(null);
185 trans
.updateFile(configDir
);
186 } catch (IOException e
) {
191 Bundles
.setDirectory(dir
);
196 * Get the (unique) {@link DataLoader} for the program.
198 * @return the {@link DataLoader}
200 public static DataLoader
getCache() {
205 * Get the (unique) {link StringIdBundle} for the program.
207 * This is used for the translations of the core parts of Fanfix.
209 * @return the {link StringIdBundle}
211 public static StringIdBundle
getTrans() {
216 * Get the (unique) {link StringIdGuiBundle} for the program.
218 * This is used for the translations of the GUI parts of Fanfix.
220 * @return the {link StringIdGuiBundle}
222 public static StringIdGuiBundle
getTransGui() {
227 * Get the (unique) {@link LocalLibrary} for the program.
229 * @return the {@link LocalLibrary}
231 public static BasicLibrary
getLibrary() {
233 throw new NullPointerException("We don't have a library to return");
240 * Return the directory where to look for default cover pages.
242 * @return the default covers directory
244 public static File
getCoverDir() {
249 * Return the directory where to store temporary files for the local reader.
251 * @return the directory
253 public static File
getReaderDir() {
258 * Return the directory where to store temporary files for the remote
259 * {@link LocalLibrary}.
262 * the remote for this host
264 * @return the directory
266 public static File
getRemoteDir(String host
) {
267 return getRemoteDir(remoteDir
, host
);
271 * Return the directory where to store temporary files for the remote
272 * {@link LocalLibrary}.
275 * the base remote directory
277 * the remote for this host
279 * @return the directory
281 private static File
getRemoteDir(File remoteDir
, String host
) {
285 return new File(remoteDir
, host
);
292 * Check if we need to check that a new version of Fanfix is available.
294 * @return TRUE if we need to
296 public static boolean isVersionCheckNeeded() {
298 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 1) * 24 * 60
301 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
303 long delay
= new Date().getTime()
304 - Long
.parseLong(lastUpString
);
311 } catch (Exception e
) {
312 // No file or bad file:
320 * Notify that we checked for a new version of Fanfix.
322 public static void setVersionChecked() {
324 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
325 Long
.toString(new Date().getTime()));
326 } catch (IOException e
) {
332 * The facility to use temporary files in this program.
334 * <b>MUST</b> be closed at end of program.
336 * @return the facility
338 public static TempFiles
getTempFiles() {
343 * The configuration directory (will check, in order of preference,
344 * {@link Bundles#getDirectory()}, the system properties, the environment
345 * and then defaults to $HOME/.fanfix).
347 * @return the config directory
349 private static String
getConfigDir() {
350 String configDir
= Bundles
.getDirectory();
352 if (configDir
== null) {
353 configDir
= System
.getProperty("CONFIG_DIR");
356 if (configDir
== null) {
357 configDir
= System
.getenv("CONFIG_DIR");
360 if (configDir
== null) {
361 configDir
= new File(getHome(), ".fanfix").getPath();
368 * Create the config variables ({@link Instance#config},
369 * {@link Instance#uiconfig}, {@link Instance#trans} and
370 * {@link Instance#transGui}).
373 * the directory where to find the configuration files
375 * TRUE to reset the configuration files from the default
378 private static void createConfigs(String configDir
, boolean refresh
) {
380 Bundles
.setDirectory(configDir
);
384 config
= new ConfigBundle();
385 config
.updateFile(configDir
);
386 } catch (IOException e
) {
391 uiconfig
= new UiConfigBundle();
392 uiconfig
.updateFile(configDir
);
393 } catch (IOException e
) {
397 // No updateFile for this one! (we do not want the user to have custom
398 // translations that won't accept updates from newer versions)
399 trans
= new StringIdBundle(getLang());
400 transGui
= new StringIdGuiBundle(getLang());
402 // Fix an old bug (we used to store custom translation files by
404 if (trans
.getString(StringId
.INPUT_DESC_CBZ
) == null) {
405 trans
.deleteFile(configDir
);
408 Boolean noutf
= checkEnv("NOUTF");
409 if (noutf
!= null && noutf
) {
410 trans
.setUnicode(false);
411 transGui
.setUnicode(false);
414 Bundles
.setDirectory(configDir
);
418 * Create the default library as specified by the config.
421 * the base remote directory if needed
423 * @return the default {@link BasicLibrary}
425 private static BasicLibrary
createDefaultLibrary(File remoteDir
) {
426 BasicLibrary lib
= null;
428 String remoteLib
= config
.getString(Config
.DEFAULT_LIBRARY
);
429 if (remoteLib
== null || remoteLib
.trim().isEmpty()) {
430 String libDir
= System
.getProperty("fanfix.libdir");
431 if (libDir
== null || libDir
.isEmpty()) {
432 libDir
= config
.getString(Config
.LIBRARY_DIR
);
435 lib
= new LocalLibrary(getFile(libDir
));
436 } catch (Exception e
) {
437 tracer
.error(new IOException(
438 "Cannot create library for directory: "
439 + getFile(libDir
), e
));
442 int pos
= remoteLib
.lastIndexOf(":");
444 String port
= remoteLib
.substring(pos
+ 1).trim();
445 remoteLib
= remoteLib
.substring(0, pos
);
446 pos
= remoteLib
.lastIndexOf(":");
448 String host
= remoteLib
.substring(pos
+ 1).trim();
449 String key
= remoteLib
.substring(0, pos
).trim();
452 tracer
.trace("Selecting remote library " + host
+ ":"
454 lib
= new RemoteLibrary(key
, host
,
455 Integer
.parseInt(port
));
456 lib
= new CacheLibrary(getRemoteDir(remoteDir
, host
),
459 } catch (Exception e
) {
465 tracer
.error(new IOException(
466 "Cannot create remote library for: " + remoteLib
));
474 * Return a path, but support the special $HOME variable.
478 private static File
getFile(Config id
) {
479 return getFile(config
.getString(id
));
483 * Return a path, but support the special $HOME variable.
487 private static File
getFile(UiConfig id
) {
488 return getFile(uiconfig
.getString(id
));
492 * Return a path, but support the special $HOME variable.
496 private static File
getFile(String path
) {
498 if (path
!= null && !path
.isEmpty()) {
499 path
= path
.replace('/', File
.separatorChar
);
500 if (path
.contains("$HOME")) {
501 path
= path
.replace("$HOME", getHome());
504 file
= new File(path
);
511 * Return the home directory from the system properties.
515 private static String
getHome() {
516 String home
= System
.getProperty("fanfix.home");
517 if (home
!= null && new File(home
).isFile()) {
521 if (home
== null || home
.trim().isEmpty()) {
522 home
= System
.getProperty("user.home");
523 if (!new File(home
).isDirectory()) {
528 if (home
== null || home
.trim().isEmpty()) {
529 home
= System
.getProperty("java.io.tmpdir");
530 if (!new File(home
).isDirectory()) {
543 * The language to use for the application (NULL = default system language).
545 * @return the language
547 private static String
getLang() {
548 String lang
= config
.getString(Config
.LANG
);
550 if (lang
== null | lang
.isEmpty()) {
551 if (System
.getenv("LANG") != null
552 && !System
.getenv("LANG").isEmpty()) {
553 lang
= System
.getenv("LANG");
557 if (lang
!= null && lang
.isEmpty()) {
565 * Check that the given environment variable is "enabled".
568 * the variable to check
570 * @return TRUE if it is
572 private static Boolean
checkEnv(String key
) {
573 String value
= System
.getenv(key
);
575 value
= value
.trim().toLowerCase();
576 if ("yes".equals(value
) || "true".equals(value
)
577 || "on".equals(value
) || "1".equals(value
)
578 || "y".equals(value
)) {