| 1 | package be.nikiroo.fanfix; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.util.Date; |
| 6 | |
| 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; |
| 17 | |
| 18 | /** |
| 19 | * Global state for the program (services and singletons). |
| 20 | * |
| 21 | * @author niki |
| 22 | */ |
| 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; |
| 34 | |
| 35 | static { |
| 36 | // Most of the rest is dependent upon this: |
| 37 | config = new ConfigBundle(); |
| 38 | |
| 39 | configDir = System.getProperty("CONFIG_DIR"); |
| 40 | if (configDir == null) { |
| 41 | configDir = System.getenv("CONFIG_DIR"); |
| 42 | } |
| 43 | |
| 44 | if (configDir == null) { |
| 45 | configDir = new File(System.getProperty("user.home"), ".fanfix") |
| 46 | .getPath(); |
| 47 | } |
| 48 | |
| 49 | if (!new File(configDir).exists()) { |
| 50 | new File(configDir).mkdirs(); |
| 51 | } else { |
| 52 | Bundles.setDirectory(configDir); |
| 53 | } |
| 54 | |
| 55 | try { |
| 56 | config = new ConfigBundle(); |
| 57 | config.updateFile(configDir); |
| 58 | } catch (IOException e) { |
| 59 | syserr(e); |
| 60 | } |
| 61 | try { |
| 62 | uiconfig = new UiConfigBundle(); |
| 63 | uiconfig.updateFile(configDir); |
| 64 | } catch (IOException e) { |
| 65 | syserr(e); |
| 66 | } |
| 67 | try { |
| 68 | trans = new StringIdBundle(getLang()); |
| 69 | trans.updateFile(configDir); |
| 70 | } catch (IOException e) { |
| 71 | syserr(e); |
| 72 | } |
| 73 | |
| 74 | Bundles.setDirectory(configDir); |
| 75 | |
| 76 | uiconfig = new UiConfigBundle(); |
| 77 | trans = new StringIdBundle(getLang()); |
| 78 | try { |
| 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)); |
| 84 | } |
| 85 | |
| 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"); |
| 91 | |
| 92 | if (checkEnv("NOUTF")) { |
| 93 | trans.setUnicode(false); |
| 94 | } |
| 95 | |
| 96 | if (checkEnv("DEBUG")) { |
| 97 | debug = true; |
| 98 | } |
| 99 | |
| 100 | // Could have used: System.getProperty("java.io.tmpdir") |
| 101 | if (tmp == null) { |
| 102 | tmp = new File(configDir, "tmp"); |
| 103 | } |
| 104 | if (readerTmp == null) { |
| 105 | readerTmp = new File(configDir, "tmp-reader"); |
| 106 | } |
| 107 | // |
| 108 | |
| 109 | if (coverDir != null && !coverDir.exists()) { |
| 110 | syserr(new IOException( |
| 111 | "The 'default covers' directory does not exists: " |
| 112 | + coverDir)); |
| 113 | coverDir = null; |
| 114 | } |
| 115 | |
| 116 | try { |
| 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); |
| 121 | |
| 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)); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get the (unique) configuration service for the program. |
| 131 | * |
| 132 | * @return the configuration service |
| 133 | */ |
| 134 | public static ConfigBundle getConfig() { |
| 135 | return config; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Get the (unique) UI configuration service for the program. |
| 140 | * |
| 141 | * @return the configuration service |
| 142 | */ |
| 143 | public static UiConfigBundle getUiConfig() { |
| 144 | return uiconfig; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Get the (unique) {@link Cache} for the program. |
| 149 | * |
| 150 | * @return the {@link Cache} |
| 151 | */ |
| 152 | public static Cache getCache() { |
| 153 | return cache; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Get the (unique) {link StringIdBundle} for the program. |
| 158 | * |
| 159 | * @return the {link StringIdBundle} |
| 160 | */ |
| 161 | public static StringIdBundle getTrans() { |
| 162 | return trans; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Get the (unique) {@link LocalLibrary} for the program. |
| 167 | * |
| 168 | * @return the {@link LocalLibrary} |
| 169 | */ |
| 170 | public static BasicLibrary getLibrary() { |
| 171 | return lib; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Return the directory where to look for default cover pages. |
| 176 | * |
| 177 | * @return the default covers directory |
| 178 | */ |
| 179 | public static File getCoverDir() { |
| 180 | return coverDir; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Return the directory where to store temporary files for the local reader. |
| 185 | * |
| 186 | * @return the directory |
| 187 | */ |
| 188 | public static File getReaderDir() { |
| 189 | return readerTmp; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Return the directory where to store temporary files for the remote |
| 194 | * {@link LocalLibrary}. |
| 195 | * |
| 196 | * @param host |
| 197 | * the remote for this host |
| 198 | * |
| 199 | * @return the directory |
| 200 | */ |
| 201 | public static File getRemoteDir(String host) { |
| 202 | remoteDir.mkdirs(); |
| 203 | |
| 204 | if (host != null) { |
| 205 | return new File(remoteDir, host); |
| 206 | } |
| 207 | |
| 208 | return remoteDir; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Check if we need to check that a new version of Fanfix is available. |
| 213 | * |
| 214 | * @return TRUE if we need to |
| 215 | */ |
| 216 | public static boolean isVersionCheckNeeded() { |
| 217 | try { |
| 218 | long wait = config.getInteger(Config.UPDATE_INTERVAL, 1) * 24 * 60 |
| 219 | * 60 * 1000; |
| 220 | if (wait >= 0) { |
| 221 | String lastUpString = IOUtils.readSmallFile(new File(configDir, |
| 222 | "LAST_UPDATE")); |
| 223 | long delay = new Date().getTime() |
| 224 | - Long.parseLong(lastUpString); |
| 225 | if (delay > wait) { |
| 226 | return true; |
| 227 | } |
| 228 | } else { |
| 229 | return false; |
| 230 | } |
| 231 | } catch (Exception e) { |
| 232 | // No file or bad file: |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Notify that we checked for a new version of Fanfix. |
| 241 | */ |
| 242 | public static void setVersionChecked() { |
| 243 | try { |
| 244 | IOUtils.writeSmallFile(new File(configDir), "LAST_UPDATE", |
| 245 | Long.toString(new Date().getTime())); |
| 246 | } catch (IOException e) { |
| 247 | syserr(e); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Report an error to the user |
| 253 | * |
| 254 | * @param e |
| 255 | * the {@link Exception} to report |
| 256 | */ |
| 257 | public static void syserr(Exception e) { |
| 258 | if (debug) { |
| 259 | e.printStackTrace(); |
| 260 | } else { |
| 261 | System.err.println(e.getMessage()); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * The program is in DEBUG mode (more verbose). |
| 267 | * |
| 268 | * @return TRUE if it is |
| 269 | */ |
| 270 | public static boolean isDebug() { |
| 271 | return debug; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Return a path, but support the special $HOME variable. |
| 276 | * |
| 277 | * @return the path |
| 278 | */ |
| 279 | private static File getFile(Config id) { |
| 280 | return getFile(config.getString(id)); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Return a path, but support the special $HOME variable. |
| 285 | * |
| 286 | * @return the path |
| 287 | */ |
| 288 | private static File getFile(UiConfig id) { |
| 289 | return getFile(uiconfig.getString(id)); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Return a path, but support the special $HOME variable. |
| 294 | * |
| 295 | * @return the path |
| 296 | */ |
| 297 | private static File getFile(String path) { |
| 298 | File file = null; |
| 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")); |
| 304 | } |
| 305 | |
| 306 | file = new File(path); |
| 307 | } |
| 308 | |
| 309 | return file; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * The language to use for the application (NULL = default system language). |
| 314 | * |
| 315 | * @return the language |
| 316 | */ |
| 317 | private static String getLang() { |
| 318 | String lang = config.getString(Config.LANG); |
| 319 | |
| 320 | if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) { |
| 321 | lang = System.getenv("LANG"); |
| 322 | } |
| 323 | |
| 324 | if (lang != null && lang.isEmpty()) { |
| 325 | lang = null; |
| 326 | } |
| 327 | |
| 328 | return lang; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Check that the given environment variable is "enabled". |
| 333 | * |
| 334 | * @param key |
| 335 | * the variable to check |
| 336 | * |
| 337 | * @return TRUE if it is |
| 338 | */ |
| 339 | private static boolean checkEnv(String key) { |
| 340 | String value = System.getenv(key); |
| 341 | if (value != null) { |
| 342 | value = value.trim().toLowerCase(); |
| 343 | if ("yes".equals(value) || "true".equals(value) |
| 344 | || "on".equals(value) || "1".equals(value) |
| 345 | || "y".equals(value)) { |
| 346 | return true; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | return false; |
| 351 | } |
| 352 | } |