Add a test for Library (before changing it)
[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;
9import be.nikiroo.fanfix.bundles.StringIdBundle;
b4dc6ab5
NR
10import be.nikiroo.fanfix.bundles.UiConfig;
11import be.nikiroo.fanfix.bundles.UiConfigBundle;
2206ef66 12import be.nikiroo.fanfix.output.BasicOutput.OutputType;
b42117f1 13import be.nikiroo.utils.IOUtils;
08fe2e33
NR
14import be.nikiroo.utils.resources.Bundles;
15
16/**
17 * Global state for the program (services and singletons).
18 *
19 * @author niki
20 */
21public class Instance {
22 private static ConfigBundle config;
b4dc6ab5 23 private static UiConfigBundle uiconfig;
08fe2e33
NR
24 private static StringIdBundle trans;
25 private static Cache cache;
26 private static Library lib;
27 private static boolean debug;
28 private static File coverDir;
3727aae2 29 private static File readerTmp;
b0e88ebd 30 private static File remoteDir;
b42117f1 31 private static String configDir;
08fe2e33
NR
32
33 static {
948637bc 34 // Most of the rest is dependent upon this:
08fe2e33
NR
35 config = new ConfigBundle();
36
b42117f1 37 configDir = System.getProperty("CONFIG_DIR");
22848428
NR
38 if (configDir == null) {
39 configDir = System.getenv("CONFIG_DIR");
40 }
b42117f1 41
fe999aa4
NR
42 if (configDir == null) {
43 configDir = new File(System.getProperty("user.home"), ".fanfix")
44 .getPath();
45 }
39c3c689 46
b42117f1
NR
47 if (!new File(configDir).exists()) {
48 new File(configDir).mkdirs();
49 } else {
fe999aa4
NR
50 Bundles.setDirectory(configDir);
51 }
52
b42117f1
NR
53 try {
54 config = new ConfigBundle();
55 config.updateFile(configDir);
56 } catch (IOException e) {
57 syserr(e);
58 }
59 try {
60 uiconfig = new UiConfigBundle();
61 uiconfig.updateFile(configDir);
62 } catch (IOException e) {
63 syserr(e);
64 }
65 try {
66 trans = new StringIdBundle(getLang());
67 trans.updateFile(configDir);
68 } catch (IOException e) {
69 syserr(e);
70 }
71
72 Bundles.setDirectory(configDir);
73
b4dc6ab5 74 uiconfig = new UiConfigBundle();
08fe2e33 75 trans = new StringIdBundle(getLang());
68686a37 76 try {
2206ef66
NR
77 lib = new Library(getFile(Config.LIBRARY_DIR),
78 OutputType.INFO_TEXT, OutputType.CBZ);
68686a37
NR
79 } catch (Exception e) {
80 syserr(new IOException("Cannot create library for directory: "
81 + getFile(Config.LIBRARY_DIR), e));
82 }
2206ef66 83
08fe2e33
NR
84 debug = Instance.getConfig().getBoolean(Config.DEBUG_ERR, false);
85 coverDir = getFile(Config.DEFAULT_COVERS_DIR);
3727aae2 86 File tmp = getFile(Config.CACHE_DIR);
b4dc6ab5 87 readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER);
b0e88ebd 88 remoteDir = new File(getFile(Config.LIBRARY_DIR), "remote");
3727aae2 89
d0114000
NR
90 if (checkEnv("NOUTF")) {
91 trans.setUnicode(false);
92 }
93
94 if (checkEnv("DEBUG")) {
95 debug = true;
96 }
97
68e370a4
NR
98 // Could have used: System.getProperty("java.io.tmpdir")
99 if (tmp == null) {
100 tmp = new File(configDir, "tmp");
101 }
102 if (readerTmp == null) {
103 readerTmp = new File(configDir, "tmp-reader");
3727aae2 104 }
68e370a4 105 //
08fe2e33
NR
106
107 if (coverDir != null && !coverDir.exists()) {
108 syserr(new IOException(
109 "The 'default covers' directory does not exists: "
110 + coverDir));
111 coverDir = null;
112 }
08fe2e33 113
08fe2e33 114 try {
08fe2e33
NR
115 String ua = config.getString(Config.USER_AGENT);
116 int hours = config.getInteger(Config.CACHE_MAX_TIME_CHANGING, -1);
117 int hoursLarge = config
118 .getInteger(Config.CACHE_MAX_TIME_STABLE, -1);
119
08fe2e33
NR
120 cache = new Cache(tmp, ua, hours, hoursLarge);
121 } catch (IOException e) {
122 syserr(new IOException(
123 "Cannot create cache (will continue without cache)", e));
124 }
125 }
126
127 /**
128 * Get the (unique) configuration service for the program.
129 *
130 * @return the configuration service
131 */
132 public static ConfigBundle getConfig() {
133 return config;
134 }
135
b4dc6ab5
NR
136 /**
137 * Get the (unique) UI configuration service for the program.
138 *
139 * @return the configuration service
140 */
141 public static UiConfigBundle getUiConfig() {
142 return uiconfig;
143 }
144
08fe2e33
NR
145 /**
146 * Get the (unique) {@link Cache} for the program.
147 *
148 * @return the {@link Cache}
149 */
150 public static Cache getCache() {
151 return cache;
152 }
153
154 /**
155 * Get the (unique) {link StringIdBundle} for the program.
39c3c689 156 *
08fe2e33
NR
157 * @return the {link StringIdBundle}
158 */
159 public static StringIdBundle getTrans() {
160 return trans;
161 }
162
163 /**
164 * Get the (unique) {@link Library} for the program.
165 *
166 * @return the {@link Library}
167 */
168 public static Library getLibrary() {
169 return lib;
170 }
171
172 /**
173 * Return the directory where to look for default cover pages.
174 *
175 * @return the default covers directory
176 */
177 public static File getCoverDir() {
178 return coverDir;
179 }
180
3727aae2
NR
181 /**
182 * Return the directory where to store temporary files for the local reader.
183 *
184 * @return the directory
185 */
186 public static File getReaderDir() {
187 return readerTmp;
188 }
189
b0e88ebd
NR
190 /**
191 * Return the directory where to store temporary files for the remote
192 * {@link Library}.
193 *
194 * @param host
195 * the remote for this host
196 *
197 * @return the directory
198 */
199 public static File getRemoteDir(String host) {
200 remoteDir.mkdirs();
201
202 if (host != null) {
203 return new File(remoteDir, host);
204 }
205
206 return remoteDir;
207 }
208
b42117f1
NR
209 /**
210 * Check if we need to check that a new version of Fanfix is available.
211 *
212 * @return TRUE if we need to
213 */
214 public static boolean isVersionCheckNeeded() {
215 try {
a3641e4b
NR
216 long wait = config.getInteger(Config.UPDATE_INTERVAL, 1) * 24 * 60
217 * 60 * 1000;
b42117f1
NR
218 if (wait >= 0) {
219 String lastUpString = IOUtils.readSmallFile(new File(configDir,
220 "LAST_UPDATE"));
221 long delay = new Date().getTime()
222 - Long.parseLong(lastUpString);
223 if (delay > wait) {
224 return true;
225 }
226 } else {
227 return false;
228 }
229 } catch (Exception e) {
230 // No file or bad file:
231 return true;
232 }
233
234 return false;
235 }
236
237 /**
238 * Notify that we checked for a new version of Fanfix.
239 */
240 public static void setVersionChecked() {
241 try {
242 IOUtils.writeSmallFile(new File(configDir), "LAST_UPDATE",
243 Long.toString(new Date().getTime()));
244 } catch (IOException e) {
245 syserr(e);
246 }
247 }
248
08fe2e33
NR
249 /**
250 * Report an error to the user
251 *
252 * @param e
253 * the {@link Exception} to report
254 */
255 public static void syserr(Exception e) {
256 if (debug) {
257 e.printStackTrace();
258 } else {
259 System.err.println(e.getMessage());
260 }
261 }
262
263 /**
264 * Return a path, but support the special $HOME variable.
265 *
266 * @return the path
267 */
268 private static File getFile(Config id) {
b4dc6ab5
NR
269 return getFile(config.getString(id));
270 }
271
272 /**
273 * Return a path, but support the special $HOME variable.
274 *
275 * @return the path
276 */
277 private static File getFile(UiConfig id) {
278 return getFile(uiconfig.getString(id));
279 }
280
281 /**
282 * Return a path, but support the special $HOME variable.
283 *
284 * @return the path
285 */
286 private static File getFile(String path) {
08fe2e33 287 File file = null;
08fe2e33
NR
288 if (path != null && !path.isEmpty()) {
289 path = path.replace('/', File.separatorChar);
290 if (path.contains("$HOME")) {
291 path = path.replace("$HOME",
292 "" + System.getProperty("user.home"));
293 }
294
295 file = new File(path);
296 }
297
298 return file;
299 }
300
301 /**
302 * The language to use for the application (NULL = default system language).
303 *
304 * @return the language
305 */
306 private static String getLang() {
307 String lang = config.getString(Config.LANG);
308
309 if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) {
310 lang = System.getenv("LANG");
311 }
312
313 if (lang != null && lang.isEmpty()) {
314 lang = null;
315 }
316
317 return lang;
318 }
d0114000
NR
319
320 /**
321 * Check that the given environment variable is "enabled".
322 *
323 * @param key
324 * the variable to check
325 *
326 * @return TRUE if it is
327 */
328 private static boolean checkEnv(String key) {
329 String value = System.getenv(key);
330 if (value != null) {
331 value = value.trim().toLowerCase();
332 if ("yes".equals(value) || "true".equals(value)
333 || "on".equals(value) || "1".equals(value)
334 || "y".equals(value)) {
335 return true;
336 }
337 }
338
339 return false;
340 }
08fe2e33 341}