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