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