From: Niki Roo Date: Wed, 8 Apr 2020 22:18:34 +0000 (+0200) Subject: beta option: download url X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=04eafeeaebf720196018d65a7a62b4276e26d0d1;p=nikiroo-utils.git beta option: download url --- diff --git a/src/be/nikiroo/fanfix_swing/Actions.java b/src/be/nikiroo/fanfix_swing/Actions.java index 5a2a5b5..5df9b2e 100644 --- a/src/be/nikiroo/fanfix_swing/Actions.java +++ b/src/be/nikiroo/fanfix_swing/Actions.java @@ -2,20 +2,30 @@ package be.nikiroo.fanfix_swing; import java.awt.BorderLayout; import java.awt.Container; +import java.awt.Toolkit; import java.awt.Window; +import java.awt.datatransfer.DataFlavor; import java.io.File; import java.io.IOException; +import java.net.URL; +import java.net.UnknownHostException; import javax.swing.JDialog; +import javax.swing.JFileChooser; import javax.swing.JLabel; +import javax.swing.JOptionPane; import javax.swing.SwingWorker; import be.nikiroo.fanfix.Instance; +import be.nikiroo.fanfix.bundles.StringIdGui; import be.nikiroo.fanfix.bundles.UiConfig; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; import be.nikiroo.fanfix.library.BasicLibrary; +import be.nikiroo.fanfix.library.LocalLibrary; +import be.nikiroo.fanfix.reader.BasicReader; import be.nikiroo.fanfix_swing.gui.utils.UiHelper; +import be.nikiroo.utils.Progress; public class Actions { static public void openExternal(final BasicLibrary lib, MetaData meta, final Container parent, @@ -155,4 +165,102 @@ public class Actions { } } } + + /** + * Import a {@link Story} into the main {@link LocalLibrary}. + *

+ * Should be called inside the UI thread. + * + * @param askUrl TRUE for an {@link URL}, false for a {@link File} + */ + static public void imprt(final Container parent, boolean askUrl, final Runnable onSuccess) { + JFileChooser fc = new JFileChooser(); + + 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.startsWith("https://"))) { + clipboard = ""; + } + + url = JOptionPane.showInputDialog(parent, + Instance.getInstance().getTransGui().getString(StringIdGui.SUBTITLE_IMPORT_URL), + Instance.getInstance().getTransGui().getString(StringIdGui.TITLE_IMPORT_URL), + JOptionPane.QUESTION_MESSAGE, null, null, clipboard); + } else if (fc.showOpenDialog(parent) != JFileChooser.CANCEL_OPTION) { + url = fc.getSelectedFile().getAbsolutePath(); + } else { + url = null; + } + + if (url != null && !url.toString().isEmpty()) { + imprt(parent, url.toString(), null, null); + } + } + + /** + * Actually import the {@link Story} into the main {@link LocalLibrary}. + *

+ * Should be called inside the UI thread. + * + * @param url the {@link Story} to import by {@link URL} + * @param onSuccess Action to execute on success + * @param onSuccessPgName the name to use for the onSuccess progress bar + */ + static public void imprt(final Container parent, final String url, final Runnable onSuccess, + String onSuccessPgName) { + final Progress pg = new Progress(); + final Progress pgImprt = new Progress(); + final Progress pgOnSuccess = new Progress(onSuccessPgName); + pg.addProgress(pgImprt, 95); + pg.addProgress(pgOnSuccess, 5); + + new SwingWorker() { + @Override + protected Void doInBackground() throws Exception { + Exception ex = null; + MetaData meta = null; + try { + meta = Instance.getInstance().getLibrary().imprt(BasicReader.getUrl(url), pgImprt); + } catch (IOException e) { + e.printStackTrace(); + ex = e; + } + + final Exception e = ex; + + final boolean ok = (e == null); + + pgOnSuccess.setProgress(0); + if (!ok) { + if (e instanceof UnknownHostException) { + UiHelper.error(parent, + Instance.getInstance().getTransGui().getString(StringIdGui.ERROR_URL_NOT_SUPPORTED, + url), + Instance.getInstance().getTransGui().getString(StringIdGui.TITLE_ERROR), null); + } else { + UiHelper.error(parent, + Instance.getInstance().getTransGui().getString(StringIdGui.ERROR_URL_IMPORT_FAILED, url, + e.getMessage()), + Instance.getInstance().getTransGui().getString(StringIdGui.TITLE_ERROR), e); + } + } else { + if (onSuccess != null) { + onSuccess.run(); + } + } + pgOnSuccess.done(); + + return null; + } + }.execute(); + } } diff --git a/src/be/nikiroo/fanfix_swing/gui/MainFrame.java b/src/be/nikiroo/fanfix_swing/gui/MainFrame.java index 36e3c6a..39762a8 100644 --- a/src/be/nikiroo/fanfix_swing/gui/MainFrame.java +++ b/src/be/nikiroo/fanfix_swing/gui/MainFrame.java @@ -12,11 +12,13 @@ import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JSplitPane; +import be.nikiroo.fanfix_swing.Actions; import be.nikiroo.utils.Version; public class MainFrame extends JFrame { private BooksPanel books; private DetailsPanel details; + private BrowserPanel browser; public MainFrame(boolean sidePanel, boolean detailsPanel) { super("Fanfix " + Version.getCurrentVersion()); @@ -26,7 +28,7 @@ public class MainFrame extends JFrame { sidePanel = true; detailsPanel = true; - final BrowserPanel browser = new BrowserPanel(); + browser = new BrowserPanel(); JComponent other = null; boolean orientationH = true; @@ -86,15 +88,40 @@ public class MainFrame extends JFrame { JMenu file = new JMenu("File"); file.setMnemonic(KeyEvent.VK_F); - JMenuItem item1 = new JMenuItem("Uuu", KeyEvent.VK_U); + JMenuItem item1 = new JMenuItem("Download", KeyEvent.VK_D); item1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - System.out.println("Uuu: ACTION"); + Actions.imprt(MainFrame.this, true, new Runnable() { + @Override + public void run() { + browser.reloadData(); + books.load(browser.getSelectedSources(), browser.getSelectedAuthors(), + browser.getSelectedTags()); + details.setBook(browser.getHighlight()); + } + }); + } + }); + + JMenuItem item2 = new JMenuItem("Import file", KeyEvent.VK_I); + item2.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Actions.imprt(MainFrame.this, false, new Runnable() { + @Override + public void run() { + browser.reloadData(); + books.load(browser.getSelectedSources(), browser.getSelectedAuthors(), + browser.getSelectedTags()); + details.setBook(browser.getHighlight()); + } + }); } }); file.add(item1); + file.add(item2); JMenu edit = new JMenu("Edit"); edit.setMnemonic(KeyEvent.VK_E);