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