86b779c343c0b964d9c80d96493ccd47ec02f7d5
[fanfix.git] / src / be / nikiroo / fanfix / reader / LocalReader.java
1 package be.nikiroo.fanfix.reader;
2
3 import java.awt.EventQueue;
4 import java.io.File;
5 import java.io.IOException;
6
7 import be.nikiroo.fanfix.Instance;
8 import be.nikiroo.fanfix.Library;
9 import be.nikiroo.fanfix.bundles.Config;
10 import be.nikiroo.fanfix.data.MetaData;
11 import be.nikiroo.fanfix.data.Story;
12 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
13
14 class 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 ?)
26 OutputType text = OutputType.valueOfNullOkUC(Instance.getConfig()
27 .getString(Config.LOCAL_READER_NON_IMAGES_DOCUMENT_TYPE));
28 if (text == null) {
29 text = OutputType.HTML;
30 }
31
32 OutputType images = OutputType.valueOfNullOkUC(Instance.getConfig()
33 .getString(Config.LOCAL_READER_IMAGES_DOCUMENT_TYPE));
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
50 // return new luid
51 public String imprt(String luid) throws IOException {
52 try {
53 Story story = Instance.getLibrary().getStory(luid);
54 if (story != null) {
55 story = lib.save(story);
56 return story.getMeta().getLuid();
57 } else {
58 throw new IOException("Cannot find story in Library: " + luid);
59 }
60 } catch (IOException e) {
61 throw new IOException(
62 "Cannot import story from library to LocalReader library: "
63 + luid, e);
64 }
65 }
66
67 public File getTarget(String luid) throws IOException {
68 MetaData meta = lib.getInfo(luid);
69 File file = lib.getFile(luid);
70 if (file == null) {
71 luid = imprt(luid);
72 file = lib.getFile(luid);
73 meta = lib.getInfo(luid);
74 }
75
76 return file;
77 }
78
79 @Override
80 public void start(String type) {
81 final String typeFinal = type;
82 EventQueue.invokeLater(new Runnable() {
83 public void run() {
84 new LocalReaderFrame(LocalReader.this, typeFinal)
85 .setVisible(true);
86 }
87 });
88 }
89 }