1 package be
.nikiroo
.fanfix
.reader
;
3 import java
.awt
.Desktop
;
4 import java
.awt
.EventQueue
;
6 import java
.io
.IOException
;
8 import be
.nikiroo
.fanfix
.Instance
;
9 import be
.nikiroo
.fanfix
.Library
;
10 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
11 import be
.nikiroo
.fanfix
.data
.MetaData
;
12 import be
.nikiroo
.fanfix
.data
.Story
;
13 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
14 import be
.nikiroo
.utils
.Progress
;
16 class LocalReader
extends BasicReader
{
19 public LocalReader() throws IOException
{
20 File dir
= Instance
.getReaderDir();
23 throw new IOException(
24 "Cannote create cache directory for local reader: " + dir
);
27 OutputType text
= null;
28 OutputType images
= null;
31 text
= OutputType
.valueOfNullOkUC(Instance
.getUiConfig().getString(
32 UiConfig
.NON_IMAGES_DOCUMENT_TYPE
));
34 text
= OutputType
.HTML
;
37 images
= OutputType
.valueOfNullOkUC(Instance
.getUiConfig()
38 .getString(UiConfig
.IMAGES_DOCUMENT_TYPE
));
40 images
= OutputType
.CBZ
;
42 } catch (Exception e
) {
43 UiConfig key
= (text
== null) ? UiConfig
.NON_IMAGES_DOCUMENT_TYPE
44 : UiConfig
.IMAGES_DOCUMENT_TYPE
;
45 String value
= Instance
.getUiConfig().getString(key
);
47 throw new IOException(
49 "The configuration option %s is not valid: %s",
53 lib
= new Library(dir
, text
, images
);
57 public void read() throws IOException
{
58 if (getStory() == null) {
59 throw new IOException("No story to read");
62 open(getStory().getMeta().getLuid(), null);
66 public void read(int chapter
) throws IOException
{
67 // TODO: show a special page?
72 * Import the story into the local reader library, and keep the same LUID.
77 * the optional progress reporter
80 * in case of I/O error
82 public void imprt(String luid
, Progress pg
) throws IOException
{
83 Progress pgGetStory
= new Progress();
84 Progress pgSave
= new Progress();
87 pg
.addProgress(pgGetStory
, 1);
88 pg
.addProgress(pgSave
, 1);
92 Story story
= Instance
.getLibrary().getStory(luid
, pgGetStory
);
94 story
= lib
.save(story
, luid
, pgSave
);
96 throw new IOException("Cannot find story in Library: " + luid
);
98 } catch (IOException e
) {
99 throw new IOException(
100 "Cannot import story from library to LocalReader library: "
106 * Get the target file related to this {@link Story}.
109 * the LUID of the {@link Story}
111 * the optional progress reporter
113 * @return the target file
115 * @throws IOException
116 * in case of I/O error
118 public File
getTarget(String luid
, Progress pg
) throws IOException
{
119 File file
= lib
.getFile(luid
);
122 file
= lib
.getFile(luid
);
129 * Check if the {@link Story} denoted by this Library UID is present in the
130 * {@link LocalReader} cache.
135 * @return TRUE if it is
137 public boolean isCached(String luid
) {
138 return lib
.getInfo(luid
) != null;
142 public void start(String type
) {
143 final String typeFinal
= type
;
144 EventQueue
.invokeLater(new Runnable() {
146 new LocalReaderFrame(LocalReader
.this, typeFinal
)
152 // refresh = delete from LocalReader cache (TODO: rename?)
153 void refresh(String luid
) {
157 // delete from main library
158 void delete(String luid
) {
160 Instance
.getLibrary().delete(luid
);
163 // open the given book
164 void open(String luid
, Progress pg
) throws IOException
{
165 MetaData meta
= Instance
.getLibrary().getInfo(luid
);
166 File target
= getTarget(luid
, pg
);
168 String program
= null;
169 if (meta
.isImageDocument()) {
170 program
= Instance
.getUiConfig().getString(
171 UiConfig
.IMAGES_DOCUMENT_READER
);
173 program
= Instance
.getUiConfig().getString(
174 UiConfig
.NON_IMAGES_DOCUMENT_READER
);
177 if (program
!= null && program
.trim().isEmpty()) {
181 if (program
== null) {
183 Desktop
.getDesktop().browse(target
.toURI());
184 } catch (UnsupportedOperationException e
) {
185 Runtime
.getRuntime().exec(
186 new String
[] { "xdg-open", target
.getAbsolutePath() });
190 Runtime
.getRuntime().exec(
191 new String
[] { program
, target
.getAbsolutePath() });