4cf6e86113e46c620ae50ed87022ddc49e4f0eeb
[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.StringIdBundle;
10 import be.nikiroo.fanfix.bundles.UiConfig;
11 import be.nikiroo.fanfix.bundles.UiConfigBundle;
12 import be.nikiroo.fanfix.library.BasicLibrary;
13 import be.nikiroo.fanfix.library.LocalLibrary;
14 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
15 import be.nikiroo.utils.IOUtils;
16 import be.nikiroo.utils.resources.Bundles;
17
18 /**
19 * Global state for the program (services and singletons).
20 *
21 * @author niki
22 */
23 public class Instance {
24 /**
25 * A handler when a recoverable exception was caught by the program.
26 *
27 * @author niki
28 */
29 public interface SyserrHandler {
30 /**
31 * An exception happened, log it.
32 *
33 * @param e
34 * the exception
35 * @param showDetails
36 * show more details (usually equivalent to the value of
37 * DEBUG)
38 */
39 public void notify(Exception e, boolean showDetails);
40 }
41
42 /**
43 * A handler when a trace message is sent.
44 *
45 * @author niki
46 */
47 public interface TraceHandler {
48 /**
49 * A trace happened, show it.
50 * <p>
51 * Will only be called if TRACE is true.
52 *
53 * @param message
54 * the trace message
55 */
56 public void trace(String message);
57 }
58
59 private static ConfigBundle config;
60 private static UiConfigBundle uiconfig;
61 private static StringIdBundle trans;
62 private static Cache cache;
63 private static LocalLibrary lib;
64 private static boolean debug;
65 private static boolean trace;
66 private static File coverDir;
67 private static File readerTmp;
68 private static File remoteDir;
69 private static String configDir;
70
71 private static SyserrHandler syserrHandler;
72
73 private static TraceHandler traceHandler;
74
75 static {
76 // Most of the rest is dependent upon this:
77 config = new ConfigBundle();
78
79 configDir = System.getProperty("CONFIG_DIR");
80 if (configDir == null) {
81 configDir = System.getenv("CONFIG_DIR");
82 }
83
84 if (configDir == null) {
85 configDir = new File(System.getProperty("user.home"), ".fanfix")
86 .getPath();
87 }
88
89 if (!new File(configDir).exists()) {
90 new File(configDir).mkdirs();
91 } else {
92 Bundles.setDirectory(configDir);
93 }
94
95 try {
96 config = new ConfigBundle();
97 config.updateFile(configDir);
98 } catch (IOException e) {
99 syserr(e);
100 }
101 try {
102 uiconfig = new UiConfigBundle();
103 uiconfig.updateFile(configDir);
104 } catch (IOException e) {
105 syserr(e);
106 }
107 try {
108 trans = new StringIdBundle(getLang());
109 trans.updateFile(configDir);
110 } catch (IOException e) {
111 syserr(e);
112 }
113
114 Bundles.setDirectory(configDir);
115
116 uiconfig = new UiConfigBundle();
117 trans = new StringIdBundle(getLang());
118 try {
119 lib = new LocalLibrary(getFile(Config.LIBRARY_DIR),
120 OutputType.INFO_TEXT, OutputType.CBZ);
121 } catch (Exception e) {
122 syserr(new IOException("Cannot create library for directory: "
123 + getFile(Config.LIBRARY_DIR), e));
124 }
125
126 debug = Instance.getConfig().getBoolean(Config.DEBUG_ERR, false);
127 trace = Instance.getConfig().getBoolean(Config.DEBUG_TRACE, false);
128 coverDir = getFile(Config.DEFAULT_COVERS_DIR);
129 File tmp = getFile(Config.CACHE_DIR);
130 readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER);
131 remoteDir = new File(getFile(Config.LIBRARY_DIR), "remote");
132
133 if (checkEnv("NOUTF")) {
134 trans.setUnicode(false);
135 }
136
137 if (checkEnv("DEBUG")) {
138 debug = true;
139 }
140
141 // Could have used: System.getProperty("java.io.tmpdir")
142 if (tmp == null) {
143 tmp = new File(configDir, "tmp");
144 }
145 if (readerTmp == null) {
146 readerTmp = new File(configDir, "tmp-reader");
147 }
148 //
149
150 if (coverDir != null && !coverDir.exists()) {
151 syserr(new IOException(
152 "The 'default covers' directory does not exists: "
153 + coverDir));
154 coverDir = null;
155 }
156
157 try {
158 String ua = config.getString(Config.USER_AGENT);
159 int hours = config.getInteger(Config.CACHE_MAX_TIME_CHANGING, -1);
160 int hoursLarge = config
161 .getInteger(Config.CACHE_MAX_TIME_STABLE, -1);
162
163 cache = new Cache(tmp, ua, hours, hoursLarge);
164 } catch (IOException e) {
165 syserr(new IOException(
166 "Cannot create cache (will continue without cache)", e));
167 }
168 }
169
170 /**
171 * Get the (unique) configuration service for the program.
172 *
173 * @return the configuration service
174 */
175 public static ConfigBundle getConfig() {
176 return config;
177 }
178
179 /**
180 * Get the (unique) UI configuration service for the program.
181 *
182 * @return the configuration service
183 */
184 public static UiConfigBundle getUiConfig() {
185 return uiconfig;
186 }
187
188 /**
189 * Get the (unique) {@link Cache} for the program.
190 *
191 * @return the {@link Cache}
192 */
193 public static Cache getCache() {
194 return cache;
195 }
196
197 /**
198 * Get the (unique) {link StringIdBundle} for the program.
199 *
200 * @return the {link StringIdBundle}
201 */
202 public static StringIdBundle getTrans() {
203 return trans;
204 }
205
206 /**
207 * Get the (unique) {@link LocalLibrary} for the program.
208 *
209 * @return the {@link LocalLibrary}
210 */
211 public static BasicLibrary getLibrary() {
212 return lib;
213 }
214
215 /**
216 * Return the directory where to look for default cover pages.
217 *
218 * @return the default covers directory
219 */
220 public static File getCoverDir() {
221 return coverDir;
222 }
223
224 /**
225 * Return the directory where to store temporary files for the local reader.
226 *
227 * @return the directory
228 */
229 public static File getReaderDir() {
230 return readerTmp;
231 }
232
233 /**
234 * Return the directory where to store temporary files for the remote
235 * {@link LocalLibrary}.
236 *
237 * @param host
238 * the remote for this host
239 *
240 * @return the directory
241 */
242 public static File getRemoteDir(String host) {
243 remoteDir.mkdirs();
244
245 if (host != null) {
246 return new File(remoteDir, host);
247 }
248
249 return remoteDir;
250 }
251
252 /**
253 * Check if we need to check that a new version of Fanfix is available.
254 *
255 * @return TRUE if we need to
256 */
257 public static boolean isVersionCheckNeeded() {
258 try {
259 long wait = config.getInteger(Config.UPDATE_INTERVAL, 1) * 24 * 60
260 * 60 * 1000;
261 if (wait >= 0) {
262 String lastUpString = IOUtils.readSmallFile(new File(configDir,
263 "LAST_UPDATE"));
264 long delay = new Date().getTime()
265 - Long.parseLong(lastUpString);
266 if (delay > wait) {
267 return true;
268 }
269 } else {
270 return false;
271 }
272 } catch (Exception e) {
273 // No file or bad file:
274 return true;
275 }
276
277 return false;
278 }
279
280 /**
281 * Notify that we checked for a new version of Fanfix.
282 */
283 public static void setVersionChecked() {
284 try {
285 IOUtils.writeSmallFile(new File(configDir), "LAST_UPDATE",
286 Long.toString(new Date().getTime()));
287 } catch (IOException e) {
288 syserr(e);
289 }
290 }
291
292 /**
293 * Replace the global syserr handler.
294 *
295 * @param syserrHandler
296 * the new syserr handler
297 */
298 public static void setSyserrHandler(SyserrHandler syserrHandler) {
299 Instance.syserrHandler = syserrHandler;
300 }
301
302 /**
303 * Replace the global trace handler.
304 *
305 * @param traceHandler
306 * the new trace handler
307 */
308 public static void setTraceHandler(TraceHandler traceHandler) {
309 Instance.traceHandler = traceHandler;
310 }
311
312 /**
313 * Report an error to the user
314 *
315 * @param e
316 * the {@link Exception} to report
317 */
318 public static void syserr(Exception e) {
319 if (syserrHandler != null) {
320 syserrHandler.notify(e, debug);
321 } else {
322 if (debug) {
323 e.printStackTrace();
324 } else {
325 System.err.println(e.getMessage());
326 }
327 }
328 }
329
330 /**
331 * Notify of a debug message.
332 *
333 * @param message
334 * the message
335 */
336 public static void trace(String message) {
337 if (trace) {
338 if (traceHandler != null) {
339 traceHandler.trace(message);
340 } else {
341 System.out.println(message);
342 }
343 }
344 }
345
346 /**
347 * Return a path, but support the special $HOME variable.
348 *
349 * @return the path
350 */
351 private static File getFile(Config id) {
352 return getFile(config.getString(id));
353 }
354
355 /**
356 * Return a path, but support the special $HOME variable.
357 *
358 * @return the path
359 */
360 private static File getFile(UiConfig id) {
361 return getFile(uiconfig.getString(id));
362 }
363
364 /**
365 * Return a path, but support the special $HOME variable.
366 *
367 * @return the path
368 */
369 private static File getFile(String path) {
370 File file = null;
371 if (path != null && !path.isEmpty()) {
372 path = path.replace('/', File.separatorChar);
373 if (path.contains("$HOME")) {
374 path = path.replace("$HOME",
375 "" + System.getProperty("user.home"));
376 }
377
378 file = new File(path);
379 }
380
381 return file;
382 }
383
384 /**
385 * The language to use for the application (NULL = default system language).
386 *
387 * @return the language
388 */
389 private static String getLang() {
390 String lang = config.getString(Config.LANG);
391
392 if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) {
393 lang = System.getenv("LANG");
394 }
395
396 if (lang != null && lang.isEmpty()) {
397 lang = null;
398 }
399
400 return lang;
401 }
402
403 /**
404 * Check that the given environment variable is "enabled".
405 *
406 * @param key
407 * the variable to check
408 *
409 * @return TRUE if it is
410 */
411 private static boolean checkEnv(String key) {
412 String value = System.getenv(key);
413 if (value != null) {
414 value = value.trim().toLowerCase();
415 if ("yes".equals(value) || "true".equals(value)
416 || "on".equals(value) || "1".equals(value)
417 || "y".equals(value)) {
418 return true;
419 }
420 }
421
422 return false;
423 }
424 }