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
, -1);
120 int hoursLarge
= config
121 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
122 cache
= new DataLoader(tmp
, ua
, hours
, hoursLarge
);
123 } catch (IOException e
) {
124 tracer
.error(new IOException(
125 "Cannot create cache (will continue without cache)", e
));
126 cache
= new DataLoader(ua
);
129 cache
.setTraceHandler(tracer
);
131 // readerTmp / coverDir
132 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
133 if (readerTmp
== null) {
134 readerTmp
= new File(configDir
, "tmp-reader");
137 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
138 if (coverDir
!= null && !coverDir
.exists()) {
139 tracer
.error(new IOException(
140 "The 'default covers' directory does not exists: "
146 tempFiles
= new TempFiles("fanfix");
147 } catch (IOException e
) {
148 tracer
.error(new IOException("Cannot create temporary directory", e
));
153 * The traces handler for this {@link Cache}.
157 * @return the traces handler (never NULL)
159 public static TraceHandler
getTraceHandler() {
164 * The traces handler for this {@link Cache}.
167 * the new traces handler or NULL
169 public static void setTraceHandler(TraceHandler tracer
) {
170 if (tracer
== null) {
171 tracer
= new TraceHandler(false, false, false);
174 Instance
.tracer
= tracer
;
175 cache
.setTraceHandler(tracer
);
179 * Get the (unique) configuration service for the program.
181 * @return the configuration service
183 public static ConfigBundle
getConfig() {
188 * Get the (unique) UI configuration service for the program.
190 * @return the configuration service
192 public static UiConfigBundle
getUiConfig() {
197 * Reset the configuration.
200 * also reset the translation files
202 public static void resetConfig(boolean resetTrans
) {
203 String dir
= Bundles
.getDirectory();
204 Bundles
.setDirectory(null);
207 ConfigBundle config
= new ConfigBundle();
208 config
.updateFile(configDir
);
209 } catch (IOException e
) {
213 UiConfigBundle uiconfig
= new UiConfigBundle();
214 uiconfig
.updateFile(configDir
);
215 } catch (IOException e
) {
221 StringIdBundle trans
= new StringIdBundle(null);
222 trans
.updateFile(configDir
);
223 } catch (IOException e
) {
228 Bundles
.setDirectory(dir
);
233 * Get the (unique) {@link DataLoader} for the program.
235 * @return the {@link DataLoader}
237 public static DataLoader
getCache() {
242 * Get the (unique) {link StringIdBundle} for the program.
244 * This is used for the translations of the core parts of Fanfix.
246 * @return the {link StringIdBundle}
248 public static StringIdBundle
getTrans() {
253 * Get the (unique) {link StringIdGuiBundle} for the program.
255 * This is used for the translations of the GUI parts of Fanfix.
257 * @return the {link StringIdGuiBundle}
259 public static StringIdGuiBundle
getTransGui() {
264 * Get the (unique) {@link LocalLibrary} for the program.
266 * @return the {@link LocalLibrary}
268 public static BasicLibrary
getLibrary() {
270 throw new NullPointerException("We don't have a library to return");
277 * Return the directory where to look for default cover pages.
279 * @return the default covers directory
281 public static File
getCoverDir() {
286 * Return the directory where to store temporary files for the local reader.
288 * @return the directory
290 public static File
getReaderDir() {
295 * Return the directory where to store temporary files for the remote
296 * {@link LocalLibrary}.
299 * the remote for this host
301 * @return the directory
303 public static File
getRemoteDir(String host
) {
304 return getRemoteDir(remoteDir
, host
);
308 * Return the directory where to store temporary files for the remote
309 * {@link LocalLibrary}.
312 * the base remote directory
314 * the remote for this host
316 * @return the directory
318 private static File
getRemoteDir(File remoteDir
, String host
) {
322 return new File(remoteDir
, host
);
329 * Check if we need to check that a new version of Fanfix is available.
331 * @return TRUE if we need to
333 public static boolean isVersionCheckNeeded() {
335 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 1) * 24 * 60
338 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
340 long delay
= new Date().getTime()
341 - Long
.parseLong(lastUpString
);
348 } catch (Exception e
) {
349 // No file or bad file:
357 * Notify that we checked for a new version of Fanfix.
359 public static void setVersionChecked() {
361 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
362 Long
.toString(new Date().getTime()));
363 } catch (IOException e
) {
369 * The facility to use temporary files in this program.
371 * <b>MUST</b> be closed at end of program.
373 * @return the facility
375 public static TempFiles
getTempFiles() {
380 * The configuration directory (will check, in order of preference,
381 * {@link Bundles#getDirectory()}, the system properties, the environment
382 * and then defaults to $HOME/.fanfix).
384 * @return the config directory
386 private static String
getConfigDir() {
387 String configDir
= Bundles
.getDirectory();
389 if (configDir
== null) {
390 configDir
= System
.getProperty("CONFIG_DIR");
393 if (configDir
== null) {
394 configDir
= System
.getenv("CONFIG_DIR");
397 if (configDir
== null) {
398 configDir
= new File(getHome(), ".fanfix").getPath();
405 * Create the config variables ({@link Instance#config},
406 * {@link Instance#uiconfig}, {@link Instance#trans} and
407 * {@link Instance#transGui}).
410 * the directory where to find the configuration files
412 * TRUE to reset the configuration files from the default
415 private static void createConfigs(String configDir
, boolean refresh
) {
417 Bundles
.setDirectory(configDir
);
421 config
= new ConfigBundle();
422 config
.updateFile(configDir
);
423 } catch (IOException e
) {
428 uiconfig
= new UiConfigBundle();
429 uiconfig
.updateFile(configDir
);
430 } catch (IOException e
) {
434 // No updateFile for this one! (we do not want the user to have custom
435 // translations that won't accept updates from newer versions)
436 trans
= new StringIdBundle(getLang());
437 transGui
= new StringIdGuiBundle(getLang());
439 // Fix an old bug (we used to store custom translation files by
441 if (trans
.getString(StringId
.INPUT_DESC_CBZ
) == null) {
442 trans
.deleteFile(configDir
);
445 Boolean noutf
= checkEnv("NOUTF");
446 if (noutf
!= null && noutf
) {
447 trans
.setUnicode(false);
448 transGui
.setUnicode(false);
451 Bundles
.setDirectory(configDir
);
455 * Create the default library as specified by the config.
458 * the base remote directory if needed
460 * @return the default {@link BasicLibrary}
462 private static BasicLibrary
createDefaultLibrary(File remoteDir
) {
463 BasicLibrary lib
= null;
465 String remoteLib
= config
.getString(Config
.DEFAULT_LIBRARY
);
466 if (remoteLib
== null || remoteLib
.trim().isEmpty()) {
467 String libDir
= System
.getProperty("fanfix.libdir");
468 if (libDir
== null || libDir
.isEmpty()) {
469 libDir
= config
.getString(Config
.LIBRARY_DIR
);
472 lib
= new LocalLibrary(getFile(libDir
));
473 } catch (Exception e
) {
474 tracer
.error(new IOException(
475 "Cannot create library for directory: "
476 + getFile(libDir
), e
));
480 int pos
= remoteLib
.lastIndexOf(":");
482 String port
= remoteLib
.substring(pos
+ 1).trim();
483 remoteLib
= remoteLib
.substring(0, pos
);
484 pos
= remoteLib
.lastIndexOf(":");
486 String host
= remoteLib
.substring(pos
+ 1).trim();
487 String key
= remoteLib
.substring(0, pos
).trim();
490 tracer
.trace("Selecting remote library " + host
+ ":"
492 lib
= new RemoteLibrary(key
, host
,
493 Integer
.parseInt(port
));
494 lib
= new CacheLibrary(getRemoteDir(remoteDir
, host
),
497 } catch (Exception e
) {
504 tracer
.error(new IOException(
505 "Cannot create remote library for: " + remoteLib
, ex
));
513 * Return a path, but support the special $HOME variable.
517 private static File
getFile(Config id
) {
518 return getFile(config
.getString(id
));
522 * Return a path, but support the special $HOME variable.
526 private static File
getFile(UiConfig id
) {
527 return getFile(uiconfig
.getString(id
));
531 * Return a path, but support the special $HOME variable.
535 private static File
getFile(String path
) {
537 if (path
!= null && !path
.isEmpty()) {
538 path
= path
.replace('/', File
.separatorChar
);
539 if (path
.contains("$HOME")) {
540 path
= path
.replace("$HOME", getHome());
543 file
= new File(path
);
550 * Return the home directory from the system properties.
554 private static String
getHome() {
555 String home
= System
.getProperty("fanfix.home");
556 if (home
!= null && new File(home
).isFile()) {
560 if (home
== null || home
.trim().isEmpty()) {
561 home
= System
.getProperty("user.home");
562 if (!new File(home
).isDirectory()) {
567 if (home
== null || home
.trim().isEmpty()) {
568 home
= System
.getProperty("java.io.tmpdir");
569 if (!new File(home
).isDirectory()) {
582 * The language to use for the application (NULL = default system language).
584 * @return the language
586 private static String
getLang() {
587 String lang
= config
.getString(Config
.LANG
);
589 if (lang
== null || lang
.isEmpty()) {
590 if (System
.getenv("LANG") != null
591 && !System
.getenv("LANG").isEmpty()) {
592 lang
= System
.getenv("LANG");
596 if (lang
!= null && lang
.isEmpty()) {
604 * Check that the given environment variable is "enabled".
607 * the variable to check
609 * @return TRUE if it is
611 private static Boolean
checkEnv(String key
) {
612 String value
= System
.getenv(key
);
614 value
= value
.trim().toLowerCase();
615 if ("yes".equals(value
) || "true".equals(value
)
616 || "on".equals(value
) || "1".equals(value
)
617 || "y".equals(value
)) {