1 package be
.nikiroo
.fanfix
.reader
;
3 import java
.awt
.Desktop
;
4 import java
.awt
.EventQueue
;
6 import java
.io
.IOException
;
7 import java
.net
.URISyntaxException
;
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
;
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
;
26 class GuiReader
extends BasicReader
{
27 static private boolean nativeLookLoaded
;
29 private CacheLibrary cacheLib
;
31 private File cacheDir
;
33 public GuiReader() throws IOException
{
34 if (!nativeLookLoaded
) {
35 UIUtils
.setLookAndFeel();
36 nativeLookLoaded
= true;
39 cacheDir
= Instance
.getReaderDir();
41 if (!cacheDir
.exists()) {
42 throw new IOException(
43 "Cannote create cache directory for local reader: "
49 public synchronized BasicLibrary
getLibrary() {
50 if (cacheLib
== null) {
51 BasicLibrary lib
= super.getLibrary();
52 if (lib
instanceof CacheLibrary
) {
53 cacheLib
= (CacheLibrary
) lib
;
55 cacheLib
= new CacheLibrary(cacheDir
, lib
);
63 public void read() throws IOException
{
64 MetaData meta
= getMeta();
67 throw new IOException("No story to read");
70 read(meta
.getLuid(), null);
74 * Check if the {@link Story} denoted by this Library UID is present in the
75 * {@link GuiReader} cache.
80 * @return TRUE if it is
82 public boolean isCached(String luid
) {
83 return cacheLib
.isCached(luid
);
87 public void browse(String type
) {
88 // TODO: improve presentation of update message
89 final VersionCheck updates
= VersionCheck
.check();
90 StringBuilder builder
= new StringBuilder();
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>");
104 builder
.append("</ul>");
108 updateMessage
.setText("<html><body>" //
112 // handle link events
113 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
115 public void hyperlinkUpdate(HyperlinkEvent e
) {
116 if (e
.getEventType().equals(
117 HyperlinkEvent
.EventType
.ACTIVATED
))
119 Desktop
.getDesktop().browse(e
.getURL().toURI());
120 } catch (IOException ee
) {
122 } catch (URISyntaxException ee
) {
127 updateMessage
.setEditable(false);
128 updateMessage
.setBackground(new JLabel().getBackground());
131 final String typeFinal
= type
;
132 EventQueue
.invokeLater(new Runnable() {
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
) {
146 new GuiReaderFrame(GuiReader
.this, typeFinal
).setVisible(true);
151 // delete from local reader library
152 void clearLocalReaderCache(String luid
) {
154 cacheLib
.clearFromCache(luid
);
155 } catch (IOException e
) {
160 // delete from main library
161 void delete(String luid
) {
163 cacheLib
.delete(luid
);
164 } catch (IOException e
) {
169 // open the given book
170 void read(String luid
, Progress pg
) throws IOException
{
171 File file
= cacheLib
.getFile(luid
, pg
);
173 // TODO: show a special page for the chapter?
174 openExternal(getLibrary().getInfo(luid
), file
);
177 void changeType(String luid
, String newSource
) {
179 cacheLib
.changeSource(luid
, newSource
, null);
180 } catch (IOException e
) {