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
.fanfix
.library
.RemoteLibrary
;
16 import be
.nikiroo
.utils
.Cache
;
17 import be
.nikiroo
.utils
.IOUtils
;
18 import be
.nikiroo
.utils
.TraceHandler
;
19 import be
.nikiroo
.utils
.resources
.Bundles
;
22 * Global state for the program (services and singletons).
26 public class Instance
{
27 private static ConfigBundle config
;
28 private static UiConfigBundle uiconfig
;
29 private static StringIdBundle trans
;
30 private static DataLoader cache
;
31 private static BasicLibrary lib
;
32 private static File coverDir
;
33 private static File readerTmp
;
34 private static File remoteDir
;
35 private static String configDir
;
36 private static TraceHandler tracer
;
39 // Before we can configure it:
40 tracer
= new TraceHandler(true, checkEnv("DEBUG"), false);
42 // Most of the rest is dependent upon this:
43 config
= new ConfigBundle();
45 configDir
= System
.getProperty("CONFIG_DIR");
46 if (configDir
== null) {
47 configDir
= System
.getenv("CONFIG_DIR");
50 if (configDir
== null) {
51 configDir
= new File(System
.getProperty("user.home"), ".fanfix")
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 // TODO: create the deleteFile method
82 // trans.deleteFile(configDir);
85 Bundles
.setDirectory(configDir
);
87 uiconfig
= new UiConfigBundle();
88 trans
= new StringIdBundle(getLang());
90 boolean debug
= Instance
.getConfig()
91 .getBoolean(Config
.DEBUG_ERR
, false);
92 boolean trace
= Instance
.getConfig().getBoolean(Config
.DEBUG_TRACE
,
94 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
95 File tmp
= getFile(Config
.CACHE_DIR
);
96 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
97 remoteDir
= new File(configDir
, "remote");
99 if (checkEnv("NOUTF")) {
100 trans
.setUnicode(false);
103 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()) {
112 lib
= new LocalLibrary(getFile(Config
.LIBRARY_DIR
));
113 } catch (Exception e
) {
114 tracer
.error(new IOException(
115 "Cannot create library for directory: "
116 + getFile(Config
.LIBRARY_DIR
), e
));
119 int pos
= remoteLib
.lastIndexOf(":");
121 String port
= remoteLib
.substring(pos
+ 1).trim();
122 remoteLib
= remoteLib
.substring(0, pos
);
123 pos
= remoteLib
.lastIndexOf(":");
125 String host
= remoteLib
.substring(pos
+ 1).trim();
126 String key
= remoteLib
.substring(0, pos
).trim();
129 tracer
.trace("Contacting remote library " + host
+ ":"
131 lib
= new RemoteLibrary(key
, host
,
132 Integer
.parseInt(port
));
133 } catch (Exception e
) {
139 tracer
.error(new IOException(
140 "Cannot create remote library for: "
141 + getFile(Config
.DEFAULT_LIBRARY
)));
145 // Could have used: System.getProperty("java.io.tmpdir")
147 tmp
= new File(configDir
, "tmp");
149 if (readerTmp
== null) {
150 readerTmp
= new File(configDir
, "tmp-reader");
154 if (coverDir
!= null && !coverDir
.exists()) {
155 tracer
.error(new IOException(
156 "The 'default covers' directory does not exists: "
162 String ua
= config
.getString(Config
.USER_AGENT
);
163 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
164 int hoursLarge
= config
165 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
167 cache
= new DataLoader(tmp
, ua
, hours
, hoursLarge
);
168 } catch (IOException e
) {
169 tracer
.error(new IOException(
170 "Cannot create cache (will continue without cache)", e
));
175 * The traces handler for this {@link Cache}.
179 * @return the traces handler (never NULL)
181 public static TraceHandler
getTraceHandler() {
186 * The traces handler for this {@link Cache}.
189 * the new traces handler or NULL
191 public static void setTraceHandler(TraceHandler tracer
) {
192 if (tracer
== null) {
193 tracer
= new TraceHandler(false, false, false);
196 Instance
.tracer
= tracer
;
200 * Get the (unique) configuration service for the program.
202 * @return the configuration service
204 public static ConfigBundle
getConfig() {
209 * Get the (unique) UI configuration service for the program.
211 * @return the configuration service
213 public static UiConfigBundle
getUiConfig() {
218 * Get the (unique) {@link DataLoader} for the program.
220 * @return the {@link DataLoader}
222 public static DataLoader
getCache() {
227 * Get the (unique) {link StringIdBundle} for the program.
229 * @return the {link StringIdBundle}
231 public static StringIdBundle
getTrans() {
236 * Get the (unique) {@link LocalLibrary} for the program.
238 * @return the {@link LocalLibrary}
240 public static BasicLibrary
getLibrary() {
242 throw new NullPointerException("We don't have a library to return");
249 * Return the directory where to look for default cover pages.
251 * @return the default covers directory
253 public static File
getCoverDir() {
258 * Return the directory where to store temporary files for the local reader.
260 * @return the directory
262 public static File
getReaderDir() {
267 * Return the directory where to store temporary files for the remote
268 * {@link LocalLibrary}.
271 * the remote for this host
273 * @return the directory
275 public static File
getRemoteDir(String host
) {
279 return new File(remoteDir
, host
);
286 * Check if we need to check that a new version of Fanfix is available.
288 * @return TRUE if we need to
290 public static boolean isVersionCheckNeeded() {
292 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 1) * 24 * 60
295 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
297 long delay
= new Date().getTime()
298 - Long
.parseLong(lastUpString
);
305 } catch (Exception e
) {
306 // No file or bad file:
314 * Notify that we checked for a new version of Fanfix.
316 public static void setVersionChecked() {
318 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
319 Long
.toString(new Date().getTime()));
320 } catch (IOException e
) {
326 * Return a path, but support the special $HOME variable.
330 private static File
getFile(Config id
) {
331 return getFile(config
.getString(id
));
335 * Return a path, but support the special $HOME variable.
339 private static File
getFile(UiConfig id
) {
340 return getFile(uiconfig
.getString(id
));
344 * Return a path, but support the special $HOME variable.
348 private static File
getFile(String path
) {
350 if (path
!= null && !path
.isEmpty()) {
351 path
= path
.replace('/', File
.separatorChar
);
352 if (path
.contains("$HOME")) {
353 path
= path
.replace("$HOME",
354 "" + System
.getProperty("user.home"));
357 file
= new File(path
);
364 * The language to use for the application (NULL = default system language).
366 * @return the language
368 private static String
getLang() {
369 String lang
= config
.getString(Config
.LANG
);
371 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
372 lang
= System
.getenv("LANG");
375 if (lang
!= null && lang
.isEmpty()) {
383 * Check that the given environment variable is "enabled".
386 * the variable to check
388 * @return TRUE if it is
390 private static boolean checkEnv(String key
) {
391 String value
= System
.getenv(key
);
393 value
= value
.trim().toLowerCase();
394 if ("yes".equals(value
) || "true".equals(value
)
395 || "on".equals(value
) || "1".equals(value
)
396 || "y".equals(value
)) {