| 1 | package be.nikiroo.fanfix_swing; |
| 2 | |
| 3 | import java.awt.BorderLayout; |
| 4 | import java.awt.Container; |
| 5 | import java.awt.Toolkit; |
| 6 | import java.awt.Window; |
| 7 | import java.awt.datatransfer.DataFlavor; |
| 8 | import java.io.File; |
| 9 | import java.io.IOException; |
| 10 | import java.net.URL; |
| 11 | import java.net.UnknownHostException; |
| 12 | |
| 13 | import javax.swing.JDialog; |
| 14 | import javax.swing.JFileChooser; |
| 15 | import javax.swing.JLabel; |
| 16 | import javax.swing.JOptionPane; |
| 17 | import javax.swing.SwingWorker; |
| 18 | |
| 19 | import be.nikiroo.fanfix.Instance; |
| 20 | import be.nikiroo.fanfix.bundles.StringIdGui; |
| 21 | import be.nikiroo.fanfix.bundles.UiConfig; |
| 22 | import be.nikiroo.fanfix.data.MetaData; |
| 23 | import be.nikiroo.fanfix.data.Story; |
| 24 | import be.nikiroo.fanfix.library.BasicLibrary; |
| 25 | import be.nikiroo.fanfix.library.LocalLibrary; |
| 26 | import be.nikiroo.fanfix.reader.BasicReader; |
| 27 | import be.nikiroo.fanfix_swing.gui.utils.UiHelper; |
| 28 | import be.nikiroo.utils.Progress; |
| 29 | |
| 30 | public class Actions { |
| 31 | static public void openExternal(final BasicLibrary lib, MetaData meta, |
| 32 | final Container parent, final Runnable onDone) { |
| 33 | Container parentWindow = parent; |
| 34 | while (!(parentWindow instanceof Window) && parentWindow != null) { |
| 35 | parentWindow = parentWindow.getParent(); |
| 36 | } |
| 37 | |
| 38 | // TODO: UI |
| 39 | final JDialog wait = new JDialog((Window) parentWindow); |
| 40 | wait.setTitle("Opening story"); |
| 41 | wait.setSize(400, 300); |
| 42 | wait.setLayout(new BorderLayout()); |
| 43 | wait.add(new JLabel("Waiting...")); |
| 44 | |
| 45 | // TODO: pg? |
| 46 | |
| 47 | final Object waitLock = new Object(); |
| 48 | final Boolean[] waitScreen = new Boolean[] { false }; |
| 49 | new Thread(new Runnable() { |
| 50 | @Override |
| 51 | public void run() { |
| 52 | try { |
| 53 | Thread.sleep(200); |
| 54 | } catch (InterruptedException e) { |
| 55 | } |
| 56 | |
| 57 | synchronized (waitLock) { |
| 58 | if (!waitScreen[0]) { |
| 59 | waitScreen[0] = true; |
| 60 | wait.setVisible(true); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | }).start(); |
| 65 | |
| 66 | final String luid = meta.getLuid(); |
| 67 | final boolean isImageDocument = meta.isImageDocument(); |
| 68 | |
| 69 | final SwingWorker<File, Void> worker = new SwingWorker<File, Void>() { |
| 70 | private File target; |
| 71 | |
| 72 | @Override |
| 73 | protected File doInBackground() throws Exception { |
| 74 | target = lib.getFile(luid, null); |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | protected void done() { |
| 80 | try { |
| 81 | get(); |
| 82 | openExternal(target, isImageDocument); |
| 83 | } catch (Exception e) { |
| 84 | // TODO: i18n |
| 85 | UiHelper.error(parent, e.getLocalizedMessage(), |
| 86 | "Cannot open the story", e); |
| 87 | } |
| 88 | |
| 89 | synchronized (waitLock) { |
| 90 | if (waitScreen[0]) { |
| 91 | wait.setVisible(false); |
| 92 | } |
| 93 | waitScreen[0] = true; |
| 94 | } |
| 95 | |
| 96 | if (onDone != null) { |
| 97 | onDone.run(); |
| 98 | } |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | worker.execute(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Open the {@link Story} with an external reader (the program will be |
| 107 | * passed the given target file). |
| 108 | * |
| 109 | * @param target |
| 110 | * the target {@link File} |
| 111 | * @param isImageDocument |
| 112 | * TRUE for image documents, FALSE for not-images documents |
| 113 | * |
| 114 | * @throws IOException |
| 115 | * in case of I/O error |
| 116 | */ |
| 117 | static public void openExternal(File target, boolean isImageDocument) |
| 118 | throws IOException { |
| 119 | String program = null; |
| 120 | if (isImageDocument) { |
| 121 | program = Instance.getInstance().getUiConfig() |
| 122 | .getString(UiConfig.IMAGES_DOCUMENT_READER); |
| 123 | } else { |
| 124 | program = Instance.getInstance().getUiConfig() |
| 125 | .getString(UiConfig.NON_IMAGES_DOCUMENT_READER); |
| 126 | } |
| 127 | |
| 128 | if (program != null && program.trim().isEmpty()) { |
| 129 | program = null; |
| 130 | } |
| 131 | |
| 132 | start(target, program, false); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Start a file and open it with the given program if given or the first |
| 137 | * default system starter we can find. |
| 138 | * |
| 139 | * @param target |
| 140 | * the target to open |
| 141 | * @param program |
| 142 | * the program to use or NULL for the default system starter |
| 143 | * @param sync |
| 144 | * execute the process synchronously (wait until it is terminated |
| 145 | * before returning) |
| 146 | * |
| 147 | * @throws IOException |
| 148 | * in case of I/O error |
| 149 | */ |
| 150 | static protected void start(File target, String program, boolean sync) |
| 151 | throws IOException { |
| 152 | Process proc = null; |
| 153 | if (program == null) { |
| 154 | boolean ok = false; |
| 155 | for (String starter : new String[] { "xdg-open", "open", "see", |
| 156 | "start", "run" }) { |
| 157 | try { |
| 158 | Instance.getInstance().getTraceHandler() |
| 159 | .trace("starting external program"); |
| 160 | proc = Runtime.getRuntime().exec( |
| 161 | new String[] { starter, target.getAbsolutePath() }); |
| 162 | ok = true; |
| 163 | break; |
| 164 | } catch (IOException e) { |
| 165 | } |
| 166 | } |
| 167 | if (!ok) { |
| 168 | throw new IOException( |
| 169 | "Cannot find a program to start the file"); |
| 170 | } |
| 171 | } else { |
| 172 | Instance.getInstance().getTraceHandler() |
| 173 | .trace("starting external program"); |
| 174 | proc = Runtime.getRuntime() |
| 175 | .exec(new String[] { program, target.getAbsolutePath() }); |
| 176 | } |
| 177 | |
| 178 | if (proc != null && sync) { |
| 179 | try { |
| 180 | proc.waitFor(); |
| 181 | } catch (InterruptedException e) { |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Import a {@link Story} into the main {@link LocalLibrary}. |
| 188 | * <p> |
| 189 | * Should be called inside the UI thread. |
| 190 | * |
| 191 | * @param askUrl |
| 192 | * TRUE for an {@link URL}, false for a {@link File} |
| 193 | */ |
| 194 | static public void imprt(final Container parent, boolean askUrl, |
| 195 | final Runnable onSuccess) { |
| 196 | JFileChooser fc = new JFileChooser(); |
| 197 | |
| 198 | Object url; |
| 199 | if (askUrl) { |
| 200 | String clipboard = ""; |
| 201 | try { |
| 202 | clipboard = ("" + Toolkit.getDefaultToolkit() |
| 203 | .getSystemClipboard().getData(DataFlavor.stringFlavor)) |
| 204 | .trim(); |
| 205 | } catch (Exception e) { |
| 206 | // No data will be handled |
| 207 | } |
| 208 | |
| 209 | if (clipboard == null || !(clipboard.startsWith("http://") || // |
| 210 | clipboard.startsWith("https://"))) { |
| 211 | clipboard = ""; |
| 212 | } |
| 213 | |
| 214 | url = JOptionPane.showInputDialog(parent, |
| 215 | Instance.getInstance().getTransGui() |
| 216 | .getString(StringIdGui.SUBTITLE_IMPORT_URL), |
| 217 | Instance.getInstance().getTransGui() |
| 218 | .getString(StringIdGui.TITLE_IMPORT_URL), |
| 219 | JOptionPane.QUESTION_MESSAGE, null, null, clipboard); |
| 220 | } else if (fc.showOpenDialog(parent) != JFileChooser.CANCEL_OPTION) { |
| 221 | url = fc.getSelectedFile().getAbsolutePath(); |
| 222 | } else { |
| 223 | url = null; |
| 224 | } |
| 225 | |
| 226 | if (url != null && !url.toString().isEmpty()) { |
| 227 | imprt(parent, url.toString(), null, null); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Actually import the {@link Story} into the main {@link LocalLibrary}. |
| 233 | * <p> |
| 234 | * Should be called inside the UI thread. |
| 235 | * |
| 236 | * @param url |
| 237 | * the {@link Story} to import by {@link URL} |
| 238 | * @param onSuccess |
| 239 | * Action to execute on success |
| 240 | * @param onSuccessPgName |
| 241 | * the name to use for the onSuccess progress bar |
| 242 | */ |
| 243 | static public void imprt(final Container parent, final String url, |
| 244 | final Runnable onSuccess, String onSuccessPgName) { |
| 245 | final Progress pg = new Progress(); |
| 246 | final Progress pgImprt = new Progress(); |
| 247 | final Progress pgOnSuccess = new Progress(onSuccessPgName); |
| 248 | pg.addProgress(pgImprt, 95); |
| 249 | pg.addProgress(pgOnSuccess, 5); |
| 250 | |
| 251 | new SwingWorker<Void, Void>() { |
| 252 | @Override |
| 253 | protected Void doInBackground() throws Exception { |
| 254 | Exception ex = null; |
| 255 | MetaData meta = null; |
| 256 | try { |
| 257 | meta = Instance.getInstance().getLibrary() |
| 258 | .imprt(BasicReader.getUrl(url), pgImprt); |
| 259 | } catch (IOException e) { |
| 260 | e.printStackTrace(); |
| 261 | ex = e; |
| 262 | } |
| 263 | |
| 264 | final Exception e = ex; |
| 265 | |
| 266 | final boolean ok = (e == null); |
| 267 | |
| 268 | pgOnSuccess.setProgress(0); |
| 269 | if (!ok) { |
| 270 | if (e instanceof UnknownHostException) { |
| 271 | UiHelper.error(parent, |
| 272 | Instance.getInstance().getTransGui().getString( |
| 273 | StringIdGui.ERROR_URL_NOT_SUPPORTED, |
| 274 | url), |
| 275 | Instance.getInstance().getTransGui().getString( |
| 276 | StringIdGui.TITLE_ERROR), |
| 277 | null); |
| 278 | } else { |
| 279 | UiHelper.error(parent, |
| 280 | Instance.getInstance().getTransGui().getString( |
| 281 | StringIdGui.ERROR_URL_IMPORT_FAILED, |
| 282 | url, e.getMessage()), |
| 283 | Instance.getInstance().getTransGui() |
| 284 | .getString(StringIdGui.TITLE_ERROR), |
| 285 | e); |
| 286 | } |
| 287 | } else { |
| 288 | if (onSuccess != null) { |
| 289 | onSuccess.run(); |
| 290 | } |
| 291 | } |
| 292 | pgOnSuccess.done(); |
| 293 | |
| 294 | return null; |
| 295 | } |
| 296 | }.execute(); |
| 297 | } |
| 298 | } |