woops, forgot half of them
[nikiroo-utils.git] / Instance.java
1 package be.nikiroo.fanfix;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Date;
6
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;
25
26 /**
27 * Global state for the program (services and singletons).
28 *
29 * @author niki
30 */
31 public class Instance {
32 static private Instance instance;
33 static private Object instancelock = new Object();
34
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;
47
48 /**
49 * Initialise the instance -- if already initialised, nothing will happen.
50 * <p>
51 * Before calling this method, you may call
52 * {@link Bundles#setDirectory(String)} if wanted.
53 * <p>
54 * Note that this method will honour some environment variables, the 3 most
55 * important ones probably being:
56 * <ul>
57 * <li><tt>DEBUG</tt>: will enable DEBUG output if set to 1 (or Y or TRUE or
58 * ON, case insensitive)</li>
59 * <li><tt>CONFIG_DIR</tt>: will use this directory as configuration
60 * directory (supports $HOME notation, defaults to $HOME/.fanfix</li>
61 * <li><tt>BOOKS_DIR</tt>: will use this directory as library directory
62 * (supports $HOME notation, defaults to $HOME/Books</li>
63 * </ul>
64 */
65 static public void init() {
66 init(false);
67 }
68
69 /**
70 * Initialise the instance -- if already initialised, nothing will happen unless
71 * you pass TRUE to <tt>force</tt>.
72 * <p>
73 * Before calling this method, you may call {@link Bundles#setDirectory(String)}
74 * if wanted.
75 * <p>
76 * Note: forcing the initialisation can be dangerous, so make sure to only make
77 * it under controlled circumstances -- for instance, at the start of the
78 * program, you could call {@link Instance#init()}, change some settings because
79 * you want to force those settings (it will also forbid users to change them!)
80 * and then call {@link Instance#init(boolean)} with <tt>force</tt> set to TRUE.
81 *
82 * @param force force the initialisation even if already initialised
83 */
84 static public void init(boolean force) {
85 synchronized (instancelock) {
86 if (instance == null || force) {
87 instance = new Instance();
88 }
89 }
90
91 }
92
93 /**
94 * Force-initialise the {@link Instance} to a known value.
95 * <p>
96 * Usually for DEBUG/Test purposes.
97 *
98 * @param instance the actual Instance to use
99 */
100 static public void init(Instance instance) {
101 Instance.instance = instance;
102 }
103
104 /**
105 * The (mostly unique) instance of this {@link Instance}.
106 *
107 * @return the (mostly unique) instance
108 */
109 public static Instance getInstance() {
110 return instance;
111 }
112
113 /**
114 * Actually initialise the instance.
115 * <p>
116 * Before calling this method, you may call {@link Bundles#setDirectory(String)}
117 * if wanted.
118 */
119 protected Instance() {
120 // Before we can configure it:
121 Boolean debug = checkEnv("DEBUG");
122 boolean trace = debug != null && debug;
123 tracer = new TraceHandler(true, trace, trace);
124
125 // config dir:
126 configDir = getConfigDir();
127 if (!new File(configDir).exists()) {
128 new File(configDir).mkdirs();
129 }
130
131 // Most of the rest is dependent upon this:
132 createConfigs(configDir, false);
133
134 // Proxy support
135 Proxy.use(config.getString(Config.NETWORK_PROXY));
136
137 // update tracer:
138 if (debug == null) {
139 debug = config.getBoolean(Config.DEBUG_ERR, false);
140 trace = config.getBoolean(Config.DEBUG_TRACE, false);
141 }
142
143 tracer = new TraceHandler(true, debug, trace);
144
145 // default Library
146 remoteDir = new File(configDir, "remote");
147 lib = createDefaultLibrary(remoteDir);
148
149 // create cache and TMP
150 File tmp = getFile(Config.CACHE_DIR, configDir, "tmp");
151 Image.setTemporaryFilesRoot(new File(tmp.getParent(), "tmp.images"));
152
153 String ua = config.getString(Config.NETWORK_USER_AGENT, "");
154 try {
155 int hours = config.getInteger(Config.CACHE_MAX_TIME_CHANGING, 0);
156 int hoursLarge = config.getInteger(Config.CACHE_MAX_TIME_STABLE, 0);
157 cache = new DataLoader(tmp, ua, hours, hoursLarge);
158 } catch (IOException e) {
159 tracer.error(new IOException("Cannot create cache (will continue without cache)", e));
160 cache = new DataLoader(ua);
161 }
162
163 cache.setTraceHandler(tracer);
164
165 // readerTmp / coverDir
166 readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER, configDir, "tmp-reader");
167 coverDir = getFile(Config.DEFAULT_COVERS_DIR, configDir, "covers");
168 coverDir.mkdirs();
169
170 try {
171 tempFiles = new TempFiles("fanfix");
172 } catch (IOException e) {
173 tracer.error(new IOException("Cannot create temporary directory", e));
174 }
175 }
176
177 /**
178 * The traces handler for this {@link Cache}.
179 * <p>
180 * It is never NULL.
181 *
182 * @return the traces handler (never NULL)
183 */
184 public TraceHandler getTraceHandler() {
185 return tracer;
186 }
187
188 /**
189 * The traces handler for this {@link Cache}.
190 *
191 * @param tracer the new traces handler or NULL
192 */
193 public void setTraceHandler(TraceHandler tracer) {
194 if (tracer == null) {
195 tracer = new TraceHandler(false, false, false);
196 }
197
198 this.tracer = tracer;
199 cache.setTraceHandler(tracer);
200 }
201
202 /**
203 * Get the (unique) configuration service for the program.
204 *
205 * @return the configuration service
206 */
207 public ConfigBundle getConfig() {
208 return config;
209 }
210
211 /**
212 * Get the (unique) UI configuration service for the program.
213 *
214 * @return the configuration service
215 */
216 public UiConfigBundle getUiConfig() {
217 return uiconfig;
218 }
219
220 /**
221 * Reset the configuration.
222 *
223 * @param resetTrans also reset the translation files
224 */
225 public void resetConfig(boolean resetTrans) {
226 String dir = Bundles.getDirectory();
227 Bundles.setDirectory(null);
228 try {
229 try {
230 ConfigBundle config = new ConfigBundle();
231 config.updateFile(configDir);
232 } catch (IOException e) {
233 tracer.error(e);
234 }
235 try {
236 UiConfigBundle uiconfig = new UiConfigBundle();
237 uiconfig.updateFile(configDir);
238 } catch (IOException e) {
239 tracer.error(e);
240 }
241
242 if (resetTrans) {
243 try {
244 StringIdBundle trans = new StringIdBundle(null);
245 trans.updateFile(configDir);
246 } catch (IOException e) {
247 tracer.error(e);
248 }
249 }
250 } finally {
251 Bundles.setDirectory(dir);
252 }
253 }
254
255 /**
256 * Get the (unique) {@link DataLoader} for the program.
257 *
258 * @return the {@link DataLoader}
259 */
260 public DataLoader getCache() {
261 return cache;
262 }
263
264 /**
265 * Get the (unique) {link StringIdBundle} for the program.
266 * <p>
267 * This is used for the translations of the core parts of Fanfix.
268 *
269 * @return the {link StringIdBundle}
270 */
271 public StringIdBundle getTrans() {
272 return trans;
273 }
274
275 /**
276 * Get the (unique) {link StringIdGuiBundle} for the program.
277 * <p>
278 * This is used for the translations of the GUI parts of Fanfix.
279 *
280 * @return the {link StringIdGuiBundle}
281 */
282 public StringIdGuiBundle getTransGui() {
283 return transGui;
284 }
285
286 /**
287 * Get the (unique) {@link BasicLibrary} for the program.
288 *
289 * @return the {@link BasicLibrary}
290 */
291 public BasicLibrary getLibrary() {
292 if (lib == null) {
293 throw new NullPointerException("We don't have a library to return");
294 }
295
296 return lib;
297 }
298
299 /**
300 * Change the default {@link BasicLibrary} for this program.
301 * <p>
302 * Be careful.
303 *
304 * @param lib
305 * the new {@link BasicLibrary}
306 */
307 public void setLibrary(BasicLibrary lib) {
308 this.lib = lib;
309 }
310
311 /**
312 * Return the directory where to look for default cover pages.
313 *
314 * @return the default covers directory
315 */
316 public File getCoverDir() {
317 return coverDir;
318 }
319
320 /**
321 * Return the directory where to store temporary files for the local reader.
322 *
323 * @return the directory
324 */
325 public File getReaderDir() {
326 return readerTmp;
327 }
328
329 /**
330 * Return the directory where to store temporary files for the remote
331 * {@link LocalLibrary}.
332 *
333 * @param host the remote for this host
334 *
335 * @return the directory
336 */
337 public File getRemoteDir(String host) {
338 return getRemoteDir(remoteDir, host);
339 }
340
341 /**
342 * Return the directory where to store temporary files for the remote
343 * {@link LocalLibrary}.
344 *
345 * @param remoteDir the base remote directory
346 * @param host the remote for this host
347 *
348 * @return the directory
349 */
350 private File getRemoteDir(File remoteDir, String host) {
351 remoteDir.mkdirs();
352
353 if (host != null) {
354 return new File(remoteDir, host);
355 }
356
357 return remoteDir;
358 }
359
360 /**
361 * Check if we need to check that a new version of Fanfix is available.
362 *
363 * @return TRUE if we need to
364 */
365 public boolean isVersionCheckNeeded() {
366 try {
367 long wait = config.getInteger(Config.NETWORK_UPDATE_INTERVAL, 0) * 24 * 60 * 60 * 1000;
368 if (wait >= 0) {
369 String lastUpString = IOUtils.readSmallFile(new File(configDir, "LAST_UPDATE"));
370 long delay = new Date().getTime() - Long.parseLong(lastUpString);
371 if (delay > wait) {
372 return true;
373 }
374 } else {
375 return false;
376 }
377 } catch (Exception e) {
378 // No file or bad file:
379 return true;
380 }
381
382 return false;
383 }
384
385 /**
386 * Notify that we checked for a new version of Fanfix.
387 */
388 public void setVersionChecked() {
389 try {
390 IOUtils.writeSmallFile(new File(configDir), "LAST_UPDATE", Long.toString(new Date().getTime()));
391 } catch (IOException e) {
392 tracer.error(e);
393 }
394 }
395
396 /**
397 * The facility to use temporary files in this program.
398 * <p>
399 * <b>MUST</b> be closed at end of program.
400 *
401 * @return the facility
402 */
403 public TempFiles getTempFiles() {
404 return tempFiles;
405 }
406
407 /**
408 * The configuration directory (will check, in order of preference, the system
409 * properties, the environment and then defaults to
410 * {@link Instance#getHome()}/.fanfix).
411 *
412 * @return the config directory
413 */
414 private String getConfigDir() {
415 String configDir = System.getProperty("CONFIG_DIR");
416
417 if (configDir == null) {
418 configDir = System.getenv("CONFIG_DIR");
419 }
420
421 if (configDir == null) {
422 configDir = new File(getHome(), ".fanfix").getPath();
423 }
424
425 return configDir;
426 }
427
428 /**
429 * Create the config variables ({@link Instance#config},
430 * {@link Instance#uiconfig}, {@link Instance#trans} and
431 * {@link Instance#transGui}).
432 *
433 * @param configDir the directory where to find the configuration files
434 * @param refresh TRUE to reset the configuration files from the default
435 * included ones
436 */
437 private void createConfigs(String configDir, boolean refresh) {
438 if (!refresh) {
439 Bundles.setDirectory(configDir);
440 }
441
442 try {
443 config = new ConfigBundle();
444 config.updateFile(configDir);
445 } catch (IOException e) {
446 tracer.error(e);
447 }
448
449 try {
450 uiconfig = new UiConfigBundle();
451 uiconfig.updateFile(configDir);
452 } catch (IOException e) {
453 tracer.error(e);
454 }
455
456 // No updateFile for this one! (we do not want the user to have custom
457 // translations that won't accept updates from newer versions)
458 trans = new StringIdBundle(getLang());
459 transGui = new StringIdGuiBundle(getLang());
460
461 // Fix an old bug (we used to store custom translation files by
462 // default):
463 if (trans.getString(StringId.INPUT_DESC_CBZ) == null) {
464 trans.deleteFile(configDir);
465 }
466
467 Boolean noutf = checkEnv("NOUTF");
468 if (noutf != null && noutf) {
469 trans.setUnicode(false);
470 transGui.setUnicode(false);
471 }
472
473 Bundles.setDirectory(configDir);
474 }
475
476 /**
477 * Create the default library as specified by the config.
478 *
479 * @param remoteDir the base remote directory if needed
480 *
481 * @return the default {@link BasicLibrary}
482 */
483 private BasicLibrary createDefaultLibrary(File remoteDir) {
484 BasicLibrary lib = null;
485
486 boolean useRemote = config.getBoolean(Config.REMOTE_LIBRARY_ENABLED, false);
487 if (useRemote) {
488 String host = null;
489 int port = -1;
490 try {
491 host = config.getString(Config.REMOTE_LIBRARY_HOST);
492 port = config.getInteger(Config.REMOTE_LIBRARY_PORT, -1);
493 String key = config.getString(Config.REMOTE_LIBRARY_KEY);
494
495 tracer.trace("Selecting remote library " + host + ":" + port);
496 lib = new RemoteLibrary(key, host, port);
497 lib = new CacheLibrary(getRemoteDir(remoteDir, host), lib, uiconfig);
498 } catch (Exception e) {
499 tracer.error(new IOException("Cannot create remote library for: " + host + ":" + port, e));
500 }
501 } else {
502 String libDir = System.getenv("BOOKS_DIR");
503 if (libDir == null || libDir.isEmpty()) {
504 libDir = getFile(Config.LIBRARY_DIR, configDir, "$HOME/Books").getPath();
505 }
506 try {
507 lib = new LocalLibrary(new File(libDir), config);
508 } catch (Exception e) {
509 tracer.error(new IOException("Cannot create library for directory: " + libDir, e));
510 }
511 }
512
513 return lib;
514 }
515
516 /**
517 * Return a path, but support the special $HOME variable.
518 *
519 * @param id the key for the path, which may contain "$HOME"
520 * @param configDir the directory to use as base if not absolute
521 * @param def the default value if none (will be configDir-rooted if needed)
522 * @return the path, with expanded "$HOME" if needed
523 */
524 protected File getFile(Config id, String configDir, String def) {
525 String path = config.getString(id, def);
526 return getFile(path, configDir);
527 }
528
529 /**
530 * Return a path, but support the special $HOME variable.
531 *
532 * @param id the key for the path, which may contain "$HOME"
533 * @param configDir the directory to use as base if not absolute
534 * @param def the default value if none (will be configDir-rooted if needed)
535 * @return the path, with expanded "$HOME" if needed
536 */
537 protected File getFile(UiConfig id, String configDir, String def) {
538 String path = uiconfig.getString(id, def);
539 return getFile(path, configDir);
540 }
541
542 /**
543 * Return a path, but support the special $HOME variable.
544 *
545 * @param path the path, which may contain "$HOME"
546 * @param configDir the directory to use as base if not absolute
547 * @return the path, with expanded "$HOME" if needed
548 */
549 protected File getFile(String path, String configDir) {
550 File file = null;
551 if (path != null && !path.isEmpty()) {
552 path = path.replace('/', File.separatorChar);
553 if (path.contains("$HOME")) {
554 path = path.replace("$HOME", getHome());
555 } else if (!path.startsWith("/")) {
556 path = new File(configDir, path).getPath();
557 }
558
559 file = new File(path);
560 }
561
562 return file;
563 }
564
565 /**
566 * Return the home directory from the environment (FANFIX_DIR) or the system
567 * properties.
568 * <p>
569 * The environment variable is tested first. Then, the custom property
570 * "fanfix.home" is tried, followed by the usual "user.home" then "java.io.tmp"
571 * if nothing else is found.
572 *
573 * @return the home
574 */
575 protected String getHome() {
576 String home = System.getenv("FANFIX_DIR");
577 if (home != null && new File(home).isFile()) {
578 home = null;
579 }
580
581 if (home == null || home.trim().isEmpty()) {
582 home = System.getProperty("fanfix.home");
583 if (home != null && new File(home).isFile()) {
584 home = null;
585 }
586 }
587
588 if (home == null || home.trim().isEmpty()) {
589 home = System.getProperty("user.home");
590 if (!new File(home).isDirectory()) {
591 home = null;
592 }
593 }
594
595 if (home == null || home.trim().isEmpty()) {
596 home = System.getProperty("java.io.tmpdir");
597 if (!new File(home).isDirectory()) {
598 home = null;
599 }
600 }
601
602 if (home == null) {
603 home = "";
604 }
605
606 return home;
607 }
608
609 /**
610 * The language to use for the application (NULL = default system language).
611 *
612 * @return the language
613 */
614 protected String getLang() {
615 String lang = config.getString(Config.LANG);
616
617 if (lang == null || lang.isEmpty()) {
618 if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) {
619 lang = System.getenv("LANG");
620 }
621 }
622
623 if (lang != null && lang.isEmpty()) {
624 lang = null;
625 }
626
627 return lang;
628 }
629
630 /**
631 * Check that the given environment variable is "enabled".
632 *
633 * @param key the variable to check
634 *
635 * @return TRUE if it is
636 */
637 protected Boolean checkEnv(String key) {
638 String value = System.getenv(key);
639 if (value != null) {
640 value = value.trim().toLowerCase();
641 if ("yes".equals(value) || "true".equals(value) || "on".equals(value) || "1".equals(value)
642 || "y".equals(value)) {
643 return true;
644 }
645
646 return false;
647 }
648
649 return null;
650 }
651 }