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 // Before we can configure it:
39 tracer
= new TraceHandler(true, checkEnv("DEBUG"), false);
41 // Most of the rest is dependent upon this:
42 config
= new ConfigBundle();
44 configDir
= System
.getProperty("CONFIG_DIR");
45 if (configDir
== null) {
46 configDir
= System
.getenv("CONFIG_DIR");
49 if (configDir
== null) {
50 configDir
= new File(System
.getProperty("user.home"), ".fanfix")
54 if (!new File(configDir
).exists()) {
55 new File(configDir
).mkdirs();
57 Bundles
.setDirectory(configDir
);
61 config
= new ConfigBundle();
62 config
.updateFile(configDir
);
63 } catch (IOException e
) {
67 uiconfig
= new UiConfigBundle();
68 uiconfig
.updateFile(configDir
);
69 } catch (IOException e
) {
73 // No updateFile for this one! (we do not want the user to have custom
74 // translations that won't accept updates from newer versions)
75 trans
= new StringIdBundle(getLang());
77 // Fix an old bug (we used to store custom translation files by
79 if (trans
.getString(StringId
.INPUT_DESC_CBZ
) == null) {
80 // TODO: create the deleteFile method
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")) {
106 tracer
= new TraceHandler(true, debug
, trace
);
109 lib
= new LocalLibrary(getFile(Config
.LIBRARY_DIR
));
110 } catch (Exception e
) {
111 tracer
.error(new IOException(
112 "Cannot create library for directory: "
113 + getFile(Config
.LIBRARY_DIR
), e
));
116 // Could have used: System.getProperty("java.io.tmpdir")
118 tmp
= new File(configDir
, "tmp");
120 if (readerTmp
== null) {
121 readerTmp
= new File(configDir
, "tmp-reader");
125 if (coverDir
!= null && !coverDir
.exists()) {
126 tracer
.error(new IOException(
127 "The 'default covers' directory does not exists: "
133 String ua
= config
.getString(Config
.USER_AGENT
);
134 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
135 int hoursLarge
= config
136 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
138 cache
= new DataLoader(tmp
, ua
, hours
, hoursLarge
);
139 } catch (IOException e
) {
140 tracer
.error(new IOException(
141 "Cannot create cache (will continue without cache)", e
));
146 * The traces handler for this {@link Cache}.
150 * @return the traces handler (never NULL)
152 public static TraceHandler
getTraceHandler() {
157 * The traces handler for this {@link Cache}.
160 * the new traces handler or NULL
162 public static void setTraceHandler(TraceHandler tracer
) {
163 if (tracer
== null) {
164 tracer
= new TraceHandler(false, false, false);
167 Instance
.tracer
= tracer
;
171 * Get the (unique) configuration service for the program.
173 * @return the configuration service
175 public static ConfigBundle
getConfig() {
180 * Get the (unique) UI configuration service for the program.
182 * @return the configuration service
184 public static UiConfigBundle
getUiConfig() {
189 * Get the (unique) {@link DataLoader} for the program.
191 * @return the {@link DataLoader}
193 public static DataLoader
getCache() {
198 * Get the (unique) {link StringIdBundle} for the program.
200 * @return the {link StringIdBundle}
202 public static StringIdBundle
getTrans() {
207 * Get the (unique) {@link LocalLibrary} for the program.
209 * @return the {@link LocalLibrary}
211 public static BasicLibrary
getLibrary() {
213 throw new NullPointerException("We don't have a library to return");
220 * Return the directory where to look for default cover pages.
222 * @return the default covers directory
224 public static File
getCoverDir() {
229 * Return the directory where to store temporary files for the local reader.
231 * @return the directory
233 public static File
getReaderDir() {
238 * Return the directory where to store temporary files for the remote
239 * {@link LocalLibrary}.
242 * the remote for this host
244 * @return the directory
246 public static File
getRemoteDir(String host
) {
250 return new File(remoteDir
, host
);
257 * Check if we need to check that a new version of Fanfix is available.
259 * @return TRUE if we need to
261 public static boolean isVersionCheckNeeded() {
263 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 1) * 24 * 60
266 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
268 long delay
= new Date().getTime()
269 - Long
.parseLong(lastUpString
);
276 } catch (Exception e
) {
277 // No file or bad file:
285 * Notify that we checked for a new version of Fanfix.
287 public static void setVersionChecked() {
289 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
290 Long
.toString(new Date().getTime()));
291 } catch (IOException e
) {
297 * Return a path, but support the special $HOME variable.
301 private static File
getFile(Config id
) {
302 return getFile(config
.getString(id
));
306 * Return a path, but support the special $HOME variable.
310 private static File
getFile(UiConfig id
) {
311 return getFile(uiconfig
.getString(id
));
315 * Return a path, but support the special $HOME variable.
319 private static File
getFile(String path
) {
321 if (path
!= null && !path
.isEmpty()) {
322 path
= path
.replace('/', File
.separatorChar
);
323 if (path
.contains("$HOME")) {
324 path
= path
.replace("$HOME",
325 "" + System
.getProperty("user.home"));
328 file
= new File(path
);
335 * The language to use for the application (NULL = default system language).
337 * @return the language
339 private static String
getLang() {
340 String lang
= config
.getString(Config
.LANG
);
342 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
343 lang
= System
.getenv("LANG");
346 if (lang
!= null && lang
.isEmpty()) {
354 * Check that the given environment variable is "enabled".
357 * the variable to check
359 * @return TRUE if it is
361 private static boolean checkEnv(String key
) {
362 String value
= System
.getenv(key
);
364 value
= value
.trim().toLowerCase();
365 if ("yes".equals(value
) || "true".equals(value
)
366 || "on".equals(value
) || "1".equals(value
)
367 || "y".equals(value
)) {