Commit | Line | Data |
---|---|---|
a6395bef NR |
1 | package be.nikiroo.fanfix.reader; |
2 | ||
edd46289 | 3 | import java.awt.Desktop; |
a6395bef NR |
4 | import java.awt.EventQueue; |
5 | import java.io.File; | |
6 | import java.io.IOException; | |
7 | ||
8 | import be.nikiroo.fanfix.Instance; | |
9 | import be.nikiroo.fanfix.Library; | |
b4dc6ab5 | 10 | import be.nikiroo.fanfix.bundles.UiConfig; |
edd46289 | 11 | import be.nikiroo.fanfix.data.MetaData; |
a6395bef NR |
12 | import be.nikiroo.fanfix.data.Story; |
13 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; | |
3b2b638f | 14 | import be.nikiroo.utils.Progress; |
a6395bef NR |
15 | |
16 | class LocalReader extends BasicReader { | |
17 | private Library lib; | |
18 | ||
19 | public LocalReader() throws IOException { | |
20 | File dir = Instance.getReaderDir(); | |
21 | dir.mkdirs(); | |
22 | if (!dir.exists()) { | |
23 | throw new IOException( | |
24 | "Cannote create cache directory for local reader: " + dir); | |
25 | } | |
26 | ||
edd46289 NR |
27 | OutputType text = null; |
28 | OutputType images = null; | |
29 | ||
30 | try { | |
31 | text = OutputType.valueOfNullOkUC(Instance.getUiConfig().getString( | |
32 | UiConfig.NON_IMAGES_DOCUMENT_TYPE)); | |
33 | if (text == null) { | |
34 | text = OutputType.HTML; | |
35 | } | |
36 | ||
37 | images = OutputType.valueOfNullOkUC(Instance.getUiConfig() | |
38 | .getString(UiConfig.IMAGES_DOCUMENT_TYPE)); | |
39 | if (images == null) { | |
40 | images = OutputType.CBZ; | |
41 | } | |
42 | } catch (Exception e) { | |
43 | UiConfig key = (text == null) ? UiConfig.NON_IMAGES_DOCUMENT_TYPE | |
44 | : UiConfig.IMAGES_DOCUMENT_TYPE; | |
45 | String value = Instance.getUiConfig().getString(key); | |
a6395bef | 46 | |
edd46289 NR |
47 | throw new IOException( |
48 | String.format( | |
49 | "The configuration option %s is not valid: %s", | |
50 | key, value), e); | |
a6395bef | 51 | } |
a6395bef NR |
52 | |
53 | lib = new Library(dir, text, images); | |
54 | } | |
55 | ||
56 | @Override | |
57 | public void read() throws IOException { | |
edd46289 NR |
58 | if (getStory() == null) { |
59 | throw new IOException("No story to read"); | |
60 | } | |
61 | ||
62 | open(getStory().getMeta().getLuid(), null); | |
a6395bef NR |
63 | } |
64 | ||
65 | @Override | |
edd46289 NR |
66 | public void read(int chapter) throws IOException { |
67 | // TODO: show a special page? | |
68 | read(); | |
a6395bef NR |
69 | } |
70 | ||
92fb0719 NR |
71 | /** |
72 | * Import the story into the local reader library, and keep the same LUID. | |
73 | * | |
74 | * @param luid | |
75 | * the Library UID | |
76 | * @param pg | |
77 | * the optional progress reporter | |
78 | * | |
79 | * @throws IOException | |
80 | * in case of I/O error | |
81 | */ | |
82 | public void imprt(String luid, Progress pg) throws IOException { | |
bee7dffe NR |
83 | Progress pgGetStory = new Progress(); |
84 | Progress pgSave = new Progress(); | |
85 | if (pg != null) { | |
86 | pg.setMax(2); | |
87 | pg.addProgress(pgGetStory, 1); | |
88 | pg.addProgress(pgSave, 1); | |
89 | } | |
90 | ||
a6395bef | 91 | try { |
bee7dffe | 92 | Story story = Instance.getLibrary().getStory(luid, pgGetStory); |
3d247bc3 | 93 | if (story != null) { |
bee7dffe | 94 | story = lib.save(story, luid, pgSave); |
3d247bc3 NR |
95 | } else { |
96 | throw new IOException("Cannot find story in Library: " + luid); | |
97 | } | |
a6395bef | 98 | } catch (IOException e) { |
3d247bc3 | 99 | throw new IOException( |
a6395bef | 100 | "Cannot import story from library to LocalReader library: " |
3d247bc3 | 101 | + luid, e); |
a6395bef | 102 | } |
a6395bef NR |
103 | } |
104 | ||
92fb0719 NR |
105 | /** |
106 | * Get the target file related to this {@link Story}. | |
107 | * | |
108 | * @param luid | |
109 | * the LUID of the {@link Story} | |
110 | * @param pg | |
111 | * the optional progress reporter | |
112 | * | |
113 | * @return the target file | |
114 | * | |
115 | * @throws IOException | |
116 | * in case of I/O error | |
117 | */ | |
118 | public File getTarget(String luid, Progress pg) throws IOException { | |
a6395bef NR |
119 | File file = lib.getFile(luid); |
120 | if (file == null) { | |
92fb0719 | 121 | imprt(luid, pg); |
a6395bef | 122 | file = lib.getFile(luid); |
a6395bef NR |
123 | } |
124 | ||
125 | return file; | |
126 | } | |
127 | ||
9843a5e5 NR |
128 | /** |
129 | * Check if the {@link Story} denoted by this Library UID is present in the | |
130 | * {@link LocalReader} cache. | |
131 | * | |
132 | * @param luid | |
133 | * the Library UID | |
134 | * | |
135 | * @return TRUE if it is | |
136 | */ | |
10d558d2 NR |
137 | public boolean isCached(String luid) { |
138 | return lib.getInfo(luid) != null; | |
139 | } | |
140 | ||
a6395bef | 141 | @Override |
333f0e7b NR |
142 | public void start(String type) { |
143 | final String typeFinal = type; | |
a6395bef NR |
144 | EventQueue.invokeLater(new Runnable() { |
145 | public void run() { | |
146 | new LocalReaderFrame(LocalReader.this, typeFinal) | |
147 | .setVisible(true); | |
148 | } | |
149 | }); | |
150 | } | |
10d558d2 | 151 | |
edd46289 | 152 | // refresh = delete from LocalReader cache (TODO: rename?) |
10d558d2 NR |
153 | void refresh(String luid) { |
154 | lib.delete(luid); | |
155 | } | |
156 | ||
edd46289 | 157 | // delete from main library |
10d558d2 NR |
158 | void delete(String luid) { |
159 | lib.delete(luid); | |
160 | Instance.getLibrary().delete(luid); | |
161 | } | |
edd46289 NR |
162 | |
163 | // open the given book | |
164 | void open(String luid, Progress pg) throws IOException { | |
165 | MetaData meta = Instance.getLibrary().getInfo(luid); | |
166 | File target = getTarget(luid, pg); | |
167 | ||
168 | String program = null; | |
169 | if (meta.isImageDocument()) { | |
170 | program = Instance.getUiConfig().getString( | |
171 | UiConfig.IMAGES_DOCUMENT_READER); | |
172 | } else { | |
173 | program = Instance.getUiConfig().getString( | |
174 | UiConfig.NON_IMAGES_DOCUMENT_READER); | |
175 | } | |
176 | ||
177 | if (program != null && program.trim().isEmpty()) { | |
178 | program = null; | |
179 | } | |
180 | ||
181 | if (program == null) { | |
182 | try { | |
183 | Desktop.getDesktop().browse(target.toURI()); | |
184 | } catch (UnsupportedOperationException e) { | |
185 | Runtime.getRuntime().exec( | |
186 | new String[] { "xdg-open", target.getAbsolutePath() }); | |
187 | ||
188 | } | |
189 | } else { | |
190 | Runtime.getRuntime().exec( | |
191 | new String[] { program, target.getAbsolutePath() }); | |
192 | ||
193 | } | |
194 | } | |
a6395bef | 195 | } |