X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2FLocalReader.java;h=b031de84e372342710a0c420f4c22304b5a497d7;hp=7c50b033444d8c740b4a973dc1512b4354ab3ab1;hb=b42117f163c9c0dd96424d43730fb95fb088b4be;hpb=3b2b638f7e1395702f843b5b19d7959327f604b2 diff --git a/src/be/nikiroo/fanfix/reader/LocalReader.java b/src/be/nikiroo/fanfix/reader/LocalReader.java index 7c50b03..b031de8 100644 --- a/src/be/nikiroo/fanfix/reader/LocalReader.java +++ b/src/be/nikiroo/fanfix/reader/LocalReader.java @@ -1,15 +1,26 @@ package be.nikiroo.fanfix.reader; +import java.awt.Desktop; import java.awt.EventQueue; import java.io.File; import java.io.IOException; +import java.net.URISyntaxException; + +import javax.swing.JEditorPane; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.event.HyperlinkEvent; +import javax.swing.event.HyperlinkListener; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.Library; +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.output.BasicOutput.OutputType; import be.nikiroo.utils.Progress; +import be.nikiroo.utils.Version; class LocalReader extends BasicReader { private Library lib; @@ -22,29 +33,48 @@ 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.getUiConfig() - .getString(UiConfig.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.getUiConfig() - .getString(UiConfig.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(); } /** @@ -104,14 +134,124 @@ class LocalReader extends BasicReader { 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) { + // TODO: improve presentation of update message + final VersionCheck updates = VersionCheck.check(); + StringBuilder builder = new StringBuilder(); + + final JEditorPane updateMessage = new JEditorPane("text/html", ""); + if (updates.isNewVersionAvailable()) { + builder.append("A new version of the program is available at https://github.com/nikiroo/fanfix/releases"); + builder.append("
"); + builder.append("
"); + for (Version v : updates.getNewer()) { + builder.append("\tVersion " + v + ""); + builder.append("
"); + builder.append(""); + } + + // html content + updateMessage.setText("" // + + builder// + + ""); + + // handle link events + updateMessage.addHyperlinkListener(new HyperlinkListener() { + public void hyperlinkUpdate(HyperlinkEvent e) { + if (e.getEventType().equals( + HyperlinkEvent.EventType.ACTIVATED)) + try { + Desktop.getDesktop().browse(e.getURL().toURI()); + } catch (IOException ee) { + Instance.syserr(ee); + } catch (URISyntaxException ee) { + Instance.syserr(ee); + } + } + }); + updateMessage.setEditable(false); + updateMessage.setBackground(new JLabel().getBackground()); + } + final String typeFinal = type; EventQueue.invokeLater(new Runnable() { public void run() { + if (updates.isNewVersionAvailable()) { + int rep = JOptionPane.showConfirmDialog(null, + updateMessage, "Updates available", + JOptionPane.OK_CANCEL_OPTION); + if (rep == JOptionPane.OK_OPTION) { + updates.ok(); + } else { + updates.ignore(); + } + } + new LocalReaderFrame(LocalReader.this, typeFinal) .setVisible(true); } }); } + + // 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() }); + + } + } }