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
.Image
;
21 import be
.nikiroo
.utils
.Proxy
;
22 import be
.nikiroo
.utils
.TempFiles
;
23 import be
.nikiroo
.utils
.TraceHandler
;
24 import be
.nikiroo
.utils
.resources
.Bundles
;
27 * Global state for the program (services and singletons).
31 public class Instance
{
32 private static ConfigBundle config
;
33 private static UiConfigBundle uiconfig
;
34 private static StringIdBundle trans
;
35 private static DataLoader cache
;
36 private static StringIdGuiBundle transGui
;
37 private static BasicLibrary lib
;
38 private static File coverDir
;
39 private static File readerTmp
;
40 private static File remoteDir
;
41 private static String configDir
;
42 private static TraceHandler tracer
;
43 private static TempFiles tempFiles
;
45 private static boolean init
;
48 * Initialise the instance -- if already initialised, nothing will happen.
50 * Before calling this method, you may call
51 * {@link Bundles#setDirectory(String)} if wanted.
53 static public void init() {
58 * Initialise the instance -- if already initialised, nothing will happen
59 * unless you pass TRUE to <tt>force</tt>.
61 * Before calling this method, you may call
62 * {@link Bundles#setDirectory(String)} if wanted.
64 * Note: forcing the initialisation can be dangerous, so make sure to only
65 * make it under controlled circumstances -- for instance, at the start of
66 * the program, you could call {@link Instance#init()}, change some settings
67 * because you want to force those settings (it will also forbid users to
68 * change them!) and then call {@link Instance#init(boolean)} with
69 * <tt>force</tt> set to TRUE.
72 * force the initialisation even if already initialised
74 static public void init(boolean force
) {
81 // Before we can configure it:
82 Boolean debug
= checkEnv("DEBUG");
83 boolean trace
= debug
!= null && debug
;
84 tracer
= new TraceHandler(true, trace
, trace
);
87 configDir
= getConfigDir();
88 if (!new File(configDir
).exists()) {
89 new File(configDir
).mkdirs();
92 // Most of the rest is dependent upon this:
93 createConfigs(configDir
, false);
96 Proxy
.use(Instance
.getConfig().getString(Config
.USE_PROXY
));
100 debug
= Instance
.getConfig().getBoolean(Config
.DEBUG_ERR
, false);
101 trace
= Instance
.getConfig().getBoolean(Config
.DEBUG_TRACE
, false);
104 tracer
= new TraceHandler(true, debug
, trace
);
107 remoteDir
= new File(configDir
, "remote");
108 lib
= createDefaultLibrary(remoteDir
);
110 // create cache and TMP
111 Image
.setTemporaryFilesRoot(new File(configDir
, "tmp.images"));
112 File tmp
= getFile(Config
.CACHE_DIR
);
114 // Could have used: System.getProperty("java.io.tmpdir")
115 tmp
= new File(configDir
, "tmp");
117 String ua
= config
.getString(Config
.USER_AGENT
, "");
119 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, 0);
120 int hoursLarge
= config
.getInteger(Config
.CACHE_MAX_TIME_STABLE
, 0);
121 cache
= new DataLoader(tmp
, ua
, hours
, hoursLarge
);
122 } catch (IOException e
) {
123 tracer
.error(new IOException(
124 "Cannot create cache (will continue without cache)", e
));
125 cache
= new DataLoader(ua
);
128 cache
.setTraceHandler(tracer
);
130 // readerTmp / coverDir
131 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
132 if (readerTmp
== null) {
133 readerTmp
= new File(configDir
, "tmp-reader");
136 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
137 if (coverDir
!= null && !coverDir
.exists()) {
138 tracer
.error(new IOException(
139 "The 'default covers' directory does not exists: "
145 tempFiles
= new TempFiles("fanfix");
146 } catch (IOException e
) {
147 tracer
.error(new IOException("Cannot create temporary directory", e
));
152 * The traces handler for this {@link Cache}.
156 * @return the traces handler (never NULL)
158 public static TraceHandler
getTraceHandler() {
163 * The traces handler for this {@link Cache}.
166 * the new traces handler or NULL
168 public static void setTraceHandler(TraceHandler tracer
) {
169 if (tracer
== null) {
170 tracer
= new TraceHandler(false, false, false);
173 Instance
.tracer
= tracer
;
174 cache
.setTraceHandler(tracer
);
178 * Get the (unique) configuration service for the program.
180 * @return the configuration service
182 public static ConfigBundle
getConfig() {
187 * Get the (unique) UI configuration service for the program.
189 * @return the configuration service
191 public static UiConfigBundle
getUiConfig() {
196 * Reset the configuration.
199 * also reset the translation files
201 public static void resetConfig(boolean resetTrans
) {
202 String dir
= Bundles
.getDirectory();
203 Bundles
.setDirectory(null);
206 ConfigBundle config
= new ConfigBundle();
207 config
.updateFile(configDir
);
208 } catch (IOException e
) {
212 UiConfigBundle uiconfig
= new UiConfigBundle();
213 uiconfig
.updateFile(configDir
);
214 } catch (IOException e
) {
220 StringIdBundle trans
= new StringIdBundle(null);
221 trans
.updateFile(configDir
);
222 } catch (IOException e
) {
227 Bundles
.setDirectory(dir
);
232 * Get the (unique) {@link DataLoader} for the program.
234 * @return the {@link DataLoader}
236 public static DataLoader
getCache() {
241 * Get the (unique) {link StringIdBundle} for the program.
243 * This is used for the translations of the core parts of Fanfix.
245 * @return the {link StringIdBundle}
247 public static StringIdBundle
getTrans() {
252 * Get the (unique) {link StringIdGuiBundle} for the program.
254 * This is used for the translations of the GUI parts of Fanfix.
256 * @return the {link StringIdGuiBundle}
258 public static StringIdGuiBundle
getTransGui() {
263 * Get the (unique) {@link LocalLibrary} for the program.
265 * @return the {@link LocalLibrary}
267 public static BasicLibrary
getLibrary() {
269 throw new NullPointerException("We don't have a library to return");
276 * Return the directory where to look for default cover pages.
278 * @return the default covers directory
280 public static File
getCoverDir() {
285 * Return the directory where to store temporary files for the local reader.
287 * @return the directory
289 public static File
getReaderDir() {
294 * Return the directory where to store temporary files for the remote
295 * {@link LocalLibrary}.
298 * the remote for this host
300 * @return the directory
302 public static File
getRemoteDir(String host
) {
303 return getRemoteDir(remoteDir
, host
);
307 * Return the directory where to store temporary files for the remote
308 * {@link LocalLibrary}.
311 * the base remote directory
313 * the remote for this host
315 * @return the directory
317 private static File
getRemoteDir(File remoteDir
, String host
) {
321 return new File(remoteDir
, host
);
328 * Check if we need to check that a new version of Fanfix is available.
330 * @return TRUE if we need to
332 public static boolean isVersionCheckNeeded() {
334 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 0) * 24 * 60
337 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
339 long delay
= new Date().getTime()
340 - Long
.parseLong(lastUpString
);
347 } catch (Exception e
) {
348 // No file or bad file:
356 * Notify that we checked for a new version of Fanfix.
358 public static void setVersionChecked() {
360 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
361 Long
.toString(new Date().getTime()));
362 } catch (IOException e
) {
368 * The facility to use temporary files in this program.
370 * <b>MUST</b> be closed at end of program.
372 * @return the facility
374 public static TempFiles
getTempFiles() {
379 * The configuration directory (will check, in order of preference,
380 * {@link Bundles#getDirectory()}, the system properties, the environment
381 * and then defaults to $HOME/.fanfix).
383 * @return the config directory
385 private static String
getConfigDir() {
386 String configDir
= Bundles
.getDirectory();
388 if (configDir
== null) {
389 configDir
= System
.getProperty("CONFIG_DIR");
392 if (configDir
== null) {
393 configDir
= System
.getenv("CONFIG_DIR");
396 if (configDir
== null) {
397 configDir
= new File(getHome(), ".fanfix").getPath();
404 * Create the config variables ({@link Instance#config},
405 * {@link Instance#uiconfig}, {@link Instance#trans} and
406 * {@link Instance#transGui}).
409 * the directory where to find the configuration files
411 * TRUE to reset the configuration files from the default
414 private static void createConfigs(String configDir
, boolean refresh
) {
416 Bundles
.setDirectory(configDir
);
420 config
= new ConfigBundle();
421 config
.updateFile(configDir
);
422 } catch (IOException e
) {
427 uiconfig
= new UiConfigBundle();
428 uiconfig
.updateFile(configDir
);
429 } catch (IOException e
) {
433 // No updateFile for this one! (we do not want the user to have custom
434 // translations that won't accept updates from newer versions)
435 trans
= new StringIdBundle(getLang());
436 transGui
= new StringIdGuiBundle(getLang());
438 // Fix an old bug (we used to store custom translation files by
440 if (trans
.getString(StringId
.INPUT_DESC_CBZ
) == null) {
441 trans
.deleteFile(configDir
);
444 Boolean noutf
= checkEnv("NOUTF");
445 if (noutf
!= null && noutf
) {
446 trans
.setUnicode(false);
447 transGui
.setUnicode(false);
450 Bundles
.setDirectory(configDir
);
454 * Create the default library as specified by the config.
457 * the base remote directory if needed
459 * @return the default {@link BasicLibrary}
461 private static BasicLibrary
createDefaultLibrary(File remoteDir
) {
462 BasicLibrary lib
= null;
464 String remoteLib
= config
.getString(Config
.DEFAULT_LIBRARY
);
465 if (remoteLib
== null || remoteLib
.trim().isEmpty()) {
466 String libDir
= System
.getProperty("fanfix.libdir");
467 if (libDir
== null || libDir
.isEmpty()) {
468 libDir
= config
.getString(Config
.LIBRARY_DIR
);
471 lib
= new LocalLibrary(getFile(libDir
));
472 } catch (Exception e
) {
473 tracer
.error(new IOException(
474 "Cannot create library for directory: "
475 + getFile(libDir
), e
));
479 int pos
= remoteLib
.lastIndexOf(":");
481 String port
= remoteLib
.substring(pos
+ 1).trim();
482 remoteLib
= remoteLib
.substring(0, pos
);
483 pos
= remoteLib
.lastIndexOf(":");
485 String host
= remoteLib
.substring(pos
+ 1).trim();
486 String key
= remoteLib
.substring(0, pos
).trim();
489 tracer
.trace("Selecting remote library " + host
+ ":"
491 lib
= new RemoteLibrary(key
, host
,
492 Integer
.parseInt(port
));
493 lib
= new CacheLibrary(getRemoteDir(remoteDir
, host
),
496 } catch (Exception e
) {
503 tracer
.error(new IOException(
504 "Cannot create remote library for: " + remoteLib
, ex
));
512 * Return a path, but support the special $HOME variable.
516 private static File
getFile(Config id
) {
517 return getFile(config
.getString(id
));
521 * Return a path, but support the special $HOME variable.
525 private static File
getFile(UiConfig id
) {
526 return getFile(uiconfig
.getString(id
));
530 * Return a path, but support the special $HOME variable.
534 private static File
getFile(String path
) {
536 if (path
!= null && !path
.isEmpty()) {
537 path
= path
.replace('/', File
.separatorChar
);
538 if (path
.contains("$HOME")) {
539 path
= path
.replace("$HOME", getHome());
542 file
= new File(path
);
549 * Return the home directory from the system properties.
553 private static String
getHome() {
554 String home
= System
.getProperty("fanfix.home");
555 if (home
!= null && new File(home
).isFile()) {
559 if (home
== null || home
.trim().isEmpty()) {
560 home
= System
.getProperty("user.home");
561 if (!new File(home
).isDirectory()) {
566 if (home
== null || home
.trim().isEmpty()) {
567 home
= System
.getProperty("java.io.tmpdir");
568 if (!new File(home
).isDirectory()) {
581 * The language to use for the application (NULL = default system language).
583 * @return the language
585 private static String
getLang() {
586 String lang
= config
.getString(Config
.LANG
);
588 if (lang
== null || lang
.isEmpty()) {
589 if (System
.getenv("LANG") != null
590 && !System
.getenv("LANG").isEmpty()) {
591 lang
= System
.getenv("LANG");
595 if (lang
!= null && lang
.isEmpty()) {
603 * Check that the given environment variable is "enabled".
606 * the variable to check
608 * @return TRUE if it is
610 private static Boolean
checkEnv(String key
) {
611 String value
= System
.getenv(key
);
613 value
= value
.trim().toLowerCase();
614 if ("yes".equals(value
) || "true".equals(value
)
615 || "on".equals(value
) || "1".equals(value
)
616 || "y".equals(value
)) {