GUI: automatically select URLs from clipboard
[fanfix.git] / src / be / nikiroo / fanfix / reader / LocalReaderFrame.java
index 985e1045c8b74cfa7d6e1f73007098733bb8f7f9..4e8d9ace0f801b45f3089152c534a45dc6ef3aaa 100644 (file)
@@ -2,8 +2,9 @@ package be.nikiroo.fanfix.reader;
 
 import java.awt.BorderLayout;
 import java.awt.Color;
-import java.awt.Desktop;
 import java.awt.Frame;
+import java.awt.Toolkit;
+import java.awt.datatransfer.DataFlavor;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.KeyEvent;
@@ -39,6 +40,7 @@ import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.fanfix.reader.LocalReaderBook.BookActionListener;
 import be.nikiroo.utils.Progress;
+import be.nikiroo.utils.Version;
 import be.nikiroo.utils.ui.ProgressBar;
 import be.nikiroo.utils.ui.WrapLayout;
 
@@ -72,7 +74,7 @@ class LocalReaderFrame extends JFrame {
         *            the type of {@link Story} to load, or NULL for all types
         */
        public LocalReaderFrame(LocalReader reader, String type) {
-               super("Fanfix Library");
+               super(String.format("Fanfix %s Library", Version.getCurrentVersion()));
 
                this.reader = reader;
 
@@ -113,6 +115,7 @@ class LocalReaderFrame extends JFrame {
                this.type = type;
                stories = Instance.getLibrary().getList(type);
                books.clear();
+               bookPane.invalidate();
                bookPane.removeAll();
                for (MetaData meta : stories) {
                        LocalReaderBook book = new LocalReaderBook(meta,
@@ -136,7 +139,8 @@ class LocalReaderFrame extends JFrame {
                                        popup.add(createMenuItemOpenBook());
                                        popup.addSeparator();
                                        popup.add(createMenuItemExport());
-                                       popup.add(createMenuItemRefresh());
+                                       popup.add(createMenuItemClearCache());
+                                       popup.add(createMenuItemRedownload());
                                        popup.addSeparator();
                                        popup.add(createMenuItemDelete());
                                        popup.show(e.getComponent(), e.getX(), e.getY());
@@ -165,13 +169,13 @@ class LocalReaderFrame extends JFrame {
                JMenu file = new JMenu("File");
                file.setMnemonic(KeyEvent.VK_F);
 
-               JMenuItem imprt = new JMenuItem("Import URL", KeyEvent.VK_U);
+               JMenuItem imprt = new JMenuItem("Import URL...", KeyEvent.VK_U);
                imprt.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                imprt(true);
                        }
                });
-               JMenuItem imprtF = new JMenuItem("Import File", KeyEvent.VK_F);
+               JMenuItem imprtF = new JMenuItem("Import File...", KeyEvent.VK_F);
                imprtF.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                imprt(false);
@@ -198,7 +202,8 @@ class LocalReaderFrame extends JFrame {
                JMenu edit = new JMenu("Edit");
                edit.setMnemonic(KeyEvent.VK_E);
 
-               edit.add(createMenuItemRefresh());
+               edit.add(createMenuItemClearCache());
+               edit.add(createMenuItemRedownload());
                edit.addSeparator();
                edit.add(createMenuItemDelete());
 
@@ -210,7 +215,6 @@ class LocalReaderFrame extends JFrame {
                List<String> tt = Instance.getLibrary().getTypes();
                tt.add(0, null);
                for (final String type : tt) {
-
                        JMenuItem item = new JMenuItem(type == null ? "All books" : type);
                        item.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent e) {
@@ -275,7 +279,8 @@ class LocalReaderFrame extends JFrame {
                                                public void run() {
                                                        try {
                                                                Instance.getLibrary().export(
-                                                                               selectedBook.getLuid(), type, path, pg);
+                                                                               selectedBook.getMeta().getLuid(), type,
+                                                                               path, pg);
                                                        } catch (IOException e) {
                                                                Instance.syserr(e);
                                                        }
@@ -288,6 +293,15 @@ class LocalReaderFrame extends JFrame {
                return export;
        }
 
+       /**
+        * Create a {@link FileFilter} that accepts all files and return the given
+        * description.
+        * 
+        * @param desc
+        *            the description
+        * 
+        * @return the filter
+        */
        private FileFilter createAllFilter(final String desc) {
                return new FileFilter() {
                        @Override
@@ -307,14 +321,14 @@ class LocalReaderFrame extends JFrame {
         * 
         * @return the item
         */
-       private JMenuItem createMenuItemRefresh() {
+       private JMenuItem createMenuItemClearCache() {
                JMenuItem refresh = new JMenuItem("Clear cache", KeyEvent.VK_C);
                refresh.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                if (selectedBook != null) {
                                        outOfUi(null, new Runnable() {
                                                public void run() {
-                                                       reader.refresh(selectedBook.getLuid());
+                                                       reader.refresh(selectedBook.getMeta().getLuid());
                                                        selectedBook.setCached(false);
                                                        SwingUtilities.invokeLater(new Runnable() {
                                                                public void run() {
@@ -330,6 +344,29 @@ class LocalReaderFrame extends JFrame {
                return refresh;
        }
 
+       /**
+        * Create the redownload (then delete original) menu item.
+        * 
+        * @return the item
+        */
+       private JMenuItem createMenuItemRedownload() {
+               JMenuItem refresh = new JMenuItem("Redownload", KeyEvent.VK_R);
+               refresh.addActionListener(new ActionListener() {
+                       public void actionPerformed(ActionEvent e) {
+                               if (selectedBook != null) {
+                                       imprt(selectedBook.getMeta().getUrl(), new Runnable() {
+                                               public void run() {
+                                                       reader.delete(selectedBook.getMeta().getLuid());
+                                                       selectedBook = null;
+                                               }
+                                       });
+                               }
+                       }
+               });
+
+               return refresh;
+       }
+
        /**
         * Create the delete menu item.
         * 
@@ -342,7 +379,7 @@ class LocalReaderFrame extends JFrame {
                                if (selectedBook != null) {
                                        outOfUi(null, new Runnable() {
                                                public void run() {
-                                                       reader.delete(selectedBook.getLuid());
+                                                       reader.delete(selectedBook.getMeta().getLuid());
                                                        selectedBook = null;
                                                        SwingUtilities.invokeLater(new Runnable() {
                                                                public void run() {
@@ -387,36 +424,14 @@ class LocalReaderFrame extends JFrame {
                outOfUi(pg, new Runnable() {
                        public void run() {
                                try {
-                                       File target = LocalReaderFrame.this.reader.getTarget(
-                                                       book.getLuid(), pg);
-                                       book.setCached(true);
-                                       // TODO: allow custom programs, with
-                                       // Desktop/xdg-open fallback
-                                       try {
-                                               Desktop.getDesktop().browse(target.toURI());
-                                       } catch (UnsupportedOperationException e) {
-                                               String browsers[] = new String[] { "xdg-open",
-                                                               "epiphany", "konqueror", "firefox", "chrome",
-                                                               "google-chrome", "mozilla" };
-
-                                               Runtime runtime = Runtime.getRuntime();
-                                               for (String browser : browsers) {
-                                                       try {
-                                                               runtime.exec(new String[] { browser,
-                                                                               target.getAbsolutePath() });
-                                                               runtime = null;
-                                                               break;
-                                                       } catch (IOException ioe) {
-                                                               // continue, try next browser
-                                                       }
-                                               }
-
-                                               if (runtime != null) {
-                                                       throw new IOException(
-                                                                       "Cannot find a working GUI browser...");
+                                       reader.open(book.getMeta().getLuid(), pg);
+                                       SwingUtilities.invokeLater(new Runnable() {
+                                               public void run() {
+                                                       book.setCached(true);
                                                }
-                                       }
+                                       });
                                } catch (IOException e) {
+                                       // TODO: error message?
                                        Instance.syserr(e);
                                }
                        }
@@ -438,15 +453,11 @@ class LocalReaderFrame extends JFrame {
        private void outOfUi(final Progress pg, final Runnable run) {
                pgBar.setProgress(pg);
 
-               SwingUtilities.invokeLater(new Runnable() {
-                       public void run() {
-                               setEnabled(false);
-                               pgBar.addActioListener(new ActionListener() {
-                                       public void actionPerformed(ActionEvent e) {
-                                               pgBar.setProgress(null);
-                                               setEnabled(true);
-                                       }
-                               });
+               setEnabled(false);
+               pgBar.addActioListener(new ActionListener() {
+                       public void actionPerformed(ActionEvent e) {
+                               pgBar.setProgress(null);
+                               setEnabled(true);
                        }
                });
 
@@ -468,6 +479,8 @@ class LocalReaderFrame extends JFrame {
 
        /**
         * Import a {@link Story} into the main {@link Library}.
+        * <p>
+        * Should be called inside the UI thread.
         * 
         * @param askUrl
         *            TRUE for an {@link URL}, false for a {@link File}
@@ -475,50 +488,80 @@ class LocalReaderFrame extends JFrame {
        private void imprt(boolean askUrl) {
                JFileChooser fc = new JFileChooser();
 
-               final String url;
+               Object url;
                if (askUrl) {
+                       String clipboard = "";
+                       try {
+                               clipboard = ("" + Toolkit.getDefaultToolkit()
+                                               .getSystemClipboard().getData(DataFlavor.stringFlavor))
+                                               .trim();
+                       } catch (Exception e) {
+                               // No data will be handled
+                       }
+
+                       if (clipboard == null || !clipboard.startsWith("http")) {
+                               clipboard = "";
+                       }
+
                        url = JOptionPane.showInputDialog(LocalReaderFrame.this,
                                        "url of the story to import?", "Importing from URL",
-                                       JOptionPane.QUESTION_MESSAGE);
+                                       JOptionPane.QUESTION_MESSAGE, null, null, clipboard);
                } else if (fc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION) {
                        url = fc.getSelectedFile().getAbsolutePath();
                } else {
                        url = null;
                }
 
-               if (url != null && !url.isEmpty()) {
-                       final Progress pg = new Progress("Importing " + url);
-                       outOfUi(pg, new Runnable() {
-                               public void run() {
-                                       Exception ex = null;
-                                       try {
-                                               Instance.getLibrary()
-                                                               .imprt(BasicReader.getUrl(url), pg);
-                                       } catch (IOException e) {
-                                               ex = e;
-                                       }
+               if (url != null && !url.toString().isEmpty()) {
+                       imprt(url.toString(), null);
+               }
+       }
 
-                                       final Exception e = ex;
+       /**
+        * Actually import the {@link Story} into the main {@link Library}.
+        * <p>
+        * Should be called inside the UI thread.
+        * 
+        * @param url
+        *            the {@link Story} to import by {@link URL}
+        * @param onSuccess
+        *            Action to execute on success
+        */
+       private void imprt(final String url, final Runnable onSuccess) {
+               final Progress pg = new Progress("Importing " + url);
+               outOfUi(pg, new Runnable() {
+                       public void run() {
+                               Exception ex = null;
+                               try {
+                                       Instance.getLibrary().imprt(BasicReader.getUrl(url), pg);
+                               } catch (IOException e) {
+                                       ex = e;
+                               }
 
-                                       final boolean ok = (e == null);
-                                       SwingUtilities.invokeLater(new Runnable() {
-                                               public void run() {
-                                                       if (!ok) {
-                                                               JOptionPane.showMessageDialog(
-                                                                               LocalReaderFrame.this,
-                                                                               "Cannot import: " + url,
-                                                                               e.getMessage(),
-                                                                               JOptionPane.ERROR_MESSAGE);
-
-                                                               setEnabled(true);
-                                                       } else {
+                               final Exception e = ex;
+
+                               final boolean ok = (e == null);
+                               SwingUtilities.invokeLater(new Runnable() {
+                                       public void run() {
+                                               if (!ok) {
+                                                       Instance.syserr(e);
+                                                       JOptionPane.showMessageDialog(
+                                                                       LocalReaderFrame.this, "Cannot import: "
+                                                                                       + url, e.getMessage(),
+                                                                       JOptionPane.ERROR_MESSAGE);
+
+                                                       setEnabled(true);
+                                               } else {
+                                                       refreshBooks(type);
+                                                       if (onSuccess != null) {
+                                                               onSuccess.run();
                                                                refreshBooks(type);
                                                        }
                                                }
-                                       });
-                               }
-                       });
-               }
+                                       }
+                               });
+                       }
+               });
        }
 
        /**
@@ -536,17 +579,14 @@ class LocalReaderFrame extends JFrame {
        public void setEnabled(boolean b) {
                for (LocalReaderBook book : books) {
                        book.setEnabled(b);
-                       book.validate();
                        book.repaint();
                }
 
                bar.setEnabled(b);
                bookPane.setEnabled(b);
-               bookPane.validate();
                bookPane.repaint();
 
                super.setEnabled(b);
-               validate();
                repaint();
        }
 }