Code cleanup 3 and update jexer-niki :
[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
a6395bef 73 public void read() throws IOException {
bc2ea776
NR
74 MetaData meta = getMeta();
75
76 if (meta == null) {
edd46289
NR
77 throw new IOException("No story to read");
78 }
79
bc2ea776 80 read(meta.getLuid(), null);
a6395bef
NR
81 }
82
92fb0719
NR
83 /**
84 * Import the story into the local reader library, and keep the same LUID.
85 *
86 * @param luid
87 * the Library UID
88 * @param pg
89 * the optional progress reporter
90 *
91 * @throws IOException
92 * in case of I/O error
93 */
94 public void imprt(String luid, Progress pg) throws IOException {
bee7dffe
NR
95 Progress pgGetStory = new Progress();
96 Progress pgSave = new Progress();
97 if (pg != null) {
98 pg.setMax(2);
99 pg.addProgress(pgGetStory, 1);
100 pg.addProgress(pgSave, 1);
101 }
102
a6395bef 103 try {
e42573a0 104 Story story = getLibrary().getStory(luid, pgGetStory);
3d247bc3 105 if (story != null) {
b0e88ebd 106 story = localLibrary.save(story, luid, pgSave);
3d247bc3
NR
107 } else {
108 throw new IOException("Cannot find story in Library: " + luid);
109 }
a6395bef 110 } catch (IOException e) {
3d247bc3 111 throw new IOException(
a6395bef 112 "Cannot import story from library to LocalReader library: "
3d247bc3 113 + luid, e);
a6395bef 114 }
a6395bef
NR
115 }
116
9843a5e5
NR
117 /**
118 * Check if the {@link Story} denoted by this Library UID is present in the
5dd985cf 119 * {@link GuiReader} cache.
9843a5e5
NR
120 *
121 * @param luid
122 * the Library UID
123 *
124 * @return TRUE if it is
125 */
10d558d2 126 public boolean isCached(String luid) {
b0e88ebd 127 return localLibrary.getInfo(luid) != null;
10d558d2
NR
128 }
129
b0e88ebd 130 public void browse(String type) {
b42117f1
NR
131 // TODO: improve presentation of update message
132 final VersionCheck updates = VersionCheck.check();
133 StringBuilder builder = new StringBuilder();
134
135 final JEditorPane updateMessage = new JEditorPane("text/html", "");
136 if (updates.isNewVersionAvailable()) {
137 builder.append("A new version of the program is available at <span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>");
138 builder.append("<br>");
139 builder.append("<br>");
140 for (Version v : updates.getNewer()) {
141 builder.append("\t<b>Version " + v + "</b>");
142 builder.append("<br>");
143 builder.append("<ul>");
144 for (String item : updates.getChanges().get(v)) {
145 builder.append("<li>" + item + "</li>");
146 }
147 builder.append("</ul>");
148 }
149
150 // html content
151 updateMessage.setText("<html><body>" //
152 + builder//
153 + "</body></html>");
154
155 // handle link events
156 updateMessage.addHyperlinkListener(new HyperlinkListener() {
157 public void hyperlinkUpdate(HyperlinkEvent e) {
158 if (e.getEventType().equals(
159 HyperlinkEvent.EventType.ACTIVATED))
160 try {
161 Desktop.getDesktop().browse(e.getURL().toURI());
162 } catch (IOException ee) {
163 Instance.syserr(ee);
164 } catch (URISyntaxException ee) {
165 Instance.syserr(ee);
166 }
167 }
168 });
169 updateMessage.setEditable(false);
170 updateMessage.setBackground(new JLabel().getBackground());
171 }
172
333f0e7b 173 final String typeFinal = type;
a6395bef
NR
174 EventQueue.invokeLater(new Runnable() {
175 public void run() {
b42117f1
NR
176 if (updates.isNewVersionAvailable()) {
177 int rep = JOptionPane.showConfirmDialog(null,
178 updateMessage, "Updates available",
179 JOptionPane.OK_CANCEL_OPTION);
180 if (rep == JOptionPane.OK_OPTION) {
181 updates.ok();
182 } else {
183 updates.ignore();
184 }
185 }
186
e42573a0 187 new GuiReaderFrame(GuiReader.this, typeFinal).setVisible(true);
a6395bef
NR
188 }
189 });
190 }
10d558d2 191
754a5bc2
NR
192 // delete from local reader library
193 void clearLocalReaderCache(String luid) {
68e2c6d2
NR
194 try {
195 localLibrary.delete(luid);
196 } catch (IOException e) {
197 Instance.syserr(e);
198 }
10d558d2
NR
199 }
200
edd46289 201 // delete from main library
10d558d2 202 void delete(String luid) {
68e2c6d2
NR
203 try {
204 localLibrary.delete(luid);
e42573a0 205 getLibrary().delete(luid);
68e2c6d2
NR
206 } catch (IOException e) {
207 Instance.syserr(e);
208 }
10d558d2 209 }
edd46289
NR
210
211 // open the given book
bc2ea776 212 void read(String luid, Progress pg) throws IOException {
b0e88ebd 213 File file = localLibrary.getFile(luid);
c1873e56
NR
214 if (file == null) {
215 imprt(luid, pg);
b0e88ebd 216 file = localLibrary.getFile(luid);
edd46289
NR
217 }
218
bc2ea776 219 // TODO: show a special page for the chapter?
6322ab64 220 openExternal(getLibrary().getInfo(luid), file);
edd46289 221 }
70c9b112 222
bc2ea776 223 void changeType(String luid, String newSource) {
68e2c6d2 224 try {
bc2ea776
NR
225 localLibrary.changeSource(luid, newSource, null);
226 getLibrary().changeSource(luid, newSource, null);
68e2c6d2
NR
227 } catch (IOException e) {
228 Instance.syserr(e);
229 }
70c9b112 230 }
a6395bef 231}