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"), false);
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(System
.getProperty("user.home"), ".fanfix")
56 if (!new File(configDir
).exists()) {
57 new File(configDir
).mkdirs();
59 Bundles
.setDirectory(configDir
);
63 config
= new ConfigBundle();
64 config
.updateFile(configDir
);
65 } catch (IOException e
) {
69 uiconfig
= new UiConfigBundle();
70 uiconfig
.updateFile(configDir
);
71 } catch (IOException e
) {
75 // No updateFile for this one! (we do not want the user to have custom
76 // translations that won't accept updates from newer versions)
77 trans
= new StringIdBundle(getLang());
79 // Fix an old bug (we used to store custom translation files by
81 if (trans
.getString(StringId
.INPUT_DESC_CBZ
) == null) {
82 // TODO: create the deleteFile method
83 // trans.deleteFile(configDir);
86 Bundles
.setDirectory(configDir
);
88 uiconfig
= new UiConfigBundle();
89 trans
= new StringIdBundle(getLang());
91 boolean debug
= Instance
.getConfig()
92 .getBoolean(Config
.DEBUG_ERR
, false);
93 boolean trace
= Instance
.getConfig().getBoolean(Config
.DEBUG_TRACE
,
95 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
96 File tmp
= getFile(Config
.CACHE_DIR
);
97 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
98 remoteDir
= new File(configDir
, "remote");
100 if (checkEnv("NOUTF")) {
101 trans
.setUnicode(false);
104 if (checkEnv("DEBUG")) {
108 tracer
= new TraceHandler(true, debug
, trace
);
110 String remoteLib
= config
.getString(Config
.DEFAULT_LIBRARY
);
111 if (remoteLib
== null || remoteLib
.trim().isEmpty()) {
113 lib
= new LocalLibrary(getFile(Config
.LIBRARY_DIR
));
114 } catch (Exception e
) {
115 tracer
.error(new IOException(
116 "Cannot create library for directory: "
117 + getFile(Config
.LIBRARY_DIR
), e
));
120 int pos
= remoteLib
.lastIndexOf(":");
122 String port
= remoteLib
.substring(pos
+ 1).trim();
123 remoteLib
= remoteLib
.substring(0, pos
);
124 pos
= remoteLib
.lastIndexOf(":");
126 String host
= remoteLib
.substring(pos
+ 1).trim();
127 String key
= remoteLib
.substring(0, pos
).trim();
130 tracer
.trace("Selecting remote library " + host
+ ":"
132 lib
= new RemoteLibrary(key
, host
,
133 Integer
.parseInt(port
));
134 lib
= new CacheLibrary(getRemoteDir(host
), lib
);
136 } catch (Exception e
) {
142 tracer
.error(new IOException(
143 "Cannot create remote library for: "
144 + getFile(Config
.DEFAULT_LIBRARY
)));
148 // Could have used: System.getProperty("java.io.tmpdir")
150 tmp
= new File(configDir
, "tmp");
152 if (readerTmp
== null) {
153 readerTmp
= new File(configDir
, "tmp-reader");
157 if (coverDir
!= null && !coverDir
.exists()) {
158 tracer
.error(new IOException(
159 "The 'default covers' directory does not exists: "
165 String ua
= config
.getString(Config
.USER_AGENT
);
166 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
167 int hoursLarge
= config
168 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
170 cache
= new DataLoader(tmp
, ua
, hours
, hoursLarge
);
171 } catch (IOException e
) {
172 tracer
.error(new IOException(
173 "Cannot create cache (will continue without cache)", e
));
178 * The traces handler for this {@link Cache}.
182 * @return the traces handler (never NULL)
184 public static TraceHandler
getTraceHandler() {
189 * The traces handler for this {@link Cache}.
192 * the new traces handler or NULL
194 public static void setTraceHandler(TraceHandler tracer
) {
195 if (tracer
== null) {
196 tracer
= new TraceHandler(false, false, false);
199 Instance
.tracer
= tracer
;
203 * Get the (unique) configuration service for the program.
205 * @return the configuration service
207 public static ConfigBundle
getConfig() {
212 * Get the (unique) UI configuration service for the program.
214 * @return the configuration service
216 public static UiConfigBundle
getUiConfig() {
221 * Get the (unique) {@link DataLoader} for the program.
223 * @return the {@link DataLoader}
225 public static DataLoader
getCache() {
230 * Get the (unique) {link StringIdBundle} for the program.
232 * @return the {link StringIdBundle}
234 public static StringIdBundle
getTrans() {
239 * Get the (unique) {@link LocalLibrary} for the program.
241 * @return the {@link LocalLibrary}
243 public static BasicLibrary
getLibrary() {
245 throw new NullPointerException("We don't have a library to return");
252 * Return the directory where to look for default cover pages.
254 * @return the default covers directory
256 public static File
getCoverDir() {
261 * Return the directory where to store temporary files for the local reader.
263 * @return the directory
265 public static File
getReaderDir() {
270 * Return the directory where to store temporary files for the remote
271 * {@link LocalLibrary}.
274 * the remote for this host
276 * @return the directory
278 public static File
getRemoteDir(String host
) {
282 return new File(remoteDir
, host
);
289 * Check if we need to check that a new version of Fanfix is available.
291 * @return TRUE if we need to
293 public static boolean isVersionCheckNeeded() {
295 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 1) * 24 * 60
298 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
300 long delay
= new Date().getTime()
301 - Long
.parseLong(lastUpString
);
308 } catch (Exception e
) {
309 // No file or bad file:
317 * Notify that we checked for a new version of Fanfix.
319 public static void setVersionChecked() {
321 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
322 Long
.toString(new Date().getTime()));
323 } catch (IOException e
) {
329 * Return a path, but support the special $HOME variable.
333 private static File
getFile(Config id
) {
334 return getFile(config
.getString(id
));
338 * Return a path, but support the special $HOME variable.
342 private static File
getFile(UiConfig id
) {
343 return getFile(uiconfig
.getString(id
));
347 * Return a path, but support the special $HOME variable.
351 private static File
getFile(String path
) {
353 if (path
!= null && !path
.isEmpty()) {
354 path
= path
.replace('/', File
.separatorChar
);
355 if (path
.contains("$HOME")) {
356 path
= path
.replace("$HOME",
357 "" + System
.getProperty("user.home"));
360 file
= new File(path
);
367 * The language to use for the application (NULL = default system language).
369 * @return the language
371 private static String
getLang() {
372 String lang
= config
.getString(Config
.LANG
);
374 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
375 lang
= System
.getenv("LANG");
378 if (lang
!= null && lang
.isEmpty()) {
386 * Check that the given environment variable is "enabled".
389 * the variable to check
391 * @return TRUE if it is
393 private static boolean checkEnv(String key
) {
394 String value
= System
.getenv(key
);
396 value
= value
.trim().toLowerCase();
397 if ("yes".equals(value
) || "true".equals(value
)
398 || "on".equals(value
) || "1".equals(value
)
399 || "y".equals(value
)) {