gui: better app exit
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReader.java
CommitLineData
16a81ef7 1package be.nikiroo.fanfix.reader.ui;
a6395bef 2
edd46289 3import java.awt.Desktop;
a6395bef 4import java.awt.EventQueue;
350bc060
NR
5import java.awt.event.WindowAdapter;
6import java.awt.event.WindowEvent;
a6395bef
NR
7import java.io.File;
8import java.io.IOException;
b42117f1
NR
9import java.net.URISyntaxException;
10
11import javax.swing.JEditorPane;
12import javax.swing.JLabel;
13import javax.swing.JOptionPane;
14import javax.swing.event.HyperlinkEvent;
15import javax.swing.event.HyperlinkListener;
a6395bef
NR
16
17import be.nikiroo.fanfix.Instance;
b42117f1 18import be.nikiroo.fanfix.VersionCheck;
bc2ea776 19import be.nikiroo.fanfix.data.MetaData;
a6395bef 20import be.nikiroo.fanfix.data.Story;
ff05b828
NR
21import be.nikiroo.fanfix.library.BasicLibrary;
22import be.nikiroo.fanfix.library.CacheLibrary;
16a81ef7 23import be.nikiroo.fanfix.reader.BasicReader;
c3b229a1 24import be.nikiroo.fanfix.reader.Reader;
3b2b638f 25import be.nikiroo.utils.Progress;
b42117f1 26import be.nikiroo.utils.Version;
b0e88ebd 27import be.nikiroo.utils.ui.UIUtils;
a6395bef 28
c3b229a1
NR
29/**
30 * This class implements a graphical {@link Reader} using the Swing library from
31 * Java.
32 * <p>
33 * It can thus be themed to look native-like, or metal-like, or use any other
34 * theme you may want to try.
35 * <p>
36 * We actually try to enable native look-alike mode on start.
37 *
38 * @author niki
39 */
5dd985cf 40class GuiReader extends BasicReader {
b0e88ebd
NR
41 static private boolean nativeLookLoaded;
42
ff05b828
NR
43 private CacheLibrary cacheLib;
44
45 private File cacheDir;
a6395bef 46
c3b229a1
NR
47 /**
48 * Create a new graphical {@link Reader}.
49 *
50 * @throws IOException
51 * in case of I/O errors
52 */
5dd985cf 53 public GuiReader() throws IOException {
c3b229a1 54 // TODO: allow different themes?
b0e88ebd
NR
55 if (!nativeLookLoaded) {
56 UIUtils.setLookAndFeel();
57 nativeLookLoaded = true;
58 }
59
ff05b828
NR
60 cacheDir = Instance.getReaderDir();
61 cacheDir.mkdirs();
62 if (!cacheDir.exists()) {
a6395bef 63 throw new IOException(
ff05b828
NR
64 "Cannote create cache directory for local reader: "
65 + cacheDir);
66 }
67 }
68
69 @Override
70 public synchronized BasicLibrary getLibrary() {
71 if (cacheLib == null) {
72 BasicLibrary lib = super.getLibrary();
73 if (lib instanceof CacheLibrary) {
74 cacheLib = (CacheLibrary) lib;
75 } else {
76 cacheLib = new CacheLibrary(cacheDir, lib);
77 }
a6395bef
NR
78 }
79
ff05b828 80 return cacheLib;
a6395bef
NR
81 }
82
211f7ddb 83 @Override
350bc060 84 public void read(boolean sync) throws IOException {
bc2ea776
NR
85 MetaData meta = getMeta();
86
87 if (meta == null) {
edd46289
NR
88 throw new IOException("No story to read");
89 }
90
350bc060 91 read(meta.getLuid(), sync, null);
a6395bef
NR
92 }
93
9843a5e5
NR
94 /**
95 * Check if the {@link Story} denoted by this Library UID is present in the
5dd985cf 96 * {@link GuiReader} cache.
9843a5e5
NR
97 *
98 * @param luid
99 * the Library UID
100 *
101 * @return TRUE if it is
102 */
10d558d2 103 public boolean isCached(String luid) {
ff05b828 104 return cacheLib.isCached(luid);
10d558d2
NR
105 }
106
211f7ddb 107 @Override
b0e88ebd 108 public void browse(String type) {
b42117f1
NR
109 // TODO: improve presentation of update message
110 final VersionCheck updates = VersionCheck.check();
111 StringBuilder builder = new StringBuilder();
350bc060 112 final Boolean[] done = new Boolean[] { false };
b42117f1
NR
113
114 final JEditorPane updateMessage = new JEditorPane("text/html", "");
115 if (updates.isNewVersionAvailable()) {
116 builder.append("A new version of the program is available at <span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>");
117 builder.append("<br>");
118 builder.append("<br>");
119 for (Version v : updates.getNewer()) {
120 builder.append("\t<b>Version " + v + "</b>");
121 builder.append("<br>");
122 builder.append("<ul>");
123 for (String item : updates.getChanges().get(v)) {
124 builder.append("<li>" + item + "</li>");
125 }
126 builder.append("</ul>");
127 }
128
129 // html content
130 updateMessage.setText("<html><body>" //
131 + builder//
132 + "</body></html>");
133
134 // handle link events
135 updateMessage.addHyperlinkListener(new HyperlinkListener() {
211f7ddb 136 @Override
b42117f1
NR
137 public void hyperlinkUpdate(HyperlinkEvent e) {
138 if (e.getEventType().equals(
139 HyperlinkEvent.EventType.ACTIVATED))
140 try {
141 Desktop.getDesktop().browse(e.getURL().toURI());
142 } catch (IOException ee) {
62c63b07 143 Instance.getTraceHandler().error(ee);
b42117f1 144 } catch (URISyntaxException ee) {
62c63b07 145 Instance.getTraceHandler().error(ee);
b42117f1
NR
146 }
147 }
148 });
149 updateMessage.setEditable(false);
150 updateMessage.setBackground(new JLabel().getBackground());
151 }
152
333f0e7b 153 final String typeFinal = type;
a6395bef 154 EventQueue.invokeLater(new Runnable() {
211f7ddb 155 @Override
a6395bef 156 public void run() {
b42117f1
NR
157 if (updates.isNewVersionAvailable()) {
158 int rep = JOptionPane.showConfirmDialog(null,
159 updateMessage, "Updates available",
160 JOptionPane.OK_CANCEL_OPTION);
161 if (rep == JOptionPane.OK_OPTION) {
162 updates.ok();
163 } else {
164 updates.ignore();
165 }
166 }
167
350bc060
NR
168 try {
169 GuiReaderFrame gui = new GuiReaderFrame(GuiReader.this,
170 typeFinal);
350bc060
NR
171 gui.addWindowListener(new WindowAdapter() {
172 @Override
31e28683
NR
173 public void windowClosing(WindowEvent e) {
174 super.windowClosing(e);
350bc060
NR
175 done[0] = true;
176 }
177 });
31e28683
NR
178
179 gui.setVisible(true);
350bc060
NR
180 } catch (Exception e) {
181 Instance.getTraceHandler().error(e);
182 done[0] = true;
183 }
a6395bef
NR
184 }
185 });
350bc060
NR
186
187 // This action must be synchronous, so wait until the frame is closed
188 while (!done[0]) {
189 try {
190 Thread.sleep(100);
191 } catch (InterruptedException e) {
192 }
193 }
a6395bef 194 }
10d558d2 195
16a81ef7 196 @Override
350bc060
NR
197 public void start(File target, String program, boolean sync)
198 throws IOException {
199
200 boolean handled = false;
201 if (program == null && !sync) {
16a81ef7
NR
202 try {
203 Desktop.getDesktop().browse(target.toURI());
350bc060 204 handled = true;
16a81ef7 205 } catch (UnsupportedOperationException e) {
16a81ef7 206 }
350bc060
NR
207 }
208
209 if (!handled) {
210 super.start(target, program, sync);
16a81ef7
NR
211 }
212 }
213
c3b229a1
NR
214 /**
215 * Delete the {@link Story} from the cache if it is present, but <b>NOT</b>
216 * from the main library.
217 * <p>
218 * The next time we try to retrieve the {@link Story}, it may be required to
219 * cache it again.
220 *
221 * @param luid
222 * the luid of the {@link Story}
223 */
754a5bc2 224 void clearLocalReaderCache(String luid) {
68e2c6d2 225 try {
ff05b828 226 cacheLib.clearFromCache(luid);
68e2c6d2 227 } catch (IOException e) {
62c63b07 228 Instance.getTraceHandler().error(e);
68e2c6d2 229 }
10d558d2
NR
230 }
231
c3b229a1
NR
232 /**
233 * Forward the delete operation to the main library.
234 * <p>
235 * The {@link Story} will be deleted from the main library as well as the
236 * cache if present.
237 *
238 * @param luid
239 * the {@link Story} to delete
240 */
10d558d2 241 void delete(String luid) {
68e2c6d2 242 try {
ff05b828 243 cacheLib.delete(luid);
68e2c6d2 244 } catch (IOException e) {
62c63b07 245 Instance.getTraceHandler().error(e);
68e2c6d2 246 }
10d558d2 247 }
edd46289 248
c3b229a1
NR
249 /**
250 * "Open" the given {@link Story}. It usually involves starting an external
251 * program adapted to the given file type.
350bc060
NR
252 * <p>
253 * Asynchronous method.
c3b229a1
NR
254 *
255 * @param luid
256 * the luid of the {@link Story} to open
350bc060
NR
257 * @param sync
258 * execute the process synchronously (wait until it is terminated
259 * before returning)
c3b229a1
NR
260 * @param pg
261 * the optional progress (we may need to prepare the
262 * {@link Story} for reading
263 *
264 * @throws IOException
265 * in case of I/O errors
266 */
350bc060 267 void read(String luid, boolean sync, Progress pg) throws IOException {
ff05b828 268 File file = cacheLib.getFile(luid, pg);
edd46289 269
bc2ea776 270 // TODO: show a special page for the chapter?
c3b229a1
NR
271 // We could also implement an internal viewer, both for image and
272 // non-image documents
350bc060 273 openExternal(getLibrary().getInfo(luid), file, sync);
edd46289 274 }
70c9b112 275
c3b229a1
NR
276 /**
277 * Change the source of the given {@link Story} (the source is the main
278 * information used to group the stories together).
279 * <p>
280 * In other words, <b>move</b> the {@link Story} into other source.
281 * <p>
282 * The source can be a new one, it needs not exist before hand.
283 *
284 * @param luid
285 * the luid of the {@link Story} to move
286 * @param newSource
287 * the new source
288 */
289 void changeSource(String luid, String newSource) {
68e2c6d2 290 try {
ff05b828 291 cacheLib.changeSource(luid, newSource, null);
68e2c6d2 292 } catch (IOException e) {
62c63b07 293 Instance.getTraceHandler().error(e);
68e2c6d2 294 }
70c9b112 295 }
c8d48938
NR
296
297 /**
298 * Change the title of the given {@link Story}.
299 *
300 * @param luid
301 * the luid of the {@link Story} to change
302 * @param newTitle
303 * the new title
304 */
305 void changeTitle(String luid, String newTitle) {
306 try {
307 cacheLib.changeTitle(luid, newTitle, null);
308 } catch (IOException e) {
309 Instance.getTraceHandler().error(e);
310 }
311 }
312
313 /**
314 * Change the author of the given {@link Story}.
315 * <p>
316 * The author can be a new one, it needs not exist before hand.
317 *
318 * @param luid
319 * the luid of the {@link Story} to change
320 * @param newAuthor
321 * the new author
322 */
323 void changeAuthor(String luid, String newAuthor) {
324 try {
325 cacheLib.changeAuthor(luid, newAuthor, null);
326 } catch (IOException e) {
327 Instance.getTraceHandler().error(e);
328 }
329 }
a6395bef 330}