X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2FGuiReader.java;h=b9d5954888333c57456d75f7027759ffcc55c2b8;hb=62c63b0724f4bc45999cb2e7186b4b3ada479a0a;hp=73df7fb8f4a25f89c5cc45782eb3fec05ad6ca9a;hpb=77e28d38d47dbfcf29030ee388854d59df3ef1d0;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/reader/GuiReader.java b/src/be/nikiroo/fanfix/reader/GuiReader.java index 73df7fb..b9d5954 100644 --- a/src/be/nikiroo/fanfix/reader/GuiReader.java +++ b/src/be/nikiroo/fanfix/reader/GuiReader.java @@ -14,11 +14,10 @@ import javax.swing.event.HyperlinkListener; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.VersionCheck; -import be.nikiroo.fanfix.bundles.UiConfig; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; -import be.nikiroo.fanfix.library.LocalLibrary; -import be.nikiroo.fanfix.output.BasicOutput.OutputType; +import be.nikiroo.fanfix.library.BasicLibrary; +import be.nikiroo.fanfix.library.CacheLibrary; import be.nikiroo.utils.Progress; import be.nikiroo.utils.Version; import be.nikiroo.utils.ui.UIUtils; @@ -26,7 +25,9 @@ import be.nikiroo.utils.ui.UIUtils; class GuiReader extends BasicReader { static private boolean nativeLookLoaded; - private LocalLibrary localLibrary; + private CacheLibrary cacheLib; + + private File cacheDir; public GuiReader() throws IOException { if (!nativeLookLoaded) { @@ -34,40 +35,27 @@ class GuiReader extends BasicReader { nativeLookLoaded = true; } - File dir = Instance.getReaderDir(); - dir.mkdirs(); - if (!dir.exists()) { + cacheDir = Instance.getReaderDir(); + cacheDir.mkdirs(); + if (!cacheDir.exists()) { throw new IOException( - "Cannote create cache directory for local reader: " + dir); + "Cannote create cache directory for local reader: " + + cacheDir); } + } - OutputType text = null; - OutputType images = null; - - try { - text = OutputType.valueOfNullOkUC(Instance.getUiConfig().getString( - UiConfig.NON_IMAGES_DOCUMENT_TYPE)); - if (text == null) { - text = OutputType.HTML; - } - - images = OutputType.valueOfNullOkUC(Instance.getUiConfig() - .getString(UiConfig.IMAGES_DOCUMENT_TYPE)); - if (images == null) { - images = OutputType.CBZ; + @Override + public synchronized BasicLibrary getLibrary() { + if (cacheLib == null) { + BasicLibrary lib = super.getLibrary(); + if (lib instanceof CacheLibrary) { + cacheLib = (CacheLibrary) lib; + } else { + cacheLib = new CacheLibrary(cacheDir, lib); } - } catch (Exception e) { - UiConfig key = (text == null) ? UiConfig.NON_IMAGES_DOCUMENT_TYPE - : UiConfig.IMAGES_DOCUMENT_TYPE; - String value = Instance.getUiConfig().getString(key); - - throw new IOException( - String.format( - "The configuration option %s is not valid: %s", - key, value), e); } - localLibrary = new LocalLibrary(dir, text, images); + return cacheLib; } @Override @@ -81,40 +69,6 @@ class GuiReader extends BasicReader { read(meta.getLuid(), null); } - /** - * Import the story into the local reader library, and keep the same LUID. - * - * @param luid - * the Library UID - * @param pg - * the optional progress reporter - * - * @throws IOException - * in case of I/O error - */ - public void imprt(String luid, Progress pg) throws IOException { - Progress pgGetStory = new Progress(); - Progress pgSave = new Progress(); - if (pg != null) { - pg.setMax(2); - pg.addProgress(pgGetStory, 1); - pg.addProgress(pgSave, 1); - } - - try { - Story story = getLibrary().getStory(luid, pgGetStory); - if (story != null) { - story = localLibrary.save(story, luid, pgSave); - } else { - throw new IOException("Cannot find story in Library: " + luid); - } - } catch (IOException e) { - throw new IOException( - "Cannot import story from library to LocalReader library: " - + luid, e); - } - } - /** * Check if the {@link Story} denoted by this Library UID is present in the * {@link GuiReader} cache. @@ -125,7 +79,7 @@ class GuiReader extends BasicReader { * @return TRUE if it is */ public boolean isCached(String luid) { - return localLibrary.getInfo(luid) != null; + return cacheLib.isCached(luid); } @Override @@ -163,9 +117,9 @@ class GuiReader extends BasicReader { try { Desktop.getDesktop().browse(e.getURL().toURI()); } catch (IOException ee) { - Instance.syserr(ee); + Instance.getTraceHandler().error(ee); } catch (URISyntaxException ee) { - Instance.syserr(ee); + Instance.getTraceHandler().error(ee); } } }); @@ -196,31 +150,24 @@ class GuiReader extends BasicReader { // delete from local reader library void clearLocalReaderCache(String luid) { try { - localLibrary.delete(luid); + cacheLib.clearFromCache(luid); } catch (IOException e) { - Instance.syserr(e); + Instance.getTraceHandler().error(e); } } // delete from main library void delete(String luid) { try { - if (localLibrary.getInfo(luid) != null) { - localLibrary.delete(luid); - } - getLibrary().delete(luid); + cacheLib.delete(luid); } catch (IOException e) { - Instance.syserr(e); + Instance.getTraceHandler().error(e); } } // open the given book void read(String luid, Progress pg) throws IOException { - File file = localLibrary.getFile(luid); - if (file == null) { - imprt(luid, pg); - file = localLibrary.getFile(luid); - } + File file = cacheLib.getFile(luid, pg); // TODO: show a special page for the chapter? openExternal(getLibrary().getInfo(luid), file); @@ -228,12 +175,9 @@ class GuiReader extends BasicReader { void changeType(String luid, String newSource) { try { - if (localLibrary.getInfo(luid) != null) { - localLibrary.changeSource(luid, newSource, null); - } - getLibrary().changeSource(luid, newSource, null); + cacheLib.changeSource(luid, newSource, null); } catch (IOException e) { - Instance.syserr(e); + Instance.getTraceHandler().error(e); } } }