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