Version 1.4.0
[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;
b42117f1 30 private static String configDir;
08fe2e33
NR
31
32 static {
948637bc 33 // Most of the rest is dependent upon this:
08fe2e33
NR
34 config = new ConfigBundle();
35
b42117f1 36 configDir = System.getProperty("CONFIG_DIR");
22848428
NR
37 if (configDir == null) {
38 configDir = System.getenv("CONFIG_DIR");
39 }
b42117f1 40
fe999aa4
NR
41 if (configDir == null) {
42 configDir = new File(System.getProperty("user.home"), ".fanfix")
43 .getPath();
44 }
39c3c689 45
b42117f1
NR
46 if (!new File(configDir).exists()) {
47 new File(configDir).mkdirs();
48 } else {
fe999aa4
NR
49 Bundles.setDirectory(configDir);
50 }
51
b42117f1
NR
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
b4dc6ab5 73 uiconfig = new UiConfigBundle();
08fe2e33 74 trans = new StringIdBundle(getLang());
68686a37 75 try {
2206ef66
NR
76 lib = new Library(getFile(Config.LIBRARY_DIR),
77 OutputType.INFO_TEXT, OutputType.CBZ);
68686a37
NR
78 } catch (Exception e) {
79 syserr(new IOException("Cannot create library for directory: "
80 + getFile(Config.LIBRARY_DIR), e));
81 }
2206ef66 82
08fe2e33
NR
83 debug = Instance.getConfig().getBoolean(Config.DEBUG_ERR, false);
84 coverDir = getFile(Config.DEFAULT_COVERS_DIR);
3727aae2 85 File tmp = getFile(Config.CACHE_DIR);
b4dc6ab5 86 readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER);
3727aae2 87
d0114000
NR
88 if (checkEnv("NOUTF")) {
89 trans.setUnicode(false);
90 }
91
92 if (checkEnv("DEBUG")) {
93 debug = true;
94 }
95
68e370a4
NR
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");
3727aae2 102 }
68e370a4 103 //
08fe2e33
NR
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 }
08fe2e33 111
08fe2e33 112 try {
08fe2e33
NR
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
08fe2e33
NR
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
b4dc6ab5
NR
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
08fe2e33
NR
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.
39c3c689 154 *
08fe2e33
NR
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
3727aae2
NR
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
b42117f1
NR
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 * 60;
196 if (wait >= 0) {
197 String lastUpString = IOUtils.readSmallFile(new File(configDir,
198 "LAST_UPDATE"));
199 long delay = new Date().getTime()
200 - Long.parseLong(lastUpString);
201 if (delay > wait) {
202 return true;
203 }
204 } else {
205 return false;
206 }
207 } catch (Exception e) {
208 // No file or bad file:
209 return true;
210 }
211
212 return false;
213 }
214
215 /**
216 * Notify that we checked for a new version of Fanfix.
217 */
218 public static void setVersionChecked() {
219 try {
220 IOUtils.writeSmallFile(new File(configDir), "LAST_UPDATE",
221 Long.toString(new Date().getTime()));
222 } catch (IOException e) {
223 syserr(e);
224 }
225 }
226
08fe2e33
NR
227 /**
228 * Report an error to the user
229 *
230 * @param e
231 * the {@link Exception} to report
232 */
233 public static void syserr(Exception e) {
234 if (debug) {
235 e.printStackTrace();
236 } else {
237 System.err.println(e.getMessage());
238 }
239 }
240
241 /**
242 * Return a path, but support the special $HOME variable.
243 *
244 * @return the path
245 */
246 private static File getFile(Config id) {
b4dc6ab5
NR
247 return getFile(config.getString(id));
248 }
249
250 /**
251 * Return a path, but support the special $HOME variable.
252 *
253 * @return the path
254 */
255 private static File getFile(UiConfig id) {
256 return getFile(uiconfig.getString(id));
257 }
258
259 /**
260 * Return a path, but support the special $HOME variable.
261 *
262 * @return the path
263 */
264 private static File getFile(String path) {
08fe2e33 265 File file = null;
08fe2e33
NR
266 if (path != null && !path.isEmpty()) {
267 path = path.replace('/', File.separatorChar);
268 if (path.contains("$HOME")) {
269 path = path.replace("$HOME",
270 "" + System.getProperty("user.home"));
271 }
272
273 file = new File(path);
274 }
275
276 return file;
277 }
278
279 /**
280 * The language to use for the application (NULL = default system language).
281 *
282 * @return the language
283 */
284 private static String getLang() {
285 String lang = config.getString(Config.LANG);
286
287 if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) {
288 lang = System.getenv("LANG");
289 }
290
291 if (lang != null && lang.isEmpty()) {
292 lang = null;
293 }
294
295 return lang;
296 }
d0114000
NR
297
298 /**
299 * Check that the given environment variable is "enabled".
300 *
301 * @param key
302 * the variable to check
303 *
304 * @return TRUE if it is
305 */
306 private static boolean checkEnv(String key) {
307 String value = System.getenv(key);
308 if (value != null) {
309 value = value.trim().toLowerCase();
310 if ("yes".equals(value) || "true".equals(value)
311 || "on".equals(value) || "1".equals(value)
312 || "y".equals(value)) {
313 return true;
314 }
315 }
316
317 return false;
318 }
08fe2e33 319}