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
.utils
.Progress
;
22 import be
.nikiroo
.utils
.Version
;
23 import be
.nikiroo
.utils
.ui
.UIUtils
;
25 class GuiReader
extends BasicReader
{
26 static private boolean nativeLookLoaded
;
28 private CacheLibrary cacheLib
;
30 private File cacheDir
;
32 public GuiReader() throws IOException
{
33 if (!nativeLookLoaded
) {
34 UIUtils
.setLookAndFeel();
35 nativeLookLoaded
= true;
38 cacheDir
= Instance
.getReaderDir();
40 if (!cacheDir
.exists()) {
41 throw new IOException(
42 "Cannote create cache directory for local reader: "
48 public synchronized BasicLibrary
getLibrary() {
49 if (cacheLib
== null) {
50 BasicLibrary lib
= super.getLibrary();
51 if (lib
instanceof CacheLibrary
) {
52 cacheLib
= (CacheLibrary
) lib
;
54 cacheLib
= new CacheLibrary(cacheDir
, lib
);
62 public void read() throws IOException
{
63 MetaData meta
= getMeta();
66 throw new IOException("No story to read");
69 read(meta
.getLuid(), null);
73 * Check if the {@link Story} denoted by this Library UID is present in the
74 * {@link GuiReader} cache.
79 * @return TRUE if it is
81 public boolean isCached(String luid
) {
82 return cacheLib
.isCached(luid
);
86 public void browse(String type
) {
87 // TODO: improve presentation of update message
88 final VersionCheck updates
= VersionCheck
.check();
89 StringBuilder builder
= new StringBuilder();
91 final JEditorPane updateMessage
= new JEditorPane("text/html", "");
92 if (updates
.isNewVersionAvailable()) {
93 builder
.append("A new version of the program is available at <span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>");
94 builder
.append("<br>");
95 builder
.append("<br>");
96 for (Version v
: updates
.getNewer()) {
97 builder
.append("\t<b>Version " + v
+ "</b>");
98 builder
.append("<br>");
99 builder
.append("<ul>");
100 for (String item
: updates
.getChanges().get(v
)) {
101 builder
.append("<li>" + item
+ "</li>");
103 builder
.append("</ul>");
107 updateMessage
.setText("<html><body>" //
111 // handle link events
112 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
114 public void hyperlinkUpdate(HyperlinkEvent e
) {
115 if (e
.getEventType().equals(
116 HyperlinkEvent
.EventType
.ACTIVATED
))
118 Desktop
.getDesktop().browse(e
.getURL().toURI());
119 } catch (IOException ee
) {
121 } catch (URISyntaxException ee
) {
126 updateMessage
.setEditable(false);
127 updateMessage
.setBackground(new JLabel().getBackground());
130 final String typeFinal
= type
;
131 EventQueue
.invokeLater(new Runnable() {
134 if (updates
.isNewVersionAvailable()) {
135 int rep
= JOptionPane
.showConfirmDialog(null,
136 updateMessage
, "Updates available",
137 JOptionPane
.OK_CANCEL_OPTION
);
138 if (rep
== JOptionPane
.OK_OPTION
) {
145 new GuiReaderFrame(GuiReader
.this, typeFinal
).setVisible(true);
150 // delete from local reader library
151 void clearLocalReaderCache(String luid
) {
153 cacheLib
.clearFromCache(luid
);
154 } catch (IOException e
) {
159 // delete from main library
160 void delete(String luid
) {
162 cacheLib
.delete(luid
);
163 } catch (IOException e
) {
168 // open the given book
169 void read(String luid
, Progress pg
) throws IOException
{
170 File file
= cacheLib
.getFile(luid
, pg
);
172 // TODO: show a special page for the chapter?
173 openExternal(getLibrary().getInfo(luid
), file
);
176 void changeType(String luid
, String newSource
) {
178 cacheLib
.changeSource(luid
, newSource
, null);
179 } catch (IOException e
) {