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 lib
= new LocalLibrary(getFile(Config
.LIBRARY_DIR
));
87 } catch (Exception e
) {
88 syserr(new IOException("Cannot create library for directory: "
89 + getFile(Config
.LIBRARY_DIR
), e
));
92 boolean debug
= Instance
.getConfig()
93 .getBoolean(Config
.DEBUG_ERR
, false);
94 boolean trace
= Instance
.getConfig().getBoolean(Config
.DEBUG_TRACE
,
96 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
97 File tmp
= getFile(Config
.CACHE_DIR
);
98 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
99 remoteDir
= new File(configDir
, "remote");
101 if (checkEnv("NOUTF")) {
102 trans
.setUnicode(false);
105 if (checkEnv("DEBUG")) {
109 tracer
= new TraceHandler();
110 tracer
.setShowErrorDetails(debug
);
111 tracer
.setShowTraces(trace
);
113 // Could have used: System.getProperty("java.io.tmpdir")
115 tmp
= new File(configDir
, "tmp");
117 if (readerTmp
== null) {
118 readerTmp
= new File(configDir
, "tmp-reader");
122 if (coverDir
!= null && !coverDir
.exists()) {
123 syserr(new IOException(
124 "The 'default covers' directory does not exists: "
130 String ua
= config
.getString(Config
.USER_AGENT
);
131 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
132 int hoursLarge
= config
133 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
135 cache
= new DataLoader(tmp
, ua
, hours
, hoursLarge
);
136 } catch (IOException e
) {
137 syserr(new IOException(
138 "Cannot create cache (will continue without cache)", e
));
143 * The traces handler for this {@link Cache}.
145 * @return the traces handler or NULL
147 public static TraceHandler
getTraceHandler() {
152 * The traces handler for this {@link Cache}.
155 * the new traces handler or NULL
157 public static void setTraceHandler(TraceHandler tracer
) {
158 Instance
.tracer
= tracer
;
162 * Get the (unique) configuration service for the program.
164 * @return the configuration service
166 public static ConfigBundle
getConfig() {
171 * Get the (unique) UI configuration service for the program.
173 * @return the configuration service
175 public static UiConfigBundle
getUiConfig() {
180 * Get the (unique) {@link DataLoader} for the program.
182 * @return the {@link DataLoader}
184 public static DataLoader
getCache() {
189 * Get the (unique) {link StringIdBundle} for the program.
191 * @return the {link StringIdBundle}
193 public static StringIdBundle
getTrans() {
198 * Get the (unique) {@link LocalLibrary} for the program.
200 * @return the {@link LocalLibrary}
202 public static BasicLibrary
getLibrary() {
207 * Return the directory where to look for default cover pages.
209 * @return the default covers directory
211 public static File
getCoverDir() {
216 * Return the directory where to store temporary files for the local reader.
218 * @return the directory
220 public static File
getReaderDir() {
225 * Return the directory where to store temporary files for the remote
226 * {@link LocalLibrary}.
229 * the remote for this host
231 * @return the directory
233 public static File
getRemoteDir(String host
) {
237 return new File(remoteDir
, host
);
244 * Check if we need to check that a new version of Fanfix is available.
246 * @return TRUE if we need to
248 public static boolean isVersionCheckNeeded() {
250 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 1) * 24 * 60
253 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
255 long delay
= new Date().getTime()
256 - Long
.parseLong(lastUpString
);
263 } catch (Exception e
) {
264 // No file or bad file:
272 * Notify that we checked for a new version of Fanfix.
274 public static void setVersionChecked() {
276 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
277 Long
.toString(new Date().getTime()));
278 } catch (IOException e
) {
284 * Report an error to the user
287 * the {@link Exception} to report
289 public static void syserr(Exception e
) {
290 if (tracer
!= null) {
296 * Notify of a debug message.
301 public static void trace(String message
) {
302 if (tracer
!= null) {
303 tracer
.trace(message
);
308 * Return a path, but support the special $HOME variable.
312 private static File
getFile(Config id
) {
313 return getFile(config
.getString(id
));
317 * Return a path, but support the special $HOME variable.
321 private static File
getFile(UiConfig id
) {
322 return getFile(uiconfig
.getString(id
));
326 * Return a path, but support the special $HOME variable.
330 private static File
getFile(String path
) {
332 if (path
!= null && !path
.isEmpty()) {
333 path
= path
.replace('/', File
.separatorChar
);
334 if (path
.contains("$HOME")) {
335 path
= path
.replace("$HOME",
336 "" + System
.getProperty("user.home"));
339 file
= new File(path
);
346 * The language to use for the application (NULL = default system language).
348 * @return the language
350 private static String
getLang() {
351 String lang
= config
.getString(Config
.LANG
);
353 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
354 lang
= System
.getenv("LANG");
357 if (lang
!= null && lang
.isEmpty()) {
365 * Check that the given environment variable is "enabled".
368 * the variable to check
370 * @return TRUE if it is
372 private static boolean checkEnv(String key
) {
373 String value
= System
.getenv(key
);
375 value
= value
.trim().toLowerCase();
376 if ("yes".equals(value
) || "true".equals(value
)
377 || "on".equals(value
) || "1".equals(value
)
378 || "y".equals(value
)) {