| 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 | import java.net.URISyntaxException; |
| 8 | |
| 9 | import javax.swing.JEditorPane; |
| 10 | import javax.swing.JLabel; |
| 11 | import javax.swing.JOptionPane; |
| 12 | import javax.swing.event.HyperlinkEvent; |
| 13 | import javax.swing.event.HyperlinkListener; |
| 14 | |
| 15 | import be.nikiroo.fanfix.Instance; |
| 16 | import be.nikiroo.fanfix.Library; |
| 17 | import be.nikiroo.fanfix.VersionCheck; |
| 18 | import be.nikiroo.fanfix.bundles.UiConfig; |
| 19 | import be.nikiroo.fanfix.data.Story; |
| 20 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; |
| 21 | import be.nikiroo.utils.Progress; |
| 22 | import be.nikiroo.utils.Version; |
| 23 | |
| 24 | class LocalReader extends BasicReader { |
| 25 | private Library lib; |
| 26 | |
| 27 | public LocalReader() throws IOException { |
| 28 | File dir = Instance.getReaderDir(); |
| 29 | dir.mkdirs(); |
| 30 | if (!dir.exists()) { |
| 31 | throw new IOException( |
| 32 | "Cannote create cache directory for local reader: " + dir); |
| 33 | } |
| 34 | |
| 35 | OutputType text = null; |
| 36 | OutputType images = null; |
| 37 | |
| 38 | try { |
| 39 | text = OutputType.valueOfNullOkUC(Instance.getUiConfig().getString( |
| 40 | UiConfig.NON_IMAGES_DOCUMENT_TYPE)); |
| 41 | if (text == null) { |
| 42 | text = OutputType.HTML; |
| 43 | } |
| 44 | |
| 45 | images = OutputType.valueOfNullOkUC(Instance.getUiConfig() |
| 46 | .getString(UiConfig.IMAGES_DOCUMENT_TYPE)); |
| 47 | if (images == null) { |
| 48 | images = OutputType.CBZ; |
| 49 | } |
| 50 | } catch (Exception e) { |
| 51 | UiConfig key = (text == null) ? UiConfig.NON_IMAGES_DOCUMENT_TYPE |
| 52 | : UiConfig.IMAGES_DOCUMENT_TYPE; |
| 53 | String value = Instance.getUiConfig().getString(key); |
| 54 | |
| 55 | throw new IOException( |
| 56 | String.format( |
| 57 | "The configuration option %s is not valid: %s", |
| 58 | key, value), e); |
| 59 | } |
| 60 | |
| 61 | lib = new Library(dir, text, images); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void read() throws IOException { |
| 66 | if (getStory() == null) { |
| 67 | throw new IOException("No story to read"); |
| 68 | } |
| 69 | |
| 70 | open(getStory().getMeta().getLuid(), null); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void read(int chapter) throws IOException { |
| 75 | // TODO: show a special page? |
| 76 | read(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Import the story into the local reader library, and keep the same LUID. |
| 81 | * |
| 82 | * @param luid |
| 83 | * the Library UID |
| 84 | * @param pg |
| 85 | * the optional progress reporter |
| 86 | * |
| 87 | * @throws IOException |
| 88 | * in case of I/O error |
| 89 | */ |
| 90 | public void imprt(String luid, Progress pg) throws IOException { |
| 91 | Progress pgGetStory = new Progress(); |
| 92 | Progress pgSave = new Progress(); |
| 93 | if (pg != null) { |
| 94 | pg.setMax(2); |
| 95 | pg.addProgress(pgGetStory, 1); |
| 96 | pg.addProgress(pgSave, 1); |
| 97 | } |
| 98 | |
| 99 | try { |
| 100 | Story story = Instance.getLibrary().getStory(luid, pgGetStory); |
| 101 | if (story != null) { |
| 102 | story = lib.save(story, luid, pgSave); |
| 103 | } else { |
| 104 | throw new IOException("Cannot find story in Library: " + luid); |
| 105 | } |
| 106 | } catch (IOException e) { |
| 107 | throw new IOException( |
| 108 | "Cannot import story from library to LocalReader library: " |
| 109 | + luid, e); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Check if the {@link Story} denoted by this Library UID is present in the |
| 115 | * {@link LocalReader} cache. |
| 116 | * |
| 117 | * @param luid |
| 118 | * the Library UID |
| 119 | * |
| 120 | * @return TRUE if it is |
| 121 | */ |
| 122 | public boolean isCached(String luid) { |
| 123 | return lib.getInfo(luid) != null; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void start(String type) { |
| 128 | // TODO: improve presentation of update message |
| 129 | final VersionCheck updates = VersionCheck.check(); |
| 130 | StringBuilder builder = new StringBuilder(); |
| 131 | |
| 132 | final JEditorPane updateMessage = new JEditorPane("text/html", ""); |
| 133 | if (updates.isNewVersionAvailable()) { |
| 134 | builder.append("A new version of the program is available at <span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>"); |
| 135 | builder.append("<br>"); |
| 136 | builder.append("<br>"); |
| 137 | for (Version v : updates.getNewer()) { |
| 138 | builder.append("\t<b>Version " + v + "</b>"); |
| 139 | builder.append("<br>"); |
| 140 | builder.append("<ul>"); |
| 141 | for (String item : updates.getChanges().get(v)) { |
| 142 | builder.append("<li>" + item + "</li>"); |
| 143 | } |
| 144 | builder.append("</ul>"); |
| 145 | } |
| 146 | |
| 147 | // html content |
| 148 | updateMessage.setText("<html><body>" // |
| 149 | + builder// |
| 150 | + "</body></html>"); |
| 151 | |
| 152 | // handle link events |
| 153 | updateMessage.addHyperlinkListener(new HyperlinkListener() { |
| 154 | public void hyperlinkUpdate(HyperlinkEvent e) { |
| 155 | if (e.getEventType().equals( |
| 156 | HyperlinkEvent.EventType.ACTIVATED)) |
| 157 | try { |
| 158 | Desktop.getDesktop().browse(e.getURL().toURI()); |
| 159 | } catch (IOException ee) { |
| 160 | Instance.syserr(ee); |
| 161 | } catch (URISyntaxException ee) { |
| 162 | Instance.syserr(ee); |
| 163 | } |
| 164 | } |
| 165 | }); |
| 166 | updateMessage.setEditable(false); |
| 167 | updateMessage.setBackground(new JLabel().getBackground()); |
| 168 | } |
| 169 | |
| 170 | final String typeFinal = type; |
| 171 | EventQueue.invokeLater(new Runnable() { |
| 172 | public void run() { |
| 173 | if (updates.isNewVersionAvailable()) { |
| 174 | int rep = JOptionPane.showConfirmDialog(null, |
| 175 | updateMessage, "Updates available", |
| 176 | JOptionPane.OK_CANCEL_OPTION); |
| 177 | if (rep == JOptionPane.OK_OPTION) { |
| 178 | updates.ok(); |
| 179 | } else { |
| 180 | updates.ignore(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | new LocalReaderFrame(LocalReader.this, typeFinal) |
| 185 | .setVisible(true); |
| 186 | } |
| 187 | }); |
| 188 | } |
| 189 | |
| 190 | // delete from local reader library |
| 191 | void clearLocalReaderCache(String luid) { |
| 192 | lib.delete(luid); |
| 193 | } |
| 194 | |
| 195 | // delete from main library |
| 196 | void delete(String luid) { |
| 197 | lib.delete(luid); |
| 198 | Instance.getLibrary().delete(luid); |
| 199 | } |
| 200 | |
| 201 | // open the given book |
| 202 | void open(String luid, Progress pg) throws IOException { |
| 203 | File file = lib.getFile(luid); |
| 204 | if (file == null) { |
| 205 | imprt(luid, pg); |
| 206 | file = lib.getFile(luid); |
| 207 | } |
| 208 | |
| 209 | open(Instance.getLibrary().getInfo(luid), file); |
| 210 | } |
| 211 | |
| 212 | void changeType(String luid, String newType) { |
| 213 | lib.changeType(luid, newType); |
| 214 | Instance.getLibrary().changeType(luid, newType); |
| 215 | } |
| 216 | } |