Update nikiroo-utils (Progress) + GuiReader perf
[fanfix.git] / src / be / nikiroo / fanfix / reader / GuiReader.java
CommitLineData
a6395bef
NR
1package be.nikiroo.fanfix.reader;
2
edd46289 3import java.awt.Desktop;
a6395bef
NR
4import java.awt.EventQueue;
5import java.io.File;
6import java.io.IOException;
b42117f1
NR
7import java.net.URISyntaxException;
8
9import javax.swing.JEditorPane;
10import javax.swing.JLabel;
11import javax.swing.JOptionPane;
12import javax.swing.event.HyperlinkEvent;
13import javax.swing.event.HyperlinkListener;
a6395bef
NR
14
15import be.nikiroo.fanfix.Instance;
b42117f1 16import be.nikiroo.fanfix.VersionCheck;
b4dc6ab5 17import be.nikiroo.fanfix.bundles.UiConfig;
bc2ea776 18import be.nikiroo.fanfix.data.MetaData;
a6395bef 19import be.nikiroo.fanfix.data.Story;
e42573a0 20import be.nikiroo.fanfix.library.LocalLibrary;
a6395bef 21import be.nikiroo.fanfix.output.BasicOutput.OutputType;
3b2b638f 22import be.nikiroo.utils.Progress;
b42117f1 23import be.nikiroo.utils.Version;
b0e88ebd 24import be.nikiroo.utils.ui.UIUtils;
a6395bef 25
5dd985cf 26class GuiReader extends BasicReader {
b0e88ebd
NR
27 static private boolean nativeLookLoaded;
28
68e2c6d2 29 private LocalLibrary localLibrary;
a6395bef 30
5dd985cf 31 public GuiReader() throws IOException {
b0e88ebd
NR
32 if (!nativeLookLoaded) {
33 UIUtils.setLookAndFeel();
34 nativeLookLoaded = true;
35 }
36
a6395bef
NR
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
edd46289
NR
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);
a6395bef 63
edd46289
NR
64 throw new IOException(
65 String.format(
66 "The configuration option %s is not valid: %s",
67 key, value), e);
a6395bef 68 }
a6395bef 69
68e2c6d2 70 localLibrary = new LocalLibrary(dir, text, images);
a6395bef
NR
71 }
72
211f7ddb 73 @Override
a6395bef 74 public void read() throws IOException {
bc2ea776
NR
75 MetaData meta = getMeta();
76
77 if (meta == null) {
edd46289
NR
78 throw new IOException("No story to read");
79 }
80
bc2ea776 81 read(meta.getLuid(), null);
a6395bef
NR
82 }
83
92fb0719
NR
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 {
a6395bef 96 try {
b89dfb6e 97 localLibrary.imprt(getLibrary(), luid, pg);
a6395bef 98 } catch (IOException e) {
3d247bc3 99 throw new IOException(
a6395bef 100 "Cannot import story from library to LocalReader library: "
3d247bc3 101 + luid, e);
a6395bef 102 }
a6395bef
NR
103 }
104
9843a5e5
NR
105 /**
106 * Check if the {@link Story} denoted by this Library UID is present in the
5dd985cf 107 * {@link GuiReader} cache.
9843a5e5
NR
108 *
109 * @param luid
110 * the Library UID
111 *
112 * @return TRUE if it is
113 */
10d558d2 114 public boolean isCached(String luid) {
b0e88ebd 115 return localLibrary.getInfo(luid) != null;
10d558d2
NR
116 }
117
211f7ddb 118 @Override
b0e88ebd 119 public void browse(String type) {
b42117f1
NR
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() {
211f7ddb 146 @Override
b42117f1
NR
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
333f0e7b 163 final String typeFinal = type;
a6395bef 164 EventQueue.invokeLater(new Runnable() {
211f7ddb 165 @Override
a6395bef 166 public void run() {
b42117f1
NR
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
e42573a0 178 new GuiReaderFrame(GuiReader.this, typeFinal).setVisible(true);
a6395bef
NR
179 }
180 });
181 }
10d558d2 182
754a5bc2
NR
183 // delete from local reader library
184 void clearLocalReaderCache(String luid) {
68e2c6d2
NR
185 try {
186 localLibrary.delete(luid);
187 } catch (IOException e) {
188 Instance.syserr(e);
189 }
10d558d2
NR
190 }
191
edd46289 192 // delete from main library
10d558d2 193 void delete(String luid) {
68e2c6d2 194 try {
77e28d38
NR
195 if (localLibrary.getInfo(luid) != null) {
196 localLibrary.delete(luid);
197 }
e42573a0 198 getLibrary().delete(luid);
68e2c6d2
NR
199 } catch (IOException e) {
200 Instance.syserr(e);
201 }
10d558d2 202 }
edd46289
NR
203
204 // open the given book
bc2ea776 205 void read(String luid, Progress pg) throws IOException {
b0e88ebd 206 File file = localLibrary.getFile(luid);
c1873e56
NR
207 if (file == null) {
208 imprt(luid, pg);
b0e88ebd 209 file = localLibrary.getFile(luid);
edd46289
NR
210 }
211
bc2ea776 212 // TODO: show a special page for the chapter?
6322ab64 213 openExternal(getLibrary().getInfo(luid), file);
edd46289 214 }
70c9b112 215
bc2ea776 216 void changeType(String luid, String newSource) {
68e2c6d2 217 try {
34eb62fc
NR
218 if (localLibrary.getInfo(luid) != null) {
219 localLibrary.changeSource(luid, newSource, null);
220 }
bc2ea776 221 getLibrary().changeSource(luid, newSource, null);
68e2c6d2
NR
222 } catch (IOException e) {
223 Instance.syserr(e);
224 }
70c9b112 225 }
a6395bef 226}