Update nikiroo-utils (Progress) + GuiReader perf
[fanfix.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.bundles.UiConfig;
18 import be.nikiroo.fanfix.data.MetaData;
19 import be.nikiroo.fanfix.data.Story;
20 import be.nikiroo.fanfix.library.LocalLibrary;
21 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
22 import be.nikiroo.utils.Progress;
23 import be.nikiroo.utils.Version;
24 import be.nikiroo.utils.ui.UIUtils;
25
26 class GuiReader extends BasicReader {
27 static private boolean nativeLookLoaded;
28
29 private LocalLibrary localLibrary;
30
31 public GuiReader() throws IOException {
32 if (!nativeLookLoaded) {
33 UIUtils.setLookAndFeel();
34 nativeLookLoaded = true;
35 }
36
37 File dir = Instance.getReaderDir();
38 dir.mkdirs();
39 if (!dir.exists()) {
40 throw new IOException(
41 "Cannote create cache directory for local reader: " + dir);
42 }
43
44 OutputType text = null;
45 OutputType images = null;
46
47 try {
48 text = OutputType.valueOfNullOkUC(Instance.getUiConfig().getString(
49 UiConfig.NON_IMAGES_DOCUMENT_TYPE));
50 if (text == null) {
51 text = OutputType.HTML;
52 }
53
54 images = OutputType.valueOfNullOkUC(Instance.getUiConfig()
55 .getString(UiConfig.IMAGES_DOCUMENT_TYPE));
56 if (images == null) {
57 images = OutputType.CBZ;
58 }
59 } catch (Exception e) {
60 UiConfig key = (text == null) ? UiConfig.NON_IMAGES_DOCUMENT_TYPE
61 : UiConfig.IMAGES_DOCUMENT_TYPE;
62 String value = Instance.getUiConfig().getString(key);
63
64 throw new IOException(
65 String.format(
66 "The configuration option %s is not valid: %s",
67 key, value), e);
68 }
69
70 localLibrary = new LocalLibrary(dir, text, images);
71 }
72
73 @Override
74 public void read() throws IOException {
75 MetaData meta = getMeta();
76
77 if (meta == null) {
78 throw new IOException("No story to read");
79 }
80
81 read(meta.getLuid(), null);
82 }
83
84 /**
85 * Import the story into the local reader library, and keep the same LUID.
86 *
87 * @param luid
88 * the Library UID
89 * @param pg
90 * the optional progress reporter
91 *
92 * @throws IOException
93 * in case of I/O error
94 */
95 public void imprt(String luid, Progress pg) throws IOException {
96 try {
97 localLibrary.imprt(getLibrary(), luid, pg);
98 } catch (IOException e) {
99 throw new IOException(
100 "Cannot import story from library to LocalReader library: "
101 + luid, e);
102 }
103 }
104
105 /**
106 * Check if the {@link Story} denoted by this Library UID is present in the
107 * {@link GuiReader} cache.
108 *
109 * @param luid
110 * the Library UID
111 *
112 * @return TRUE if it is
113 */
114 public boolean isCached(String luid) {
115 return localLibrary.getInfo(luid) != null;
116 }
117
118 @Override
119 public void browse(String type) {
120 // TODO: improve presentation of update message
121 final VersionCheck updates = VersionCheck.check();
122 StringBuilder builder = new StringBuilder();
123
124 final JEditorPane updateMessage = new JEditorPane("text/html", "");
125 if (updates.isNewVersionAvailable()) {
126 builder.append("A new version of the program is available at <span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>");
127 builder.append("<br>");
128 builder.append("<br>");
129 for (Version v : updates.getNewer()) {
130 builder.append("\t<b>Version " + v + "</b>");
131 builder.append("<br>");
132 builder.append("<ul>");
133 for (String item : updates.getChanges().get(v)) {
134 builder.append("<li>" + item + "</li>");
135 }
136 builder.append("</ul>");
137 }
138
139 // html content
140 updateMessage.setText("<html><body>" //
141 + builder//
142 + "</body></html>");
143
144 // handle link events
145 updateMessage.addHyperlinkListener(new HyperlinkListener() {
146 @Override
147 public void hyperlinkUpdate(HyperlinkEvent e) {
148 if (e.getEventType().equals(
149 HyperlinkEvent.EventType.ACTIVATED))
150 try {
151 Desktop.getDesktop().browse(e.getURL().toURI());
152 } catch (IOException ee) {
153 Instance.syserr(ee);
154 } catch (URISyntaxException ee) {
155 Instance.syserr(ee);
156 }
157 }
158 });
159 updateMessage.setEditable(false);
160 updateMessage.setBackground(new JLabel().getBackground());
161 }
162
163 final String typeFinal = type;
164 EventQueue.invokeLater(new Runnable() {
165 @Override
166 public void run() {
167 if (updates.isNewVersionAvailable()) {
168 int rep = JOptionPane.showConfirmDialog(null,
169 updateMessage, "Updates available",
170 JOptionPane.OK_CANCEL_OPTION);
171 if (rep == JOptionPane.OK_OPTION) {
172 updates.ok();
173 } else {
174 updates.ignore();
175 }
176 }
177
178 new GuiReaderFrame(GuiReader.this, typeFinal).setVisible(true);
179 }
180 });
181 }
182
183 // delete from local reader library
184 void clearLocalReaderCache(String luid) {
185 try {
186 localLibrary.delete(luid);
187 } catch (IOException e) {
188 Instance.syserr(e);
189 }
190 }
191
192 // delete from main library
193 void delete(String luid) {
194 try {
195 if (localLibrary.getInfo(luid) != null) {
196 localLibrary.delete(luid);
197 }
198 getLibrary().delete(luid);
199 } catch (IOException e) {
200 Instance.syserr(e);
201 }
202 }
203
204 // open the given book
205 void read(String luid, Progress pg) throws IOException {
206 File file = localLibrary.getFile(luid);
207 if (file == null) {
208 imprt(luid, pg);
209 file = localLibrary.getFile(luid);
210 }
211
212 // TODO: show a special page for the chapter?
213 openExternal(getLibrary().getInfo(luid), file);
214 }
215
216 void changeType(String luid, String newSource) {
217 try {
218 if (localLibrary.getInfo(luid) != null) {
219 localLibrary.changeSource(luid, newSource, null);
220 }
221 getLibrary().changeSource(luid, newSource, null);
222 } catch (IOException e) {
223 Instance.syserr(e);
224 }
225 }
226 }