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
.Library
;
17 import be
.nikiroo
.fanfix
.VersionCheck
;
18 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
19 import be
.nikiroo
.fanfix
.data
.Story
;
20 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
21 import be
.nikiroo
.utils
.Progress
;
22 import be
.nikiroo
.utils
.Version
;
24 class LocalReader
extends BasicReader
{
27 public LocalReader() throws IOException
{
28 File dir
= Instance
.getReaderDir();
31 throw new IOException(
32 "Cannote create cache directory for local reader: " + dir
);
35 OutputType text
= null;
36 OutputType images
= null;
39 text
= OutputType
.valueOfNullOkUC(Instance
.getUiConfig().getString(
40 UiConfig
.NON_IMAGES_DOCUMENT_TYPE
));
42 text
= OutputType
.HTML
;
45 images
= OutputType
.valueOfNullOkUC(Instance
.getUiConfig()
46 .getString(UiConfig
.IMAGES_DOCUMENT_TYPE
));
48 images
= OutputType
.CBZ
;
50 } catch (Exception e
) {
51 UiConfig key
= (text
== null) ? UiConfig
.NON_IMAGES_DOCUMENT_TYPE
52 : UiConfig
.IMAGES_DOCUMENT_TYPE
;
53 String value
= Instance
.getUiConfig().getString(key
);
55 throw new IOException(
57 "The configuration option %s is not valid: %s",
61 lib
= new Library(dir
, text
, images
);
65 public void read() throws IOException
{
66 if (getStory() == null) {
67 throw new IOException("No story to read");
70 open(getStory().getMeta().getLuid(), null);
74 public void read(int chapter
) throws IOException
{
75 // TODO: show a special page?
80 * Import the story into the local reader library, and keep the same LUID.
85 * the optional progress reporter
88 * in case of I/O error
90 public void imprt(String luid
, Progress pg
) throws IOException
{
91 Progress pgGetStory
= new Progress();
92 Progress pgSave
= new Progress();
95 pg
.addProgress(pgGetStory
, 1);
96 pg
.addProgress(pgSave
, 1);
100 Story story
= Instance
.getLibrary().getStory(luid
, pgGetStory
);
102 story
= lib
.save(story
, luid
, pgSave
);
104 throw new IOException("Cannot find story in Library: " + luid
);
106 } catch (IOException e
) {
107 throw new IOException(
108 "Cannot import story from library to LocalReader library: "
114 * Check if the {@link Story} denoted by this Library UID is present in the
115 * {@link LocalReader} cache.
120 * @return TRUE if it is
122 public boolean isCached(String luid
) {
123 return lib
.getInfo(luid
) != null;
127 public void start(String type
) {
128 // TODO: improve presentation of update message
129 final VersionCheck updates
= VersionCheck
.check();
130 StringBuilder builder
= new StringBuilder();
132 final JEditorPane updateMessage
= new JEditorPane("text/html", "");
133 if (updates
.isNewVersionAvailable()) {
134 builder
.append("A new version of the program is available at <span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>");
135 builder
.append("<br>");
136 builder
.append("<br>");
137 for (Version v
: updates
.getNewer()) {
138 builder
.append("\t<b>Version " + v
+ "</b>");
139 builder
.append("<br>");
140 builder
.append("<ul>");
141 for (String item
: updates
.getChanges().get(v
)) {
142 builder
.append("<li>" + item
+ "</li>");
144 builder
.append("</ul>");
148 updateMessage
.setText("<html><body>" //
152 // handle link events
153 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
154 public void hyperlinkUpdate(HyperlinkEvent e
) {
155 if (e
.getEventType().equals(
156 HyperlinkEvent
.EventType
.ACTIVATED
))
158 Desktop
.getDesktop().browse(e
.getURL().toURI());
159 } catch (IOException ee
) {
161 } catch (URISyntaxException ee
) {
166 updateMessage
.setEditable(false);
167 updateMessage
.setBackground(new JLabel().getBackground());
170 final String typeFinal
= type
;
171 EventQueue
.invokeLater(new Runnable() {
173 if (updates
.isNewVersionAvailable()) {
174 int rep
= JOptionPane
.showConfirmDialog(null,
175 updateMessage
, "Updates available",
176 JOptionPane
.OK_CANCEL_OPTION
);
177 if (rep
== JOptionPane
.OK_OPTION
) {
184 new LocalReaderFrame(LocalReader
.this, typeFinal
)
190 // delete from local reader library
191 void clearLocalReaderCache(String luid
) {
195 // delete from main library
196 void delete(String luid
) {
198 Instance
.getLibrary().delete(luid
);
201 // open the given book
202 void open(String luid
, Progress pg
) throws IOException
{
203 File file
= lib
.getFile(luid
);
206 file
= lib
.getFile(luid
);
209 open(Instance
.getLibrary().getInfo(luid
), file
);
212 void changeType(String luid
, String newType
) {
213 lib
.changeType(luid
, newType
);
214 Instance
.getLibrary().changeType(luid
, newType
);