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