c17e83eaa097531eb423473d544c60eabff92a64
[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.UiConfig;
10 import be.nikiroo.fanfix.data.Story;
11 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
12
13 class 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 ?)
25 OutputType text = OutputType.valueOfNullOkUC(Instance.getUiConfig()
26 .getString(UiConfig.LOCAL_READER_NON_IMAGES_DOCUMENT_TYPE));
27 if (text == null) {
28 text = OutputType.HTML;
29 }
30
31 OutputType images = OutputType.valueOfNullOkUC(Instance.getUiConfig()
32 .getString(UiConfig.LOCAL_READER_IMAGES_DOCUMENT_TYPE));
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
49 // keep same luid
50 public void imprt(String luid) throws IOException {
51 try {
52 Story story = Instance.getLibrary().getStory(luid);
53 if (story != null) {
54 story = lib.save(story, luid);
55 } else {
56 throw new IOException("Cannot find story in Library: " + luid);
57 }
58 } catch (IOException e) {
59 throw new IOException(
60 "Cannot import story from library to LocalReader library: "
61 + luid, e);
62 }
63 }
64
65 public File getTarget(String luid) throws IOException {
66 File file = lib.getFile(luid);
67 if (file == null) {
68 imprt(luid);
69 file = lib.getFile(luid);
70 }
71
72 return file;
73 }
74
75 @Override
76 public void start(String type) {
77 final String typeFinal = type;
78 EventQueue.invokeLater(new Runnable() {
79 public void run() {
80 new LocalReaderFrame(LocalReader.this, typeFinal)
81 .setVisible(true);
82 }
83 });
84 }
85 }