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; | |
23 | ||
24 | static { | |
25 | config = new ConfigBundle(); | |
26 | ||
27 | // config dependent: | |
28 | trans = new StringIdBundle(getLang()); | |
29 | lib = new Library(getFile(Config.LIBRARY_DIR)); | |
30 | debug = Instance.getConfig().getBoolean(Config.DEBUG_ERR, false); | |
31 | coverDir = getFile(Config.DEFAULT_COVERS_DIR); | |
32 | ||
33 | if (coverDir != null && !coverDir.exists()) { | |
34 | syserr(new IOException( | |
35 | "The 'default covers' directory does not exists: " | |
36 | + coverDir)); | |
37 | coverDir = null; | |
38 | } | |
39 | // | |
40 | ||
41 | String noutf = System.getenv("NOUTF"); | |
42 | if (noutf != null) { | |
43 | noutf = noutf.trim().toLowerCase(); | |
44 | if ("yes".equals(noutf) || "true".equals(noutf) | |
45 | || "on".equals(noutf) || "1".equals(noutf) | |
46 | || "y".equals(noutf)) { | |
47 | trans.setUnicode(false); | |
48 | } | |
49 | } | |
50 | ||
51 | String configDir = System.getenv("CONFIG_DIR"); | |
52 | if (configDir != null) { | |
53 | if (new File(configDir).isDirectory()) { | |
54 | Bundles.setDirectory(configDir); | |
55 | try { | |
56 | config = new ConfigBundle(); | |
57 | config.updateFile(configDir); | |
58 | } catch (IOException e) { | |
59 | syserr(e); | |
60 | } | |
61 | try { | |
62 | trans = new StringIdBundle(getLang()); | |
63 | trans.updateFile(configDir); | |
64 | } catch (IOException e) { | |
65 | syserr(e); | |
66 | } | |
67 | } else { | |
68 | syserr(new IOException("Configuration directory not found: " | |
69 | + configDir)); | |
70 | } | |
71 | } | |
72 | ||
73 | try { | |
74 | File tmp = getFile(Config.CACHE_DIR); | |
75 | String ua = config.getString(Config.USER_AGENT); | |
76 | int hours = config.getInteger(Config.CACHE_MAX_TIME_CHANGING, -1); | |
77 | int hoursLarge = config | |
78 | .getInteger(Config.CACHE_MAX_TIME_STABLE, -1); | |
79 | ||
80 | if (tmp == null) { | |
81 | String tmpDir = System.getProperty("java.io.tmpdir"); | |
82 | if (tmpDir != null) { | |
83 | tmp = new File(tmpDir, "fanfic-tmp"); | |
84 | } else { | |
85 | syserr(new IOException( | |
86 | "The system does not have a default temporary directory")); | |
87 | } | |
88 | } | |
89 | ||
90 | cache = new Cache(tmp, ua, hours, hoursLarge); | |
91 | } catch (IOException e) { | |
92 | syserr(new IOException( | |
93 | "Cannot create cache (will continue without cache)", e)); | |
94 | } | |
95 | } | |
96 | ||
97 | /** | |
98 | * Get the (unique) configuration service for the program. | |
99 | * | |
100 | * @return the configuration service | |
101 | */ | |
102 | public static ConfigBundle getConfig() { | |
103 | return config; | |
104 | } | |
105 | ||
106 | /** | |
107 | * Get the (unique) {@link Cache} for the program. | |
108 | * | |
109 | * @return the {@link Cache} | |
110 | */ | |
111 | public static Cache getCache() { | |
112 | return cache; | |
113 | } | |
114 | ||
115 | /** | |
116 | * Get the (unique) {link StringIdBundle} for the program. | |
117 | * | |
118 | * @return the {link StringIdBundle} | |
119 | */ | |
120 | public static StringIdBundle getTrans() { | |
121 | return trans; | |
122 | } | |
123 | ||
124 | /** | |
125 | * Get the (unique) {@link Library} for the program. | |
126 | * | |
127 | * @return the {@link Library} | |
128 | */ | |
129 | public static Library getLibrary() { | |
130 | return lib; | |
131 | } | |
132 | ||
133 | /** | |
134 | * Return the directory where to look for default cover pages. | |
135 | * | |
136 | * @return the default covers directory | |
137 | */ | |
138 | public static File getCoverDir() { | |
139 | return coverDir; | |
140 | } | |
141 | ||
142 | /** | |
143 | * Report an error to the user | |
144 | * | |
145 | * @param e | |
146 | * the {@link Exception} to report | |
147 | */ | |
148 | public static void syserr(Exception e) { | |
149 | if (debug) { | |
150 | e.printStackTrace(); | |
151 | } else { | |
152 | System.err.println(e.getMessage()); | |
153 | } | |
154 | } | |
155 | ||
156 | /** | |
157 | * Return a path, but support the special $HOME variable. | |
158 | * | |
159 | * @return the path | |
160 | */ | |
161 | private static File getFile(Config id) { | |
162 | File file = null; | |
163 | String path = config.getString(id); | |
164 | if (path != null && !path.isEmpty()) { | |
165 | path = path.replace('/', File.separatorChar); | |
166 | if (path.contains("$HOME")) { | |
167 | path = path.replace("$HOME", | |
168 | "" + System.getProperty("user.home")); | |
169 | } | |
170 | ||
171 | file = new File(path); | |
172 | } | |
173 | ||
174 | return file; | |
175 | } | |
176 | ||
177 | /** | |
178 | * The language to use for the application (NULL = default system language). | |
179 | * | |
180 | * @return the language | |
181 | */ | |
182 | private static String getLang() { | |
183 | String lang = config.getString(Config.LANG); | |
184 | ||
185 | if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) { | |
186 | lang = System.getenv("LANG"); | |
187 | } | |
188 | ||
189 | if (lang != null && lang.isEmpty()) { | |
190 | lang = null; | |
191 | } | |
192 | ||
193 | return lang; | |
194 | } | |
195 | } |