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; |
5bc9573b | 11 | import be.nikiroo.fanfix.bundles.StringIdGuiBundle; |
b4dc6ab5 NR |
12 | import be.nikiroo.fanfix.bundles.UiConfig; |
13 | import be.nikiroo.fanfix.bundles.UiConfigBundle; | |
e42573a0 | 14 | import be.nikiroo.fanfix.library.BasicLibrary; |
5895a958 | 15 | import be.nikiroo.fanfix.library.CacheLibrary; |
e42573a0 | 16 | import be.nikiroo.fanfix.library.LocalLibrary; |
e023483b | 17 | import be.nikiroo.fanfix.library.RemoteLibrary; |
581d42c0 | 18 | import be.nikiroo.utils.Cache; |
b42117f1 | 19 | import be.nikiroo.utils.IOUtils; |
12443642 | 20 | import be.nikiroo.utils.Image; |
dddfac1d | 21 | import be.nikiroo.utils.Proxy; |
2aac79c7 | 22 | import be.nikiroo.utils.TempFiles; |
581d42c0 | 23 | import be.nikiroo.utils.TraceHandler; |
08fe2e33 NR |
24 | import be.nikiroo.utils.resources.Bundles; |
25 | ||
26 | /** | |
27 | * Global state for the program (services and singletons). | |
28 | * | |
29 | * @author niki | |
30 | */ | |
31 | public class Instance { | |
d66deb8d NR |
32 | static private Instance instance; |
33 | static private Object instancelock = new Object(); | |
34 | ||
35 | private ConfigBundle config; | |
36 | private UiConfigBundle uiconfig; | |
37 | private StringIdBundle trans; | |
38 | private DataLoader cache; | |
39 | private StringIdGuiBundle transGui; | |
40 | private BasicLibrary lib; | |
41 | private File coverDir; | |
42 | private File readerTmp; | |
43 | private File remoteDir; | |
44 | private String configDir; | |
45 | private TraceHandler tracer; | |
46 | private TempFiles tempFiles; | |
ee9b7083 NR |
47 | |
48 | /** | |
49 | * Initialise the instance -- if already initialised, nothing will happen. | |
50 | * <p> | |
4ce96da3 NR |
51 | * Before calling this method, you may call |
52 | * {@link Bundles#setDirectory(String)} if wanted. | |
53 | * <p> | |
54 | * Note that this method will honour some environment variables, the 3 most | |
55 | * important ones probably being: | |
56 | * <ul> | |
57 | * <li><tt>DEBUG</tt>: will enable DEBUG output if set to 1 (or Y or TRUE or | |
58 | * ON, case insensitive)</li> | |
59 | * <li><tt>CONFIG_DIR</tt>: will use this directory as configuration | |
60 | * directory (supports $HOME notation, defaults to $HOME/.fanfix</li> | |
61 | * <li><tt>BOOKS_DIR</tt>: will use this directory as library directory | |
62 | * (supports $HOME notation, defaults to $HOME/Books</li> | |
63 | * </ul> | |
ee9b7083 NR |
64 | */ |
65 | static public void init() { | |
6a211e41 NR |
66 | init(false); |
67 | } | |
68 | ||
69 | /** | |
d66deb8d NR |
70 | * Initialise the instance -- if already initialised, nothing will happen unless |
71 | * you pass TRUE to <tt>force</tt>. | |
6a211e41 | 72 | * <p> |
d66deb8d NR |
73 | * Before calling this method, you may call {@link Bundles#setDirectory(String)} |
74 | * if wanted. | |
6a211e41 | 75 | * <p> |
d66deb8d NR |
76 | * Note: forcing the initialisation can be dangerous, so make sure to only make |
77 | * it under controlled circumstances -- for instance, at the start of the | |
78 | * program, you could call {@link Instance#init()}, change some settings because | |
79 | * you want to force those settings (it will also forbid users to change them!) | |
80 | * and then call {@link Instance#init(boolean)} with <tt>force</tt> set to TRUE. | |
6a211e41 | 81 | * |
d66deb8d | 82 | * @param force force the initialisation even if already initialised |
6a211e41 NR |
83 | */ |
84 | static public void init(boolean force) { | |
d66deb8d NR |
85 | synchronized (instancelock) { |
86 | if (instance == null || force) { | |
87 | instance = new Instance(); | |
88 | } | |
ee9b7083 NR |
89 | } |
90 | ||
d66deb8d NR |
91 | } |
92 | ||
93 | /** | |
94 | * Force-initialise the {@link Instance} to a known value. | |
95 | * <p> | |
96 | * Usually for DEBUG/Test purposes. | |
97 | * | |
98 | * @param instance the actual Instance to use | |
99 | */ | |
100 | static public void init(Instance instance) { | |
101 | Instance.instance = instance; | |
102 | } | |
103 | ||
104 | /** | |
105 | * The (mostly unique) instance of this {@link Instance}. | |
106 | * | |
107 | * @return the (mostly unique) instance | |
108 | */ | |
109 | public static Instance getInstance() { | |
110 | return instance; | |
111 | } | |
ee9b7083 | 112 | |
d66deb8d NR |
113 | /** |
114 | * Actually initialise the instance. | |
115 | * <p> | |
116 | * Before calling this method, you may call {@link Bundles#setDirectory(String)} | |
117 | * if wanted. | |
118 | */ | |
119 | protected Instance() { | |
62c63b07 | 120 | // Before we can configure it: |
a3c35586 NR |
121 | Boolean debug = checkEnv("DEBUG"); |
122 | boolean trace = debug != null && debug; | |
123 | tracer = new TraceHandler(true, trace, trace); | |
62c63b07 | 124 | |
9f705f1a NR |
125 | // config dir: |
126 | configDir = getConfigDir(); | |
b42117f1 NR |
127 | if (!new File(configDir).exists()) { |
128 | new File(configDir).mkdirs(); | |
fe999aa4 NR |
129 | } |
130 | ||
9f705f1a | 131 | // Most of the rest is dependent upon this: |
38febea9 | 132 | createConfigs(configDir, false); |
a3c35586 | 133 | |
4ff0b1a9 | 134 | // Proxy support |
d66deb8d | 135 | Proxy.use(config.getString(Config.NETWORK_PROXY)); |
2206ef66 | 136 | |
9f705f1a | 137 | // update tracer: |
a3c35586 | 138 | if (debug == null) { |
d66deb8d NR |
139 | debug = config.getBoolean(Config.DEBUG_ERR, false); |
140 | trace = config.getBoolean(Config.DEBUG_TRACE, false); | |
d0114000 NR |
141 | } |
142 | ||
62c63b07 | 143 | tracer = new TraceHandler(true, debug, trace); |
581d42c0 | 144 | |
9f705f1a NR |
145 | // default Library |
146 | remoteDir = new File(configDir, "remote"); | |
147 | lib = createDefaultLibrary(remoteDir); | |
778d8d85 | 148 | |
12443642 | 149 | // create cache and TMP |
0275353d | 150 | File tmp = getFile(Config.CACHE_DIR, configDir, "tmp"); |
7cd006eb NR |
151 | Image.setTemporaryFilesRoot(new File(tmp.getParent(), "tmp.images")); |
152 | ||
13fdb89a | 153 | String ua = config.getString(Config.NETWORK_USER_AGENT, ""); |
08fe2e33 | 154 | try { |
b7cd9db8 NR |
155 | int hours = config.getInteger(Config.CACHE_MAX_TIME_CHANGING, 0); |
156 | int hoursLarge = config.getInteger(Config.CACHE_MAX_TIME_STABLE, 0); | |
f1fb834c | 157 | cache = new DataLoader(tmp, ua, hours, hoursLarge); |
08fe2e33 | 158 | } catch (IOException e) { |
d66deb8d | 159 | tracer.error(new IOException("Cannot create cache (will continue without cache)", e)); |
ae78e517 | 160 | cache = new DataLoader(ua); |
08fe2e33 | 161 | } |
ae78e517 NR |
162 | |
163 | cache.setTraceHandler(tracer); | |
9f705f1a NR |
164 | |
165 | // readerTmp / coverDir | |
0275353d NR |
166 | readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER, configDir, "tmp-reader"); |
167 | coverDir = getFile(Config.DEFAULT_COVERS_DIR, configDir, "covers"); | |
7cd006eb | 168 | coverDir.mkdirs(); |
2aac79c7 NR |
169 | |
170 | try { | |
171 | tempFiles = new TempFiles("fanfix"); | |
172 | } catch (IOException e) { | |
173 | tracer.error(new IOException("Cannot create temporary directory", e)); | |
174 | } | |
08fe2e33 NR |
175 | } |
176 | ||
581d42c0 NR |
177 | /** |
178 | * The traces handler for this {@link Cache}. | |
62c63b07 NR |
179 | * <p> |
180 | * It is never NULL. | |
581d42c0 | 181 | * |
62c63b07 | 182 | * @return the traces handler (never NULL) |
581d42c0 | 183 | */ |
d66deb8d | 184 | public TraceHandler getTraceHandler() { |
581d42c0 NR |
185 | return tracer; |
186 | } | |
187 | ||
188 | /** | |
189 | * The traces handler for this {@link Cache}. | |
190 | * | |
d66deb8d | 191 | * @param tracer the new traces handler or NULL |
581d42c0 | 192 | */ |
d66deb8d | 193 | public void setTraceHandler(TraceHandler tracer) { |
62c63b07 NR |
194 | if (tracer == null) { |
195 | tracer = new TraceHandler(false, false, false); | |
196 | } | |
197 | ||
d66deb8d | 198 | this.tracer = tracer; |
ae78e517 | 199 | cache.setTraceHandler(tracer); |
581d42c0 NR |
200 | } |
201 | ||
08fe2e33 NR |
202 | /** |
203 | * Get the (unique) configuration service for the program. | |
204 | * | |
205 | * @return the configuration service | |
206 | */ | |
d66deb8d | 207 | public ConfigBundle getConfig() { |
08fe2e33 NR |
208 | return config; |
209 | } | |
210 | ||
b4dc6ab5 NR |
211 | /** |
212 | * Get the (unique) UI configuration service for the program. | |
213 | * | |
214 | * @return the configuration service | |
215 | */ | |
d66deb8d | 216 | public UiConfigBundle getUiConfig() { |
b4dc6ab5 NR |
217 | return uiconfig; |
218 | } | |
219 | ||
ae78e517 NR |
220 | /** |
221 | * Reset the configuration. | |
222 | * | |
d66deb8d | 223 | * @param resetTrans also reset the translation files |
ae78e517 | 224 | */ |
d66deb8d | 225 | public void resetConfig(boolean resetTrans) { |
ae78e517 NR |
226 | String dir = Bundles.getDirectory(); |
227 | Bundles.setDirectory(null); | |
228 | try { | |
229 | try { | |
230 | ConfigBundle config = new ConfigBundle(); | |
231 | config.updateFile(configDir); | |
232 | } catch (IOException e) { | |
233 | tracer.error(e); | |
234 | } | |
235 | try { | |
236 | UiConfigBundle uiconfig = new UiConfigBundle(); | |
237 | uiconfig.updateFile(configDir); | |
238 | } catch (IOException e) { | |
239 | tracer.error(e); | |
240 | } | |
241 | ||
242 | if (resetTrans) { | |
243 | try { | |
244 | StringIdBundle trans = new StringIdBundle(null); | |
245 | trans.updateFile(configDir); | |
246 | } catch (IOException e) { | |
247 | tracer.error(e); | |
248 | } | |
249 | } | |
250 | } finally { | |
251 | Bundles.setDirectory(dir); | |
252 | } | |
253 | } | |
254 | ||
08fe2e33 | 255 | /** |
f1fb834c | 256 | * Get the (unique) {@link DataLoader} for the program. |
08fe2e33 | 257 | * |
f1fb834c | 258 | * @return the {@link DataLoader} |
08fe2e33 | 259 | */ |
d66deb8d | 260 | public DataLoader getCache() { |
08fe2e33 NR |
261 | return cache; |
262 | } | |
263 | ||
264 | /** | |
265 | * Get the (unique) {link StringIdBundle} for the program. | |
5bc9573b NR |
266 | * <p> |
267 | * This is used for the translations of the core parts of Fanfix. | |
39c3c689 | 268 | * |
08fe2e33 NR |
269 | * @return the {link StringIdBundle} |
270 | */ | |
d66deb8d | 271 | public StringIdBundle getTrans() { |
08fe2e33 NR |
272 | return trans; |
273 | } | |
274 | ||
5bc9573b NR |
275 | /** |
276 | * Get the (unique) {link StringIdGuiBundle} for the program. | |
277 | * <p> | |
278 | * This is used for the translations of the GUI parts of Fanfix. | |
279 | * | |
280 | * @return the {link StringIdGuiBundle} | |
281 | */ | |
d66deb8d | 282 | public StringIdGuiBundle getTransGui() { |
5bc9573b NR |
283 | return transGui; |
284 | } | |
285 | ||
08fe2e33 | 286 | /** |
68e2c6d2 | 287 | * Get the (unique) {@link LocalLibrary} for the program. |
08fe2e33 | 288 | * |
68e2c6d2 | 289 | * @return the {@link LocalLibrary} |
08fe2e33 | 290 | */ |
d66deb8d | 291 | public BasicLibrary getLibrary() { |
778d8d85 NR |
292 | if (lib == null) { |
293 | throw new NullPointerException("We don't have a library to return"); | |
294 | } | |
295 | ||
08fe2e33 NR |
296 | return lib; |
297 | } | |
298 | ||
299 | /** | |
300 | * Return the directory where to look for default cover pages. | |
301 | * | |
302 | * @return the default covers directory | |
303 | */ | |
d66deb8d | 304 | public File getCoverDir() { |
08fe2e33 NR |
305 | return coverDir; |
306 | } | |
307 | ||
3727aae2 NR |
308 | /** |
309 | * Return the directory where to store temporary files for the local reader. | |
310 | * | |
311 | * @return the directory | |
312 | */ | |
d66deb8d | 313 | public File getReaderDir() { |
3727aae2 NR |
314 | return readerTmp; |
315 | } | |
316 | ||
b0e88ebd NR |
317 | /** |
318 | * Return the directory where to store temporary files for the remote | |
68e2c6d2 | 319 | * {@link LocalLibrary}. |
b0e88ebd | 320 | * |
d66deb8d | 321 | * @param host the remote for this host |
b0e88ebd NR |
322 | * |
323 | * @return the directory | |
324 | */ | |
d66deb8d | 325 | public File getRemoteDir(String host) { |
9f705f1a NR |
326 | return getRemoteDir(remoteDir, host); |
327 | } | |
328 | ||
329 | /** | |
330 | * Return the directory where to store temporary files for the remote | |
331 | * {@link LocalLibrary}. | |
332 | * | |
d66deb8d NR |
333 | * @param remoteDir the base remote directory |
334 | * @param host the remote for this host | |
9f705f1a NR |
335 | * |
336 | * @return the directory | |
337 | */ | |
d66deb8d | 338 | private File getRemoteDir(File remoteDir, String host) { |
b0e88ebd NR |
339 | remoteDir.mkdirs(); |
340 | ||
341 | if (host != null) { | |
342 | return new File(remoteDir, host); | |
343 | } | |
344 | ||
345 | return remoteDir; | |
346 | } | |
347 | ||
b42117f1 NR |
348 | /** |
349 | * Check if we need to check that a new version of Fanfix is available. | |
350 | * | |
351 | * @return TRUE if we need to | |
352 | */ | |
d66deb8d | 353 | public boolean isVersionCheckNeeded() { |
b42117f1 | 354 | try { |
d66deb8d | 355 | long wait = config.getInteger(Config.NETWORK_UPDATE_INTERVAL, 0) * 24 * 60 * 60 * 1000; |
b42117f1 | 356 | if (wait >= 0) { |
d66deb8d NR |
357 | String lastUpString = IOUtils.readSmallFile(new File(configDir, "LAST_UPDATE")); |
358 | long delay = new Date().getTime() - Long.parseLong(lastUpString); | |
b42117f1 NR |
359 | if (delay > wait) { |
360 | return true; | |
361 | } | |
362 | } else { | |
363 | return false; | |
364 | } | |
365 | } catch (Exception e) { | |
366 | // No file or bad file: | |
367 | return true; | |
368 | } | |
369 | ||
370 | return false; | |
371 | } | |
372 | ||
373 | /** | |
374 | * Notify that we checked for a new version of Fanfix. | |
375 | */ | |
d66deb8d | 376 | public void setVersionChecked() { |
b42117f1 | 377 | try { |
d66deb8d | 378 | IOUtils.writeSmallFile(new File(configDir), "LAST_UPDATE", Long.toString(new Date().getTime())); |
b42117f1 | 379 | } catch (IOException e) { |
581d42c0 | 380 | tracer.error(e); |
08fe2e33 NR |
381 | } |
382 | } | |
383 | ||
2aac79c7 NR |
384 | /** |
385 | * The facility to use temporary files in this program. | |
386 | * <p> | |
387 | * <b>MUST</b> be closed at end of program. | |
388 | * | |
389 | * @return the facility | |
390 | */ | |
d66deb8d | 391 | public TempFiles getTempFiles() { |
2aac79c7 NR |
392 | return tempFiles; |
393 | } | |
394 | ||
9f705f1a | 395 | /** |
d66deb8d NR |
396 | * The configuration directory (will check, in order of preference, the system |
397 | * properties, the environment and then defaults to | |
f466a217 | 398 | * {@link Instance#getHome()}/.fanfix). |
9f705f1a NR |
399 | * |
400 | * @return the config directory | |
401 | */ | |
d66deb8d | 402 | private String getConfigDir() { |
f466a217 | 403 | String configDir = System.getProperty("CONFIG_DIR"); |
9f705f1a NR |
404 | |
405 | if (configDir == null) { | |
406 | configDir = System.getenv("CONFIG_DIR"); | |
407 | } | |
408 | ||
409 | if (configDir == null) { | |
410 | configDir = new File(getHome(), ".fanfix").getPath(); | |
411 | } | |
412 | ||
413 | return configDir; | |
414 | } | |
415 | ||
416 | /** | |
417 | * Create the config variables ({@link Instance#config}, | |
5bc9573b NR |
418 | * {@link Instance#uiconfig}, {@link Instance#trans} and |
419 | * {@link Instance#transGui}). | |
9f705f1a | 420 | * |
d66deb8d NR |
421 | * @param configDir the directory where to find the configuration files |
422 | * @param refresh TRUE to reset the configuration files from the default | |
423 | * included ones | |
9f705f1a | 424 | */ |
d66deb8d | 425 | private void createConfigs(String configDir, boolean refresh) { |
9f705f1a NR |
426 | if (!refresh) { |
427 | Bundles.setDirectory(configDir); | |
428 | } | |
429 | ||
430 | try { | |
431 | config = new ConfigBundle(); | |
432 | config.updateFile(configDir); | |
433 | } catch (IOException e) { | |
434 | tracer.error(e); | |
435 | } | |
436 | ||
437 | try { | |
438 | uiconfig = new UiConfigBundle(); | |
439 | uiconfig.updateFile(configDir); | |
440 | } catch (IOException e) { | |
441 | tracer.error(e); | |
442 | } | |
443 | ||
444 | // No updateFile for this one! (we do not want the user to have custom | |
445 | // translations that won't accept updates from newer versions) | |
446 | trans = new StringIdBundle(getLang()); | |
5bc9573b | 447 | transGui = new StringIdGuiBundle(getLang()); |
9f705f1a NR |
448 | |
449 | // Fix an old bug (we used to store custom translation files by | |
450 | // default): | |
451 | if (trans.getString(StringId.INPUT_DESC_CBZ) == null) { | |
452 | trans.deleteFile(configDir); | |
453 | } | |
454 | ||
a3c35586 NR |
455 | Boolean noutf = checkEnv("NOUTF"); |
456 | if (noutf != null && noutf) { | |
9f705f1a | 457 | trans.setUnicode(false); |
5bc9573b | 458 | transGui.setUnicode(false); |
9f705f1a NR |
459 | } |
460 | ||
461 | Bundles.setDirectory(configDir); | |
462 | } | |
463 | ||
464 | /** | |
465 | * Create the default library as specified by the config. | |
466 | * | |
d66deb8d | 467 | * @param remoteDir the base remote directory if needed |
9f705f1a NR |
468 | * |
469 | * @return the default {@link BasicLibrary} | |
470 | */ | |
d66deb8d | 471 | private BasicLibrary createDefaultLibrary(File remoteDir) { |
9f705f1a NR |
472 | BasicLibrary lib = null; |
473 | ||
d66deb8d | 474 | boolean useRemote = config.getBoolean(Config.REMOTE_LIBRARY_ENABLED, false); |
459b4c28 NR |
475 | if (useRemote) { |
476 | String host = null; | |
477 | int port = -1; | |
478 | try { | |
479 | host = config.getString(Config.REMOTE_LIBRARY_HOST); | |
480 | port = config.getInteger(Config.REMOTE_LIBRARY_PORT, -1); | |
481 | String key = config.getString(Config.REMOTE_LIBRARY_KEY); | |
482 | ||
483 | tracer.trace("Selecting remote library " + host + ":" + port); | |
484 | lib = new RemoteLibrary(key, host, port); | |
d66deb8d | 485 | lib = new CacheLibrary(getRemoteDir(remoteDir, host), lib, uiconfig); |
459b4c28 | 486 | } catch (Exception e) { |
d66deb8d | 487 | tracer.error(new IOException("Cannot create remote library for: " + host + ":" + port, e)); |
459b4c28 NR |
488 | } |
489 | } else { | |
7cd006eb | 490 | String libDir = System.getenv("BOOKS_DIR"); |
9f705f1a | 491 | if (libDir == null || libDir.isEmpty()) { |
0275353d | 492 | libDir = getFile(Config.LIBRARY_DIR, configDir, "$HOME/Books").getPath(); |
9f705f1a NR |
493 | } |
494 | try { | |
0275353d | 495 | lib = new LocalLibrary(new File(libDir), config); |
9f705f1a | 496 | } catch (Exception e) { |
0275353d | 497 | tracer.error(new IOException("Cannot create library for directory: " + libDir, e)); |
9f705f1a | 498 | } |
9f705f1a NR |
499 | } |
500 | ||
501 | return lib; | |
502 | } | |
503 | ||
08fe2e33 NR |
504 | /** |
505 | * Return a path, but support the special $HOME variable. | |
506 | * | |
d66deb8d | 507 | * @param id the key for the path, which may contain "$HOME" |
0275353d NR |
508 | * @param configDir the directory to use as base if not absolute |
509 | * @param def the default value if none (will be configDir-rooted if needed) | |
d66deb8d | 510 | * @return the path, with expanded "$HOME" if needed |
08fe2e33 | 511 | */ |
0275353d NR |
512 | protected File getFile(Config id, String configDir, String def) { |
513 | String path = config.getString(id, def); | |
514 | return getFile(path, configDir); | |
b4dc6ab5 NR |
515 | } |
516 | ||
517 | /** | |
518 | * Return a path, but support the special $HOME variable. | |
519 | * | |
d66deb8d | 520 | * @param id the key for the path, which may contain "$HOME" |
0275353d NR |
521 | * @param configDir the directory to use as base if not absolute |
522 | * @param def the default value if none (will be configDir-rooted if needed) | |
d66deb8d | 523 | * @return the path, with expanded "$HOME" if needed |
b4dc6ab5 | 524 | */ |
0275353d NR |
525 | protected File getFile(UiConfig id, String configDir, String def) { |
526 | String path = uiconfig.getString(id, def); | |
527 | return getFile(path, configDir); | |
b4dc6ab5 NR |
528 | } |
529 | ||
530 | /** | |
531 | * Return a path, but support the special $HOME variable. | |
532 | * | |
d66deb8d | 533 | * @param path the path, which may contain "$HOME" |
0275353d | 534 | * @param configDir the directory to use as base if not absolute |
d66deb8d | 535 | * @return the path, with expanded "$HOME" if needed |
b4dc6ab5 | 536 | */ |
0275353d | 537 | protected File getFile(String path, String configDir) { |
08fe2e33 | 538 | File file = null; |
08fe2e33 NR |
539 | if (path != null && !path.isEmpty()) { |
540 | path = path.replace('/', File.separatorChar); | |
541 | if (path.contains("$HOME")) { | |
ae78e517 | 542 | path = path.replace("$HOME", getHome()); |
0275353d NR |
543 | } else if (!path.startsWith("/")) { |
544 | path = new File(configDir, path).getPath(); | |
08fe2e33 NR |
545 | } |
546 | ||
547 | file = new File(path); | |
548 | } | |
549 | ||
550 | return file; | |
551 | } | |
552 | ||
ae78e517 | 553 | /** |
7cd006eb NR |
554 | * Return the home directory from the environment (FANFIX_DIR) or the system |
555 | * properties. | |
556 | * <p> | |
557 | * The environment variable is tested first. Then, the custom property | |
d66deb8d NR |
558 | * "fanfix.home" is tried, followed by the usual "user.home" then "java.io.tmp" |
559 | * if nothing else is found. | |
ae78e517 NR |
560 | * |
561 | * @return the home | |
562 | */ | |
d66deb8d | 563 | protected String getHome() { |
7cd006eb | 564 | String home = System.getenv("FANFIX_DIR"); |
b4f9071c NR |
565 | if (home != null && new File(home).isFile()) { |
566 | home = null; | |
567 | } | |
568 | ||
7cd006eb NR |
569 | if (home == null || home.trim().isEmpty()) { |
570 | home = System.getProperty("fanfix.home"); | |
571 | if (home != null && new File(home).isFile()) { | |
572 | home = null; | |
573 | } | |
574 | } | |
575 | ||
b4f9071c NR |
576 | if (home == null || home.trim().isEmpty()) { |
577 | home = System.getProperty("user.home"); | |
578 | if (!new File(home).isDirectory()) { | |
579 | home = null; | |
580 | } | |
581 | } | |
582 | ||
ae78e517 NR |
583 | if (home == null || home.trim().isEmpty()) { |
584 | home = System.getProperty("java.io.tmpdir"); | |
b4f9071c NR |
585 | if (!new File(home).isDirectory()) { |
586 | home = null; | |
587 | } | |
ae78e517 NR |
588 | } |
589 | ||
590 | if (home == null) { | |
591 | home = ""; | |
592 | } | |
593 | ||
594 | return home; | |
595 | } | |
596 | ||
08fe2e33 NR |
597 | /** |
598 | * The language to use for the application (NULL = default system language). | |
599 | * | |
600 | * @return the language | |
601 | */ | |
d66deb8d | 602 | protected String getLang() { |
08fe2e33 NR |
603 | String lang = config.getString(Config.LANG); |
604 | ||
f83510cf | 605 | if (lang == null || lang.isEmpty()) { |
d66deb8d | 606 | if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) { |
fee80815 NR |
607 | lang = System.getenv("LANG"); |
608 | } | |
08fe2e33 NR |
609 | } |
610 | ||
611 | if (lang != null && lang.isEmpty()) { | |
612 | lang = null; | |
613 | } | |
614 | ||
615 | return lang; | |
616 | } | |
d0114000 NR |
617 | |
618 | /** | |
619 | * Check that the given environment variable is "enabled". | |
620 | * | |
d66deb8d | 621 | * @param key the variable to check |
d0114000 NR |
622 | * |
623 | * @return TRUE if it is | |
624 | */ | |
d66deb8d | 625 | protected Boolean checkEnv(String key) { |
d0114000 NR |
626 | String value = System.getenv(key); |
627 | if (value != null) { | |
628 | value = value.trim().toLowerCase(); | |
d66deb8d | 629 | if ("yes".equals(value) || "true".equals(value) || "on".equals(value) || "1".equals(value) |
d0114000 NR |
630 | || "y".equals(value)) { |
631 | return true; | |
632 | } | |
a3c35586 NR |
633 | |
634 | return false; | |
d0114000 NR |
635 | } |
636 | ||
a3c35586 | 637 | return null; |
d0114000 | 638 | } |
08fe2e33 | 639 | } |