| 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.io.IOException; |
| 20 | |
| 21 | import net.sf.jomic.tools.AbstractTask; |
| 22 | import net.sf.jomic.tools.ExtractPdfImagesTask; |
| 23 | import net.sf.jomic.tools.ExtractRarTask; |
| 24 | import net.sf.jomic.tools.ExtractZipTask; |
| 25 | import net.sf.jomic.tools.FileTools; |
| 26 | import net.sf.jomic.tools.ProgressChangeListener; |
| 27 | import net.sf.jomic.tools.Task; |
| 28 | |
| 29 | /** |
| 30 | * Task to extract a comic independent of its format. |
| 31 | * |
| 32 | * @see net.sf.jomic.tools.ExtractPdfImagesTask |
| 33 | * @see net.sf.jomic.tools.ExtractRarTask |
| 34 | * @see net.sf.jomic.tools.ExtractZipTask |
| 35 | * @author Thomas Aglassinger |
| 36 | */ |
| 37 | public class ExtractComicTask extends AbstractTask implements ProgressChangeListener |
| 38 | { |
| 39 | private File comicFile; |
| 40 | private Task delegateTask; |
| 41 | private FileTools fileTools; |
| 42 | private File targetFolder; |
| 43 | |
| 44 | public ExtractComicTask(File newComicFile, File newTargetFolder) |
| 45 | throws IOException { |
| 46 | this(); |
| 47 | comicFile = newComicFile; |
| 48 | targetFolder = newTargetFolder; |
| 49 | |
| 50 | String comicFormat = fileTools.obtainComicFormat(comicFile); |
| 51 | |
| 52 | if (comicFormat.equals(FileTools.FORMAT_PDF)) { |
| 53 | delegateTask = new ExtractPdfImagesTask(comicFile, targetFolder); |
| 54 | } else if (comicFormat.equals(FileTools.FORMAT_RAR)) { |
| 55 | ExtractRarTask extractRarTask = new ExtractRarTask(comicFile, targetFolder); |
| 56 | |
| 57 | delegateTask = extractRarTask; |
| 58 | } else if (comicFormat.equals(FileTools.FORMAT_ZIP)) { |
| 59 | delegateTask = new ExtractZipTask(comicFile, targetFolder); |
| 60 | } else { |
| 61 | assert false : "fileType=" + comicFormat; |
| 62 | } |
| 63 | setMaxProgress(delegateTask.getMaxProgress()); |
| 64 | } |
| 65 | |
| 66 | private ExtractComicTask() { |
| 67 | super(); |
| 68 | fileTools = FileTools.instance(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Set progress to progress of <code>source</code> task, which does the actual extraction. |
| 73 | */ |
| 74 | public void progressChanged(Task source) { |
| 75 | setProgress(source.getProgress()); |
| 76 | } |
| 77 | |
| 78 | public void start() |
| 79 | throws Exception { |
| 80 | delegateTask.addProgressChangeListener(this); |
| 81 | try { |
| 82 | delegateTask.start(); |
| 83 | } finally { |
| 84 | delegateTask.removeProgressChangeListener(this); |
| 85 | } |
| 86 | } |
| 87 | } |