Commit | Line | Data |
---|---|---|
a6395bef NR |
1 | package be.nikiroo.fanfix.reader; |
2 | ||
edd46289 | 3 | import java.awt.Desktop; |
a6395bef NR |
4 | import java.awt.EventQueue; |
5 | import java.io.File; | |
6 | import java.io.IOException; | |
b42117f1 NR |
7 | import java.net.URISyntaxException; |
8 | ||
9 | import javax.swing.JEditorPane; | |
10 | import javax.swing.JLabel; | |
11 | import javax.swing.JOptionPane; | |
12 | import javax.swing.event.HyperlinkEvent; | |
13 | import javax.swing.event.HyperlinkListener; | |
a6395bef NR |
14 | |
15 | import be.nikiroo.fanfix.Instance; | |
b42117f1 | 16 | import be.nikiroo.fanfix.VersionCheck; |
b4dc6ab5 | 17 | import be.nikiroo.fanfix.bundles.UiConfig; |
bc2ea776 | 18 | import be.nikiroo.fanfix.data.MetaData; |
a6395bef | 19 | import be.nikiroo.fanfix.data.Story; |
e42573a0 | 20 | import be.nikiroo.fanfix.library.LocalLibrary; |
3b2b638f | 21 | import be.nikiroo.utils.Progress; |
b42117f1 | 22 | import be.nikiroo.utils.Version; |
b0e88ebd | 23 | import be.nikiroo.utils.ui.UIUtils; |
a6395bef | 24 | |
5dd985cf | 25 | class GuiReader extends BasicReader { |
b0e88ebd NR |
26 | static private boolean nativeLookLoaded; |
27 | ||
68e2c6d2 | 28 | private LocalLibrary localLibrary; |
a6395bef | 29 | |
5dd985cf | 30 | public GuiReader() throws IOException { |
b0e88ebd NR |
31 | if (!nativeLookLoaded) { |
32 | UIUtils.setLookAndFeel(); | |
33 | nativeLookLoaded = true; | |
34 | } | |
35 | ||
a6395bef NR |
36 | File dir = Instance.getReaderDir(); |
37 | dir.mkdirs(); | |
38 | if (!dir.exists()) { | |
39 | throw new IOException( | |
40 | "Cannote create cache directory for local reader: " + dir); | |
41 | } | |
42 | ||
e604986c NR |
43 | localLibrary = new LocalLibrary(dir, Instance.getUiConfig().getString( |
44 | UiConfig.GUI_NON_IMAGES_DOCUMENT_TYPE), Instance.getUiConfig() | |
45 | .getString(UiConfig.GUI_IMAGES_DOCUMENT_TYPE), true); | |
a6395bef NR |
46 | } |
47 | ||
211f7ddb | 48 | @Override |
a6395bef | 49 | public void read() throws IOException { |
bc2ea776 NR |
50 | MetaData meta = getMeta(); |
51 | ||
52 | if (meta == null) { | |
edd46289 NR |
53 | throw new IOException("No story to read"); |
54 | } | |
55 | ||
bc2ea776 | 56 | read(meta.getLuid(), null); |
a6395bef NR |
57 | } |
58 | ||
92fb0719 NR |
59 | /** |
60 | * Import the story into the local reader library, and keep the same LUID. | |
61 | * | |
62 | * @param luid | |
63 | * the Library UID | |
64 | * @param pg | |
65 | * the optional progress reporter | |
66 | * | |
67 | * @throws IOException | |
68 | * in case of I/O error | |
69 | */ | |
70 | public void imprt(String luid, Progress pg) throws IOException { | |
a6395bef | 71 | try { |
b89dfb6e | 72 | localLibrary.imprt(getLibrary(), luid, pg); |
a6395bef | 73 | } catch (IOException e) { |
3d247bc3 | 74 | throw new IOException( |
a6395bef | 75 | "Cannot import story from library to LocalReader library: " |
3d247bc3 | 76 | + luid, e); |
a6395bef | 77 | } |
a6395bef NR |
78 | } |
79 | ||
9843a5e5 NR |
80 | /** |
81 | * Check if the {@link Story} denoted by this Library UID is present in the | |
5dd985cf | 82 | * {@link GuiReader} cache. |
9843a5e5 NR |
83 | * |
84 | * @param luid | |
85 | * the Library UID | |
86 | * | |
87 | * @return TRUE if it is | |
88 | */ | |
10d558d2 | 89 | public boolean isCached(String luid) { |
b0e88ebd | 90 | return localLibrary.getInfo(luid) != null; |
10d558d2 NR |
91 | } |
92 | ||
211f7ddb | 93 | @Override |
b0e88ebd | 94 | public void browse(String type) { |
b42117f1 NR |
95 | // TODO: improve presentation of update message |
96 | final VersionCheck updates = VersionCheck.check(); | |
97 | StringBuilder builder = new StringBuilder(); | |
98 | ||
99 | final JEditorPane updateMessage = new JEditorPane("text/html", ""); | |
100 | if (updates.isNewVersionAvailable()) { | |
101 | builder.append("A new version of the program is available at <span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>"); | |
102 | builder.append("<br>"); | |
103 | builder.append("<br>"); | |
104 | for (Version v : updates.getNewer()) { | |
105 | builder.append("\t<b>Version " + v + "</b>"); | |
106 | builder.append("<br>"); | |
107 | builder.append("<ul>"); | |
108 | for (String item : updates.getChanges().get(v)) { | |
109 | builder.append("<li>" + item + "</li>"); | |
110 | } | |
111 | builder.append("</ul>"); | |
112 | } | |
113 | ||
114 | // html content | |
115 | updateMessage.setText("<html><body>" // | |
116 | + builder// | |
117 | + "</body></html>"); | |
118 | ||
119 | // handle link events | |
120 | updateMessage.addHyperlinkListener(new HyperlinkListener() { | |
211f7ddb | 121 | @Override |
b42117f1 NR |
122 | public void hyperlinkUpdate(HyperlinkEvent e) { |
123 | if (e.getEventType().equals( | |
124 | HyperlinkEvent.EventType.ACTIVATED)) | |
125 | try { | |
126 | Desktop.getDesktop().browse(e.getURL().toURI()); | |
127 | } catch (IOException ee) { | |
128 | Instance.syserr(ee); | |
129 | } catch (URISyntaxException ee) { | |
130 | Instance.syserr(ee); | |
131 | } | |
132 | } | |
133 | }); | |
134 | updateMessage.setEditable(false); | |
135 | updateMessage.setBackground(new JLabel().getBackground()); | |
136 | } | |
137 | ||
333f0e7b | 138 | final String typeFinal = type; |
a6395bef | 139 | EventQueue.invokeLater(new Runnable() { |
211f7ddb | 140 | @Override |
a6395bef | 141 | public void run() { |
b42117f1 NR |
142 | if (updates.isNewVersionAvailable()) { |
143 | int rep = JOptionPane.showConfirmDialog(null, | |
144 | updateMessage, "Updates available", | |
145 | JOptionPane.OK_CANCEL_OPTION); | |
146 | if (rep == JOptionPane.OK_OPTION) { | |
147 | updates.ok(); | |
148 | } else { | |
149 | updates.ignore(); | |
150 | } | |
151 | } | |
152 | ||
e42573a0 | 153 | new GuiReaderFrame(GuiReader.this, typeFinal).setVisible(true); |
a6395bef NR |
154 | } |
155 | }); | |
156 | } | |
10d558d2 | 157 | |
754a5bc2 NR |
158 | // delete from local reader library |
159 | void clearLocalReaderCache(String luid) { | |
68e2c6d2 NR |
160 | try { |
161 | localLibrary.delete(luid); | |
162 | } catch (IOException e) { | |
163 | Instance.syserr(e); | |
164 | } | |
10d558d2 NR |
165 | } |
166 | ||
edd46289 | 167 | // delete from main library |
10d558d2 | 168 | void delete(String luid) { |
68e2c6d2 | 169 | try { |
77e28d38 NR |
170 | if (localLibrary.getInfo(luid) != null) { |
171 | localLibrary.delete(luid); | |
172 | } | |
e42573a0 | 173 | getLibrary().delete(luid); |
68e2c6d2 NR |
174 | } catch (IOException e) { |
175 | Instance.syserr(e); | |
176 | } | |
10d558d2 | 177 | } |
edd46289 NR |
178 | |
179 | // open the given book | |
bc2ea776 | 180 | void read(String luid, Progress pg) throws IOException { |
b0e88ebd | 181 | File file = localLibrary.getFile(luid); |
c1873e56 NR |
182 | if (file == null) { |
183 | imprt(luid, pg); | |
b0e88ebd | 184 | file = localLibrary.getFile(luid); |
edd46289 NR |
185 | } |
186 | ||
bc2ea776 | 187 | // TODO: show a special page for the chapter? |
6322ab64 | 188 | openExternal(getLibrary().getInfo(luid), file); |
edd46289 | 189 | } |
70c9b112 | 190 | |
bc2ea776 | 191 | void changeType(String luid, String newSource) { |
68e2c6d2 | 192 | try { |
34eb62fc NR |
193 | if (localLibrary.getInfo(luid) != null) { |
194 | localLibrary.changeSource(luid, newSource, null); | |
195 | } | |
bc2ea776 | 196 | getLibrary().changeSource(luid, newSource, null); |
68e2c6d2 NR |
197 | } catch (IOException e) { |
198 | Instance.syserr(e); | |
199 | } | |
70c9b112 | 200 | } |
a6395bef | 201 | } |