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
.UiConfig
;
12 import be
.nikiroo
.fanfix
.bundles
.UiConfigBundle
;
13 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
14 import be
.nikiroo
.fanfix
.library
.CacheLibrary
;
15 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
16 import be
.nikiroo
.fanfix
.library
.RemoteLibrary
;
17 import be
.nikiroo
.utils
.Cache
;
18 import be
.nikiroo
.utils
.IOUtils
;
19 import be
.nikiroo
.utils
.TraceHandler
;
20 import be
.nikiroo
.utils
.resources
.Bundles
;
23 * Global state for the program (services and singletons).
27 public class Instance
{
28 private static ConfigBundle config
;
29 private static UiConfigBundle uiconfig
;
30 private static StringIdBundle trans
;
31 private static DataLoader cache
;
32 private static BasicLibrary lib
;
33 private static File coverDir
;
34 private static File readerTmp
;
35 private static File remoteDir
;
36 private static String configDir
;
37 private static TraceHandler tracer
;
40 // Before we can configure it:
41 tracer
= new TraceHandler(true, checkEnv("DEBUG"), checkEnv("DEBUG"));
43 // Most of the rest is dependent upon this:
44 config
= new ConfigBundle();
46 configDir
= System
.getProperty("CONFIG_DIR");
47 if (configDir
== null) {
48 configDir
= System
.getenv("CONFIG_DIR");
51 if (configDir
== null) {
52 configDir
= new File(getHome(), ".fanfix").getPath();
55 if (!new File(configDir
).exists()) {
56 new File(configDir
).mkdirs();
58 Bundles
.setDirectory(configDir
);
62 config
= new ConfigBundle();
63 config
.updateFile(configDir
);
64 } catch (IOException e
) {
68 uiconfig
= new UiConfigBundle();
69 uiconfig
.updateFile(configDir
);
70 } catch (IOException e
) {
74 // No updateFile for this one! (we do not want the user to have custom
75 // translations that won't accept updates from newer versions)
76 trans
= new StringIdBundle(getLang());
78 // Fix an old bug (we used to store custom translation files by
80 if (trans
.getString(StringId
.INPUT_DESC_CBZ
) == null) {
81 trans
.deleteFile(configDir
);
84 Bundles
.setDirectory(configDir
);
86 uiconfig
= new UiConfigBundle();
87 trans
= new StringIdBundle(getLang());
89 boolean debug
= Instance
.getConfig()
90 .getBoolean(Config
.DEBUG_ERR
, false);
91 boolean trace
= Instance
.getConfig().getBoolean(Config
.DEBUG_TRACE
,
93 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
94 File tmp
= getFile(Config
.CACHE_DIR
);
95 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
96 remoteDir
= new File(configDir
, "remote");
98 if (checkEnv("NOUTF")) {
99 trans
.setUnicode(false);
102 if (checkEnv("DEBUG")) {
107 tracer
= new TraceHandler(true, debug
, trace
);
109 String remoteLib
= config
.getString(Config
.DEFAULT_LIBRARY
);
110 if (remoteLib
== null || remoteLib
.trim().isEmpty()) {
111 String libDir
= System
.getProperty("fanfix.libdir");
112 if (libDir
== null || libDir
.isEmpty()) {
113 config
.getString(Config
.LIBRARY_DIR
);
116 lib
= new LocalLibrary(getFile(libDir
));
117 } catch (Exception e
) {
118 tracer
.error(new IOException(
119 "Cannot create library for directory: "
120 + getFile(libDir
), e
));
123 int pos
= remoteLib
.lastIndexOf(":");
125 String port
= remoteLib
.substring(pos
+ 1).trim();
126 remoteLib
= remoteLib
.substring(0, pos
);
127 pos
= remoteLib
.lastIndexOf(":");
129 String host
= remoteLib
.substring(pos
+ 1).trim();
130 String key
= remoteLib
.substring(0, pos
).trim();
133 tracer
.trace("Selecting remote library " + host
+ ":"
135 lib
= new RemoteLibrary(key
, host
,
136 Integer
.parseInt(port
));
137 lib
= new CacheLibrary(getRemoteDir(host
), lib
);
139 } catch (Exception e
) {
145 tracer
.error(new IOException(
146 "Cannot create remote library for: " + remoteLib
));
150 // Could have used: System.getProperty("java.io.tmpdir")
152 tmp
= new File(configDir
, "tmp");
154 if (readerTmp
== null) {
155 readerTmp
= new File(configDir
, "tmp-reader");
159 if (coverDir
!= null && !coverDir
.exists()) {
160 tracer
.error(new IOException(
161 "The 'default covers' directory does not exists: "
166 String ua
= config
.getString(Config
.USER_AGENT
);
168 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
169 int hoursLarge
= config
170 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
171 cache
= new DataLoader(tmp
, ua
, hours
, hoursLarge
);
172 } catch (IOException e
) {
173 tracer
.error(new IOException(
174 "Cannot create cache (will continue without cache)", e
));
175 cache
= new DataLoader(ua
);
178 cache
.setTraceHandler(tracer
);
182 * The traces handler for this {@link Cache}.
186 * @return the traces handler (never NULL)
188 public static TraceHandler
getTraceHandler() {
193 * The traces handler for this {@link Cache}.
196 * the new traces handler or NULL
198 public static void setTraceHandler(TraceHandler tracer
) {
199 if (tracer
== null) {
200 tracer
= new TraceHandler(false, false, false);
203 Instance
.tracer
= tracer
;
204 cache
.setTraceHandler(tracer
);
208 * Get the (unique) configuration service for the program.
210 * @return the configuration service
212 public static ConfigBundle
getConfig() {
217 * Get the (unique) UI configuration service for the program.
219 * @return the configuration service
221 public static UiConfigBundle
getUiConfig() {
226 * Reset the configuration.
229 * also reset the translation files
231 public static void resetConfig(boolean resetTrans
) {
232 String dir
= Bundles
.getDirectory();
233 Bundles
.setDirectory(null);
236 ConfigBundle config
= new ConfigBundle();
237 config
.updateFile(configDir
);
238 } catch (IOException e
) {
242 UiConfigBundle uiconfig
= new UiConfigBundle();
243 uiconfig
.updateFile(configDir
);
244 } catch (IOException e
) {
250 StringIdBundle trans
= new StringIdBundle(null);
251 trans
.updateFile(configDir
);
252 } catch (IOException e
) {
257 Bundles
.setDirectory(dir
);
262 * Get the (unique) {@link DataLoader} for the program.
264 * @return the {@link DataLoader}
266 public static DataLoader
getCache() {
271 * Get the (unique) {link StringIdBundle} for the program.
273 * @return the {link StringIdBundle}
275 public static StringIdBundle
getTrans() {
280 * Get the (unique) {@link LocalLibrary} for the program.
282 * @return the {@link LocalLibrary}
284 public static 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 static File
getCoverDir() {
302 * Return the directory where to store temporary files for the local reader.
304 * @return the directory
306 public static File
getReaderDir() {
311 * Return the directory where to store temporary files for the remote
312 * {@link LocalLibrary}.
315 * the remote for this host
317 * @return the directory
319 public static File
getRemoteDir(String host
) {
323 return new File(remoteDir
, host
);
330 * Check if we need to check that a new version of Fanfix is available.
332 * @return TRUE if we need to
334 public static boolean isVersionCheckNeeded() {
336 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 1) * 24 * 60
339 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
341 long delay
= new Date().getTime()
342 - Long
.parseLong(lastUpString
);
349 } catch (Exception e
) {
350 // No file or bad file:
358 * Notify that we checked for a new version of Fanfix.
360 public static void setVersionChecked() {
362 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
363 Long
.toString(new Date().getTime()));
364 } catch (IOException e
) {
370 * Return a path, but support the special $HOME variable.
374 private static File
getFile(Config id
) {
375 return getFile(config
.getString(id
));
379 * Return a path, but support the special $HOME variable.
383 private static File
getFile(UiConfig id
) {
384 return getFile(uiconfig
.getString(id
));
388 * Return a path, but support the special $HOME variable.
392 private static File
getFile(String path
) {
394 if (path
!= null && !path
.isEmpty()) {
395 path
= path
.replace('/', File
.separatorChar
);
396 if (path
.contains("$HOME")) {
397 path
= path
.replace("$HOME", getHome());
400 file
= new File(path
);
407 * Return the home directory from the system properties.
411 private static String
getHome() {
412 String home
= System
.getProperty("fanfix.home");
413 if (home
!= null && new File(home
).isFile()) {
417 if (home
== null || home
.trim().isEmpty()) {
418 home
= System
.getProperty("user.home");
419 if (!new File(home
).isDirectory()) {
424 if (home
== null || home
.trim().isEmpty()) {
425 home
= System
.getProperty("java.io.tmpdir");
426 if (!new File(home
).isDirectory()) {
439 * The language to use for the application (NULL = default system language).
441 * @return the language
443 private static String
getLang() {
444 String lang
= config
.getString(Config
.LANG
);
446 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
447 lang
= System
.getenv("LANG");
450 if (lang
!= null && lang
.isEmpty()) {
458 * Check that the given environment variable is "enabled".
461 * the variable to check
463 * @return TRUE if it is
465 private static boolean checkEnv(String key
) {
466 String value
= System
.getenv(key
);
468 value
= value
.trim().toLowerCase();
469 if ("yes".equals(value
) || "true".equals(value
)
470 || "on".equals(value
) || "1".equals(value
)
471 || "y".equals(value
)) {