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
.LocalLibrary
;
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
;
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
);
73 public void read() throws IOException
{
74 if (getStory() == null) {
75 throw new IOException("No story to read");
78 open(getStory().getMeta().getLuid(), null);
82 public void read(int chapter
) throws IOException
{
83 // TODO: show a special page?
88 * Import the story into the local reader library, and keep the same LUID.
93 * the optional progress reporter
96 * in case of I/O error
98 public void imprt(String luid
, Progress pg
) throws IOException
{
99 Progress pgGetStory
= new Progress();
100 Progress pgSave
= new Progress();
103 pg
.addProgress(pgGetStory
, 1);
104 pg
.addProgress(pgSave
, 1);
108 Story story
= Instance
.getLibrary().getStory(luid
, pgGetStory
);
110 story
= localLibrary
.save(story
, luid
, pgSave
);
112 throw new IOException("Cannot find story in Library: " + luid
);
114 } catch (IOException e
) {
115 throw new IOException(
116 "Cannot import story from library to LocalReader library: "
122 * Check if the {@link Story} denoted by this Library UID is present in the
123 * {@link GuiReader} cache.
128 * @return TRUE if it is
130 public boolean isCached(String luid
) {
131 return localLibrary
.getInfo(luid
) != null;
135 public void browse(String type
) {
136 // TODO: improve presentation of update message
137 final VersionCheck updates
= VersionCheck
.check();
138 StringBuilder builder
= new StringBuilder();
140 final JEditorPane updateMessage
= new JEditorPane("text/html", "");
141 if (updates
.isNewVersionAvailable()) {
142 builder
.append("A new version of the program is available at <span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>");
143 builder
.append("<br>");
144 builder
.append("<br>");
145 for (Version v
: updates
.getNewer()) {
146 builder
.append("\t<b>Version " + v
+ "</b>");
147 builder
.append("<br>");
148 builder
.append("<ul>");
149 for (String item
: updates
.getChanges().get(v
)) {
150 builder
.append("<li>" + item
+ "</li>");
152 builder
.append("</ul>");
156 updateMessage
.setText("<html><body>" //
160 // handle link events
161 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
162 public void hyperlinkUpdate(HyperlinkEvent e
) {
163 if (e
.getEventType().equals(
164 HyperlinkEvent
.EventType
.ACTIVATED
))
166 Desktop
.getDesktop().browse(e
.getURL().toURI());
167 } catch (IOException ee
) {
169 } catch (URISyntaxException ee
) {
174 updateMessage
.setEditable(false);
175 updateMessage
.setBackground(new JLabel().getBackground());
178 final String typeFinal
= type
;
179 EventQueue
.invokeLater(new Runnable() {
181 if (updates
.isNewVersionAvailable()) {
182 int rep
= JOptionPane
.showConfirmDialog(null,
183 updateMessage
, "Updates available",
184 JOptionPane
.OK_CANCEL_OPTION
);
185 if (rep
== JOptionPane
.OK_OPTION
) {
192 new GuiReaderFrame(GuiReader
.this, typeFinal
)
198 // delete from local reader library
199 void clearLocalReaderCache(String luid
) {
201 localLibrary
.delete(luid
);
202 } catch (IOException e
) {
207 // delete from main library
208 void delete(String luid
) {
210 localLibrary
.delete(luid
);
211 Instance
.getLibrary().delete(luid
);
212 } catch (IOException e
) {
217 // open the given book
218 void open(String luid
, Progress pg
) throws IOException
{
219 File file
= localLibrary
.getFile(luid
);
222 file
= localLibrary
.getFile(luid
);
225 open(getLibrary().getInfo(luid
), file
);
228 void changeType(String luid
, String newType
) {
230 localLibrary
.changeSource(luid
, newType
, null);
231 Instance
.getLibrary().changeSource(luid
, newType
, null);
232 } catch (IOException e
) {