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 { | |
948637bc | 26 | // Most of the rest is dependent 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) { | |
7de079f1 | 51 | readerTmp = new File(tmpDir, "fanfic-reader"); |
3727aae2 NR |
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"); | |
948637bc NR |
67 | if (configDir == null) { |
68 | configDir = new File(System.getProperty("user.home"), ".fanfix") | |
69 | .getPath(); | |
70 | } | |
08fe2e33 | 71 | if (configDir != null) { |
948637bc NR |
72 | if (!new File(configDir).exists()) { |
73 | new File(configDir).mkdirs(); | |
08fe2e33 | 74 | } else { |
948637bc | 75 | Bundles.setDirectory(configDir); |
08fe2e33 | 76 | } |
948637bc NR |
77 | |
78 | try { | |
79 | config = new ConfigBundle(); | |
80 | config.updateFile(configDir); | |
81 | } catch (IOException e) { | |
82 | syserr(e); | |
83 | } | |
84 | try { | |
85 | trans = new StringIdBundle(getLang()); | |
86 | trans.updateFile(configDir); | |
87 | } catch (IOException e) { | |
88 | syserr(e); | |
89 | } | |
90 | ||
91 | Bundles.setDirectory(configDir); | |
08fe2e33 NR |
92 | } |
93 | ||
94 | try { | |
08fe2e33 NR |
95 | String ua = config.getString(Config.USER_AGENT); |
96 | int hours = config.getInteger(Config.CACHE_MAX_TIME_CHANGING, -1); | |
97 | int hoursLarge = config | |
98 | .getInteger(Config.CACHE_MAX_TIME_STABLE, -1); | |
99 | ||
08fe2e33 NR |
100 | cache = new Cache(tmp, ua, hours, hoursLarge); |
101 | } catch (IOException e) { | |
102 | syserr(new IOException( | |
103 | "Cannot create cache (will continue without cache)", e)); | |
104 | } | |
105 | } | |
106 | ||
107 | /** | |
108 | * Get the (unique) configuration service for the program. | |
109 | * | |
110 | * @return the configuration service | |
111 | */ | |
112 | public static ConfigBundle getConfig() { | |
113 | return config; | |
114 | } | |
115 | ||
116 | /** | |
117 | * Get the (unique) {@link Cache} for the program. | |
118 | * | |
119 | * @return the {@link Cache} | |
120 | */ | |
121 | public static Cache getCache() { | |
122 | return cache; | |
123 | } | |
124 | ||
125 | /** | |
126 | * Get the (unique) {link StringIdBundle} for the program. | |
127 | * | |
128 | * @return the {link StringIdBundle} | |
129 | */ | |
130 | public static StringIdBundle getTrans() { | |
131 | return trans; | |
132 | } | |
133 | ||
134 | /** | |
135 | * Get the (unique) {@link Library} for the program. | |
136 | * | |
137 | * @return the {@link Library} | |
138 | */ | |
139 | public static Library getLibrary() { | |
140 | return lib; | |
141 | } | |
142 | ||
143 | /** | |
144 | * Return the directory where to look for default cover pages. | |
145 | * | |
146 | * @return the default covers directory | |
147 | */ | |
148 | public static File getCoverDir() { | |
149 | return coverDir; | |
150 | } | |
151 | ||
3727aae2 NR |
152 | /** |
153 | * Return the directory where to store temporary files for the local reader. | |
154 | * | |
155 | * @return the directory | |
156 | */ | |
157 | public static File getReaderDir() { | |
158 | return readerTmp; | |
159 | } | |
160 | ||
08fe2e33 NR |
161 | /** |
162 | * Report an error to the user | |
163 | * | |
164 | * @param e | |
165 | * the {@link Exception} to report | |
166 | */ | |
167 | public static void syserr(Exception e) { | |
168 | if (debug) { | |
169 | e.printStackTrace(); | |
170 | } else { | |
171 | System.err.println(e.getMessage()); | |
172 | } | |
173 | } | |
174 | ||
175 | /** | |
176 | * Return a path, but support the special $HOME variable. | |
177 | * | |
178 | * @return the path | |
179 | */ | |
180 | private static File getFile(Config id) { | |
181 | File file = null; | |
182 | String path = config.getString(id); | |
183 | if (path != null && !path.isEmpty()) { | |
184 | path = path.replace('/', File.separatorChar); | |
185 | if (path.contains("$HOME")) { | |
186 | path = path.replace("$HOME", | |
187 | "" + System.getProperty("user.home")); | |
188 | } | |
189 | ||
190 | file = new File(path); | |
191 | } | |
192 | ||
193 | return file; | |
194 | } | |
195 | ||
196 | /** | |
197 | * The language to use for the application (NULL = default system language). | |
198 | * | |
199 | * @return the language | |
200 | */ | |
201 | private static String getLang() { | |
202 | String lang = config.getString(Config.LANG); | |
203 | ||
204 | if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) { | |
205 | lang = System.getenv("LANG"); | |
206 | } | |
207 | ||
208 | if (lang != null && lang.isEmpty()) { | |
209 | lang = null; | |
210 | } | |
211 | ||
212 | return lang; | |
213 | } | |
d0114000 NR |
214 | |
215 | /** | |
216 | * Check that the given environment variable is "enabled". | |
217 | * | |
218 | * @param key | |
219 | * the variable to check | |
220 | * | |
221 | * @return TRUE if it is | |
222 | */ | |
223 | private static boolean checkEnv(String key) { | |
224 | String value = System.getenv(key); | |
225 | if (value != null) { | |
226 | value = value.trim().toLowerCase(); | |
227 | if ("yes".equals(value) || "true".equals(value) | |
228 | || "on".equals(value) || "1".equals(value) | |
229 | || "y".equals(value)) { | |
230 | return true; | |
231 | } | |
232 | } | |
233 | ||
234 | return false; | |
235 | } | |
08fe2e33 | 236 | } |