Fix name of imported cbz, fix library check
[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
86 boolean debug = Instance.getConfig()
87 .getBoolean(Config.DEBUG_ERR, false);
88 boolean trace = Instance.getConfig().getBoolean(Config.DEBUG_TRACE,
89 false);
90 coverDir = getFile(Config.DEFAULT_COVERS_DIR);
91 File tmp = getFile(Config.CACHE_DIR);
92 readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER);
93 remoteDir = new File(configDir, "remote");
94
95 if (checkEnv("NOUTF")) {
96 trans.setUnicode(false);
97 }
98
99 if (checkEnv("DEBUG")) {
100 debug = true;
101 }
102
103 tracer = new TraceHandler();
104 tracer.setShowErrorDetails(debug);
105 tracer.setShowTraces(trace);
106
107 try {
108 lib = new LocalLibrary(getFile(Config.LIBRARY_DIR));
109 } catch (Exception e) {
110 syserr(new IOException("Cannot create library for directory: "
111 + getFile(Config.LIBRARY_DIR), e));
112 }
113
114 // Could have used: System.getProperty("java.io.tmpdir")
115 if (tmp == null) {
116 tmp = new File(configDir, "tmp");
117 }
118 if (readerTmp == null) {
119 readerTmp = new File(configDir, "tmp-reader");
120 }
121 //
122
123 if (coverDir != null && !coverDir.exists()) {
124 syserr(new IOException(
125 "The 'default covers' directory does not exists: "
126 + coverDir));
127 coverDir = null;
128 }
129
130 try {
131 String ua = config.getString(Config.USER_AGENT);
132 int hours = config.getInteger(Config.CACHE_MAX_TIME_CHANGING, -1);
133 int hoursLarge = config
134 .getInteger(Config.CACHE_MAX_TIME_STABLE, -1);
135
136 cache = new DataLoader(tmp, ua, hours, hoursLarge);
137 } catch (IOException e) {
138 syserr(new IOException(
139 "Cannot create cache (will continue without cache)", e));
140 }
141 }
142
143 /**
144 * The traces handler for this {@link Cache}.
145 *
146 * @return the traces handler or NULL
147 */
148 public static TraceHandler getTraceHandler() {
149 return tracer;
150 }
151
152 /**
153 * The traces handler for this {@link Cache}.
154 *
155 * @param tracer
156 * the new traces handler or NULL
157 */
158 public static void setTraceHandler(TraceHandler tracer) {
159 Instance.tracer = tracer;
160 }
161
162 /**
163 * Get the (unique) configuration service for the program.
164 *
165 * @return the configuration service
166 */
167 public static ConfigBundle getConfig() {
168 return config;
169 }
170
171 /**
172 * Get the (unique) UI configuration service for the program.
173 *
174 * @return the configuration service
175 */
176 public static UiConfigBundle getUiConfig() {
177 return uiconfig;
178 }
179
180 /**
181 * Get the (unique) {@link DataLoader} for the program.
182 *
183 * @return the {@link DataLoader}
184 */
185 public static DataLoader getCache() {
186 return cache;
187 }
188
189 /**
190 * Get the (unique) {link StringIdBundle} for the program.
191 *
192 * @return the {link StringIdBundle}
193 */
194 public static StringIdBundle getTrans() {
195 return trans;
196 }
197
198 /**
199 * Get the (unique) {@link LocalLibrary} for the program.
200 *
201 * @return the {@link LocalLibrary}
202 */
203 public static BasicLibrary getLibrary() {
204 if (lib == null) {
205 throw new NullPointerException("We don't have a library to return");
206 }
207
208 return lib;
209 }
210
211 /**
212 * Return the directory where to look for default cover pages.
213 *
214 * @return the default covers directory
215 */
216 public static File getCoverDir() {
217 return coverDir;
218 }
219
220 /**
221 * Return the directory where to store temporary files for the local reader.
222 *
223 * @return the directory
224 */
225 public static File getReaderDir() {
226 return readerTmp;
227 }
228
229 /**
230 * Return the directory where to store temporary files for the remote
231 * {@link LocalLibrary}.
232 *
233 * @param host
234 * the remote for this host
235 *
236 * @return the directory
237 */
238 public static File getRemoteDir(String host) {
239 remoteDir.mkdirs();
240
241 if (host != null) {
242 return new File(remoteDir, host);
243 }
244
245 return remoteDir;
246 }
247
248 /**
249 * Check if we need to check that a new version of Fanfix is available.
250 *
251 * @return TRUE if we need to
252 */
253 public static boolean isVersionCheckNeeded() {
254 try {
255 long wait = config.getInteger(Config.UPDATE_INTERVAL, 1) * 24 * 60
256 * 60 * 1000;
257 if (wait >= 0) {
258 String lastUpString = IOUtils.readSmallFile(new File(configDir,
259 "LAST_UPDATE"));
260 long delay = new Date().getTime()
261 - Long.parseLong(lastUpString);
262 if (delay > wait) {
263 return true;
264 }
265 } else {
266 return false;
267 }
268 } catch (Exception e) {
269 // No file or bad file:
270 return true;
271 }
272
273 return false;
274 }
275
276 /**
277 * Notify that we checked for a new version of Fanfix.
278 */
279 public static void setVersionChecked() {
280 try {
281 IOUtils.writeSmallFile(new File(configDir), "LAST_UPDATE",
282 Long.toString(new Date().getTime()));
283 } catch (IOException e) {
284 syserr(e);
285 }
286 }
287
288 /**
289 * Report an error to the user
290 *
291 * @param e
292 * the {@link Exception} to report
293 */
294 public static void syserr(Exception e) {
295 if (tracer != null) {
296 tracer.error(e);
297 }
298 }
299
300 /**
301 * Notify of a debug message.
302 *
303 * @param message
304 * the message
305 */
306 public static void trace(String message) {
307 if (tracer != null) {
308 tracer.trace(message);
309 }
310 }
311
312 /**
313 * Return a path, but support the special $HOME variable.
314 *
315 * @return the path
316 */
317 private static File getFile(Config id) {
318 return getFile(config.getString(id));
319 }
320
321 /**
322 * Return a path, but support the special $HOME variable.
323 *
324 * @return the path
325 */
326 private static File getFile(UiConfig id) {
327 return getFile(uiconfig.getString(id));
328 }
329
330 /**
331 * Return a path, but support the special $HOME variable.
332 *
333 * @return the path
334 */
335 private static File getFile(String path) {
336 File file = null;
337 if (path != null && !path.isEmpty()) {
338 path = path.replace('/', File.separatorChar);
339 if (path.contains("$HOME")) {
340 path = path.replace("$HOME",
341 "" + System.getProperty("user.home"));
342 }
343
344 file = new File(path);
345 }
346
347 return file;
348 }
349
350 /**
351 * The language to use for the application (NULL = default system language).
352 *
353 * @return the language
354 */
355 private static String getLang() {
356 String lang = config.getString(Config.LANG);
357
358 if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) {
359 lang = System.getenv("LANG");
360 }
361
362 if (lang != null && lang.isEmpty()) {
363 lang = null;
364 }
365
366 return lang;
367 }
368
369 /**
370 * Check that the given environment variable is "enabled".
371 *
372 * @param key
373 * the variable to check
374 *
375 * @return TRUE if it is
376 */
377 private static boolean checkEnv(String key) {
378 String value = System.getenv(key);
379 if (value != null) {
380 value = value.trim().toLowerCase();
381 if ("yes".equals(value) || "true".equals(value)
382 || "on".equals(value) || "1".equals(value)
383 || "y".equals(value)) {
384 return true;
385 }
386 }
387
388 return false;
389 }
390 }