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