Add a new TUI system based upon Jexer (WIP)
[fanfix.git] / src / be / nikiroo / fanfix / reader / LocalReader.java
index 45fca8c10a41feb43e2fb9d13bf406241de4e88b..2b92e721db959fc52aa46af7ebb7799daa4afec5 100644 (file)
@@ -4,14 +4,22 @@ 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;
@@ -102,29 +110,6 @@ class LocalReader extends BasicReader {
                }
        }
 
-       /**
-        * 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) {
-                       imprt(luid, pg);
-                       file = lib.getFile(luid);
-               }
-
-               return file;
-       }
-
        /**
         * Check if the {@link Story} denoted by this Library UID is present in the
         * {@link LocalReader} cache.
@@ -140,17 +125,70 @@ class LocalReader extends BasicReader {
 
        @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 <span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>");
+                       builder.append("<br>");
+                       builder.append("<br>");
+                       for (Version v : updates.getNewer()) {
+                               builder.append("\t<b>Version " + v + "</b>");
+                               builder.append("<br>");
+                               builder.append("<ul>");
+                               for (String item : updates.getChanges().get(v)) {
+                                       builder.append("<li>" + item + "</li>");
+                               }
+                               builder.append("</ul>");
+                       }
+
+                       // html content
+                       updateMessage.setText("<html><body>" //
+                                       + builder//
+                                       + "</body></html>");
+
+                       // 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) {
+       // delete from local reader library
+       void clearLocalReaderCache(String luid) {
                lib.delete(luid);
        }
 
@@ -162,34 +200,17 @@ class LocalReader extends BasicReader {
 
        // 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;
+               File file = lib.getFile(luid);
+               if (file == null) {
+                       imprt(luid, pg);
+                       file = lib.getFile(luid);
                }
 
-               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() });
+               open(Instance.getLibrary().getInfo(luid), file);
+       }
 
-               }
+       void changeType(String luid, String newType) {
+               lib.changeType(luid, newType);
+               Instance.getLibrary().changeType(luid, newType);
        }
 }