Version 1.0.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;
a6395bef
NR
12
13class LocalReader extends BasicReader {
14 private Library lib;
15
16 public LocalReader() throws IOException {
17 File dir = Instance.getReaderDir();
18 dir.mkdirs();
19 if (!dir.exists()) {
20 throw new IOException(
21 "Cannote create cache directory for local reader: " + dir);
22 }
23
24 // TODO: can throw an exception, manage that (convert to IOEx ?)
b4dc6ab5
NR
25 OutputType text = OutputType.valueOfNullOkUC(Instance.getUiConfig()
26 .getString(UiConfig.LOCAL_READER_NON_IMAGES_DOCUMENT_TYPE));
a6395bef
NR
27 if (text == null) {
28 text = OutputType.HTML;
29 }
30
b4dc6ab5
NR
31 OutputType images = OutputType.valueOfNullOkUC(Instance.getUiConfig()
32 .getString(UiConfig.LOCAL_READER_IMAGES_DOCUMENT_TYPE));
a6395bef
NR
33 if (images == null) {
34 images = OutputType.CBZ;
35 }
36 //
37
38 lib = new Library(dir, text, images);
39 }
40
41 @Override
42 public void read() throws IOException {
43 }
44
45 @Override
46 public void read(int chapter) {
47 }
48
b4dc6ab5
NR
49 // keep same luid
50 public void imprt(String luid) throws IOException {
a6395bef
NR
51 try {
52 Story story = Instance.getLibrary().getStory(luid);
3d247bc3 53 if (story != null) {
b4dc6ab5 54 story = lib.save(story, luid);
3d247bc3
NR
55 } else {
56 throw new IOException("Cannot find story in Library: " + luid);
57 }
a6395bef 58 } catch (IOException e) {
3d247bc3 59 throw new IOException(
a6395bef 60 "Cannot import story from library to LocalReader library: "
3d247bc3 61 + luid, e);
a6395bef 62 }
a6395bef
NR
63 }
64
3d247bc3 65 public File getTarget(String luid) throws IOException {
a6395bef
NR
66 File file = lib.getFile(luid);
67 if (file == null) {
b4dc6ab5 68 imprt(luid);
a6395bef 69 file = lib.getFile(luid);
a6395bef
NR
70 }
71
72 return file;
73 }
74
75 @Override
333f0e7b
NR
76 public void start(String type) {
77 final String typeFinal = type;
a6395bef
NR
78 EventQueue.invokeLater(new Runnable() {
79 public void run() {
80 new LocalReaderFrame(LocalReader.this, typeFinal)
81 .setVisible(true);
82 }
83 });
84 }
a6395bef 85}