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; | |
581d42c0 | 15 | import be.nikiroo.utils.Cache; |
b42117f1 | 16 | import be.nikiroo.utils.IOUtils; |
581d42c0 | 17 | import be.nikiroo.utils.TraceHandler; |
08fe2e33 NR |
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; | |
b4dc6ab5 | 27 | private static UiConfigBundle uiconfig; |
08fe2e33 | 28 | private static StringIdBundle trans; |
f1fb834c | 29 | private static DataLoader cache; |
68e2c6d2 | 30 | private static LocalLibrary lib; |
08fe2e33 | 31 | private static File coverDir; |
3727aae2 | 32 | private static File readerTmp; |
b0e88ebd | 33 | private static File remoteDir; |
b42117f1 | 34 | private static String configDir; |
581d42c0 | 35 | private static TraceHandler tracer; |
a8209dd0 | 36 | |
08fe2e33 | 37 | static { |
948637bc | 38 | // Most of the rest is dependent upon this: |
08fe2e33 NR |
39 | config = new ConfigBundle(); |
40 | ||
b42117f1 | 41 | configDir = System.getProperty("CONFIG_DIR"); |
22848428 NR |
42 | if (configDir == null) { |
43 | configDir = System.getenv("CONFIG_DIR"); | |
44 | } | |
b42117f1 | 45 | |
fe999aa4 NR |
46 | if (configDir == null) { |
47 | configDir = new File(System.getProperty("user.home"), ".fanfix") | |
48 | .getPath(); | |
49 | } | |
39c3c689 | 50 | |
b42117f1 NR |
51 | if (!new File(configDir).exists()) { |
52 | new File(configDir).mkdirs(); | |
53 | } else { | |
fe999aa4 NR |
54 | Bundles.setDirectory(configDir); |
55 | } | |
56 | ||
b42117f1 NR |
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 | } | |
99ccbdf6 NR |
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); | |
b42117f1 NR |
79 | } |
80 | ||
81 | Bundles.setDirectory(configDir); | |
82 | ||
b4dc6ab5 | 83 | uiconfig = new UiConfigBundle(); |
08fe2e33 | 84 | trans = new StringIdBundle(getLang()); |
2206ef66 | 85 | |
581d42c0 NR |
86 | boolean debug = Instance.getConfig() |
87 | .getBoolean(Config.DEBUG_ERR, false); | |
88 | boolean trace = Instance.getConfig().getBoolean(Config.DEBUG_TRACE, | |
89 | false); | |
08fe2e33 | 90 | coverDir = getFile(Config.DEFAULT_COVERS_DIR); |
3727aae2 | 91 | File tmp = getFile(Config.CACHE_DIR); |
b4dc6ab5 | 92 | readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER); |
e604986c | 93 | remoteDir = new File(configDir, "remote"); |
3727aae2 | 94 | |
d0114000 NR |
95 | if (checkEnv("NOUTF")) { |
96 | trans.setUnicode(false); | |
97 | } | |
98 | ||
99 | if (checkEnv("DEBUG")) { | |
100 | debug = true; | |
101 | } | |
102 | ||
581d42c0 NR |
103 | tracer = new TraceHandler(); |
104 | tracer.setShowErrorDetails(debug); | |
105 | tracer.setShowTraces(trace); | |
106 | ||
778d8d85 NR |
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 | ||
68e370a4 NR |
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"); | |
3727aae2 | 120 | } |
68e370a4 | 121 | // |
08fe2e33 NR |
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 | } | |
08fe2e33 | 129 | |
08fe2e33 | 130 | try { |
08fe2e33 NR |
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 | ||
f1fb834c | 136 | cache = new DataLoader(tmp, ua, hours, hoursLarge); |
08fe2e33 NR |
137 | } catch (IOException e) { |
138 | syserr(new IOException( | |
139 | "Cannot create cache (will continue without cache)", e)); | |
140 | } | |
141 | } | |
142 | ||
581d42c0 NR |
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 | ||
08fe2e33 NR |
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 | ||
b4dc6ab5 NR |
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 | ||
08fe2e33 | 180 | /** |
f1fb834c | 181 | * Get the (unique) {@link DataLoader} for the program. |
08fe2e33 | 182 | * |
f1fb834c | 183 | * @return the {@link DataLoader} |
08fe2e33 | 184 | */ |
f1fb834c | 185 | public static DataLoader getCache() { |
08fe2e33 NR |
186 | return cache; |
187 | } | |
188 | ||
189 | /** | |
190 | * Get the (unique) {link StringIdBundle} for the program. | |
39c3c689 | 191 | * |
08fe2e33 NR |
192 | * @return the {link StringIdBundle} |
193 | */ | |
194 | public static StringIdBundle getTrans() { | |
195 | return trans; | |
196 | } | |
197 | ||
198 | /** | |
68e2c6d2 | 199 | * Get the (unique) {@link LocalLibrary} for the program. |
08fe2e33 | 200 | * |
68e2c6d2 | 201 | * @return the {@link LocalLibrary} |
08fe2e33 | 202 | */ |
68e2c6d2 | 203 | public static BasicLibrary getLibrary() { |
778d8d85 NR |
204 | if (lib == null) { |
205 | throw new NullPointerException("We don't have a library to return"); | |
206 | } | |
207 | ||
08fe2e33 NR |
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 | ||
3727aae2 NR |
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 | ||
b0e88ebd NR |
229 | /** |
230 | * Return the directory where to store temporary files for the remote | |
68e2c6d2 | 231 | * {@link LocalLibrary}. |
b0e88ebd NR |
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 | ||
b42117f1 NR |
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 { | |
a3641e4b NR |
255 | long wait = config.getInteger(Config.UPDATE_INTERVAL, 1) * 24 * 60 |
256 | * 60 * 1000; | |
b42117f1 NR |
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 | ||
08fe2e33 NR |
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) { | |
581d42c0 NR |
295 | if (tracer != null) { |
296 | tracer.error(e); | |
08fe2e33 NR |
297 | } |
298 | } | |
299 | ||
315f14ae | 300 | /** |
a8209dd0 | 301 | * Notify of a debug message. |
315f14ae | 302 | * |
a8209dd0 NR |
303 | * @param message |
304 | * the message | |
315f14ae | 305 | */ |
a8209dd0 | 306 | public static void trace(String message) { |
581d42c0 NR |
307 | if (tracer != null) { |
308 | tracer.trace(message); | |
a8209dd0 | 309 | } |
315f14ae NR |
310 | } |
311 | ||
08fe2e33 NR |
312 | /** |
313 | * Return a path, but support the special $HOME variable. | |
314 | * | |
315 | * @return the path | |
316 | */ | |
317 | private static File getFile(Config id) { | |
b4dc6ab5 NR |
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) { | |
08fe2e33 | 336 | File file = null; |
08fe2e33 NR |
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 | } | |
d0114000 NR |
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 | } | |
08fe2e33 | 390 | } |