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