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