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