+ invalidateInfo();
+ }
+
+ @Override
+ public synchronized Image getCustomSourceCover(String source) {
+ if (sourceCovers == null) {
+ sourceCovers = new HashMap<String, Image>();
+ }
+
+ Image img = sourceCovers.get(source);
+ if (img != null) {
+ return img;
+ }
+
+ File coverDir = new File(baseDir, source);
+ if (coverDir.isDirectory()) {
+ File cover = new File(coverDir, ".cover.png");
+ if (cover.exists()) {
+ InputStream in;
+ try {
+ in = new FileInputStream(cover);
+ try {
+ sourceCovers.put(source, new Image(in));
+ } finally {
+ in.close();
+ }
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ Instance.getTraceHandler().error(
+ new IOException(
+ "Cannot load the existing custom source cover: "
+ + cover, e));
+ }
+ }
+ }
+
+ return sourceCovers.get(source);
+ }
+
+ @Override
+ public void setSourceCover(String source, String luid) {
+ setSourceCover(source, getCover(luid));
+ }
+
+ /**
+ * Fix the source cover to the given story cover.
+ *
+ * @param source
+ * the source to change
+ * @param coverImage
+ * the cover image
+ */
+ synchronized void setSourceCover(String source, Image coverImage) {
+ File dir = getExpectedDir(source);
+ dir.mkdirs();
+ File cover = new File(dir, ".cover");
+ try {
+ Instance.getCache().saveAsImage(coverImage, cover, true);
+ if (sourceCovers != null) {
+ sourceCovers.put(source, coverImage);
+ }
+ } catch (IOException e) {
+ Instance.getTraceHandler().error(e);
+ }
+ }
+
+ @Override
+ public void imprt(BasicLibrary other, String luid, Progress pg)
+ throws IOException {
+ if (pg == null) {
+ pg = new Progress();
+ }
+
+ // Check if we can simply copy the files instead of the whole process
+ if (other instanceof LocalLibrary) {
+ LocalLibrary otherLocalLibrary = (LocalLibrary) other;
+
+ MetaData meta = otherLocalLibrary.getInfo(luid);
+ String expectedType = ""
+ + (meta != null && meta.isImageDocument() ? image : text);
+ if (meta != null && meta.getType().equals(expectedType)) {
+ File from = otherLocalLibrary.getExpectedDir(meta.getSource());
+ File to = this.getExpectedDir(meta.getSource());
+ List<File> sources = otherLocalLibrary.getRelatedFiles(luid);
+ if (!sources.isEmpty()) {
+ pg.setMinMax(0, sources.size());
+ }
+
+ for (File source : sources) {
+ File target = new File(source.getAbsolutePath().replace(
+ from.getAbsolutePath(), to.getAbsolutePath()));
+ if (!source.equals(target)) {
+ target.getParentFile().mkdirs();
+ InputStream in = null;
+ try {
+ in = new FileInputStream(source);
+ IOUtils.write(in, target);
+ } catch (IOException e) {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (Exception ee) {
+ }
+ }
+
+ pg.done();
+ throw e;
+ }
+ }
+
+ pg.add(1);
+ }
+
+ invalidateInfo();
+ pg.done();
+ return;
+ }
+ }
+
+ super.imprt(other, luid, pg);