Library fixes + "make install" fix
[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.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;
23 private static File readerTmp;
24
25 static {
26 // Most of the rest is dependent upon this:
27 config = new ConfigBundle();
28
29 String configDir = System.getenv("CONFIG_DIR");
30 if (configDir == null) {
31 configDir = new File(System.getProperty("user.home"), ".fanfix")
32 .getPath();
33 }
34 if (configDir != null) {
35 if (!new File(configDir).exists()) {
36 new File(configDir).mkdirs();
37 } else {
38 Bundles.setDirectory(configDir);
39 }
40
41 try {
42 config = new ConfigBundle();
43 config.updateFile(configDir);
44 } catch (IOException e) {
45 syserr(e);
46 }
47 try {
48 trans = new StringIdBundle(getLang());
49 trans.updateFile(configDir);
50 } catch (IOException e) {
51 syserr(e);
52 }
53
54 Bundles.setDirectory(configDir);
55 }
56
57 trans = new StringIdBundle(getLang());
58 lib = new Library(getFile(Config.LIBRARY_DIR));
59 debug = Instance.getConfig().getBoolean(Config.DEBUG_ERR, false);
60 coverDir = getFile(Config.DEFAULT_COVERS_DIR);
61 File tmp = getFile(Config.CACHE_DIR);
62 readerTmp = getFile(Config.CACHE_DIR_LOCAL_READER);
63
64 if (checkEnv("NOUTF")) {
65 trans.setUnicode(false);
66 }
67
68 if (checkEnv("DEBUG")) {
69 debug = true;
70 }
71
72 if (tmp == null || readerTmp == null) {
73 String tmpDir = System.getProperty("java.io.tmpdir");
74 if (tmpDir != null) {
75 if (tmp == null) {
76 tmp = new File(tmpDir, "fanfic-tmp");
77 }
78 if (readerTmp == null) {
79 readerTmp = new File(tmpDir, "fanfic-reader");
80 }
81 } else {
82 syserr(new IOException(
83 "The system does not have a default temporary directory"));
84 }
85 }
86
87 if (coverDir != null && !coverDir.exists()) {
88 syserr(new IOException(
89 "The 'default covers' directory does not exists: "
90 + coverDir));
91 coverDir = null;
92 }
93
94 try {
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
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
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
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 }
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 }
236 }