Fix the traces handler system
[nikiroo-utils.git] / src / be / nikiroo / fanfix / Instance.java
1 package be.nikiroo.fanfix;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Date;
6
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;
19
20 /**
21 * Global state for the program (services and singletons).
22 *
23 * @author niki
24 */
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;
36
37 static {
38 // Most of the rest is dependent upon this:
39 config = new ConfigBundle();
40
41 configDir = System.getProperty("CONFIG_DIR");
42 if (configDir == null) {
43 configDir = System.getenv("CONFIG_DIR");
44 }
45
46 if (configDir == null) {
47 configDir = new File(System.getProperty("user.home"), ".fanfix")
48 .getPath();
49 }
50
51 if (!new File(configDir).exists()) {
52 new File(configDir).mkdirs();
53 } else {
54 Bundles.setDirectory(configDir);
55 }
56
57 try {
58 config = new ConfigBundle();
59 config.updateFile(configDir);
60 } catch (IOException e) {
61 syserr(e);
62 }
63 try {
64 uiconfig = new UiConfigBundle();
65 uiconfig.updateFile(configDir);
66 } catch (IOException e) {
67 syserr(e);
68 }
69
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());
73
74 // Fix an old bug (we used to store custom translation files by
75 // default):
76 if (trans.getString(StringId.INPUT_DESC_CBZ) == null) {
77 // TODO: create the deleteFile method
78 // trans.deleteFile(configDir);
79 }
80
81 Bundles.setDirectory(configDir);
82
83 uiconfig = new UiConfigBundle();
84 trans = new StringIdBundle(getLang());
85 try {
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));
90 }
91
92 boolean debug = Instance.getConfig()
93 .getBoolean(Config.DEBUG_ERR, false);
94 boolean trace = Instance.getConfig().getBoolean(Config.DEBUG_TRACE,
95 false);
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");
100
101 if (checkEnv("NOUTF")) {
102 trans.setUnicode(false);
103 }
104
105 if (checkEnv("DEBUG")) {
106 debug = true;
107 }
108
109 tracer = new TraceHandler();
110 tracer.setShowErrorDetails(debug);
111 tracer.setShowTraces(trace);
112
113 // Could have used: System.getProperty("java.io.tmpdir")
114 if (tmp == null) {
115 tmp = new File(configDir, "tmp");
116 }
117 if (readerTmp == null) {
118 readerTmp = new File(configDir, "tmp-reader");
119 }
120 //
121
122 if (coverDir != null && !coverDir.exists()) {
123 syserr(new IOException(
124 "The 'default covers' directory does not exists: "
125 + coverDir));
126 coverDir = null;
127 }
128
129 try {
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);
134
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));
139 }
140 }
141
142 /**
143 * The traces handler for this {@link Cache}.
144 *
145 * @return the traces handler or NULL
146 */
147 public static TraceHandler getTraceHandler() {
148 return tracer;
149 }
150
151 /**
152 * The traces handler for this {@link Cache}.
153 *
154 * @param tracer
155 * the new traces handler or NULL
156 */
157 public static void setTraceHandler(TraceHandler tracer) {
158 Instance.tracer = tracer;
159 }
160
161 /**
162 * Get the (unique) configuration service for the program.
163 *
164 * @return the configuration service
165 */
166 public static ConfigBundle getConfig() {
167 return config;
168 }
169
170 /**
171 * Get the (unique) UI configuration service for the program.
172 *
173 * @return the configuration service
174 */
175 public static UiConfigBundle getUiConfig() {
176 return uiconfig;
177 }
178
179 /**
180 * Get the (unique) {@link DataLoader} for the program.
181 *
182 * @return the {@link DataLoader}
183 */
184 public static DataLoader getCache() {
185 return cache;
186 }
187
188 /**
189 * Get the (unique) {link StringIdBundle} for the program.
190 *
191 * @return the {link StringIdBundle}
192 */
193 public static StringIdBundle getTrans() {
194 return trans;
195 }
196
197 /**
198 * Get the (unique) {@link LocalLibrary} for the program.
199 *
200 * @return the {@link LocalLibrary}
201 */
202 public static BasicLibrary getLibrary() {
203 return lib;
204 }
205
206 /**
207 * Return the directory where to look for default cover pages.
208 *
209 * @return the default covers directory
210 */
211 public static File getCoverDir() {
212 return coverDir;
213 }
214
215 /**
216 * Return the directory where to store temporary files for the local reader.
217 *
218 * @return the directory
219 */
220 public static File getReaderDir() {
221 return readerTmp;
222 }
223
224 /**
225 * Return the directory where to store temporary files for the remote
226 * {@link LocalLibrary}.
227 *
228 * @param host
229 * the remote for this host
230 *
231 * @return the directory
232 */
233 public static File getRemoteDir(String host) {
234 remoteDir.mkdirs();
235
236 if (host != null) {
237 return new File(remoteDir, host);
238 }
239
240 return remoteDir;
241 }
242
243 /**
244 * Check if we need to check that a new version of Fanfix is available.
245 *
246 * @return TRUE if we need to
247 */
248 public static boolean isVersionCheckNeeded() {
249 try {
250 long wait = config.getInteger(Config.UPDATE_INTERVAL, 1) * 24 * 60
251 * 60 * 1000;
252 if (wait >= 0) {
253 String lastUpString = IOUtils.readSmallFile(new File(configDir,
254 "LAST_UPDATE"));
255 long delay = new Date().getTime()
256 - Long.parseLong(lastUpString);
257 if (delay > wait) {
258 return true;
259 }
260 } else {
261 return false;
262 }
263 } catch (Exception e) {
264 // No file or bad file:
265 return true;
266 }
267
268 return false;
269 }
270
271 /**
272 * Notify that we checked for a new version of Fanfix.
273 */
274 public static void setVersionChecked() {
275 try {
276 IOUtils.writeSmallFile(new File(configDir), "LAST_UPDATE",
277 Long.toString(new Date().getTime()));
278 } catch (IOException e) {
279 syserr(e);
280 }
281 }
282
283 /**
284 * Report an error to the user
285 *
286 * @param e
287 * the {@link Exception} to report
288 */
289 public static void syserr(Exception e) {
290 if (tracer != null) {
291 tracer.error(e);
292 }
293 }
294
295 /**
296 * Notify of a debug message.
297 *
298 * @param message
299 * the message
300 */
301 public static void trace(String message) {
302 if (tracer != null) {
303 tracer.trace(message);
304 }
305 }
306
307 /**
308 * Return a path, but support the special $HOME variable.
309 *
310 * @return the path
311 */
312 private static File getFile(Config id) {
313 return getFile(config.getString(id));
314 }
315
316 /**
317 * Return a path, but support the special $HOME variable.
318 *
319 * @return the path
320 */
321 private static File getFile(UiConfig id) {
322 return getFile(uiconfig.getString(id));
323 }
324
325 /**
326 * Return a path, but support the special $HOME variable.
327 *
328 * @return the path
329 */
330 private static File getFile(String path) {
331 File file = null;
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"));
337 }
338
339 file = new File(path);
340 }
341
342 return file;
343 }
344
345 /**
346 * The language to use for the application (NULL = default system language).
347 *
348 * @return the language
349 */
350 private static String getLang() {
351 String lang = config.getString(Config.LANG);
352
353 if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) {
354 lang = System.getenv("LANG");
355 }
356
357 if (lang != null && lang.isEmpty()) {
358 lang = null;
359 }
360
361 return lang;
362 }
363
364 /**
365 * Check that the given environment variable is "enabled".
366 *
367 * @param key
368 * the variable to check
369 *
370 * @return TRUE if it is
371 */
372 private static boolean checkEnv(String key) {
373 String value = System.getenv(key);
374 if (value != null) {
375 value = value.trim().toLowerCase();
376 if ("yes".equals(value) || "true".equals(value)
377 || "on".equals(value) || "1".equals(value)
378 || "y".equals(value)) {
379 return true;
380 }
381 }
382
383 return false;
384 }
385 }