Commit | Line | Data |
---|---|---|
a6395bef NR |
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 | import be.nikiroo.fanfix.supported.BasicSupport.SupportType; | |
14 | ||
15 | class LocalReader extends BasicReader { | |
16 | private Library lib; | |
17 | ||
18 | public LocalReader() throws IOException { | |
19 | File dir = Instance.getReaderDir(); | |
20 | dir.mkdirs(); | |
21 | if (!dir.exists()) { | |
22 | throw new IOException( | |
23 | "Cannote create cache directory for local reader: " + dir); | |
24 | } | |
25 | ||
26 | // TODO: can throw an exception, manage that (convert to IOEx ?) | |
27 | OutputType text = OutputType.valueOfNullOkUC(Instance.getConfig() | |
28 | .getString(Config.LOCAL_READER_NON_IMAGES_DOCUMENT_TYPE)); | |
29 | if (text == null) { | |
30 | text = OutputType.HTML; | |
31 | } | |
32 | ||
33 | OutputType images = OutputType.valueOfNullOkUC(Instance.getConfig() | |
34 | .getString(Config.LOCAL_READER_IMAGES_DOCUMENT_TYPE)); | |
35 | if (images == null) { | |
36 | images = OutputType.CBZ; | |
37 | } | |
38 | // | |
39 | ||
40 | lib = new Library(dir, text, images); | |
41 | } | |
42 | ||
43 | @Override | |
44 | public void read() throws IOException { | |
45 | } | |
46 | ||
47 | @Override | |
48 | public void read(int chapter) { | |
49 | } | |
50 | ||
51 | // return new luid | |
52 | public String imprt(String luid) { | |
53 | try { | |
54 | Story story = Instance.getLibrary().getStory(luid); | |
55 | story = lib.save(story); | |
56 | return story.getMeta().getLuid(); | |
57 | } catch (IOException e) { | |
58 | Instance.syserr(new IOException( | |
59 | "Cannot import story from library to LocalReader library: " | |
60 | + luid, e)); | |
61 | } | |
62 | ||
63 | return null; | |
64 | } | |
65 | ||
66 | public File getTarget(String luid) { | |
67 | MetaData meta = lib.getInfo(luid); | |
68 | File file = lib.getFile(luid); | |
69 | if (file == null) { | |
70 | luid = imprt(luid); | |
71 | file = lib.getFile(luid); | |
72 | meta = lib.getInfo(luid); | |
73 | } | |
74 | ||
75 | return file; | |
76 | } | |
77 | ||
78 | @Override | |
79 | public void start(SupportType type) { | |
80 | final SupportType typeFinal = type; | |
81 | EventQueue.invokeLater(new Runnable() { | |
82 | public void run() { | |
83 | new LocalReaderFrame(LocalReader.this, typeFinal) | |
84 | .setVisible(true); | |
85 | } | |
86 | }); | |
87 | } | |
88 | ||
89 | public static void main(String[] args) throws IOException { | |
90 | new LocalReader().start(null); | |
91 | } | |
92 | } |