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
.library
.BasicLibrary
;
13 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
14 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
15 import be
.nikiroo
.utils
.IOUtils
;
16 import be
.nikiroo
.utils
.resources
.Bundles
;
19 * Global state for the program (services and singletons).
23 public class Instance
{
24 private static ConfigBundle config
;
25 private static UiConfigBundle uiconfig
;
26 private static StringIdBundle trans
;
27 private static Cache cache
;
28 private static LocalLibrary lib
;
29 private static boolean debug
;
30 private static File coverDir
;
31 private static File readerTmp
;
32 private static File remoteDir
;
33 private static String configDir
;
36 // Most of the rest is dependent upon this:
37 config
= new ConfigBundle();
39 configDir
= System
.getProperty("CONFIG_DIR");
40 if (configDir
== null) {
41 configDir
= System
.getenv("CONFIG_DIR");
44 if (configDir
== null) {
45 configDir
= new File(System
.getProperty("user.home"), ".fanfix")
49 if (!new File(configDir
).exists()) {
50 new File(configDir
).mkdirs();
52 Bundles
.setDirectory(configDir
);
56 config
= new ConfigBundle();
57 config
.updateFile(configDir
);
58 } catch (IOException e
) {
62 uiconfig
= new UiConfigBundle();
63 uiconfig
.updateFile(configDir
);
64 } catch (IOException e
) {
68 trans
= new StringIdBundle(getLang());
69 trans
.updateFile(configDir
);
70 } catch (IOException e
) {
74 Bundles
.setDirectory(configDir
);
76 uiconfig
= new UiConfigBundle();
77 trans
= new StringIdBundle(getLang());
79 lib
= new LocalLibrary(getFile(Config
.LIBRARY_DIR
),
80 OutputType
.INFO_TEXT
, OutputType
.CBZ
);
81 } catch (Exception e
) {
82 syserr(new IOException("Cannot create library for directory: "
83 + getFile(Config
.LIBRARY_DIR
), e
));
86 debug
= Instance
.getConfig().getBoolean(Config
.DEBUG_ERR
, false);
87 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
88 File tmp
= getFile(Config
.CACHE_DIR
);
89 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
90 remoteDir
= new File(getFile(Config
.LIBRARY_DIR
), "remote");
92 if (checkEnv("NOUTF")) {
93 trans
.setUnicode(false);
96 if (checkEnv("DEBUG")) {
100 // Could have used: System.getProperty("java.io.tmpdir")
102 tmp
= new File(configDir
, "tmp");
104 if (readerTmp
== null) {
105 readerTmp
= new File(configDir
, "tmp-reader");
109 if (coverDir
!= null && !coverDir
.exists()) {
110 syserr(new IOException(
111 "The 'default covers' directory does not exists: "
117 String ua
= config
.getString(Config
.USER_AGENT
);
118 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
119 int hoursLarge
= config
120 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
122 cache
= new Cache(tmp
, ua
, hours
, hoursLarge
);
123 } catch (IOException e
) {
124 syserr(new IOException(
125 "Cannot create cache (will continue without cache)", e
));
130 * Get the (unique) configuration service for the program.
132 * @return the configuration service
134 public static ConfigBundle
getConfig() {
139 * Get the (unique) UI configuration service for the program.
141 * @return the configuration service
143 public static UiConfigBundle
getUiConfig() {
148 * Get the (unique) {@link Cache} for the program.
150 * @return the {@link Cache}
152 public static Cache
getCache() {
157 * Get the (unique) {link StringIdBundle} for the program.
159 * @return the {link StringIdBundle}
161 public static StringIdBundle
getTrans() {
166 * Get the (unique) {@link LocalLibrary} for the program.
168 * @return the {@link LocalLibrary}
170 public static BasicLibrary
getLibrary() {
175 * Return the directory where to look for default cover pages.
177 * @return the default covers directory
179 public static File
getCoverDir() {
184 * Return the directory where to store temporary files for the local reader.
186 * @return the directory
188 public static File
getReaderDir() {
193 * Return the directory where to store temporary files for the remote
194 * {@link LocalLibrary}.
197 * the remote for this host
199 * @return the directory
201 public static File
getRemoteDir(String host
) {
205 return new File(remoteDir
, host
);
212 * Check if we need to check that a new version of Fanfix is available.
214 * @return TRUE if we need to
216 public static boolean isVersionCheckNeeded() {
218 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 1) * 24 * 60
221 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
223 long delay
= new Date().getTime()
224 - Long
.parseLong(lastUpString
);
231 } catch (Exception e
) {
232 // No file or bad file:
240 * Notify that we checked for a new version of Fanfix.
242 public static void setVersionChecked() {
244 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
245 Long
.toString(new Date().getTime()));
246 } catch (IOException e
) {
252 * Report an error to the user
255 * the {@link Exception} to report
257 public static void syserr(Exception e
) {
261 System
.err
.println(e
.getMessage());
266 * The program is in DEBUG mode (more verbose).
268 * @return TRUE if it is
270 public static boolean isDebug() {
275 * Return a path, but support the special $HOME variable.
279 private static File
getFile(Config id
) {
280 return getFile(config
.getString(id
));
284 * Return a path, but support the special $HOME variable.
288 private static File
getFile(UiConfig id
) {
289 return getFile(uiconfig
.getString(id
));
293 * Return a path, but support the special $HOME variable.
297 private static File
getFile(String path
) {
299 if (path
!= null && !path
.isEmpty()) {
300 path
= path
.replace('/', File
.separatorChar
);
301 if (path
.contains("$HOME")) {
302 path
= path
.replace("$HOME",
303 "" + System
.getProperty("user.home"));
306 file
= new File(path
);
313 * The language to use for the application (NULL = default system language).
315 * @return the language
317 private static String
getLang() {
318 String lang
= config
.getString(Config
.LANG
);
320 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
321 lang
= System
.getenv("LANG");
324 if (lang
!= null && lang
.isEmpty()) {
332 * Check that the given environment variable is "enabled".
335 * the variable to check
337 * @return TRUE if it is
339 private static boolean checkEnv(String key
) {
340 String value
= System
.getenv(key
);
342 value
= value
.trim().toLowerCase();
343 if ("yes".equals(value
) || "true".equals(value
)
344 || "on".equals(value
) || "1".equals(value
)
345 || "y".equals(value
)) {