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