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 LocalLibrary lib
;
27 private static boolean debug
;
28 private static File coverDir
;
29 private static File readerTmp
;
30 private static File remoteDir
;
31 private static String configDir
;
34 // Most of the rest is dependent upon this:
35 config
= new ConfigBundle();
37 configDir
= System
.getProperty("CONFIG_DIR");
38 if (configDir
== null) {
39 configDir
= System
.getenv("CONFIG_DIR");
42 if (configDir
== null) {
43 configDir
= new File(System
.getProperty("user.home"), ".fanfix")
47 if (!new File(configDir
).exists()) {
48 new File(configDir
).mkdirs();
50 Bundles
.setDirectory(configDir
);
54 config
= new ConfigBundle();
55 config
.updateFile(configDir
);
56 } catch (IOException e
) {
60 uiconfig
= new UiConfigBundle();
61 uiconfig
.updateFile(configDir
);
62 } catch (IOException e
) {
66 trans
= new StringIdBundle(getLang());
67 trans
.updateFile(configDir
);
68 } catch (IOException e
) {
72 Bundles
.setDirectory(configDir
);
74 uiconfig
= new UiConfigBundle();
75 trans
= new StringIdBundle(getLang());
77 lib
= new LocalLibrary(getFile(Config
.LIBRARY_DIR
),
78 OutputType
.INFO_TEXT
, OutputType
.CBZ
);
79 } catch (Exception e
) {
80 syserr(new IOException("Cannot create library for directory: "
81 + getFile(Config
.LIBRARY_DIR
), e
));
84 debug
= Instance
.getConfig().getBoolean(Config
.DEBUG_ERR
, false);
85 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
86 File tmp
= getFile(Config
.CACHE_DIR
);
87 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
88 remoteDir
= new File(getFile(Config
.LIBRARY_DIR
), "remote");
90 if (checkEnv("NOUTF")) {
91 trans
.setUnicode(false);
94 if (checkEnv("DEBUG")) {
98 // Could have used: System.getProperty("java.io.tmpdir")
100 tmp
= new File(configDir
, "tmp");
102 if (readerTmp
== null) {
103 readerTmp
= new File(configDir
, "tmp-reader");
107 if (coverDir
!= null && !coverDir
.exists()) {
108 syserr(new IOException(
109 "The 'default covers' directory does not exists: "
115 String ua
= config
.getString(Config
.USER_AGENT
);
116 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
117 int hoursLarge
= config
118 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
120 cache
= new Cache(tmp
, ua
, hours
, hoursLarge
);
121 } catch (IOException e
) {
122 syserr(new IOException(
123 "Cannot create cache (will continue without cache)", e
));
128 * Get the (unique) configuration service for the program.
130 * @return the configuration service
132 public static ConfigBundle
getConfig() {
137 * Get the (unique) UI configuration service for the program.
139 * @return the configuration service
141 public static UiConfigBundle
getUiConfig() {
146 * Get the (unique) {@link Cache} for the program.
148 * @return the {@link Cache}
150 public static Cache
getCache() {
155 * Get the (unique) {link StringIdBundle} for the program.
157 * @return the {link StringIdBundle}
159 public static StringIdBundle
getTrans() {
164 * Get the (unique) {@link LocalLibrary} for the program.
166 * @return the {@link LocalLibrary}
168 public static BasicLibrary
getLibrary() {
173 * Return the directory where to look for default cover pages.
175 * @return the default covers directory
177 public static File
getCoverDir() {
182 * Return the directory where to store temporary files for the local reader.
184 * @return the directory
186 public static File
getReaderDir() {
191 * Return the directory where to store temporary files for the remote
192 * {@link LocalLibrary}.
195 * the remote for this host
197 * @return the directory
199 public static File
getRemoteDir(String host
) {
203 return new File(remoteDir
, host
);
210 * Check if we need to check that a new version of Fanfix is available.
212 * @return TRUE if we need to
214 public static boolean isVersionCheckNeeded() {
216 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 1) * 24 * 60
219 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
221 long delay
= new Date().getTime()
222 - Long
.parseLong(lastUpString
);
229 } catch (Exception e
) {
230 // No file or bad file:
238 * Notify that we checked for a new version of Fanfix.
240 public static void setVersionChecked() {
242 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
243 Long
.toString(new Date().getTime()));
244 } catch (IOException e
) {
250 * Report an error to the user
253 * the {@link Exception} to report
255 public static void syserr(Exception e
) {
259 System
.err
.println(e
.getMessage());
264 * Return a path, but support the special $HOME variable.
268 private static File
getFile(Config id
) {
269 return getFile(config
.getString(id
));
273 * Return a path, but support the special $HOME variable.
277 private static File
getFile(UiConfig id
) {
278 return getFile(uiconfig
.getString(id
));
282 * Return a path, but support the special $HOME variable.
286 private static File
getFile(String path
) {
288 if (path
!= null && !path
.isEmpty()) {
289 path
= path
.replace('/', File
.separatorChar
);
290 if (path
.contains("$HOME")) {
291 path
= path
.replace("$HOME",
292 "" + System
.getProperty("user.home"));
295 file
= new File(path
);
302 * The language to use for the application (NULL = default system language).
304 * @return the language
306 private static String
getLang() {
307 String lang
= config
.getString(Config
.LANG
);
309 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
310 lang
= System
.getenv("LANG");
313 if (lang
!= null && lang
.isEmpty()) {
321 * Check that the given environment variable is "enabled".
324 * the variable to check
326 * @return TRUE if it is
328 private static boolean checkEnv(String key
) {
329 String value
= System
.getenv(key
);
331 value
= value
.trim().toLowerCase();
332 if ("yes".equals(value
) || "true".equals(value
)
333 || "on".equals(value
) || "1".equals(value
)
334 || "y".equals(value
)) {