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