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; | |
b4dc6ab5 | 9 | import be.nikiroo.fanfix.bundles.UiConfig; |
a6395bef NR |
10 | import be.nikiroo.fanfix.data.Story; |
11 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; | |
3b2b638f | 12 | import be.nikiroo.utils.Progress; |
a6395bef NR |
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 ?) | |
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 { | |
bee7dffe NR |
62 | Progress pgGetStory = new Progress(); |
63 | Progress pgSave = new Progress(); | |
64 | if (pg != null) { | |
65 | pg.setMax(2); | |
66 | pg.addProgress(pgGetStory, 1); | |
67 | pg.addProgress(pgSave, 1); | |
68 | } | |
69 | ||
a6395bef | 70 | try { |
bee7dffe | 71 | Story story = Instance.getLibrary().getStory(luid, pgGetStory); |
3d247bc3 | 72 | if (story != null) { |
bee7dffe | 73 | story = lib.save(story, luid, pgSave); |
3d247bc3 NR |
74 | } else { |
75 | throw new IOException("Cannot find story in Library: " + luid); | |
76 | } | |
a6395bef | 77 | } catch (IOException e) { |
3d247bc3 | 78 | throw new IOException( |
a6395bef | 79 | "Cannot import story from library to LocalReader library: " |
3d247bc3 | 80 | + luid, e); |
a6395bef | 81 | } |
a6395bef NR |
82 | } |
83 | ||
92fb0719 NR |
84 | /** |
85 | * Get the target file related to this {@link Story}. | |
86 | * | |
87 | * @param luid | |
88 | * the LUID of the {@link Story} | |
89 | * @param pg | |
90 | * the optional progress reporter | |
91 | * | |
92 | * @return the target file | |
93 | * | |
94 | * @throws IOException | |
95 | * in case of I/O error | |
96 | */ | |
97 | public File getTarget(String luid, Progress pg) throws IOException { | |
a6395bef NR |
98 | File file = lib.getFile(luid); |
99 | if (file == null) { | |
92fb0719 | 100 | imprt(luid, pg); |
a6395bef | 101 | file = lib.getFile(luid); |
a6395bef NR |
102 | } |
103 | ||
104 | return file; | |
105 | } | |
106 | ||
107 | @Override | |
333f0e7b NR |
108 | public void start(String type) { |
109 | final String typeFinal = type; | |
a6395bef NR |
110 | EventQueue.invokeLater(new Runnable() { |
111 | public void run() { | |
112 | new LocalReaderFrame(LocalReader.this, typeFinal) | |
113 | .setVisible(true); | |
114 | } | |
115 | }); | |
116 | } | |
a6395bef | 117 | } |