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
;
46 // Before we can configure it:
47 Boolean debug
= checkEnv("DEBUG");
48 boolean trace
= debug
!= null && debug
;
49 tracer
= new TraceHandler(true, trace
, trace
);
52 configDir
= getConfigDir();
53 if (!new File(configDir
).exists()) {
54 new File(configDir
).mkdirs();
57 // Most of the rest is dependent upon this:
58 createConfigs(configDir
, false);
61 Proxy
.use(Instance
.getConfig().getString(Config
.USE_PROXY
));
65 debug
= Instance
.getConfig().getBoolean(Config
.DEBUG_ERR
, false);
66 trace
= Instance
.getConfig().getBoolean(Config
.DEBUG_TRACE
, false);
69 tracer
= new TraceHandler(true, debug
, trace
);
72 remoteDir
= new File(configDir
, "remote");
73 lib
= createDefaultLibrary(remoteDir
);
75 // create cache and TMP
76 Image
.setTemporaryFilesRoot(new File(configDir
, "tmp.images"));
77 File tmp
= getFile(Config
.CACHE_DIR
);
79 // Could have used: System.getProperty("java.io.tmpdir")
80 tmp
= new File(configDir
, "tmp");
82 String ua
= config
.getString(Config
.USER_AGENT
);
84 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
85 int hoursLarge
= config
86 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
87 cache
= new DataLoader(tmp
, ua
, hours
, hoursLarge
);
88 } catch (IOException e
) {
89 tracer
.error(new IOException(
90 "Cannot create cache (will continue without cache)", e
));
91 cache
= new DataLoader(ua
);
94 cache
.setTraceHandler(tracer
);
96 // readerTmp / coverDir
97 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
98 if (readerTmp
== null) {
99 readerTmp
= new File(configDir
, "tmp-reader");
102 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
103 if (coverDir
!= null && !coverDir
.exists()) {
104 tracer
.error(new IOException(
105 "The 'default covers' directory does not exists: "
111 tempFiles
= new TempFiles("fanfix");
112 } catch (IOException e
) {
113 tracer
.error(new IOException("Cannot create temporary directory", e
));
118 * The traces handler for this {@link Cache}.
122 * @return the traces handler (never NULL)
124 public static TraceHandler
getTraceHandler() {
129 * The traces handler for this {@link Cache}.
132 * the new traces handler or NULL
134 public static void setTraceHandler(TraceHandler tracer
) {
135 if (tracer
== null) {
136 tracer
= new TraceHandler(false, false, false);
139 Instance
.tracer
= tracer
;
140 cache
.setTraceHandler(tracer
);
144 * Get the (unique) configuration service for the program.
146 * @return the configuration service
148 public static ConfigBundle
getConfig() {
153 * Get the (unique) UI configuration service for the program.
155 * @return the configuration service
157 public static UiConfigBundle
getUiConfig() {
162 * Reset the configuration.
165 * also reset the translation files
167 public static void resetConfig(boolean resetTrans
) {
168 String dir
= Bundles
.getDirectory();
169 Bundles
.setDirectory(null);
172 ConfigBundle config
= new ConfigBundle();
173 config
.updateFile(configDir
);
174 } catch (IOException e
) {
178 UiConfigBundle uiconfig
= new UiConfigBundle();
179 uiconfig
.updateFile(configDir
);
180 } catch (IOException e
) {
186 StringIdBundle trans
= new StringIdBundle(null);
187 trans
.updateFile(configDir
);
188 } catch (IOException e
) {
193 Bundles
.setDirectory(dir
);
198 * Get the (unique) {@link DataLoader} for the program.
200 * @return the {@link DataLoader}
202 public static DataLoader
getCache() {
207 * Get the (unique) {link StringIdBundle} for the program.
209 * This is used for the translations of the core parts of Fanfix.
211 * @return the {link StringIdBundle}
213 public static StringIdBundle
getTrans() {
218 * Get the (unique) {link StringIdGuiBundle} for the program.
220 * This is used for the translations of the GUI parts of Fanfix.
222 * @return the {link StringIdGuiBundle}
224 public static StringIdGuiBundle
getTransGui() {
229 * Get the (unique) {@link LocalLibrary} for the program.
231 * @return the {@link LocalLibrary}
233 public static BasicLibrary
getLibrary() {
235 throw new NullPointerException("We don't have a library to return");
242 * Return the directory where to look for default cover pages.
244 * @return the default covers directory
246 public static File
getCoverDir() {
251 * Return the directory where to store temporary files for the local reader.
253 * @return the directory
255 public static File
getReaderDir() {
260 * Return the directory where to store temporary files for the remote
261 * {@link LocalLibrary}.
264 * the remote for this host
266 * @return the directory
268 public static File
getRemoteDir(String host
) {
269 return getRemoteDir(remoteDir
, host
);
273 * Return the directory where to store temporary files for the remote
274 * {@link LocalLibrary}.
277 * the base remote directory
279 * the remote for this host
281 * @return the directory
283 private static File
getRemoteDir(File remoteDir
, String host
) {
287 return new File(remoteDir
, host
);
294 * Check if we need to check that a new version of Fanfix is available.
296 * @return TRUE if we need to
298 public static boolean isVersionCheckNeeded() {
300 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 1) * 24 * 60
303 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
305 long delay
= new Date().getTime()
306 - Long
.parseLong(lastUpString
);
313 } catch (Exception e
) {
314 // No file or bad file:
322 * Notify that we checked for a new version of Fanfix.
324 public static void setVersionChecked() {
326 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
327 Long
.toString(new Date().getTime()));
328 } catch (IOException e
) {
334 * The facility to use temporary files in this program.
336 * <b>MUST</b> be closed at end of program.
338 * @return the facility
340 public static TempFiles
getTempFiles() {
345 * The configuration directory (will check, in order of preference,
346 * {@link Bundles#getDirectory()}, the system properties, the environment
347 * and then defaults to $HOME/.fanfix).
349 * @return the config directory
351 private static String
getConfigDir() {
352 String configDir
= Bundles
.getDirectory();
354 if (configDir
== null) {
355 configDir
= System
.getProperty("CONFIG_DIR");
358 if (configDir
== null) {
359 configDir
= System
.getenv("CONFIG_DIR");
362 if (configDir
== null) {
363 configDir
= new File(getHome(), ".fanfix").getPath();
370 * Create the config variables ({@link Instance#config},
371 * {@link Instance#uiconfig}, {@link Instance#trans} and
372 * {@link Instance#transGui}).
375 * the directory where to find the configuration files
377 * TRUE to reset the configuration files from the default
380 private static void createConfigs(String configDir
, boolean refresh
) {
382 Bundles
.setDirectory(configDir
);
386 config
= new ConfigBundle();
387 config
.updateFile(configDir
);
388 } catch (IOException e
) {
393 uiconfig
= new UiConfigBundle();
394 uiconfig
.updateFile(configDir
);
395 } catch (IOException e
) {
399 // No updateFile for this one! (we do not want the user to have custom
400 // translations that won't accept updates from newer versions)
401 trans
= new StringIdBundle(getLang());
402 transGui
= new StringIdGuiBundle(getLang());
404 // Fix an old bug (we used to store custom translation files by
406 if (trans
.getString(StringId
.INPUT_DESC_CBZ
) == null) {
407 trans
.deleteFile(configDir
);
410 Boolean noutf
= checkEnv("NOUTF");
411 if (noutf
!= null && noutf
) {
412 trans
.setUnicode(false);
413 transGui
.setUnicode(false);
416 Bundles
.setDirectory(configDir
);
420 * Create the default library as specified by the config.
423 * the base remote directory if needed
425 * @return the default {@link BasicLibrary}
427 private static BasicLibrary
createDefaultLibrary(File remoteDir
) {
428 BasicLibrary lib
= null;
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
);
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
));
445 int pos
= remoteLib
.lastIndexOf(":");
447 String port
= remoteLib
.substring(pos
+ 1).trim();
448 remoteLib
= remoteLib
.substring(0, pos
);
449 pos
= remoteLib
.lastIndexOf(":");
451 String host
= remoteLib
.substring(pos
+ 1).trim();
452 String key
= remoteLib
.substring(0, pos
).trim();
455 tracer
.trace("Selecting remote library " + host
+ ":"
457 lib
= new RemoteLibrary(key
, host
,
458 Integer
.parseInt(port
));
459 lib
= new CacheLibrary(getRemoteDir(remoteDir
, host
),
462 } catch (Exception e
) {
469 tracer
.error(new IOException(
470 "Cannot create remote library for: " + remoteLib
, ex
));
478 * Return a path, but support the special $HOME variable.
482 private static File
getFile(Config id
) {
483 return getFile(config
.getString(id
));
487 * Return a path, but support the special $HOME variable.
491 private static File
getFile(UiConfig id
) {
492 return getFile(uiconfig
.getString(id
));
496 * Return a path, but support the special $HOME variable.
500 private static File
getFile(String path
) {
502 if (path
!= null && !path
.isEmpty()) {
503 path
= path
.replace('/', File
.separatorChar
);
504 if (path
.contains("$HOME")) {
505 path
= path
.replace("$HOME", getHome());
508 file
= new File(path
);
515 * Return the home directory from the system properties.
519 private static String
getHome() {
520 String home
= System
.getProperty("fanfix.home");
521 if (home
!= null && new File(home
).isFile()) {
525 if (home
== null || home
.trim().isEmpty()) {
526 home
= System
.getProperty("user.home");
527 if (!new File(home
).isDirectory()) {
532 if (home
== null || home
.trim().isEmpty()) {
533 home
= System
.getProperty("java.io.tmpdir");
534 if (!new File(home
).isDirectory()) {
547 * The language to use for the application (NULL = default system language).
549 * @return the language
551 private static String
getLang() {
552 String lang
= config
.getString(Config
.LANG
);
554 if (lang
== null || lang
.isEmpty()) {
555 if (System
.getenv("LANG") != null
556 && !System
.getenv("LANG").isEmpty()) {
557 lang
= System
.getenv("LANG");
561 if (lang
!= null && lang
.isEmpty()) {
569 * Check that the given environment variable is "enabled".
572 * the variable to check
574 * @return TRUE if it is
576 private static Boolean
checkEnv(String key
) {
577 String value
= System
.getenv(key
);
579 value
= value
.trim().toLowerCase();
580 if ("yes".equals(value
) || "true".equals(value
)
581 || "on".equals(value
) || "1".equals(value
)
582 || "y".equals(value
)) {