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
.LocalLibrary
;
15 import be
.nikiroo
.utils
.Cache
;
16 import be
.nikiroo
.utils
.IOUtils
;
17 import be
.nikiroo
.utils
.TraceHandler
;
18 import be
.nikiroo
.utils
.resources
.Bundles
;
21 * Global state for the program (services and singletons).
25 public class Instance
{
26 private static ConfigBundle config
;
27 private static UiConfigBundle uiconfig
;
28 private static StringIdBundle trans
;
29 private static DataLoader cache
;
30 private static LocalLibrary lib
;
31 private static File coverDir
;
32 private static File readerTmp
;
33 private static File remoteDir
;
34 private static String configDir
;
35 private static TraceHandler tracer
;
38 // Most of the rest is dependent upon this:
39 config
= new ConfigBundle();
41 configDir
= System
.getProperty("CONFIG_DIR");
42 if (configDir
== null) {
43 configDir
= System
.getenv("CONFIG_DIR");
46 if (configDir
== null) {
47 configDir
= new File(System
.getProperty("user.home"), ".fanfix")
51 if (!new File(configDir
).exists()) {
52 new File(configDir
).mkdirs();
54 Bundles
.setDirectory(configDir
);
58 config
= new ConfigBundle();
59 config
.updateFile(configDir
);
60 } catch (IOException e
) {
64 uiconfig
= new UiConfigBundle();
65 uiconfig
.updateFile(configDir
);
66 } catch (IOException e
) {
70 // No updateFile for this one! (we do not want the user to have custom
71 // translations that won't accept updates from newer versions)
72 trans
= new StringIdBundle(getLang());
74 // Fix an old bug (we used to store custom translation files by
76 if (trans
.getString(StringId
.INPUT_DESC_CBZ
) == null) {
77 // TODO: create the deleteFile method
78 // trans.deleteFile(configDir);
81 Bundles
.setDirectory(configDir
);
83 uiconfig
= new UiConfigBundle();
84 trans
= new StringIdBundle(getLang());
86 boolean debug
= Instance
.getConfig()
87 .getBoolean(Config
.DEBUG_ERR
, false);
88 boolean trace
= Instance
.getConfig().getBoolean(Config
.DEBUG_TRACE
,
90 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
91 File tmp
= getFile(Config
.CACHE_DIR
);
92 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
93 remoteDir
= new File(configDir
, "remote");
95 if (checkEnv("NOUTF")) {
96 trans
.setUnicode(false);
99 if (checkEnv("DEBUG")) {
103 tracer
= new TraceHandler();
104 tracer
.setShowErrorDetails(debug
);
105 tracer
.setShowTraces(trace
);
108 lib
= new LocalLibrary(getFile(Config
.LIBRARY_DIR
));
109 } catch (Exception e
) {
110 syserr(new IOException("Cannot create library for directory: "
111 + getFile(Config
.LIBRARY_DIR
), e
));
114 // Could have used: System.getProperty("java.io.tmpdir")
116 tmp
= new File(configDir
, "tmp");
118 if (readerTmp
== null) {
119 readerTmp
= new File(configDir
, "tmp-reader");
123 if (coverDir
!= null && !coverDir
.exists()) {
124 syserr(new IOException(
125 "The 'default covers' directory does not exists: "
131 String ua
= config
.getString(Config
.USER_AGENT
);
132 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
133 int hoursLarge
= config
134 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
136 cache
= new DataLoader(tmp
, ua
, hours
, hoursLarge
);
137 } catch (IOException e
) {
138 syserr(new IOException(
139 "Cannot create cache (will continue without cache)", e
));
144 * The traces handler for this {@link Cache}.
146 * @return the traces handler or NULL
148 public static TraceHandler
getTraceHandler() {
153 * The traces handler for this {@link Cache}.
156 * the new traces handler or NULL
158 public static void setTraceHandler(TraceHandler tracer
) {
159 Instance
.tracer
= tracer
;
163 * Get the (unique) configuration service for the program.
165 * @return the configuration service
167 public static ConfigBundle
getConfig() {
172 * Get the (unique) UI configuration service for the program.
174 * @return the configuration service
176 public static UiConfigBundle
getUiConfig() {
181 * Get the (unique) {@link DataLoader} for the program.
183 * @return the {@link DataLoader}
185 public static DataLoader
getCache() {
190 * Get the (unique) {link StringIdBundle} for the program.
192 * @return the {link StringIdBundle}
194 public static StringIdBundle
getTrans() {
199 * Get the (unique) {@link LocalLibrary} for the program.
201 * @return the {@link LocalLibrary}
203 public static BasicLibrary
getLibrary() {
205 throw new NullPointerException("We don't have a library to return");
212 * Return the directory where to look for default cover pages.
214 * @return the default covers directory
216 public static File
getCoverDir() {
221 * Return the directory where to store temporary files for the local reader.
223 * @return the directory
225 public static File
getReaderDir() {
230 * Return the directory where to store temporary files for the remote
231 * {@link LocalLibrary}.
234 * the remote for this host
236 * @return the directory
238 public static File
getRemoteDir(String host
) {
242 return new File(remoteDir
, host
);
249 * Check if we need to check that a new version of Fanfix is available.
251 * @return TRUE if we need to
253 public static boolean isVersionCheckNeeded() {
255 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 1) * 24 * 60
258 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
260 long delay
= new Date().getTime()
261 - Long
.parseLong(lastUpString
);
268 } catch (Exception e
) {
269 // No file or bad file:
277 * Notify that we checked for a new version of Fanfix.
279 public static void setVersionChecked() {
281 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
282 Long
.toString(new Date().getTime()));
283 } catch (IOException e
) {
289 * Report an error to the user
292 * the {@link Exception} to report
294 public static void syserr(Exception e
) {
295 if (tracer
!= null) {
301 * Notify of a debug message.
306 public static void trace(String message
) {
307 if (tracer
!= null) {
308 tracer
.trace(message
);
313 * Return a path, but support the special $HOME variable.
317 private static File
getFile(Config id
) {
318 return getFile(config
.getString(id
));
322 * Return a path, but support the special $HOME variable.
326 private static File
getFile(UiConfig id
) {
327 return getFile(uiconfig
.getString(id
));
331 * Return a path, but support the special $HOME variable.
335 private static File
getFile(String path
) {
337 if (path
!= null && !path
.isEmpty()) {
338 path
= path
.replace('/', File
.separatorChar
);
339 if (path
.contains("$HOME")) {
340 path
= path
.replace("$HOME",
341 "" + System
.getProperty("user.home"));
344 file
= new File(path
);
351 * The language to use for the application (NULL = default system language).
353 * @return the language
355 private static String
getLang() {
356 String lang
= config
.getString(Config
.LANG
);
358 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
359 lang
= System
.getenv("LANG");
362 if (lang
!= null && lang
.isEmpty()) {
370 * Check that the given environment variable is "enabled".
373 * the variable to check
375 * @return TRUE if it is
377 private static boolean checkEnv(String key
) {
378 String value
= System
.getenv(key
);
380 value
= value
.trim().toLowerCase();
381 if ("yes".equals(value
) || "true".equals(value
)
382 || "on".equals(value
) || "1".equals(value
)
383 || "y".equals(value
)) {