1 package be
.nikiroo
.fanfix
;
4 import java
.io
.IOException
;
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
.fanfix
.bundles
.UiConfig
;
10 import be
.nikiroo
.fanfix
.bundles
.UiConfigBundle
;
11 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
12 import be
.nikiroo
.utils
.resources
.Bundles
;
15 * Global state for the program (services and singletons).
19 public class Instance
{
20 private static ConfigBundle config
;
21 private static UiConfigBundle uiconfig
;
22 private static StringIdBundle trans
;
23 private static Cache cache
;
24 private static Library lib
;
25 private static boolean debug
;
26 private static File coverDir
;
27 private static File readerTmp
;
30 // Most of the rest is dependent upon this:
31 config
= new ConfigBundle();
33 String configDir
= System
.getProperty("CONFIG_DIR");
34 if (configDir
== null) {
35 configDir
= System
.getenv("CONFIG_DIR");
37 if (configDir
== null) {
38 configDir
= new File(System
.getProperty("user.home"), ".fanfix")
42 if (configDir
!= null) {
43 if (!new File(configDir
).exists()) {
44 new File(configDir
).mkdirs();
46 Bundles
.setDirectory(configDir
);
50 config
= new ConfigBundle();
51 config
.updateFile(configDir
);
52 } catch (IOException e
) {
56 uiconfig
= new UiConfigBundle();
57 uiconfig
.updateFile(configDir
);
58 } catch (IOException e
) {
62 trans
= new StringIdBundle(getLang());
63 trans
.updateFile(configDir
);
64 } catch (IOException e
) {
68 Bundles
.setDirectory(configDir
);
71 uiconfig
= new UiConfigBundle();
72 trans
= new StringIdBundle(getLang());
74 lib
= new Library(getFile(Config
.LIBRARY_DIR
),
75 OutputType
.INFO_TEXT
, OutputType
.CBZ
);
76 } catch (Exception e
) {
77 syserr(new IOException("Cannot create library for directory: "
78 + getFile(Config
.LIBRARY_DIR
), e
));
81 debug
= Instance
.getConfig().getBoolean(Config
.DEBUG_ERR
, false);
82 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
83 File tmp
= getFile(Config
.CACHE_DIR
);
84 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
86 if (checkEnv("NOUTF")) {
87 trans
.setUnicode(false);
90 if (checkEnv("DEBUG")) {
94 // Could have used: System.getProperty("java.io.tmpdir")
96 tmp
= new File(configDir
, "tmp");
98 if (readerTmp
== null) {
99 readerTmp
= new File(configDir
, "tmp-reader");
103 if (coverDir
!= null && !coverDir
.exists()) {
104 syserr(new IOException(
105 "The 'default covers' directory does not exists: "
111 String ua
= config
.getString(Config
.USER_AGENT
);
112 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
113 int hoursLarge
= config
114 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
116 cache
= new Cache(tmp
, ua
, hours
, hoursLarge
);
117 } catch (IOException e
) {
118 syserr(new IOException(
119 "Cannot create cache (will continue without cache)", e
));
124 * Get the (unique) configuration service for the program.
126 * @return the configuration service
128 public static ConfigBundle
getConfig() {
133 * Get the (unique) UI configuration service for the program.
135 * @return the configuration service
137 public static UiConfigBundle
getUiConfig() {
142 * Get the (unique) {@link Cache} for the program.
144 * @return the {@link Cache}
146 public static Cache
getCache() {
151 * Get the (unique) {link StringIdBundle} for the program.
153 * @return the {link StringIdBundle}
155 public static StringIdBundle
getTrans() {
160 * Get the (unique) {@link Library} for the program.
162 * @return the {@link Library}
164 public static Library
getLibrary() {
169 * Return the directory where to look for default cover pages.
171 * @return the default covers directory
173 public static File
getCoverDir() {
178 * Return the directory where to store temporary files for the local reader.
180 * @return the directory
182 public static File
getReaderDir() {
187 * Report an error to the user
190 * the {@link Exception} to report
192 public static void syserr(Exception e
) {
196 System
.err
.println(e
.getMessage());
201 * Return a path, but support the special $HOME variable.
205 private static File
getFile(Config id
) {
206 return getFile(config
.getString(id
));
210 * Return a path, but support the special $HOME variable.
214 private static File
getFile(UiConfig id
) {
215 return getFile(uiconfig
.getString(id
));
219 * Return a path, but support the special $HOME variable.
223 private static File
getFile(String path
) {
225 if (path
!= null && !path
.isEmpty()) {
226 path
= path
.replace('/', File
.separatorChar
);
227 if (path
.contains("$HOME")) {
228 path
= path
.replace("$HOME",
229 "" + System
.getProperty("user.home"));
232 file
= new File(path
);
239 * The language to use for the application (NULL = default system language).
241 * @return the language
243 private static String
getLang() {
244 String lang
= config
.getString(Config
.LANG
);
246 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
247 lang
= System
.getenv("LANG");
250 if (lang
!= null && lang
.isEmpty()) {
258 * Check that the given environment variable is "enabled".
261 * the variable to check
263 * @return TRUE if it is
265 private static boolean checkEnv(String key
) {
266 String value
= System
.getenv(key
);
268 value
= value
.trim().toLowerCase();
269 if ("yes".equals(value
) || "true".equals(value
)
270 || "on".equals(value
) || "1".equals(value
)
271 || "y".equals(value
)) {