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
);
73 public void read() throws IOException
{
74 MetaData meta
= getMeta();
77 throw new IOException("No story to read");
80 read(meta
.getLuid(), null);
84 * Import the story into the local reader library, and keep the same LUID.
89 * the optional progress reporter
92 * in case of I/O error
94 public void imprt(String luid
, Progress pg
) throws IOException
{
95 Progress pgGetStory
= new Progress();
96 Progress pgSave
= new Progress();
99 pg
.addProgress(pgGetStory
, 1);
100 pg
.addProgress(pgSave
, 1);
104 Story story
= getLibrary().getStory(luid
, pgGetStory
);
106 story
= localLibrary
.save(story
, luid
, pgSave
);
108 throw new IOException("Cannot find story in Library: " + luid
);
110 } catch (IOException e
) {
111 throw new IOException(
112 "Cannot import story from library to LocalReader library: "
118 * Check if the {@link Story} denoted by this Library UID is present in the
119 * {@link GuiReader} cache.
124 * @return TRUE if it is
126 public boolean isCached(String luid
) {
127 return localLibrary
.getInfo(luid
) != null;
130 public void browse(String type
) {
131 // TODO: improve presentation of update message
132 final VersionCheck updates
= VersionCheck
.check();
133 StringBuilder builder
= new StringBuilder();
135 final JEditorPane updateMessage
= new JEditorPane("text/html", "");
136 if (updates
.isNewVersionAvailable()) {
137 builder
.append("A new version of the program is available at <span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>");
138 builder
.append("<br>");
139 builder
.append("<br>");
140 for (Version v
: updates
.getNewer()) {
141 builder
.append("\t<b>Version " + v
+ "</b>");
142 builder
.append("<br>");
143 builder
.append("<ul>");
144 for (String item
: updates
.getChanges().get(v
)) {
145 builder
.append("<li>" + item
+ "</li>");
147 builder
.append("</ul>");
151 updateMessage
.setText("<html><body>" //
155 // handle link events
156 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
157 public void hyperlinkUpdate(HyperlinkEvent e
) {
158 if (e
.getEventType().equals(
159 HyperlinkEvent
.EventType
.ACTIVATED
))
161 Desktop
.getDesktop().browse(e
.getURL().toURI());
162 } catch (IOException ee
) {
164 } catch (URISyntaxException ee
) {
169 updateMessage
.setEditable(false);
170 updateMessage
.setBackground(new JLabel().getBackground());
173 final String typeFinal
= type
;
174 EventQueue
.invokeLater(new Runnable() {
176 if (updates
.isNewVersionAvailable()) {
177 int rep
= JOptionPane
.showConfirmDialog(null,
178 updateMessage
, "Updates available",
179 JOptionPane
.OK_CANCEL_OPTION
);
180 if (rep
== JOptionPane
.OK_OPTION
) {
187 new GuiReaderFrame(GuiReader
.this, typeFinal
).setVisible(true);
192 // delete from local reader library
193 void clearLocalReaderCache(String luid
) {
195 localLibrary
.delete(luid
);
196 } catch (IOException e
) {
201 // delete from main library
202 void delete(String luid
) {
204 localLibrary
.delete(luid
);
205 getLibrary().delete(luid
);
206 } catch (IOException e
) {
211 // open the given book
212 void read(String luid
, Progress pg
) throws IOException
{
213 File file
= localLibrary
.getFile(luid
);
216 file
= localLibrary
.getFile(luid
);
219 // TODO: show a special page for the chapter?
220 openExternal(getLibrary().getInfo(luid
), file
);
223 void changeType(String luid
, String newSource
) {
225 localLibrary
.changeSource(luid
, newSource
, null);
226 getLibrary().changeSource(luid
, newSource
, null);
227 } catch (IOException e
) {