Version 1.0.0
[fanfix.git] / src / be / nikiroo / fanfix / Instance.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix;
2
3import java.io.File;
4import java.io.IOException;
5
6import be.nikiroo.fanfix.bundles.Config;
7import be.nikiroo.fanfix.bundles.ConfigBundle;
8import be.nikiroo.fanfix.bundles.StringIdBundle;
b4dc6ab5
NR
9import be.nikiroo.fanfix.bundles.UiConfig;
10import be.nikiroo.fanfix.bundles.UiConfigBundle;
2206ef66 11import be.nikiroo.fanfix.output.BasicOutput.OutputType;
08fe2e33
NR
12import be.nikiroo.utils.resources.Bundles;
13
14/**
15 * Global state for the program (services and singletons).
16 *
17 * @author niki
18 */
19public 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
3727aae2
NR
90 if (tmp == null || readerTmp == null) {
91 String tmpDir = System.getProperty("java.io.tmpdir");
92 if (tmpDir != null) {
93 if (tmp == null) {
94 tmp = new File(tmpDir, "fanfic-tmp");
95 }
96 if (readerTmp == null) {
7de079f1 97 readerTmp = new File(tmpDir, "fanfic-reader");
3727aae2
NR
98 }
99 } else {
100 syserr(new IOException(
101 "The system does not have a default temporary directory"));
102 }
103 }
08fe2e33
NR
104
105 if (coverDir != null && !coverDir.exists()) {
106 syserr(new IOException(
107 "The 'default covers' directory does not exists: "
108 + coverDir));
109 coverDir = null;
110 }
08fe2e33 111
08fe2e33 112 try {
08fe2e33
NR
113 String ua = config.getString(Config.USER_AGENT);
114 int hours = config.getInteger(Config.CACHE_MAX_TIME_CHANGING, -1);
115 int hoursLarge = config
116 .getInteger(Config.CACHE_MAX_TIME_STABLE, -1);
117
08fe2e33
NR
118 cache = new Cache(tmp, ua, hours, hoursLarge);
119 } catch (IOException e) {
120 syserr(new IOException(
121 "Cannot create cache (will continue without cache)", e));
122 }
123 }
124
125 /**
126 * Get the (unique) configuration service for the program.
127 *
128 * @return the configuration service
129 */
130 public static ConfigBundle getConfig() {
131 return config;
132 }
133
b4dc6ab5
NR
134 /**
135 * Get the (unique) UI configuration service for the program.
136 *
137 * @return the configuration service
138 */
139 public static UiConfigBundle getUiConfig() {
140 return uiconfig;
141 }
142
08fe2e33
NR
143 /**
144 * Get the (unique) {@link Cache} for the program.
145 *
146 * @return the {@link Cache}
147 */
148 public static Cache getCache() {
149 return cache;
150 }
151
152 /**
153 * Get the (unique) {link StringIdBundle} for the program.
154 *
155 * @return the {link StringIdBundle}
156 */
157 public static StringIdBundle getTrans() {
158 return trans;
159 }
160
161 /**
162 * Get the (unique) {@link Library} for the program.
163 *
164 * @return the {@link Library}
165 */
166 public static Library getLibrary() {
167 return lib;
168 }
169
170 /**
171 * Return the directory where to look for default cover pages.
172 *
173 * @return the default covers directory
174 */
175 public static File getCoverDir() {
176 return coverDir;
177 }
178
3727aae2
NR
179 /**
180 * Return the directory where to store temporary files for the local reader.
181 *
182 * @return the directory
183 */
184 public static File getReaderDir() {
185 return readerTmp;
186 }
187
08fe2e33
NR
188 /**
189 * Report an error to the user
190 *
191 * @param e
192 * the {@link Exception} to report
193 */
194 public static void syserr(Exception e) {
195 if (debug) {
196 e.printStackTrace();
197 } else {
198 System.err.println(e.getMessage());
199 }
200 }
201
202 /**
203 * Return a path, but support the special $HOME variable.
204 *
205 * @return the path
206 */
207 private static File getFile(Config id) {
b4dc6ab5
NR
208 return getFile(config.getString(id));
209 }
210
211 /**
212 * Return a path, but support the special $HOME variable.
213 *
214 * @return the path
215 */
216 private static File getFile(UiConfig id) {
217 return getFile(uiconfig.getString(id));
218 }
219
220 /**
221 * Return a path, but support the special $HOME variable.
222 *
223 * @return the path
224 */
225 private static File getFile(String path) {
08fe2e33 226 File file = null;
08fe2e33
NR
227 if (path != null && !path.isEmpty()) {
228 path = path.replace('/', File.separatorChar);
229 if (path.contains("$HOME")) {
230 path = path.replace("$HOME",
231 "" + System.getProperty("user.home"));
232 }
233
234 file = new File(path);
235 }
236
237 return file;
238 }
239
240 /**
241 * The language to use for the application (NULL = default system language).
242 *
243 * @return the language
244 */
245 private static String getLang() {
246 String lang = config.getString(Config.LANG);
247
248 if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) {
249 lang = System.getenv("LANG");
250 }
251
252 if (lang != null && lang.isEmpty()) {
253 lang = null;
254 }
255
256 return lang;
257 }
d0114000
NR
258
259 /**
260 * Check that the given environment variable is "enabled".
261 *
262 * @param key
263 * the variable to check
264 *
265 * @return TRUE if it is
266 */
267 private static boolean checkEnv(String key) {
268 String value = System.getenv(key);
269 if (value != null) {
270 value = value.trim().toLowerCase();
271 if ("yes".equals(value) || "true".equals(value)
272 || "on".equals(value) || "1".equals(value)
273 || "y".equals(value)) {
274 return true;
275 }
276 }
277
278 return false;
279 }
08fe2e33 280}