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
.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
;
26 class GuiReader
extends BasicReader
{
27 static private boolean nativeLookLoaded
;
29 private LocalLibrary localLibrary
;
31 public GuiReader() throws IOException
{
32 if (!nativeLookLoaded
) {
33 UIUtils
.setLookAndFeel();
34 nativeLookLoaded
= true;
37 File dir
= Instance
.getReaderDir();
40 throw new IOException(
41 "Cannote create cache directory for local reader: " + dir
);
44 OutputType text
= null;
45 OutputType images
= null;
48 text
= OutputType
.valueOfNullOkUC(Instance
.getUiConfig().getString(
49 UiConfig
.NON_IMAGES_DOCUMENT_TYPE
));
51 text
= OutputType
.HTML
;
54 images
= OutputType
.valueOfNullOkUC(Instance
.getUiConfig()
55 .getString(UiConfig
.IMAGES_DOCUMENT_TYPE
));
57 images
= OutputType
.CBZ
;
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
);
64 throw new IOException(
66 "The configuration option %s is not valid: %s",
70 localLibrary
= new LocalLibrary(dir
, text
, images
);
74 public void read() throws IOException
{
75 MetaData meta
= getMeta();
78 throw new IOException("No story to read");
81 read(meta
.getLuid(), null);
85 * Import the story into the local reader library, and keep the same LUID.
90 * the optional progress reporter
93 * in case of I/O error
95 public void imprt(String luid
, Progress pg
) throws IOException
{
97 localLibrary
.imprt(getLibrary(), luid
, pg
);
98 } catch (IOException e
) {
99 throw new IOException(
100 "Cannot import story from library to LocalReader library: "
106 * Check if the {@link Story} denoted by this Library UID is present in the
107 * {@link GuiReader} cache.
112 * @return TRUE if it is
114 public boolean isCached(String luid
) {
115 return localLibrary
.getInfo(luid
) != null;
119 public void browse(String type
) {
120 // TODO: improve presentation of update message
121 final VersionCheck updates
= VersionCheck
.check();
122 StringBuilder builder
= new StringBuilder();
124 final JEditorPane updateMessage
= new JEditorPane("text/html", "");
125 if (updates
.isNewVersionAvailable()) {
126 builder
.append("A new version of the program is available at <span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>");
127 builder
.append("<br>");
128 builder
.append("<br>");
129 for (Version v
: updates
.getNewer()) {
130 builder
.append("\t<b>Version " + v
+ "</b>");
131 builder
.append("<br>");
132 builder
.append("<ul>");
133 for (String item
: updates
.getChanges().get(v
)) {
134 builder
.append("<li>" + item
+ "</li>");
136 builder
.append("</ul>");
140 updateMessage
.setText("<html><body>" //
144 // handle link events
145 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
147 public void hyperlinkUpdate(HyperlinkEvent e
) {
148 if (e
.getEventType().equals(
149 HyperlinkEvent
.EventType
.ACTIVATED
))
151 Desktop
.getDesktop().browse(e
.getURL().toURI());
152 } catch (IOException ee
) {
154 } catch (URISyntaxException ee
) {
159 updateMessage
.setEditable(false);
160 updateMessage
.setBackground(new JLabel().getBackground());
163 final String typeFinal
= type
;
164 EventQueue
.invokeLater(new Runnable() {
167 if (updates
.isNewVersionAvailable()) {
168 int rep
= JOptionPane
.showConfirmDialog(null,
169 updateMessage
, "Updates available",
170 JOptionPane
.OK_CANCEL_OPTION
);
171 if (rep
== JOptionPane
.OK_OPTION
) {
178 new GuiReaderFrame(GuiReader
.this, typeFinal
).setVisible(true);
183 // delete from local reader library
184 void clearLocalReaderCache(String luid
) {
186 localLibrary
.delete(luid
);
187 } catch (IOException e
) {
192 // delete from main library
193 void delete(String luid
) {
195 if (localLibrary
.getInfo(luid
) != null) {
196 localLibrary
.delete(luid
);
198 getLibrary().delete(luid
);
199 } catch (IOException e
) {
204 // open the given book
205 void read(String luid
, Progress pg
) throws IOException
{
206 File file
= localLibrary
.getFile(luid
);
209 file
= localLibrary
.getFile(luid
);
212 // TODO: show a special page for the chapter?
213 openExternal(getLibrary().getInfo(luid
), file
);
216 void changeType(String luid
, String newSource
) {
218 if (localLibrary
.getInfo(luid
) != null) {
219 localLibrary
.changeSource(luid
, newSource
, null);
221 getLibrary().changeSource(luid
, newSource
, null);
222 } catch (IOException e
) {