Merge branch 'master' into subtree
[nikiroo-utils.git] / output / InfoCover.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.output;
2
3d247bc3 3import java.io.BufferedWriter;
08fe2e33 4import java.io.File;
3d247bc3 5import java.io.FileOutputStream;
08fe2e33 6import java.io.IOException;
3d247bc3 7import java.io.OutputStreamWriter;
e992c260 8import java.util.Arrays;
08fe2e33 9
08fe2e33
NR
10import be.nikiroo.fanfix.Instance;
11import be.nikiroo.fanfix.bundles.Config;
e992c260 12import be.nikiroo.fanfix.data.Chapter;
08fe2e33 13import be.nikiroo.fanfix.data.MetaData;
e992c260 14import be.nikiroo.fanfix.data.Story;
08fe2e33 15
e992c260
NR
16/**
17 * Helper class to write info, cover and summary (resume) files.
18 *
19 * @author niki
20 */
70c9b112 21public class InfoCover {
e992c260
NR
22 /**
23 * Write both the <tt>.info<tt> and the <tt>.summary</tt> files by taking
24 * the information from the {@link MetaData}.
25 *
26 * @param targetDir
27 * the directory where to write the 2 files
28 * @param targetName
29 * the target name (no extension) to use (so you will get
30 * <tt>targetName.info</tt> and <tt>targetName.summary</tt>)
31 * @param meta
32 * the {@link MetaData} to get the data out of
33 *
34 * @throws IOException
35 * in case of I/O error
36 */
08fe2e33
NR
37 public static void writeInfo(File targetDir, String targetName,
38 MetaData meta) throws IOException {
39 File info = new File(targetDir, targetName + ".info");
08fe2e33 40
e992c260
NR
41 if (meta != null) {
42 BufferedWriter infoWriter = null;
43 try {
44 infoWriter = new BufferedWriter(new OutputStreamWriter(
45 new FileOutputStream(info), "UTF-8"));
9fe3f177 46
08fe2e33
NR
47 String tags = "";
48 if (meta.getTags() != null) {
49 for (String tag : meta.getTags()) {
50 if (!tags.isEmpty()) {
51 tags += ", ";
52 }
53 tags += tag;
54 }
55 }
56
08fe2e33
NR
57 writeMeta(infoWriter, "TITLE", meta.getTitle());
58 writeMeta(infoWriter, "AUTHOR", meta.getAuthor());
59 writeMeta(infoWriter, "DATE", meta.getDate());
60 writeMeta(infoWriter, "SUBJECT", meta.getSubject());
61 writeMeta(infoWriter, "SOURCE", meta.getSource());
2206ef66 62 writeMeta(infoWriter, "URL", meta.getUrl());
08fe2e33
NR
63 writeMeta(infoWriter, "TAGS", tags);
64 writeMeta(infoWriter, "UUID", meta.getUuid());
65 writeMeta(infoWriter, "LUID", meta.getLuid());
2206ef66
NR
66 writeMeta(infoWriter, "LANG", meta.getLang() == null ? ""
67 : meta.getLang().toLowerCase());
08fe2e33
NR
68 writeMeta(infoWriter, "IMAGES_DOCUMENT",
69 meta.isImageDocument() ? "true" : "false");
fe999aa4 70 writeMeta(infoWriter, "TYPE", meta.getType());
08fe2e33 71 if (meta.getCover() != null) {
d66deb8d 72 String format = Instance.getInstance().getConfig()
e992c260
NR
73 .getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER)
74 .toLowerCase();
08fe2e33
NR
75 writeMeta(infoWriter, "COVER", targetName + "." + format);
76 } else {
77 writeMeta(infoWriter, "COVER", "");
78 }
79 writeMeta(infoWriter, "EPUBCREATOR", BasicOutput.EPUB_CREATOR);
80 writeMeta(infoWriter, "PUBLISHER", meta.getPublisher());
793f1071
NR
81 writeMeta(infoWriter, "WORDCOUNT",
82 Long.toString(meta.getWords()));
83 writeMeta(infoWriter, "CREATION_DATE", meta.getCreationDate());
a9eb3f46
NR
84 writeMeta(infoWriter, "FAKE_COVER",
85 Boolean.toString(meta.isFakeCover()));
e992c260
NR
86 } finally {
87 if (infoWriter != null) {
88 infoWriter.close();
89 }
9fe3f177 90 }
e992c260
NR
91
92 if (meta.getResume() != null) {
93 Story fakeStory = new Story();
94 fakeStory.setMeta(meta);
95 fakeStory.setChapters(Arrays.asList(meta.getResume()));
96
97 Text summaryText = new Text() {
98 @Override
99 protected boolean isWriteCover() {
100 return false;
101 }
102
103 @Override
104 protected boolean isWriteInfo() {
105 return false; // infinite loop if not!
106 }
107
108 @Override
109 public String getDefaultExtension(boolean readerTarget) {
110 return ".summary";
111 }
112
113 @Override
114 protected void writeStoryHeader(Story story)
115 throws IOException {
116 }
117
118 @Override
119 protected void writeStoryFooter(Story story)
120 throws IOException {
121 }
122
123 @Override
124 protected void writeChapterHeader(Chapter chap)
125 throws IOException {
126 }
127
128 @Override
129 protected void writeChapterFooter(Chapter chap)
130 throws IOException {
131 }
132 };
133
134 summaryText.process(fakeStory, targetDir, targetName);
08fe2e33
NR
135 }
136 }
137 }
138
e992c260
NR
139 /**
140 * Write the cover file.
141 *
142 * @param targetDir
143 * the target directory
144 * @param targetName
145 * the target name for the cover (the extension will be added)
146 * @param meta
147 * the meta to get the information out of
148 */
08fe2e33
NR
149 public static void writeCover(File targetDir, String targetName,
150 MetaData meta) {
151 if (meta != null && meta.getCover() != null) {
152 try {
d66deb8d 153 Instance.getInstance().getCache().saveAsImage(meta.getCover(), new File(targetDir, targetName), true);
08fe2e33
NR
154 } catch (IOException e) {
155 // Allow to continue without cover
d66deb8d 156 Instance.getInstance().getTraceHandler().error(new IOException("Failed to save the cover image", e));
08fe2e33
NR
157 }
158 }
159 }
160
3d247bc3
NR
161 private static void writeMeta(BufferedWriter writer, String key,
162 String value) throws IOException {
08fe2e33
NR
163 if (value == null) {
164 value = "";
165 }
166
167 writer.write(String.format("%s=\"%s\"\n", key, value.replace("\"", "'")));
168 }
169}