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