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