X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2FLocalReader.java;h=45fca8c10a41feb43e2fb9d13bf406241de4e88b;hb=edd4628984f5f06e955606651fc828ac839f7f43;hp=86b779c343c0b964d9c80d96493ccd47ec02f7d5;hpb=3d247bc3bb955a9b85686f0db431157cb9dc4a10;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/reader/LocalReader.java b/src/be/nikiroo/fanfix/reader/LocalReader.java index 86b779c..45fca8c 100644 --- a/src/be/nikiroo/fanfix/reader/LocalReader.java +++ b/src/be/nikiroo/fanfix/reader/LocalReader.java @@ -1,15 +1,17 @@ package be.nikiroo.fanfix.reader; +import java.awt.Desktop; import java.awt.EventQueue; import java.io.File; import java.io.IOException; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.Library; -import be.nikiroo.fanfix.bundles.Config; +import be.nikiroo.fanfix.bundles.UiConfig; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; import be.nikiroo.fanfix.output.BasicOutput.OutputType; +import be.nikiroo.utils.Progress; class LocalReader extends BasicReader { private Library lib; @@ -22,38 +24,74 @@ class LocalReader extends BasicReader { "Cannote create cache directory for local reader: " + dir); } - // TODO: can throw an exception, manage that (convert to IOEx ?) - OutputType text = OutputType.valueOfNullOkUC(Instance.getConfig() - .getString(Config.LOCAL_READER_NON_IMAGES_DOCUMENT_TYPE)); - if (text == null) { - text = OutputType.HTML; - } + OutputType text = null; + OutputType images = null; + + try { + text = OutputType.valueOfNullOkUC(Instance.getUiConfig().getString( + UiConfig.NON_IMAGES_DOCUMENT_TYPE)); + if (text == null) { + text = OutputType.HTML; + } - OutputType images = OutputType.valueOfNullOkUC(Instance.getConfig() - .getString(Config.LOCAL_READER_IMAGES_DOCUMENT_TYPE)); - if (images == null) { - images = OutputType.CBZ; + images = OutputType.valueOfNullOkUC(Instance.getUiConfig() + .getString(UiConfig.IMAGES_DOCUMENT_TYPE)); + if (images == null) { + images = OutputType.CBZ; + } + } 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); } - // lib = new Library(dir, text, images); } @Override public void read() throws IOException { + if (getStory() == null) { + throw new IOException("No story to read"); + } + + open(getStory().getMeta().getLuid(), null); } @Override - public void read(int chapter) { + public void read(int chapter) throws IOException { + // TODO: show a special page? + read(); } - // return new luid - public String imprt(String luid) throws IOException { + /** + * 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 = Instance.getLibrary().getStory(luid); + Story story = Instance.getLibrary().getStory(luid, pgGetStory); if (story != null) { - story = lib.save(story); - return story.getMeta().getLuid(); + story = lib.save(story, luid, pgSave); } else { throw new IOException("Cannot find story in Library: " + luid); } @@ -64,18 +102,42 @@ class LocalReader extends BasicReader { } } - public File getTarget(String luid) throws IOException { - MetaData meta = lib.getInfo(luid); + /** + * Get the target file related to this {@link Story}. + * + * @param luid + * the LUID of the {@link Story} + * @param pg + * the optional progress reporter + * + * @return the target file + * + * @throws IOException + * in case of I/O error + */ + public File getTarget(String luid, Progress pg) throws IOException { File file = lib.getFile(luid); if (file == null) { - luid = imprt(luid); + imprt(luid, pg); file = lib.getFile(luid); - meta = lib.getInfo(luid); } return file; } + /** + * Check if the {@link Story} denoted by this Library UID is present in the + * {@link LocalReader} cache. + * + * @param luid + * the Library UID + * + * @return TRUE if it is + */ + public boolean isCached(String luid) { + return lib.getInfo(luid) != null; + } + @Override public void start(String type) { final String typeFinal = type; @@ -86,4 +148,48 @@ class LocalReader extends BasicReader { } }); } + + // refresh = delete from LocalReader cache (TODO: rename?) + void refresh(String luid) { + lib.delete(luid); + } + + // delete from main library + void delete(String luid) { + lib.delete(luid); + Instance.getLibrary().delete(luid); + } + + // open the given book + void open(String luid, Progress pg) throws IOException { + MetaData meta = Instance.getLibrary().getInfo(luid); + File target = getTarget(luid, pg); + + String program = null; + if (meta.isImageDocument()) { + program = Instance.getUiConfig().getString( + UiConfig.IMAGES_DOCUMENT_READER); + } else { + program = Instance.getUiConfig().getString( + UiConfig.NON_IMAGES_DOCUMENT_READER); + } + + if (program != null && program.trim().isEmpty()) { + program = null; + } + + if (program == null) { + try { + Desktop.getDesktop().browse(target.toURI()); + } catch (UnsupportedOperationException e) { + Runtime.getRuntime().exec( + new String[] { "xdg-open", target.getAbsolutePath() }); + + } + } else { + Runtime.getRuntime().exec( + new String[] { program, target.getAbsolutePath() }); + + } + } }