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
.output
.BasicOutput
.OutputType
;
10 import be
.nikiroo
.utils
.resources
.Bundles
;
13 * Global state for the program (services and singletons).
17 public class Instance
{
18 private static ConfigBundle config
;
19 private static StringIdBundle trans
;
20 private static Cache cache
;
21 private static Library lib
;
22 private static boolean debug
;
23 private static File coverDir
;
24 private static File readerTmp
;
27 // Most of the rest is dependent upon this:
28 config
= new ConfigBundle();
30 String configDir
= System
.getenv("CONFIG_DIR");
31 if (configDir
== null) {
32 configDir
= new File(System
.getProperty("user.home"), ".fanfix")
35 if (configDir
!= null) {
36 if (!new File(configDir
).exists()) {
37 new File(configDir
).mkdirs();
39 Bundles
.setDirectory(configDir
);
43 config
= new ConfigBundle();
44 config
.updateFile(configDir
);
45 } catch (IOException e
) {
49 trans
= new StringIdBundle(getLang());
50 trans
.updateFile(configDir
);
51 } catch (IOException e
) {
55 Bundles
.setDirectory(configDir
);
58 trans
= new StringIdBundle(getLang());
60 lib
= new Library(getFile(Config
.LIBRARY_DIR
),
61 OutputType
.INFO_TEXT
, OutputType
.CBZ
);
62 } catch (Exception e
) {
63 syserr(new IOException("Cannot create library for directory: "
64 + getFile(Config
.LIBRARY_DIR
), e
));
67 debug
= Instance
.getConfig().getBoolean(Config
.DEBUG_ERR
, false);
68 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
69 File tmp
= getFile(Config
.CACHE_DIR
);
70 readerTmp
= getFile(Config
.CACHE_DIR_LOCAL_READER
);
72 if (checkEnv("NOUTF")) {
73 trans
.setUnicode(false);
76 if (checkEnv("DEBUG")) {
80 if (tmp
== null || readerTmp
== null) {
81 String tmpDir
= System
.getProperty("java.io.tmpdir");
84 tmp
= new File(tmpDir
, "fanfic-tmp");
86 if (readerTmp
== null) {
87 readerTmp
= new File(tmpDir
, "fanfic-reader");
90 syserr(new IOException(
91 "The system does not have a default temporary directory"));
95 if (coverDir
!= null && !coverDir
.exists()) {
96 syserr(new IOException(
97 "The 'default covers' directory does not exists: "
103 String ua
= config
.getString(Config
.USER_AGENT
);
104 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
105 int hoursLarge
= config
106 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
108 cache
= new Cache(tmp
, ua
, hours
, hoursLarge
);
109 } catch (IOException e
) {
110 syserr(new IOException(
111 "Cannot create cache (will continue without cache)", e
));
116 * Get the (unique) configuration service for the program.
118 * @return the configuration service
120 public static ConfigBundle
getConfig() {
125 * Get the (unique) {@link Cache} for the program.
127 * @return the {@link Cache}
129 public static Cache
getCache() {
134 * Get the (unique) {link StringIdBundle} for the program.
136 * @return the {link StringIdBundle}
138 public static StringIdBundle
getTrans() {
143 * Get the (unique) {@link Library} for the program.
145 * @return the {@link Library}
147 public static Library
getLibrary() {
152 * Return the directory where to look for default cover pages.
154 * @return the default covers directory
156 public static File
getCoverDir() {
161 * Return the directory where to store temporary files for the local reader.
163 * @return the directory
165 public static File
getReaderDir() {
170 * Report an error to the user
173 * the {@link Exception} to report
175 public static void syserr(Exception e
) {
179 System
.err
.println(e
.getMessage());
184 * Return a path, but support the special $HOME variable.
188 private static File
getFile(Config id
) {
190 String path
= config
.getString(id
);
191 if (path
!= null && !path
.isEmpty()) {
192 path
= path
.replace('/', File
.separatorChar
);
193 if (path
.contains("$HOME")) {
194 path
= path
.replace("$HOME",
195 "" + System
.getProperty("user.home"));
198 file
= new File(path
);
205 * The language to use for the application (NULL = default system language).
207 * @return the language
209 private static String
getLang() {
210 String lang
= config
.getString(Config
.LANG
);
212 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
213 lang
= System
.getenv("LANG");
216 if (lang
!= null && lang
.isEmpty()) {
224 * Check that the given environment variable is "enabled".
227 * the variable to check
229 * @return TRUE if it is
231 private static boolean checkEnv(String key
) {
232 String value
= System
.getenv(key
);
234 value
= value
.trim().toLowerCase();
235 if ("yes".equals(value
) || "true".equals(value
)
236 || "on".equals(value
) || "1".equals(value
)
237 || "y".equals(value
)) {