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