doc
[nikiroo-utils.git] / 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.data.MetaData;
18 import be.nikiroo.fanfix.data.Story;
19 import be.nikiroo.fanfix.library.BasicLibrary;
20 import be.nikiroo.fanfix.library.CacheLibrary;
21 import be.nikiroo.fanfix.library.LocalLibrary;
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 CacheLibrary cacheLib;
30
31 private File cacheDir;
32
33 public GuiReader() throws IOException {
34 if (!nativeLookLoaded) {
35 UIUtils.setLookAndFeel();
36 nativeLookLoaded = true;
37 }
38
39 cacheDir = Instance.getReaderDir();
40 cacheDir.mkdirs();
41 if (!cacheDir.exists()) {
42 throw new IOException(
43 "Cannote create cache directory for local reader: "
44 + cacheDir);
45 }
46 }
47
48 @Override
49 public synchronized BasicLibrary getLibrary() {
50 if (cacheLib == null) {
51 BasicLibrary lib = super.getLibrary();
52 if (lib instanceof CacheLibrary) {
53 cacheLib = (CacheLibrary) lib;
54 } else {
55 cacheLib = new CacheLibrary(cacheDir, lib);
56 }
57 }
58
59 return cacheLib;
60 }
61
62 @Override
63 public void read() throws IOException {
64 MetaData meta = getMeta();
65
66 if (meta == null) {
67 throw new IOException("No story to read");
68 }
69
70 read(meta.getLuid(), null);
71 }
72
73 /**
74 * Check if the {@link Story} denoted by this Library UID is present in the
75 * {@link GuiReader} cache.
76 *
77 * @param luid
78 * the Library UID
79 *
80 * @return TRUE if it is
81 */
82 public boolean isCached(String luid) {
83 return cacheLib.isCached(luid);
84 }
85
86 @Override
87 public void browse(String type) {
88 // TODO: improve presentation of update message
89 final VersionCheck updates = VersionCheck.check();
90 StringBuilder builder = new StringBuilder();
91
92 final JEditorPane updateMessage = new JEditorPane("text/html", "");
93 if (updates.isNewVersionAvailable()) {
94 builder.append("A new version of the program is available at <span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>");
95 builder.append("<br>");
96 builder.append("<br>");
97 for (Version v : updates.getNewer()) {
98 builder.append("\t<b>Version " + v + "</b>");
99 builder.append("<br>");
100 builder.append("<ul>");
101 for (String item : updates.getChanges().get(v)) {
102 builder.append("<li>" + item + "</li>");
103 }
104 builder.append("</ul>");
105 }
106
107 // html content
108 updateMessage.setText("<html><body>" //
109 + builder//
110 + "</body></html>");
111
112 // handle link events
113 updateMessage.addHyperlinkListener(new HyperlinkListener() {
114 @Override
115 public void hyperlinkUpdate(HyperlinkEvent e) {
116 if (e.getEventType().equals(
117 HyperlinkEvent.EventType.ACTIVATED))
118 try {
119 Desktop.getDesktop().browse(e.getURL().toURI());
120 } catch (IOException ee) {
121 Instance.syserr(ee);
122 } catch (URISyntaxException ee) {
123 Instance.syserr(ee);
124 }
125 }
126 });
127 updateMessage.setEditable(false);
128 updateMessage.setBackground(new JLabel().getBackground());
129 }
130
131 final String typeFinal = type;
132 EventQueue.invokeLater(new Runnable() {
133 @Override
134 public void run() {
135 if (updates.isNewVersionAvailable()) {
136 int rep = JOptionPane.showConfirmDialog(null,
137 updateMessage, "Updates available",
138 JOptionPane.OK_CANCEL_OPTION);
139 if (rep == JOptionPane.OK_OPTION) {
140 updates.ok();
141 } else {
142 updates.ignore();
143 }
144 }
145
146 new GuiReaderFrame(GuiReader.this, typeFinal).setVisible(true);
147 }
148 });
149 }
150
151 // delete from local reader library
152 void clearLocalReaderCache(String luid) {
153 try {
154 cacheLib.clearFromCache(luid);
155 } catch (IOException e) {
156 Instance.syserr(e);
157 }
158 }
159
160 // delete from main library
161 void delete(String luid) {
162 try {
163 cacheLib.delete(luid);
164 } catch (IOException e) {
165 Instance.syserr(e);
166 }
167 }
168
169 // open the given book
170 void read(String luid, Progress pg) throws IOException {
171 File file = cacheLib.getFile(luid, pg);
172
173 // TODO: show a special page for the chapter?
174 openExternal(getLibrary().getInfo(luid), file);
175 }
176
177 void changeType(String luid, String newSource) {
178 try {
179 cacheLib.changeSource(luid, newSource, null);
180 } catch (IOException e) {
181 Instance.syserr(e);
182 }
183 }
184 }