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