Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix; |
2 | ||
3 | import java.io.File; | |
4 | import java.io.IOException; | |
b42117f1 | 5 | import java.util.Date; |
08fe2e33 NR |
6 | |
7 | import be.nikiroo.fanfix.bundles.Config; | |
8 | import be.nikiroo.fanfix.bundles.ConfigBundle; | |
99ccbdf6 | 9 | import be.nikiroo.fanfix.bundles.StringId; |
08fe2e33 | 10 | import be.nikiroo.fanfix.bundles.StringIdBundle; |
b4dc6ab5 NR |
11 | import be.nikiroo.fanfix.bundles.UiConfig; |
12 | import be.nikiroo.fanfix.bundles.UiConfigBundle; | |
e42573a0 NR |
13 | import be.nikiroo.fanfix.library.BasicLibrary; |
14 | import be.nikiroo.fanfix.library.LocalLibrary; | |
e023483b | 15 | import be.nikiroo.fanfix.library.RemoteLibrary; |
581d42c0 | 16 | import be.nikiroo.utils.Cache; |
b42117f1 | 17 | import be.nikiroo.utils.IOUtils; |
581d42c0 | 18 | import be.nikiroo.utils.TraceHandler; |
08fe2e33 NR |
19 | import be.nikiroo.utils.resources.Bundles; |
20 | ||
21 | /** | |
22 | * Global state for the program (services and singletons). | |
23 | * | |
24 | * @author niki | |
25 | */ | |
26 | public class Instance { | |
27 | private static ConfigBundle config; | |
b4dc6ab5 | 28 | private static UiConfigBundle uiconfig; |
08fe2e33 | 29 | private static StringIdBundle trans; |
f1fb834c | 30 | private static DataLoader cache; |
e023483b | 31 | private static BasicLibrary lib; |
08fe2e33 | 32 | private static File coverDir; |
3727aae2 | 33 | private static File readerTmp; |
b0e88ebd | 34 | private static File remoteDir; |
b42117f1 | 35 | private static String configDir; |
581d42c0 | 36 | private static TraceHandler tracer; |
a8209dd0 | 37 | |
08fe2e33 | 38 | static { |
62c63b07 NR |
39 | // Before we can configure it: |
40 | tracer = new TraceHandler(true, checkEnv("DEBUG"), false); | |
41 | ||
948637bc | 42 | // Most of the rest is dependent upon this: |
08fe2e33 NR |
43 | config = new ConfigBundle(); |
44 | ||
b42117f1 | 45 | configDir = System.getProperty("CONFIG_DIR"); |
22848428 NR |
46 | if (configDir == null) { |
47 | configDir = System.getenv("CONFIG_DIR"); | |
48 | } | |
b42117f1 | 49 | |
fe999aa4 NR |
50 | if (configDir == null) { |
51 | configDir = new File(System.getProperty("user.home"), ".fanfix") | |
52 | .getPath(); | |
53 | } | |
39c3c689 | 54 | |
b42117f1 NR |
55 | if (!new File(configDir).exists()) { |
56 | new File(configDir).mkdirs(); | |
57 | } else { | |
fe999aa4 NR |
58 | Bundles.setDirectory(configDir); |
59 | } | |
60 | ||
b42117f1 NR |
61 | try { |
62 | config = new ConfigBundle(); | |
63 | config.updateFile(configDir); | |
64 | } catch (IOException e) { | |
62c63b07 | 65 | tracer.error(e); |
b42117f1 NR |
66 | } |
67 | try { | |
68 | uiconfig = new UiConfigBundle(); | |
69 | uiconfig.updateFile(configDir); | |
70 | } catch (IOException e) { | |
62c63b07 | 71 | tracer.error(e); |
b42117f1 | 72 | } |
99ccbdf6 NR |
73 | |
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()); | |
77 | ||
78 | // Fix an old bug (we used to store custom translation files by | |
79 | // default): | |
80 | if (trans.getString(StringId.INPUT_DESC_CBZ) == null) { | |
81 | // TODO: create the deleteFile method | |
82 | // trans.deleteFile(configDir); | |
b42117f1 NR |
83 | } |
84 | ||
85 | Bundles.setDirectory(configDir); | |
86 | ||
b4dc6ab5 | 87 | uiconfig = new UiConfigBundle(); |
08fe2e33 | 88 | trans = new StringIdBundle(getLang()); |
2206ef66 | 89 | |
581d42c0 NR |
90 | boolean debug = Instance.getConfig() |
91 | .getBoolean(Config.DEBUG_ERR, false); | |
92 | boolean trace = Instance.getConfig().getBoolean(Config.DEBUG_TRACE, | |
93 | false); | |
08fe2e33 | 94 | coverDir = getFile(Config.DEFAULT_COVERS_DIR); |
3727aae2 | 95 | File tmp = getFile(Config.CACHE_DIR); |
b4dc6ab5 | 96 | readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER); |
e604986c | 97 | remoteDir = new File(configDir, "remote"); |
3727aae2 | 98 | |
d0114000 NR |
99 | if (checkEnv("NOUTF")) { |
100 | trans.setUnicode(false); | |
101 | } | |
102 | ||
103 | if (checkEnv("DEBUG")) { | |
104 | debug = true; | |
105 | } | |
106 | ||
62c63b07 | 107 | tracer = new TraceHandler(true, debug, trace); |
581d42c0 | 108 | |
e023483b NR |
109 | String remoteLib = config.getString(Config.DEFAULT_LIBRARY); |
110 | if (remoteLib == null || remoteLib.trim().isEmpty()) { | |
111 | try { | |
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)); | |
117 | } | |
118 | } else { | |
119 | int pos = remoteLib.lastIndexOf(":"); | |
120 | if (pos >= 0) { | |
121 | String port = remoteLib.substring(pos + 1).trim(); | |
122 | remoteLib = remoteLib.substring(0, pos); | |
123 | pos = remoteLib.lastIndexOf(":"); | |
124 | if (pos >= 0) { | |
125 | String host = remoteLib.substring(pos + 1).trim(); | |
126 | String key = remoteLib.substring(0, pos).trim(); | |
127 | ||
128 | try { | |
129 | tracer.trace("Contacting remote library " + host + ":" | |
130 | + port); | |
131 | lib = new RemoteLibrary(key, host, | |
132 | Integer.parseInt(port)); | |
133 | } catch (Exception e) { | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | if (lib == null) { | |
139 | tracer.error(new IOException( | |
140 | "Cannot create remote library for: " | |
141 | + getFile(Config.DEFAULT_LIBRARY))); | |
142 | } | |
778d8d85 NR |
143 | } |
144 | ||
68e370a4 NR |
145 | // Could have used: System.getProperty("java.io.tmpdir") |
146 | if (tmp == null) { | |
147 | tmp = new File(configDir, "tmp"); | |
148 | } | |
149 | if (readerTmp == null) { | |
150 | readerTmp = new File(configDir, "tmp-reader"); | |
3727aae2 | 151 | } |
68e370a4 | 152 | // |
08fe2e33 NR |
153 | |
154 | if (coverDir != null && !coverDir.exists()) { | |
62c63b07 | 155 | tracer.error(new IOException( |
08fe2e33 NR |
156 | "The 'default covers' directory does not exists: " |
157 | + coverDir)); | |
158 | coverDir = null; | |
159 | } | |
08fe2e33 | 160 | |
08fe2e33 | 161 | try { |
08fe2e33 NR |
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); | |
166 | ||
f1fb834c | 167 | cache = new DataLoader(tmp, ua, hours, hoursLarge); |
08fe2e33 | 168 | } catch (IOException e) { |
62c63b07 | 169 | tracer.error(new IOException( |
08fe2e33 NR |
170 | "Cannot create cache (will continue without cache)", e)); |
171 | } | |
172 | } | |
173 | ||
581d42c0 NR |
174 | /** |
175 | * The traces handler for this {@link Cache}. | |
62c63b07 NR |
176 | * <p> |
177 | * It is never NULL. | |
581d42c0 | 178 | * |
62c63b07 | 179 | * @return the traces handler (never NULL) |
581d42c0 NR |
180 | */ |
181 | public static TraceHandler getTraceHandler() { | |
182 | return tracer; | |
183 | } | |
184 | ||
185 | /** | |
186 | * The traces handler for this {@link Cache}. | |
187 | * | |
188 | * @param tracer | |
189 | * the new traces handler or NULL | |
190 | */ | |
191 | public static void setTraceHandler(TraceHandler tracer) { | |
62c63b07 NR |
192 | if (tracer == null) { |
193 | tracer = new TraceHandler(false, false, false); | |
194 | } | |
195 | ||
581d42c0 NR |
196 | Instance.tracer = tracer; |
197 | } | |
198 | ||
08fe2e33 NR |
199 | /** |
200 | * Get the (unique) configuration service for the program. | |
201 | * | |
202 | * @return the configuration service | |
203 | */ | |
204 | public static ConfigBundle getConfig() { | |
205 | return config; | |
206 | } | |
207 | ||
b4dc6ab5 NR |
208 | /** |
209 | * Get the (unique) UI configuration service for the program. | |
210 | * | |
211 | * @return the configuration service | |
212 | */ | |
213 | public static UiConfigBundle getUiConfig() { | |
214 | return uiconfig; | |
215 | } | |
216 | ||
08fe2e33 | 217 | /** |
f1fb834c | 218 | * Get the (unique) {@link DataLoader} for the program. |
08fe2e33 | 219 | * |
f1fb834c | 220 | * @return the {@link DataLoader} |
08fe2e33 | 221 | */ |
f1fb834c | 222 | public static DataLoader getCache() { |
08fe2e33 NR |
223 | return cache; |
224 | } | |
225 | ||
226 | /** | |
227 | * Get the (unique) {link StringIdBundle} for the program. | |
39c3c689 | 228 | * |
08fe2e33 NR |
229 | * @return the {link StringIdBundle} |
230 | */ | |
231 | public static StringIdBundle getTrans() { | |
232 | return trans; | |
233 | } | |
234 | ||
235 | /** | |
68e2c6d2 | 236 | * Get the (unique) {@link LocalLibrary} for the program. |
08fe2e33 | 237 | * |
68e2c6d2 | 238 | * @return the {@link LocalLibrary} |
08fe2e33 | 239 | */ |
68e2c6d2 | 240 | public static BasicLibrary getLibrary() { |
778d8d85 NR |
241 | if (lib == null) { |
242 | throw new NullPointerException("We don't have a library to return"); | |
243 | } | |
244 | ||
08fe2e33 NR |
245 | return lib; |
246 | } | |
247 | ||
248 | /** | |
249 | * Return the directory where to look for default cover pages. | |
250 | * | |
251 | * @return the default covers directory | |
252 | */ | |
253 | public static File getCoverDir() { | |
254 | return coverDir; | |
255 | } | |
256 | ||
3727aae2 NR |
257 | /** |
258 | * Return the directory where to store temporary files for the local reader. | |
259 | * | |
260 | * @return the directory | |
261 | */ | |
262 | public static File getReaderDir() { | |
263 | return readerTmp; | |
264 | } | |
265 | ||
b0e88ebd NR |
266 | /** |
267 | * Return the directory where to store temporary files for the remote | |
68e2c6d2 | 268 | * {@link LocalLibrary}. |
b0e88ebd NR |
269 | * |
270 | * @param host | |
271 | * the remote for this host | |
272 | * | |
273 | * @return the directory | |
274 | */ | |
275 | public static File getRemoteDir(String host) { | |
276 | remoteDir.mkdirs(); | |
277 | ||
278 | if (host != null) { | |
279 | return new File(remoteDir, host); | |
280 | } | |
281 | ||
282 | return remoteDir; | |
283 | } | |
284 | ||
b42117f1 NR |
285 | /** |
286 | * Check if we need to check that a new version of Fanfix is available. | |
287 | * | |
288 | * @return TRUE if we need to | |
289 | */ | |
290 | public static boolean isVersionCheckNeeded() { | |
291 | try { | |
a3641e4b NR |
292 | long wait = config.getInteger(Config.UPDATE_INTERVAL, 1) * 24 * 60 |
293 | * 60 * 1000; | |
b42117f1 NR |
294 | if (wait >= 0) { |
295 | String lastUpString = IOUtils.readSmallFile(new File(configDir, | |
296 | "LAST_UPDATE")); | |
297 | long delay = new Date().getTime() | |
298 | - Long.parseLong(lastUpString); | |
299 | if (delay > wait) { | |
300 | return true; | |
301 | } | |
302 | } else { | |
303 | return false; | |
304 | } | |
305 | } catch (Exception e) { | |
306 | // No file or bad file: | |
307 | return true; | |
308 | } | |
309 | ||
310 | return false; | |
311 | } | |
312 | ||
313 | /** | |
314 | * Notify that we checked for a new version of Fanfix. | |
315 | */ | |
316 | public static void setVersionChecked() { | |
317 | try { | |
318 | IOUtils.writeSmallFile(new File(configDir), "LAST_UPDATE", | |
319 | Long.toString(new Date().getTime())); | |
320 | } catch (IOException e) { | |
581d42c0 | 321 | tracer.error(e); |
08fe2e33 NR |
322 | } |
323 | } | |
324 | ||
325 | /** | |
326 | * Return a path, but support the special $HOME variable. | |
327 | * | |
328 | * @return the path | |
329 | */ | |
330 | private static File getFile(Config id) { | |
b4dc6ab5 NR |
331 | return getFile(config.getString(id)); |
332 | } | |
333 | ||
334 | /** | |
335 | * Return a path, but support the special $HOME variable. | |
336 | * | |
337 | * @return the path | |
338 | */ | |
339 | private static File getFile(UiConfig id) { | |
340 | return getFile(uiconfig.getString(id)); | |
341 | } | |
342 | ||
343 | /** | |
344 | * Return a path, but support the special $HOME variable. | |
345 | * | |
346 | * @return the path | |
347 | */ | |
348 | private static File getFile(String path) { | |
08fe2e33 | 349 | File file = null; |
08fe2e33 NR |
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")); | |
355 | } | |
356 | ||
357 | file = new File(path); | |
358 | } | |
359 | ||
360 | return file; | |
361 | } | |
362 | ||
363 | /** | |
364 | * The language to use for the application (NULL = default system language). | |
365 | * | |
366 | * @return the language | |
367 | */ | |
368 | private static String getLang() { | |
369 | String lang = config.getString(Config.LANG); | |
370 | ||
371 | if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) { | |
372 | lang = System.getenv("LANG"); | |
373 | } | |
374 | ||
375 | if (lang != null && lang.isEmpty()) { | |
376 | lang = null; | |
377 | } | |
378 | ||
379 | return lang; | |
380 | } | |
d0114000 NR |
381 | |
382 | /** | |
383 | * Check that the given environment variable is "enabled". | |
384 | * | |
385 | * @param key | |
386 | * the variable to check | |
387 | * | |
388 | * @return TRUE if it is | |
389 | */ | |
390 | private static boolean checkEnv(String key) { | |
391 | String value = System.getenv(key); | |
392 | if (value != null) { | |
393 | value = value.trim().toLowerCase(); | |
394 | if ("yes".equals(value) || "true".equals(value) | |
395 | || "on".equals(value) || "1".equals(value) | |
396 | || "y".equals(value)) { | |
397 | return true; | |
398 | } | |
399 | } | |
400 | ||
401 | return false; | |
402 | } | |
08fe2e33 | 403 | } |