Commit | Line | Data |
---|---|---|
3cdf3fd8 NR |
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; | |
04eafeea NR |
8 | import java.net.URL; |
9 | import java.net.UnknownHostException; | |
3cdf3fd8 NR |
10 | |
11 | import javax.swing.JDialog; | |
12 | import javax.swing.JLabel; | |
13 | import javax.swing.SwingWorker; | |
14 | ||
15 | import be.nikiroo.fanfix.Instance; | |
04eafeea | 16 | import be.nikiroo.fanfix.bundles.StringIdGui; |
3cdf3fd8 NR |
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; | |
04eafeea NR |
21 | import be.nikiroo.fanfix.library.LocalLibrary; |
22 | import be.nikiroo.fanfix.reader.BasicReader; | |
59253323 | 23 | import be.nikiroo.fanfix_swing.gui.utils.UiHelper; |
04eafeea | 24 | import be.nikiroo.utils.Progress; |
3cdf3fd8 NR |
25 | |
26 | public class Actions { | |
32ed6089 NR |
27 | static public void openExternal(final BasicLibrary lib, MetaData meta, |
28 | final Container parent, final Runnable onDone) { | |
59253323 NR |
29 | Container parentWindow = parent; |
30 | while (!(parentWindow instanceof Window) && parentWindow != null) { | |
31 | parentWindow = parentWindow.getParent(); | |
3cdf3fd8 NR |
32 | } |
33 | ||
34 | // TODO: UI | |
59253323 | 35 | final JDialog wait = new JDialog((Window) parentWindow); |
3cdf3fd8 NR |
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 { | |
59253323 | 77 | get(); |
3cdf3fd8 | 78 | openExternal(target, isImageDocument); |
59253323 NR |
79 | } catch (Exception e) { |
80 | // TODO: i18n | |
32ed6089 NR |
81 | UiHelper.error(parent, e.getLocalizedMessage(), |
82 | "Cannot open the story", e); | |
3cdf3fd8 NR |
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 | /** | |
32ed6089 NR |
102 | * Open the {@link Story} with an external reader (the program will be |
103 | * passed the given target file). | |
3cdf3fd8 | 104 | * |
32ed6089 NR |
105 | * @param target |
106 | * the target {@link File} | |
107 | * @param isImageDocument | |
108 | * TRUE for image documents, FALSE for not-images documents | |
3cdf3fd8 | 109 | * |
32ed6089 NR |
110 | * @throws IOException |
111 | * in case of I/O error | |
3cdf3fd8 | 112 | */ |
32ed6089 NR |
113 | static public void openExternal(File target, boolean isImageDocument) |
114 | throws IOException { | |
3cdf3fd8 NR |
115 | String program = null; |
116 | if (isImageDocument) { | |
32ed6089 NR |
117 | program = Instance.getInstance().getUiConfig() |
118 | .getString(UiConfig.IMAGES_DOCUMENT_READER); | |
3cdf3fd8 | 119 | } else { |
32ed6089 NR |
120 | program = Instance.getInstance().getUiConfig() |
121 | .getString(UiConfig.NON_IMAGES_DOCUMENT_READER); | |
3cdf3fd8 NR |
122 | } |
123 | ||
124 | if (program != null && program.trim().isEmpty()) { | |
125 | program = null; | |
126 | } | |
127 | ||
128 | start(target, program, false); | |
129 | } | |
130 | ||
131 | /** | |
32ed6089 NR |
132 | * Start a file and open it with the given program if given or the first |
133 | * default system starter we can find. | |
3cdf3fd8 | 134 | * |
32ed6089 NR |
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) | |
3cdf3fd8 | 142 | * |
32ed6089 NR |
143 | * @throws IOException |
144 | * in case of I/O error | |
3cdf3fd8 | 145 | */ |
32ed6089 NR |
146 | static protected void start(File target, String program, boolean sync) |
147 | throws IOException { | |
3cdf3fd8 NR |
148 | Process proc = null; |
149 | if (program == null) { | |
150 | boolean ok = false; | |
32ed6089 NR |
151 | for (String starter : new String[] { "xdg-open", "open", "see", |
152 | "start", "run" }) { | |
3cdf3fd8 | 153 | try { |
32ed6089 NR |
154 | Instance.getInstance().getTraceHandler() |
155 | .trace("starting external program"); | |
156 | proc = Runtime.getRuntime().exec( | |
157 | new String[] { starter, target.getAbsolutePath() }); | |
3cdf3fd8 NR |
158 | ok = true; |
159 | break; | |
160 | } catch (IOException e) { | |
161 | } | |
162 | } | |
163 | if (!ok) { | |
32ed6089 NR |
164 | throw new IOException( |
165 | "Cannot find a program to start the file"); | |
3cdf3fd8 NR |
166 | } |
167 | } else { | |
32ed6089 NR |
168 | Instance.getInstance().getTraceHandler() |
169 | .trace("starting external program"); | |
170 | proc = Runtime.getRuntime() | |
171 | .exec(new String[] { program, target.getAbsolutePath() }); | |
3cdf3fd8 NR |
172 | } |
173 | ||
174 | if (proc != null && sync) { | |
175 | try { | |
176 | proc.waitFor(); | |
177 | } catch (InterruptedException e) { | |
178 | } | |
179 | } | |
180 | } | |
04eafeea | 181 | |
04eafeea NR |
182 | /** |
183 | * Actually import the {@link Story} into the main {@link LocalLibrary}. | |
184 | * <p> | |
e3fef8b5 NR |
185 | * Should be called inside the UI thread, will start a worker (i.e., this is |
186 | * asynchronous). | |
04eafeea | 187 | * |
e3fef8b5 NR |
188 | * @param parent |
189 | * a container we can use to show error messages if any | |
32ed6089 NR |
190 | * @param url |
191 | * the {@link Story} to import by {@link URL} | |
e3fef8b5 NR |
192 | * @param pg |
193 | * the optional progress reporter | |
32ed6089 NR |
194 | * @param onSuccess |
195 | * Action to execute on success | |
04eafeea | 196 | */ |
32ed6089 | 197 | static public void imprt(final Container parent, final String url, |
e3fef8b5 NR |
198 | final Progress pg, final Runnable onSuccess) { |
199 | final Progress fpg = pg; | |
04eafeea NR |
200 | new SwingWorker<Void, Void>() { |
201 | @Override | |
202 | protected Void doInBackground() throws Exception { | |
e3fef8b5 NR |
203 | Progress pg = fpg; |
204 | if (pg == null) | |
205 | pg = new Progress(); | |
04eafeea | 206 | |
e3fef8b5 NR |
207 | try { |
208 | Instance.getInstance().getLibrary() | |
209 | .imprt(BasicReader.getUrl(url), fpg); | |
04eafeea | 210 | |
e3fef8b5 NR |
211 | fpg.done(); |
212 | if (onSuccess != null) { | |
213 | onSuccess.run(); | |
214 | } | |
215 | } catch (IOException e) { | |
216 | fpg.done(); | |
04eafeea NR |
217 | if (e instanceof UnknownHostException) { |
218 | UiHelper.error(parent, | |
32ed6089 NR |
219 | Instance.getInstance().getTransGui().getString( |
220 | StringIdGui.ERROR_URL_NOT_SUPPORTED, | |
04eafeea | 221 | url), |
32ed6089 NR |
222 | Instance.getInstance().getTransGui().getString( |
223 | StringIdGui.TITLE_ERROR), | |
224 | null); | |
04eafeea NR |
225 | } else { |
226 | UiHelper.error(parent, | |
32ed6089 NR |
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); | |
04eafeea | 233 | } |
04eafeea | 234 | } |
32ed6089 | 235 | |
04eafeea NR |
236 | return null; |
237 | } | |
238 | }.execute(); | |
239 | } | |
3cdf3fd8 | 240 | } |