Fix default remote lib not using cache
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / GuiReader.java
1 package be.nikiroo.fanfix.reader;
2
3 import java.awt.Desktop;
4 import java.awt.EventQueue;
5 import java.io.File;
6 import java.io.IOException;
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;
14
15 import be.nikiroo.fanfix.Instance;
16 import be.nikiroo.fanfix.VersionCheck;
17 import be.nikiroo.fanfix.data.MetaData;
18 import be.nikiroo.fanfix.data.Story;
19 import be.nikiroo.fanfix.library.BasicLibrary;
20 import be.nikiroo.fanfix.library.CacheLibrary;
21 import be.nikiroo.utils.Progress;
22 import be.nikiroo.utils.Version;
23 import be.nikiroo.utils.ui.UIUtils;
24
25 class GuiReader extends BasicReader {
26 static private boolean nativeLookLoaded;
27
28 private CacheLibrary cacheLib;
29
30 private File cacheDir;
31
32 public GuiReader() throws IOException {
33 if (!nativeLookLoaded) {
34 UIUtils.setLookAndFeel();
35 nativeLookLoaded = true;
36 }
37
38 cacheDir = Instance.getReaderDir();
39 cacheDir.mkdirs();
40 if (!cacheDir.exists()) {
41 throw new IOException(
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 }
56 }
57
58 return cacheLib;
59 }
60
61 @Override
62 public void read() throws IOException {
63 MetaData meta = getMeta();
64
65 if (meta == null) {
66 throw new IOException("No story to read");
67 }
68
69 read(meta.getLuid(), null);
70 }
71
72 /**
73 * Check if the {@link Story} denoted by this Library UID is present in the
74 * {@link GuiReader} cache.
75 *
76 * @param luid
77 * the Library UID
78 *
79 * @return TRUE if it is
80 */
81 public boolean isCached(String luid) {
82 return cacheLib.isCached(luid);
83 }
84
85 @Override
86 public void browse(String type) {
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() {
113 @Override
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.getTraceHandler().error(ee);
121 } catch (URISyntaxException ee) {
122 Instance.getTraceHandler().error(ee);
123 }
124 }
125 });
126 updateMessage.setEditable(false);
127 updateMessage.setBackground(new JLabel().getBackground());
128 }
129
130 final String typeFinal = type;
131 EventQueue.invokeLater(new Runnable() {
132 @Override
133 public void run() {
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
145 new GuiReaderFrame(GuiReader.this, typeFinal).setVisible(true);
146 }
147 });
148 }
149
150 // delete from local reader library
151 void clearLocalReaderCache(String luid) {
152 try {
153 cacheLib.clearFromCache(luid);
154 } catch (IOException e) {
155 Instance.getTraceHandler().error(e);
156 }
157 }
158
159 // delete from main library
160 void delete(String luid) {
161 try {
162 cacheLib.delete(luid);
163 } catch (IOException e) {
164 Instance.getTraceHandler().error(e);
165 }
166 }
167
168 // open the given book
169 void read(String luid, Progress pg) throws IOException {
170 File file = cacheLib.getFile(luid, pg);
171
172 // TODO: show a special page for the chapter?
173 openExternal(getLibrary().getInfo(luid), file);
174 }
175
176 void changeType(String luid, String newSource) {
177 try {
178 cacheLib.changeSource(luid, newSource, null);
179 } catch (IOException e) {
180 Instance.getTraceHandler().error(e);
181 }
182 }
183 }