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 static private Instance instance
;
33 static private Object instancelock
= new Object();
35 private ConfigBundle config
;
36 private UiConfigBundle uiconfig
;
37 private StringIdBundle trans
;
38 private DataLoader cache
;
39 private StringIdGuiBundle transGui
;
40 private BasicLibrary lib
;
41 private File coverDir
;
42 private File readerTmp
;
43 private File remoteDir
;
44 private String configDir
;
45 private TraceHandler tracer
;
46 private TempFiles tempFiles
;
49 * Initialise the instance -- if already initialised, nothing will happen.
51 * Before calling this method, you may call {@link Bundles#setDirectory(String)}
54 static public void init() {
59 * Initialise the instance -- if already initialised, nothing will happen unless
60 * you pass TRUE to <tt>force</tt>.
62 * Before calling this method, you may call {@link Bundles#setDirectory(String)}
65 * Note: forcing the initialisation can be dangerous, so make sure to only make
66 * it under controlled circumstances -- for instance, at the start of the
67 * program, you could call {@link Instance#init()}, change some settings because
68 * you want to force those settings (it will also forbid users to change them!)
69 * and then call {@link Instance#init(boolean)} with <tt>force</tt> set to TRUE.
71 * @param force force the initialisation even if already initialised
73 static public void init(boolean force
) {
74 synchronized (instancelock
) {
75 if (instance
== null || force
) {
76 instance
= new Instance();
83 * Force-initialise the {@link Instance} to a known value.
85 * Usually for DEBUG/Test purposes.
87 * @param instance the actual Instance to use
89 static public void init(Instance instance
) {
90 Instance
.instance
= instance
;
94 * The (mostly unique) instance of this {@link Instance}.
96 * @return the (mostly unique) instance
98 public static Instance
getInstance() {
103 * Actually initialise the instance.
105 * Before calling this method, you may call {@link Bundles#setDirectory(String)}
108 protected Instance() {
109 // Before we can configure it:
110 Boolean debug
= checkEnv("DEBUG");
111 boolean trace
= debug
!= null && debug
;
112 tracer
= new TraceHandler(true, trace
, trace
);
115 configDir
= getConfigDir();
116 if (!new File(configDir
).exists()) {
117 new File(configDir
).mkdirs();
120 // Most of the rest is dependent upon this:
121 createConfigs(configDir
, false);
124 Proxy
.use(config
.getString(Config
.NETWORK_PROXY
));
128 debug
= config
.getBoolean(Config
.DEBUG_ERR
, false);
129 trace
= config
.getBoolean(Config
.DEBUG_TRACE
, false);
132 tracer
= new TraceHandler(true, debug
, trace
);
135 remoteDir
= new File(configDir
, "remote");
136 lib
= createDefaultLibrary(remoteDir
);
138 // create cache and TMP
139 File tmp
= getFile(Config
.CACHE_DIR
, new File(configDir
, "tmp"));
140 if (!tmp
.isAbsolute()) {
141 tmp
= new File(configDir
, tmp
.getPath());
143 Image
.setTemporaryFilesRoot(new File(tmp
.getParent(), "tmp.images"));
145 String ua
= config
.getString(Config
.NETWORK_USER_AGENT
, "");
147 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, 0);
148 int hoursLarge
= config
.getInteger(Config
.CACHE_MAX_TIME_STABLE
, 0);
149 cache
= new DataLoader(tmp
, ua
, hours
, hoursLarge
);
150 } catch (IOException e
) {
151 tracer
.error(new IOException("Cannot create cache (will continue without cache)", e
));
152 cache
= new DataLoader(ua
);
155 cache
.setTraceHandler(tracer
);
157 // readerTmp / coverDir
158 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
, new File(configDir
, "tmp-reader"));
160 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
, new File(configDir
, "covers"));
164 tempFiles
= new TempFiles("fanfix");
165 } catch (IOException e
) {
166 tracer
.error(new IOException("Cannot create temporary directory", e
));
171 * The traces handler for this {@link Cache}.
175 * @return the traces handler (never NULL)
177 public TraceHandler
getTraceHandler() {
182 * The traces handler for this {@link Cache}.
184 * @param tracer the new traces handler or NULL
186 public void setTraceHandler(TraceHandler tracer
) {
187 if (tracer
== null) {
188 tracer
= new TraceHandler(false, false, false);
191 this.tracer
= tracer
;
192 cache
.setTraceHandler(tracer
);
196 * Get the (unique) configuration service for the program.
198 * @return the configuration service
200 public ConfigBundle
getConfig() {
205 * Get the (unique) UI configuration service for the program.
207 * @return the configuration service
209 public UiConfigBundle
getUiConfig() {
214 * Reset the configuration.
216 * @param resetTrans also reset the translation files
218 public void resetConfig(boolean resetTrans
) {
219 String dir
= Bundles
.getDirectory();
220 Bundles
.setDirectory(null);
223 ConfigBundle config
= new ConfigBundle();
224 config
.updateFile(configDir
);
225 } catch (IOException e
) {
229 UiConfigBundle uiconfig
= new UiConfigBundle();
230 uiconfig
.updateFile(configDir
);
231 } catch (IOException e
) {
237 StringIdBundle trans
= new StringIdBundle(null);
238 trans
.updateFile(configDir
);
239 } catch (IOException e
) {
244 Bundles
.setDirectory(dir
);
249 * Get the (unique) {@link DataLoader} for the program.
251 * @return the {@link DataLoader}
253 public DataLoader
getCache() {
258 * Get the (unique) {link StringIdBundle} for the program.
260 * This is used for the translations of the core parts of Fanfix.
262 * @return the {link StringIdBundle}
264 public StringIdBundle
getTrans() {
269 * Get the (unique) {link StringIdGuiBundle} for the program.
271 * This is used for the translations of the GUI parts of Fanfix.
273 * @return the {link StringIdGuiBundle}
275 public StringIdGuiBundle
getTransGui() {
280 * Get the (unique) {@link LocalLibrary} for the program.
282 * @return the {@link LocalLibrary}
284 public BasicLibrary
getLibrary() {
286 throw new NullPointerException("We don't have a library to return");
293 * Return the directory where to look for default cover pages.
295 * @return the default covers directory
297 public File
getCoverDir() {
302 * Return the directory where to store temporary files for the local reader.
304 * @return the directory
306 public File
getReaderDir() {
311 * Return the directory where to store temporary files for the remote
312 * {@link LocalLibrary}.
314 * @param host the remote for this host
316 * @return the directory
318 public File
getRemoteDir(String host
) {
319 return getRemoteDir(remoteDir
, host
);
323 * Return the directory where to store temporary files for the remote
324 * {@link LocalLibrary}.
326 * @param remoteDir the base remote directory
327 * @param host the remote for this host
329 * @return the directory
331 private File
getRemoteDir(File remoteDir
, String host
) {
335 return new File(remoteDir
, host
);
342 * Check if we need to check that a new version of Fanfix is available.
344 * @return TRUE if we need to
346 public boolean isVersionCheckNeeded() {
348 long wait
= config
.getInteger(Config
.NETWORK_UPDATE_INTERVAL
, 0) * 24 * 60 * 60 * 1000;
350 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
, "LAST_UPDATE"));
351 long delay
= new Date().getTime() - Long
.parseLong(lastUpString
);
358 } catch (Exception e
) {
359 // No file or bad file:
367 * Notify that we checked for a new version of Fanfix.
369 public void setVersionChecked() {
371 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE", Long
.toString(new Date().getTime()));
372 } catch (IOException e
) {
378 * The facility to use temporary files in this program.
380 * <b>MUST</b> be closed at end of program.
382 * @return the facility
384 public TempFiles
getTempFiles() {
389 * The configuration directory (will check, in order of preference, the system
390 * properties, the environment and then defaults to
391 * {@link Instance#getHome()}/.fanfix).
393 * @return the config directory
395 private String
getConfigDir() {
396 String configDir
= System
.getProperty("CONFIG_DIR");
398 if (configDir
== null) {
399 configDir
= System
.getenv("CONFIG_DIR");
402 if (configDir
== null) {
403 configDir
= new File(getHome(), ".fanfix").getPath();
410 * Create the config variables ({@link Instance#config},
411 * {@link Instance#uiconfig}, {@link Instance#trans} and
412 * {@link Instance#transGui}).
414 * @param configDir the directory where to find the configuration files
415 * @param refresh TRUE to reset the configuration files from the default
418 private void createConfigs(String configDir
, boolean refresh
) {
420 Bundles
.setDirectory(configDir
);
424 config
= new ConfigBundle();
425 config
.updateFile(configDir
);
426 } catch (IOException e
) {
431 uiconfig
= new UiConfigBundle();
432 uiconfig
.updateFile(configDir
);
433 } catch (IOException e
) {
437 // No updateFile for this one! (we do not want the user to have custom
438 // translations that won't accept updates from newer versions)
439 trans
= new StringIdBundle(getLang());
440 transGui
= new StringIdGuiBundle(getLang());
442 // Fix an old bug (we used to store custom translation files by
444 if (trans
.getString(StringId
.INPUT_DESC_CBZ
) == null) {
445 trans
.deleteFile(configDir
);
448 Boolean noutf
= checkEnv("NOUTF");
449 if (noutf
!= null && noutf
) {
450 trans
.setUnicode(false);
451 transGui
.setUnicode(false);
454 Bundles
.setDirectory(configDir
);
458 * Create the default library as specified by the config.
460 * @param remoteDir the base remote directory if needed
462 * @return the default {@link BasicLibrary}
464 private BasicLibrary
createDefaultLibrary(File remoteDir
) {
465 BasicLibrary lib
= null;
467 boolean useRemote
= config
.getBoolean(Config
.REMOTE_LIBRARY_ENABLED
, false);
473 host
= config
.getString(Config
.REMOTE_LIBRARY_HOST
);
474 port
= config
.getInteger(Config
.REMOTE_LIBRARY_PORT
, -1);
475 String key
= config
.getString(Config
.REMOTE_LIBRARY_KEY
);
477 tracer
.trace("Selecting remote library " + host
+ ":" + port
);
478 lib
= new RemoteLibrary(key
, host
, port
);
479 lib
= new CacheLibrary(getRemoteDir(remoteDir
, host
), lib
, uiconfig
);
480 } catch (Exception e
) {
481 tracer
.error(new IOException("Cannot create remote library for: " + host
+ ":" + port
, e
));
484 String libDir
= System
.getenv("BOOKS_DIR");
485 if (libDir
== null || libDir
.isEmpty()) {
486 libDir
= config
.getString(Config
.LIBRARY_DIR
, "$HOME/Books");
487 if (!getFile(libDir
).isAbsolute()) {
488 libDir
= new File(configDir
, libDir
).getPath();
492 lib
= new LocalLibrary(getFile(libDir
), config
);
493 } catch (Exception e
) {
494 tracer
.error(new IOException("Cannot create library for directory: " + getFile(libDir
), e
));
502 * Return a path, but support the special $HOME variable.
504 * @param id the key for the path, which may contain "$HOME"
505 * @param def the default value if none
506 * @return the path, with expanded "$HOME" if needed
508 protected File
getFile(Config id
, File def
) {
509 String path
= config
.getString(id
, def
.getPath());
510 return getFile(path
);
514 * Return a path, but support the special $HOME variable.
516 * @param id the key for the path, which may contain "$HOME"
517 * @param def the default value if none
518 * @return the path, with expanded "$HOME" if needed
520 protected File
getFile(UiConfig id
, File def
) {
521 String path
= uiconfig
.getString(id
, def
.getPath());
522 return getFile(path
);
526 * Return a path, but support the special $HOME variable.
528 * @param path the path, which may contain "$HOME"
529 * @return the path, with expanded "$HOME" if needed
531 protected File
getFile(String path
) {
533 if (path
!= null && !path
.isEmpty()) {
534 path
= path
.replace('/', File
.separatorChar
);
535 if (path
.contains("$HOME")) {
536 path
= path
.replace("$HOME", getHome());
539 file
= new File(path
);
546 * Return the home directory from the environment (FANFIX_DIR) or the system
549 * The environment variable is tested first. Then, the custom property
550 * "fanfix.home" is tried, followed by the usual "user.home" then "java.io.tmp"
551 * if nothing else is found.
555 protected String
getHome() {
556 String home
= System
.getenv("FANFIX_DIR");
557 if (home
!= null && new File(home
).isFile()) {
561 if (home
== null || home
.trim().isEmpty()) {
562 home
= System
.getProperty("fanfix.home");
563 if (home
!= null && new File(home
).isFile()) {
568 if (home
== null || home
.trim().isEmpty()) {
569 home
= System
.getProperty("user.home");
570 if (!new File(home
).isDirectory()) {
575 if (home
== null || home
.trim().isEmpty()) {
576 home
= System
.getProperty("java.io.tmpdir");
577 if (!new File(home
).isDirectory()) {
590 * The language to use for the application (NULL = default system language).
592 * @return the language
594 protected String
getLang() {
595 String lang
= config
.getString(Config
.LANG
);
597 if (lang
== null || lang
.isEmpty()) {
598 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
599 lang
= System
.getenv("LANG");
603 if (lang
!= null && lang
.isEmpty()) {
611 * Check that the given environment variable is "enabled".
613 * @param key the variable to check
615 * @return TRUE if it is
617 protected Boolean
checkEnv(String key
) {
618 String value
= System
.getenv(key
);
620 value
= value
.trim().toLowerCase();
621 if ("yes".equals(value
) || "true".equals(value
) || "on".equals(value
) || "1".equals(value
)
622 || "y".equals(value
)) {