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
.Story
;
19 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
20 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
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 LocalLibrary localLibrary
;
30 public GuiReader() throws IOException
{
31 if (!nativeLookLoaded
) {
32 UIUtils
.setLookAndFeel();
33 nativeLookLoaded
= true;
36 File dir
= Instance
.getReaderDir();
39 throw new IOException(
40 "Cannote create cache directory for local reader: " + dir
);
43 OutputType text
= null;
44 OutputType images
= null;
47 text
= OutputType
.valueOfNullOkUC(Instance
.getUiConfig().getString(
48 UiConfig
.NON_IMAGES_DOCUMENT_TYPE
));
50 text
= OutputType
.HTML
;
53 images
= OutputType
.valueOfNullOkUC(Instance
.getUiConfig()
54 .getString(UiConfig
.IMAGES_DOCUMENT_TYPE
));
56 images
= OutputType
.CBZ
;
58 } catch (Exception e
) {
59 UiConfig key
= (text
== null) ? UiConfig
.NON_IMAGES_DOCUMENT_TYPE
60 : UiConfig
.IMAGES_DOCUMENT_TYPE
;
61 String value
= Instance
.getUiConfig().getString(key
);
63 throw new IOException(
65 "The configuration option %s is not valid: %s",
69 localLibrary
= new LocalLibrary(dir
, text
, images
);
72 public void read() throws IOException
{
73 if (getStory() == null) {
74 throw new IOException("No story to read");
77 open(getStory().getMeta().getLuid(), null);
80 public void read(int chapter
) throws IOException
{
81 // TODO: show a special page?
86 * Import the story into the local reader library, and keep the same LUID.
91 * the optional progress reporter
94 * in case of I/O error
96 public void imprt(String luid
, Progress pg
) throws IOException
{
97 Progress pgGetStory
= new Progress();
98 Progress pgSave
= new Progress();
101 pg
.addProgress(pgGetStory
, 1);
102 pg
.addProgress(pgSave
, 1);
106 Story story
= getLibrary().getStory(luid
, pgGetStory
);
108 story
= localLibrary
.save(story
, luid
, pgSave
);
110 throw new IOException("Cannot find story in Library: " + luid
);
112 } catch (IOException e
) {
113 throw new IOException(
114 "Cannot import story from library to LocalReader library: "
120 * Check if the {@link Story} denoted by this Library UID is present in the
121 * {@link GuiReader} cache.
126 * @return TRUE if it is
128 public boolean isCached(String luid
) {
129 return localLibrary
.getInfo(luid
) != null;
132 public void browse(String type
) {
133 // TODO: improve presentation of update message
134 final VersionCheck updates
= VersionCheck
.check();
135 StringBuilder builder
= new StringBuilder();
137 final JEditorPane updateMessage
= new JEditorPane("text/html", "");
138 if (updates
.isNewVersionAvailable()) {
139 builder
.append("A new version of the program is available at <span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>");
140 builder
.append("<br>");
141 builder
.append("<br>");
142 for (Version v
: updates
.getNewer()) {
143 builder
.append("\t<b>Version " + v
+ "</b>");
144 builder
.append("<br>");
145 builder
.append("<ul>");
146 for (String item
: updates
.getChanges().get(v
)) {
147 builder
.append("<li>" + item
+ "</li>");
149 builder
.append("</ul>");
153 updateMessage
.setText("<html><body>" //
157 // handle link events
158 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
159 public void hyperlinkUpdate(HyperlinkEvent e
) {
160 if (e
.getEventType().equals(
161 HyperlinkEvent
.EventType
.ACTIVATED
))
163 Desktop
.getDesktop().browse(e
.getURL().toURI());
164 } catch (IOException ee
) {
166 } catch (URISyntaxException ee
) {
171 updateMessage
.setEditable(false);
172 updateMessage
.setBackground(new JLabel().getBackground());
175 final String typeFinal
= type
;
176 EventQueue
.invokeLater(new Runnable() {
178 if (updates
.isNewVersionAvailable()) {
179 int rep
= JOptionPane
.showConfirmDialog(null,
180 updateMessage
, "Updates available",
181 JOptionPane
.OK_CANCEL_OPTION
);
182 if (rep
== JOptionPane
.OK_OPTION
) {
189 new GuiReaderFrame(GuiReader
.this, typeFinal
).setVisible(true);
194 // delete from local reader library
195 void clearLocalReaderCache(String luid
) {
197 localLibrary
.delete(luid
);
198 } catch (IOException e
) {
203 // delete from main library
204 void delete(String luid
) {
206 localLibrary
.delete(luid
);
207 getLibrary().delete(luid
);
208 } catch (IOException e
) {
213 // open the given book
214 void open(String luid
, Progress pg
) throws IOException
{
215 File file
= localLibrary
.getFile(luid
);
218 file
= localLibrary
.getFile(luid
);
221 open(getLibrary().getInfo(luid
), file
);
224 void changeType(String luid
, String newType
) {
226 localLibrary
.changeSource(luid
, newType
, null);
227 getLibrary().changeSource(luid
, newType
, null);
228 } catch (IOException e
) {