Commit | Line | Data |
---|---|---|
08fe2e33 NR |
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; | |
b4dc6ab5 NR |
9 | import be.nikiroo.fanfix.bundles.UiConfig; |
10 | import be.nikiroo.fanfix.bundles.UiConfigBundle; | |
2206ef66 | 11 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; |
08fe2e33 NR |
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; | |
b4dc6ab5 | 21 | private static UiConfigBundle uiconfig; |
08fe2e33 NR |
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; | |
3727aae2 | 27 | private static File readerTmp; |
08fe2e33 NR |
28 | |
29 | static { | |
948637bc | 30 | // Most of the rest is dependent upon this: |
08fe2e33 NR |
31 | config = new ConfigBundle(); |
32 | ||
fe999aa4 NR |
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 | } | |
b4dc6ab5 NR |
51 | try { |
52 | uiconfig = new UiConfigBundle(); | |
53 | uiconfig.updateFile(configDir); | |
54 | } catch (IOException e) { | |
55 | syserr(e); | |
56 | } | |
fe999aa4 NR |
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 | ||
b4dc6ab5 | 67 | uiconfig = new UiConfigBundle(); |
08fe2e33 | 68 | trans = new StringIdBundle(getLang()); |
68686a37 | 69 | try { |
2206ef66 NR |
70 | lib = new Library(getFile(Config.LIBRARY_DIR), |
71 | OutputType.INFO_TEXT, OutputType.CBZ); | |
68686a37 NR |
72 | } catch (Exception e) { |
73 | syserr(new IOException("Cannot create library for directory: " | |
74 | + getFile(Config.LIBRARY_DIR), e)); | |
75 | } | |
2206ef66 | 76 | |
08fe2e33 NR |
77 | debug = Instance.getConfig().getBoolean(Config.DEBUG_ERR, false); |
78 | coverDir = getFile(Config.DEFAULT_COVERS_DIR); | |
3727aae2 | 79 | File tmp = getFile(Config.CACHE_DIR); |
b4dc6ab5 | 80 | readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER); |
3727aae2 | 81 | |
d0114000 NR |
82 | if (checkEnv("NOUTF")) { |
83 | trans.setUnicode(false); | |
84 | } | |
85 | ||
86 | if (checkEnv("DEBUG")) { | |
87 | debug = true; | |
88 | } | |
89 | ||
68e370a4 NR |
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"); | |
3727aae2 | 96 | } |
68e370a4 | 97 | // |
08fe2e33 NR |
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 | } | |
08fe2e33 | 105 | |
08fe2e33 | 106 | try { |
08fe2e33 NR |
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 | ||
08fe2e33 NR |
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 | ||
b4dc6ab5 NR |
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 | ||
08fe2e33 NR |
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 | ||
3727aae2 NR |
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 | ||
08fe2e33 NR |
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) { | |
b4dc6ab5 NR |
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) { | |
08fe2e33 | 220 | File file = null; |
08fe2e33 NR |
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 | } | |
d0114000 NR |
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 | } | |
08fe2e33 | 274 | } |