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
.StringId
;
10 import be
.nikiroo
.fanfix
.bundles
.StringIdBundle
;
11 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
12 import be
.nikiroo
.fanfix
.bundles
.UiConfigBundle
;
13 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
14 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
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
{
25 * A handler when a recoverable exception was caught by the program.
29 public interface SyserrHandler
{
31 * An exception happened, log it.
36 * show more details (usually equivalent to the value of
39 public void notify(Exception e
, boolean showDetails
);
43 * A handler when a trace message is sent.
47 public interface TraceHandler
{
49 * A trace happened, show it.
51 * Will only be called if TRACE is true.
56 public void trace(String message
);
59 private static ConfigBundle config
;
60 private static UiConfigBundle uiconfig
;
61 private static StringIdBundle trans
;
62 private static DataLoader cache
;
63 private static LocalLibrary lib
;
64 private static boolean debug
;
65 private static boolean trace
;
66 private static File coverDir
;
67 private static File readerTmp
;
68 private static File remoteDir
;
69 private static String configDir
;
71 private static SyserrHandler syserrHandler
;
73 private static TraceHandler traceHandler
;
76 // Most of the rest is dependent upon this:
77 config
= new ConfigBundle();
79 configDir
= System
.getProperty("CONFIG_DIR");
80 if (configDir
== null) {
81 configDir
= System
.getenv("CONFIG_DIR");
84 if (configDir
== null) {
85 configDir
= new File(System
.getProperty("user.home"), ".fanfix")
89 if (!new File(configDir
).exists()) {
90 new File(configDir
).mkdirs();
92 Bundles
.setDirectory(configDir
);
96 config
= new ConfigBundle();
97 config
.updateFile(configDir
);
98 } catch (IOException e
) {
102 uiconfig
= new UiConfigBundle();
103 uiconfig
.updateFile(configDir
);
104 } catch (IOException e
) {
108 // No updateFile for this one! (we do not want the user to have custom
109 // translations that won't accept updates from newer versions)
110 trans
= new StringIdBundle(getLang());
112 // Fix an old bug (we used to store custom translation files by
114 if (trans
.getString(StringId
.INPUT_DESC_CBZ
) == null) {
115 // TODO: create the deleteFile method
116 // trans.deleteFile(configDir);
119 Bundles
.setDirectory(configDir
);
121 uiconfig
= new UiConfigBundle();
122 trans
= new StringIdBundle(getLang());
124 lib
= new LocalLibrary(getFile(Config
.LIBRARY_DIR
));
125 } catch (Exception e
) {
126 syserr(new IOException("Cannot create library for directory: "
127 + getFile(Config
.LIBRARY_DIR
), e
));
130 debug
= Instance
.getConfig().getBoolean(Config
.DEBUG_ERR
, false);
131 trace
= Instance
.getConfig().getBoolean(Config
.DEBUG_TRACE
, false);
132 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
133 File tmp
= getFile(Config
.CACHE_DIR
);
134 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
135 remoteDir
= new File(configDir
, "remote");
137 if (checkEnv("NOUTF")) {
138 trans
.setUnicode(false);
141 if (checkEnv("DEBUG")) {
145 // Could have used: System.getProperty("java.io.tmpdir")
147 tmp
= new File(configDir
, "tmp");
149 if (readerTmp
== null) {
150 readerTmp
= new File(configDir
, "tmp-reader");
154 if (coverDir
!= null && !coverDir
.exists()) {
155 syserr(new IOException(
156 "The 'default covers' directory does not exists: "
162 String ua
= config
.getString(Config
.USER_AGENT
);
163 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
164 int hoursLarge
= config
165 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
167 cache
= new DataLoader(tmp
, ua
, hours
, hoursLarge
);
168 } catch (IOException e
) {
169 syserr(new IOException(
170 "Cannot create cache (will continue without cache)", e
));
175 * Get the (unique) configuration service for the program.
177 * @return the configuration service
179 public static ConfigBundle
getConfig() {
184 * Get the (unique) UI configuration service for the program.
186 * @return the configuration service
188 public static UiConfigBundle
getUiConfig() {
193 * Get the (unique) {@link DataLoader} for the program.
195 * @return the {@link DataLoader}
197 public static DataLoader
getCache() {
202 * Get the (unique) {link StringIdBundle} for the program.
204 * @return the {link StringIdBundle}
206 public static StringIdBundle
getTrans() {
211 * Get the (unique) {@link LocalLibrary} for the program.
213 * @return the {@link LocalLibrary}
215 public static BasicLibrary
getLibrary() {
220 * Return the directory where to look for default cover pages.
222 * @return the default covers directory
224 public static File
getCoverDir() {
229 * Return the directory where to store temporary files for the local reader.
231 * @return the directory
233 public static File
getReaderDir() {
238 * Return the directory where to store temporary files for the remote
239 * {@link LocalLibrary}.
242 * the remote for this host
244 * @return the directory
246 public static File
getRemoteDir(String host
) {
250 return new File(remoteDir
, host
);
257 * Check if we need to check that a new version of Fanfix is available.
259 * @return TRUE if we need to
261 public static boolean isVersionCheckNeeded() {
263 long wait
= config
.getInteger(Config
.UPDATE_INTERVAL
, 1) * 24 * 60
266 String lastUpString
= IOUtils
.readSmallFile(new File(configDir
,
268 long delay
= new Date().getTime()
269 - Long
.parseLong(lastUpString
);
276 } catch (Exception e
) {
277 // No file or bad file:
285 * Notify that we checked for a new version of Fanfix.
287 public static void setVersionChecked() {
289 IOUtils
.writeSmallFile(new File(configDir
), "LAST_UPDATE",
290 Long
.toString(new Date().getTime()));
291 } catch (IOException e
) {
297 * Replace the global syserr handler.
299 * @param syserrHandler
300 * the new syserr handler
302 public static void setSyserrHandler(SyserrHandler syserrHandler
) {
303 Instance
.syserrHandler
= syserrHandler
;
307 * Replace the global trace handler.
309 * @param traceHandler
310 * the new trace handler
312 public static void setTraceHandler(TraceHandler traceHandler
) {
313 Instance
.traceHandler
= traceHandler
;
317 * Report an error to the user
320 * the {@link Exception} to report
322 public static void syserr(Exception e
) {
323 if (syserrHandler
!= null) {
324 syserrHandler
.notify(e
, debug
);
329 System
.err
.println(e
.getMessage());
335 * Notify of a debug message.
340 public static void trace(String message
) {
342 if (traceHandler
!= null) {
343 traceHandler
.trace(message
);
345 System
.out
.println(message
);
351 * Return a path, but support the special $HOME variable.
355 private static File
getFile(Config id
) {
356 return getFile(config
.getString(id
));
360 * Return a path, but support the special $HOME variable.
364 private static File
getFile(UiConfig id
) {
365 return getFile(uiconfig
.getString(id
));
369 * Return a path, but support the special $HOME variable.
373 private static File
getFile(String path
) {
375 if (path
!= null && !path
.isEmpty()) {
376 path
= path
.replace('/', File
.separatorChar
);
377 if (path
.contains("$HOME")) {
378 path
= path
.replace("$HOME",
379 "" + System
.getProperty("user.home"));
382 file
= new File(path
);
389 * The language to use for the application (NULL = default system language).
391 * @return the language
393 private static String
getLang() {
394 String lang
= config
.getString(Config
.LANG
);
396 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
397 lang
= System
.getenv("LANG");
400 if (lang
!= null && lang
.isEmpty()) {
408 * Check that the given environment variable is "enabled".
411 * the variable to check
413 * @return TRUE if it is
415 private static boolean checkEnv(String key
) {
416 String value
= System
.getenv(key
);
418 value
= value
.trim().toLowerCase();
419 if ("yes".equals(value
) || "true".equals(value
)
420 || "on".equals(value
) || "1".equals(value
)
421 || "y".equals(value
)) {