28849b325510ccec2b17b813de99d8fef9036650
1 package be
.nikiroo
.fanfix
;
4 import java
.io
.IOException
;
7 import be
.nikiroo
.fanfix
.bundles
.Config
;
8 import be
.nikiroo
.fanfix
.bundles
.ConfigBundle
;
9 import be
.nikiroo
.fanfix
.bundles
.StringIdBundle
;
10 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
11 import be
.nikiroo
.fanfix
.bundles
.UiConfigBundle
;
12 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
13 import be
.nikiroo
.utils
.IOUtils
;
14 import be
.nikiroo
.utils
.resources
.Bundles
;
17 * Global state for the program (services and singletons).
21 public class Instance
{
22 private static ConfigBundle config
;
23 private static UiConfigBundle uiconfig
;
24 private static StringIdBundle trans
;
25 private static Cache cache
;
26 private static Library lib
;
27 private static boolean debug
;
28 private static File coverDir
;
29 private static File readerTmp
;
30 private static String configDir
;
33 // Most of the rest is dependent upon this:
34 config
= new ConfigBundle();
36 configDir
= System
.getProperty("CONFIG_DIR");
37 if (configDir
== null) {
38 configDir
= System
.getenv("CONFIG_DIR");
41 if (configDir
== null) {
42 configDir
= new File(System
.getProperty("user.home"), ".fanfix")
46 if (!new File(configDir
).exists()) {
47 new File(configDir
).mkdirs();
49 Bundles
.setDirectory(configDir
);
53 config
= new ConfigBundle();
54 config
.updateFile(configDir
);
55 } catch (IOException e
) {
59 uiconfig
= new UiConfigBundle();
60 uiconfig
.updateFile(configDir
);
61 } catch (IOException e
) {
65 trans
= new StringIdBundle(getLang());
66 trans
.updateFile(configDir
);
67 } catch (IOException e
) {
71 Bundles
.setDirectory(configDir
);
73 uiconfig
= new UiConfigBundle();
74 trans
= new StringIdBundle(getLang());
76 lib
= new Library(getFile(Config
.LIBRARY_DIR
),
77 OutputType
.INFO_TEXT
, OutputType
.CBZ
);
78 } catch (Exception e
) {
79 syserr(new IOException("Cannot create library for directory: "
80 + getFile(Config
.LIBRARY_DIR
), e
));
83 debug
= Instance
.getConfig().getBoolean(Config
.DEBUG_ERR
, false);
84 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
85 File tmp
= getFile(Config
.CACHE_DIR
);
86 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
88 if (checkEnv("NOUTF")) {
89 trans
.setUnicode(false);
92 if (checkEnv("DEBUG")) {
96 // Could have used: System.getProperty("java.io.tmpdir")
98 tmp
= new File(configDir
, "tmp");
100 if (readerTmp
== null) {
101 readerTmp
= new File(configDir
, "tmp-reader");
105 if (coverDir
!= null && !coverDir
.exists()) {
106 syserr(new IOException(
107 "The 'default covers' directory does not exists: "
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);
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
));
126 * Get the (unique) configuration service for the program.
128 * @return the configuration service
130 public static ConfigBundle
getConfig() {
135 * Get the (unique) UI configuration service for the program.
137 * @return the configuration service
139 public static UiConfigBundle
getUiConfig() {
144 * Get the (unique) {@link Cache} for the program.
146 * @return the {@link Cache}
148 public static Cache
getCache() {
153 * Get the (unique) {link StringIdBundle} for the program.
155 * @return the {link StringIdBundle}
157 public static StringIdBundle
getTrans() {
162 * Get the (unique) {@link Library} for the program.
164 * @return the {@link Library}
166 public static Library
getLibrary() {
171 * Return the directory where to look for default cover pages.
173 * @return the default covers directory
175 public static File
getCoverDir() {
180 * Return the directory where to store temporary files for the local reader.
182 * @return the directory
184 public static File
getReaderDir() {
189 * Check if we need to check that a new version of Fanfix is available.
191 * @return TRUE if we need to
193 public static boolean isVersionCheckNeeded() {
195 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 1) * 24 * 60
198 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
200 long delay
= new Date().getTime()
201 - Long
.parseLong(lastUpString
);
208 } catch (Exception e
) {
209 // No file or bad file:
217 * Notify that we checked for a new version of Fanfix.
219 public static void setVersionChecked() {
221 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
222 Long
.toString(new Date().getTime()));
223 } catch (IOException e
) {
229 * Report an error to the user
232 * the {@link Exception} to report
234 public static void syserr(Exception e
) {
238 System
.err
.println(e
.getMessage());
243 * Return a path, but support the special $HOME variable.
247 private static File
getFile(Config id
) {
248 return getFile(config
.getString(id
));
252 * Return a path, but support the special $HOME variable.
256 private static File
getFile(UiConfig id
) {
257 return getFile(uiconfig
.getString(id
));
261 * Return a path, but support the special $HOME variable.
265 private static File
getFile(String path
) {
267 if (path
!= null && !path
.isEmpty()) {
268 path
= path
.replace('/', File
.separatorChar
);
269 if (path
.contains("$HOME")) {
270 path
= path
.replace("$HOME",
271 "" + System
.getProperty("user.home"));
274 file
= new File(path
);
281 * The language to use for the application (NULL = default system language).
283 * @return the language
285 private static String
getLang() {
286 String lang
= config
.getString(Config
.LANG
);
288 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
289 lang
= System
.getenv("LANG");
292 if (lang
!= null && lang
.isEmpty()) {
300 * Check that the given environment variable is "enabled".
303 * the variable to check
305 * @return TRUE if it is
307 private static boolean checkEnv(String key
) {
308 String value
= System
.getenv(key
);
310 value
= value
.trim().toLowerCase();
311 if ("yes".equals(value
) || "true".equals(value
)
312 || "on".equals(value
) || "1".equals(value
)
313 || "y".equals(value
)) {