1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | package net.sf.jomic.comic; |
17 | |
18 | import java.io.File; |
19 | import java.util.ArrayList; |
20 | import java.util.Collections; |
21 | import java.util.List; |
22 | |
23 | import net.sf.jomic.tools.AbstractTask; |
24 | import net.sf.jomic.tools.CreateImagInfoMapTask; |
25 | import net.sf.jomic.tools.FileTools; |
26 | import net.sf.jomic.tools.ImageInfo; |
27 | import net.sf.jomic.tools.NaturalCaseInsensitiveOrderComparator; |
28 | import net.sf.jomic.tools.ProgressChangeListener; |
29 | import net.sf.jomic.tools.Task; |
30 | |
31 | import org.apache.commons.logging.Log; |
32 | import org.apache.commons.logging.LogFactory; |
33 | |
34 | /** |
35 | * Task to convert a comic according to a <code>Conversion</code>. |
36 | * |
37 | * @see net.sf.jomic.comic.Conversion |
38 | * @author Thomas Aglassinger |
39 | */ |
40 | public class ConvertComicTask extends AbstractTask implements ProgressChangeListener |
41 | { |
42 | private static final int SUBTASK_ANALYSE_FILES = 1; |
43 | private static final int SUBTASK_CREATE_TARGET = 2; |
44 | private static final int SUBTASK_EXTRACT_SOURCE = 0; |
45 | private static final double WEIGHT_ANALYSE_FILES = 0.10; |
46 | private static final double WEIGHT_CREATE_TARGET = 0.5; |
47 | private static final double WEIGHT_EXTRACT_SOURCE = 0.3; |
48 | private Conversion conversion; |
49 | private FileTools fileTools; |
50 | private Log logger; |
51 | private ConversionReportItem reportItem; |
52 | private File sourceComic; |
53 | private long[] subtaskBaseProgress; |
54 | private int subtaskIndex; |
55 | private double[] subtaskWeights; |
56 | private File targetComic; |
57 | |
58 | public ConvertComicTask(File newSourceComic, File newTargetComic, Conversion newConversion, |
59 | ConversionReportItem newReportItem) { |
60 | this(); |
61 | assert newSourceComic != null; |
62 | assert newTargetComic != null; |
63 | assert newConversion != null; |
64 | |
65 | sourceComic = newSourceComic; |
66 | targetComic = newTargetComic; |
67 | conversion = newConversion; |
68 | reportItem = newReportItem; |
69 | setMaxProgress(sourceComic.length() + 1); |
70 | subtaskWeights = new double[]{ |
71 | WEIGHT_EXTRACT_SOURCE, WEIGHT_ANALYSE_FILES, WEIGHT_CREATE_TARGET |
72 | }; |
73 | subtaskBaseProgress = new long[subtaskWeights.length]; |
74 | subtaskBaseProgress[0] = 0; |
75 | for (int i = 1; i < subtaskWeights.length; i += 1) { |
76 | subtaskBaseProgress[i] = subtaskBaseProgress[i - 1] + Math.round(getMaxProgress() * subtaskWeights[i - 1]); |
77 | } |
78 | } |
79 | |
80 | public ConvertComicTask(File newSourceComic, File newTargetComic, Conversion newConversion) { |
81 | this(newSourceComic, newTargetComic, newConversion, null); |
82 | } |
83 | |
84 | private ConvertComicTask() { |
85 | super(); |
86 | logger = LogFactory.getLog(ConvertComicTask.class); |
87 | fileTools = FileTools.instance(); |
88 | } |
89 | |
90 | private void setReportItemState(String newState) { |
91 | if (reportItem != null) { |
92 | reportItem.setState(newState); |
93 | } |
94 | } |
95 | |
96 | public void progressChanged(Task source) { |
97 | long baseProgress = subtaskBaseProgress[subtaskIndex]; |
98 | long weightedProgress = Math.round(subtaskWeights[subtaskIndex] * source.getProgress()); |
99 | |
100 | setProgress(baseProgress + weightedProgress); |
101 | } |
102 | |
103 | public void start() |
104 | throws Exception { |
105 | setProgress(0); |
106 | setReportItemState(ConversionReportItem.STATE_CONVERTING); |
107 | |
108 | File tempFolder = fileTools.createTempDir("jomic-convert-"); |
109 | File[] extractedFiles; |
110 | Task extractSourceTask; |
111 | CreateImagInfoMapTask analyseImagesTask; |
112 | Task createTargetTask; |
113 | |
114 | try { |
115 | extractSourceTask = new ExtractComicTask(sourceComic, tempFolder); |
116 | startSubtask(SUBTASK_EXTRACT_SOURCE, extractSourceTask); |
117 | extractedFiles = fileTools.listFilesRecursively(tempFolder); |
118 | |
119 | analyseImagesTask = new CreateImagInfoMapTask(extractedFiles); |
120 | startSubtask(SUBTASK_ANALYSE_FILES, analyseImagesTask); |
121 | |
122 | List imageFiles = new ArrayList(); |
123 | List otherFiles = new ArrayList(); |
124 | |
125 | for (int fileIndex = 0; fileIndex < extractedFiles.length; fileIndex += 1) { |
126 | File fileToExamine = extractedFiles[fileIndex]; |
127 | ImageInfo imageInfo = (ImageInfo) analyseImagesTask.getImageInfoMap().get(fileToExamine); |
128 | |
129 | if (imageInfo.hasError()) { |
130 | otherFiles.add(fileToExamine); |
131 | } else if (conversion.isAddable(imageInfo)) { |
132 | imageFiles.add(fileToExamine); |
133 | } else { |
134 | logger.info("excluding file: " + fileToExamine); |
135 | } |
136 | } |
137 | |
138 | List filesToInclude = new ArrayList(imageFiles); |
139 | |
140 | if (!conversion.isAddOnlyImages() && !conversion.getComicFormat().equals(Conversion.COMIC_FORMAT_PDF)) { |
141 | filesToInclude.addAll(otherFiles); |
142 | } |
143 | if (filesToInclude.size() > 0) { |
144 | Collections.sort(filesToInclude, new NaturalCaseInsensitiveOrderComparator()); |
145 | |
146 | File[] filesToIncludeArray = (File[]) filesToInclude.toArray(new File[]{}); |
147 | String[] namesOfFilesToInclude = fileTools.getRelativePaths(tempFolder, filesToIncludeArray); |
148 | |
149 | createTargetTask = new CreateComicTask(tempFolder, namesOfFilesToInclude, targetComic, conversion); |
150 | startSubtask(SUBTASK_CREATE_TARGET, createTargetTask); |
151 | } |
152 | if (isInterrupted()) { |
153 | setReportItemState(ConversionReportItem.STATE_CANCELLED); |
154 | } else { |
155 | setReportItemState(ConversionReportItem.STATE_DONE); |
156 | } |
157 | } catch (Exception error) { |
158 | if (reportItem != null) { |
159 | reportItem.setState(ConversionReportItem.STATE_ERROR_DURING_CONVERSION); |
160 | reportItem.setConversionError(error); |
161 | } |
162 | throw error; |
163 | } finally { |
164 | fileTools.attemptToDeleteAll(tempFolder, logger); |
165 | } |
166 | |
167 | setProgress(getMaxProgress()); |
168 | } |
169 | |
170 | private void startSubtask(int newSubtaskIndex, Task subtask) |
171 | throws Exception { |
172 | subtaskIndex = newSubtaskIndex; |
173 | subtask.addProgressChangeListener(this); |
174 | try { |
175 | subtask.start(); |
176 | } finally { |
177 | subtask.removeProgressChangeListener(this); |
178 | } |
179 | } |
180 | } |