Commit | Line | Data |
---|---|---|
3cdf3fd8 NR |
1 | package be.nikiroo.fanfix_swing; |
2 | ||
3 | import java.awt.BorderLayout; | |
4 | import java.awt.Container; | |
04eafeea | 5 | import java.awt.Toolkit; |
3cdf3fd8 | 6 | import java.awt.Window; |
04eafeea | 7 | import java.awt.datatransfer.DataFlavor; |
3cdf3fd8 NR |
8 | import java.io.File; |
9 | import java.io.IOException; | |
04eafeea NR |
10 | import java.net.URL; |
11 | import java.net.UnknownHostException; | |
3cdf3fd8 NR |
12 | |
13 | import javax.swing.JDialog; | |
04eafeea | 14 | import javax.swing.JFileChooser; |
3cdf3fd8 | 15 | import javax.swing.JLabel; |
04eafeea | 16 | import javax.swing.JOptionPane; |
3cdf3fd8 NR |
17 | import javax.swing.SwingWorker; |
18 | ||
19 | import be.nikiroo.fanfix.Instance; | |
04eafeea | 20 | import be.nikiroo.fanfix.bundles.StringIdGui; |
3cdf3fd8 NR |
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; | |
04eafeea NR |
25 | import be.nikiroo.fanfix.library.LocalLibrary; |
26 | import be.nikiroo.fanfix.reader.BasicReader; | |
59253323 | 27 | import be.nikiroo.fanfix_swing.gui.utils.UiHelper; |
04eafeea | 28 | import be.nikiroo.utils.Progress; |
3cdf3fd8 NR |
29 | |
30 | public class Actions { | |
32ed6089 NR |
31 | static public void openExternal(final BasicLibrary lib, MetaData meta, |
32 | final Container parent, final Runnable onDone) { | |
59253323 NR |
33 | Container parentWindow = parent; |
34 | while (!(parentWindow instanceof Window) && parentWindow != null) { | |
35 | parentWindow = parentWindow.getParent(); | |
3cdf3fd8 NR |
36 | } |
37 | ||
38 | // TODO: UI | |
59253323 | 39 | final JDialog wait = new JDialog((Window) parentWindow); |
3cdf3fd8 NR |
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 { | |
59253323 | 81 | get(); |
3cdf3fd8 | 82 | openExternal(target, isImageDocument); |
59253323 NR |
83 | } catch (Exception e) { |
84 | // TODO: i18n | |
32ed6089 NR |
85 | UiHelper.error(parent, e.getLocalizedMessage(), |
86 | "Cannot open the story", e); | |
3cdf3fd8 NR |
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 | /** | |
32ed6089 NR |
106 | * Open the {@link Story} with an external reader (the program will be |
107 | * passed the given target file). | |
3cdf3fd8 | 108 | * |
32ed6089 NR |
109 | * @param target |
110 | * the target {@link File} | |
111 | * @param isImageDocument | |
112 | * TRUE for image documents, FALSE for not-images documents | |
3cdf3fd8 | 113 | * |
32ed6089 NR |
114 | * @throws IOException |
115 | * in case of I/O error | |
3cdf3fd8 | 116 | */ |
32ed6089 NR |
117 | static public void openExternal(File target, boolean isImageDocument) |
118 | throws IOException { | |
3cdf3fd8 NR |
119 | String program = null; |
120 | if (isImageDocument) { | |
32ed6089 NR |
121 | program = Instance.getInstance().getUiConfig() |
122 | .getString(UiConfig.IMAGES_DOCUMENT_READER); | |
3cdf3fd8 | 123 | } else { |
32ed6089 NR |
124 | program = Instance.getInstance().getUiConfig() |
125 | .getString(UiConfig.NON_IMAGES_DOCUMENT_READER); | |
3cdf3fd8 NR |
126 | } |
127 | ||
128 | if (program != null && program.trim().isEmpty()) { | |
129 | program = null; | |
130 | } | |
131 | ||
132 | start(target, program, false); | |
133 | } | |
134 | ||
135 | /** | |
32ed6089 NR |
136 | * Start a file and open it with the given program if given or the first |
137 | * default system starter we can find. | |
3cdf3fd8 | 138 | * |
32ed6089 NR |
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) | |
3cdf3fd8 | 146 | * |
32ed6089 NR |
147 | * @throws IOException |
148 | * in case of I/O error | |
3cdf3fd8 | 149 | */ |
32ed6089 NR |
150 | static protected void start(File target, String program, boolean sync) |
151 | throws IOException { | |
3cdf3fd8 NR |
152 | Process proc = null; |
153 | if (program == null) { | |
154 | boolean ok = false; | |
32ed6089 NR |
155 | for (String starter : new String[] { "xdg-open", "open", "see", |
156 | "start", "run" }) { | |
3cdf3fd8 | 157 | try { |
32ed6089 NR |
158 | Instance.getInstance().getTraceHandler() |
159 | .trace("starting external program"); | |
160 | proc = Runtime.getRuntime().exec( | |
161 | new String[] { starter, target.getAbsolutePath() }); | |
3cdf3fd8 NR |
162 | ok = true; |
163 | break; | |
164 | } catch (IOException e) { | |
165 | } | |
166 | } | |
167 | if (!ok) { | |
32ed6089 NR |
168 | throw new IOException( |
169 | "Cannot find a program to start the file"); | |
3cdf3fd8 NR |
170 | } |
171 | } else { | |
32ed6089 NR |
172 | Instance.getInstance().getTraceHandler() |
173 | .trace("starting external program"); | |
174 | proc = Runtime.getRuntime() | |
175 | .exec(new String[] { program, target.getAbsolutePath() }); | |
3cdf3fd8 NR |
176 | } |
177 | ||
178 | if (proc != null && sync) { | |
179 | try { | |
180 | proc.waitFor(); | |
181 | } catch (InterruptedException e) { | |
182 | } | |
183 | } | |
184 | } | |
04eafeea NR |
185 | |
186 | /** | |
187 | * Import a {@link Story} into the main {@link LocalLibrary}. | |
188 | * <p> | |
189 | * Should be called inside the UI thread. | |
190 | * | |
32ed6089 NR |
191 | * @param askUrl |
192 | * TRUE for an {@link URL}, false for a {@link File} | |
04eafeea | 193 | */ |
32ed6089 NR |
194 | static public void imprt(final Container parent, boolean askUrl, |
195 | final Runnable onSuccess) { | |
04eafeea NR |
196 | JFileChooser fc = new JFileChooser(); |
197 | ||
198 | Object url; | |
199 | if (askUrl) { | |
200 | String clipboard = ""; | |
201 | try { | |
32ed6089 NR |
202 | clipboard = ("" + Toolkit.getDefaultToolkit() |
203 | .getSystemClipboard().getData(DataFlavor.stringFlavor)) | |
204 | .trim(); | |
04eafeea NR |
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, | |
32ed6089 NR |
215 | Instance.getInstance().getTransGui() |
216 | .getString(StringIdGui.SUBTITLE_IMPORT_URL), | |
217 | Instance.getInstance().getTransGui() | |
218 | .getString(StringIdGui.TITLE_IMPORT_URL), | |
04eafeea NR |
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 | * | |
32ed6089 NR |
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 | |
04eafeea | 242 | */ |
32ed6089 NR |
243 | static public void imprt(final Container parent, final String url, |
244 | final Runnable onSuccess, String onSuccessPgName) { | |
04eafeea NR |
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 { | |
32ed6089 NR |
257 | meta = Instance.getInstance().getLibrary() |
258 | .imprt(BasicReader.getUrl(url), pgImprt); | |
04eafeea NR |
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, | |
32ed6089 NR |
272 | Instance.getInstance().getTransGui().getString( |
273 | StringIdGui.ERROR_URL_NOT_SUPPORTED, | |
04eafeea | 274 | url), |
32ed6089 NR |
275 | Instance.getInstance().getTransGui().getString( |
276 | StringIdGui.TITLE_ERROR), | |
277 | null); | |
04eafeea NR |
278 | } else { |
279 | UiHelper.error(parent, | |
32ed6089 NR |
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); | |
04eafeea NR |
286 | } |
287 | } else { | |
288 | if (onSuccess != null) { | |
289 | onSuccess.run(); | |
290 | } | |
291 | } | |
292 | pgOnSuccess.done(); | |
32ed6089 | 293 | |
04eafeea NR |
294 | return null; |
295 | } | |
296 | }.execute(); | |
297 | } | |
3cdf3fd8 | 298 | } |