Version 1.1.0
[fanfix.git] / src / be / nikiroo / fanfix / reader / LocalReader.java
CommitLineData
a6395bef
NR
1package be.nikiroo.fanfix.reader;
2
3import java.awt.EventQueue;
4import java.io.File;
5import java.io.IOException;
6
7import be.nikiroo.fanfix.Instance;
8import be.nikiroo.fanfix.Library;
b4dc6ab5 9import be.nikiroo.fanfix.bundles.UiConfig;
a6395bef
NR
10import be.nikiroo.fanfix.data.Story;
11import be.nikiroo.fanfix.output.BasicOutput.OutputType;
92fb0719 12import be.nikiroo.utils.ui.Progress;
a6395bef
NR
13
14class LocalReader extends BasicReader {
15 private Library lib;
16
17 public LocalReader() throws IOException {
18 File dir = Instance.getReaderDir();
19 dir.mkdirs();
20 if (!dir.exists()) {
21 throw new IOException(
22 "Cannote create cache directory for local reader: " + dir);
23 }
24
25 // TODO: can throw an exception, manage that (convert to IOEx ?)
b4dc6ab5
NR
26 OutputType text = OutputType.valueOfNullOkUC(Instance.getUiConfig()
27 .getString(UiConfig.LOCAL_READER_NON_IMAGES_DOCUMENT_TYPE));
a6395bef
NR
28 if (text == null) {
29 text = OutputType.HTML;
30 }
31
b4dc6ab5
NR
32 OutputType images = OutputType.valueOfNullOkUC(Instance.getUiConfig()
33 .getString(UiConfig.LOCAL_READER_IMAGES_DOCUMENT_TYPE));
a6395bef
NR
34 if (images == null) {
35 images = OutputType.CBZ;
36 }
37 //
38
39 lib = new Library(dir, text, images);
40 }
41
42 @Override
43 public void read() throws IOException {
44 }
45
46 @Override
47 public void read(int chapter) {
48 }
49
92fb0719
NR
50 /**
51 * Import the story into the local reader library, and keep the same LUID.
52 *
53 * @param luid
54 * the Library UID
55 * @param pg
56 * the optional progress reporter
57 *
58 * @throws IOException
59 * in case of I/O error
60 */
61 public void imprt(String luid, Progress pg) throws IOException {
a6395bef 62 try {
92fb0719 63 Story story = Instance.getLibrary().getStory(luid, pg);
3d247bc3 64 if (story != null) {
b4dc6ab5 65 story = lib.save(story, luid);
3d247bc3
NR
66 } else {
67 throw new IOException("Cannot find story in Library: " + luid);
68 }
a6395bef 69 } catch (IOException e) {
3d247bc3 70 throw new IOException(
a6395bef 71 "Cannot import story from library to LocalReader library: "
3d247bc3 72 + luid, e);
a6395bef 73 }
a6395bef
NR
74 }
75
92fb0719
NR
76 /**
77 * Get the target file related to this {@link Story}.
78 *
79 * @param luid
80 * the LUID of the {@link Story}
81 * @param pg
82 * the optional progress reporter
83 *
84 * @return the target file
85 *
86 * @throws IOException
87 * in case of I/O error
88 */
89 public File getTarget(String luid, Progress pg) throws IOException {
a6395bef
NR
90 File file = lib.getFile(luid);
91 if (file == null) {
92fb0719 92 imprt(luid, pg);
a6395bef 93 file = lib.getFile(luid);
a6395bef
NR
94 }
95
96 return file;
97 }
98
99 @Override
333f0e7b
NR
100 public void start(String type) {
101 final String typeFinal = type;
a6395bef
NR
102 EventQueue.invokeLater(new Runnable() {
103 public void run() {
104 new LocalReaderFrame(LocalReader.this, typeFinal)
105 .setVisible(true);
106 }
107 });
108 }
a6395bef 109}