Fix some bad line breaks on HTML supports
[fanfix.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 {
a3641e4b
NR
195 long wait = config.getInteger(Config.UPDATE_INTERVAL, 1) * 24 * 60
196 * 60 * 1000;
b42117f1
NR
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
08fe2e33
NR
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) {
b4dc6ab5
NR
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) {
08fe2e33 266 File file = null;
08fe2e33
NR
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 }
d0114000
NR
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 }
08fe2e33 320}