Version 1.2.2: fixes, export in UI
[fanfix.git] / src / be / nikiroo / fanfix / reader / LocalReader.java
1 package be.nikiroo.fanfix.reader;
2
3 import java.awt.Desktop;
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;
10 import be.nikiroo.fanfix.bundles.UiConfig;
11 import be.nikiroo.fanfix.data.MetaData;
12 import be.nikiroo.fanfix.data.Story;
13 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
14 import be.nikiroo.utils.Progress;
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
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);
46
47 throw new IOException(
48 String.format(
49 "The configuration option %s is not valid: %s",
50 key, value), e);
51 }
52
53 lib = new Library(dir, text, images);
54 }
55
56 @Override
57 public void read() throws IOException {
58 if (getStory() == null) {
59 throw new IOException("No story to read");
60 }
61
62 open(getStory().getMeta().getLuid(), null);
63 }
64
65 @Override
66 public void read(int chapter) throws IOException {
67 // TODO: show a special page?
68 read();
69 }
70
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 {
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
91 try {
92 Story story = Instance.getLibrary().getStory(luid, pgGetStory);
93 if (story != null) {
94 story = lib.save(story, luid, pgSave);
95 } else {
96 throw new IOException("Cannot find story in Library: " + luid);
97 }
98 } catch (IOException e) {
99 throw new IOException(
100 "Cannot import story from library to LocalReader library: "
101 + luid, e);
102 }
103 }
104
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 {
119 File file = lib.getFile(luid);
120 if (file == null) {
121 imprt(luid, pg);
122 file = lib.getFile(luid);
123 }
124
125 return file;
126 }
127
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 */
137 public boolean isCached(String luid) {
138 return lib.getInfo(luid) != null;
139 }
140
141 @Override
142 public void start(String type) {
143 final String typeFinal = type;
144 EventQueue.invokeLater(new Runnable() {
145 public void run() {
146 new LocalReaderFrame(LocalReader.this, typeFinal)
147 .setVisible(true);
148 }
149 });
150 }
151
152 // refresh = delete from LocalReader cache (TODO: rename?)
153 void refresh(String luid) {
154 lib.delete(luid);
155 }
156
157 // delete from main library
158 void delete(String luid) {
159 lib.delete(luid);
160 Instance.getLibrary().delete(luid);
161 }
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 }
195 }