OSDN Git Service

ConvertStopFlag, TextProgressListenerクラス削除
[coroid/inqubus.git] / frontend / src / saccubus / converter / Converter.java
index 13dc2da..a208b8b 100644 (file)
@@ -4,6 +4,8 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;
 import java.util.concurrent.Callable;
+import java.util.logging.Logger;
+import javax.swing.SwingWorker;
 import nicobrowser.GetFlvResult;
 import nicobrowser.NamePattern;
 import nicobrowser.NicoHttpClient;
@@ -11,14 +13,12 @@ import nicobrowser.ProgressListener;
 import nicobrowser.WayBackInfo;
 import nicobrowser.entity.NicoContent.Status;
 import org.apache.http.HttpException;
-import saccubus.ConvertStopFlag;
-import saccubus.net.TextProgressListener;
-import yukihane.saccubus.converter.profile.CommentProfile;
-import yukihane.saccubus.converter.profile.FfmpegOption;
-import yukihane.saccubus.converter.profile.GeneralProfile;
-import yukihane.saccubus.converter.profile.OutputProfile;
-import yukihane.saccubus.converter.profile.Profile;
-import yukihane.saccubus.converter.profile.ProxyProfile;
+import saccubus.converter.profile.CommentProfile;
+import saccubus.converter.profile.FfmpegProfile;
+import saccubus.converter.profile.GeneralProfile;
+import saccubus.converter.profile.OutputProfile;
+import saccubus.converter.profile.Profile;
+import saccubus.converter.profile.ProxyProfile;
 
 /**
  * <p>タイトル: さきゅばす</p>
@@ -32,8 +32,9 @@ import yukihane.saccubus.converter.profile.ProxyProfile;
  * @author 未入力
  * @version 1.0
  */
-public class Converter extends AbstractCommand implements Callable<Boolean> {
+public class Converter extends SwingWorker<ConvertResult, ConvertProgress> implements Callable<Boolean> {
 
+    private static final Logger logger = Logger.getLogger(Converter.class.getName());
     private final Profile profile;
     private final String movieId;
 
@@ -45,46 +46,44 @@ public class Converter extends AbstractCommand implements Callable<Boolean> {
      * @param listener
      * @param flag
      */
-    public Converter(String id, Profile profile,
-            TextProgressListener listener, ConvertStopFlag flag) {
-        super(listener, flag);
+    public Converter(String id, Profile profile) {
         this.movieId = id;
         this.profile = profile;
     }
 
     @Override
     public Boolean call() throws Exception {
-        boolean result = false;
         try {
-            result = runConvert();
+            final ConvertResult result = doInBackground();
+            return Boolean.valueOf(result.getResultValue());
         } finally {
-            getStopFlag().finished();
+            // TODO 何か処理が必要?
+//            getStopFlag().finished();
         }
-        return Boolean.valueOf(result);
     }
 
-    // TODO Runnableを実装しなくなったので削除する
-    public void run() {
-        try {
-            call();
-        } catch (Exception ex) {
-            String text = (ex.getMessage() != null) ? ex.getMessage() : "予期しないエラー発生のため中断しました。";
-            sendText(text);
-            ex.printStackTrace();
-        }
-    }
-
-    private boolean runConvert() throws IOException, InterruptedException, Exception {
+//    // TODO Runnableを実装しなくなったので削除する
+//    public void run() {
+//        try {
+//            call();
+//        } catch (Exception ex) {
+//            String text = (ex.getMessage() != null) ? ex.getMessage() : "予期しないエラー発生のため中断しました。";
+//            sendText(text);
+//            logger.log(Level.SEVERE, null, ex);
+//        }
+//    }
+    @Override
+    protected ConvertResult doInBackground() throws Exception {
         if (!shouldRun(profile)) {
-            sendText("何もすることがありません");
-            return true;
+            publish(new ConvertProgress("何もすることがありません"));
+            return new ConvertResult(true);
         }
 
         validSetting();
-        final FfmpegOption ov = profile.getFfmpeg().getFfmpegOption();
+        final FfmpegProfile ov = profile.getFfmpeg().getFfmpegOption();
 
         // TODO ログインしないで良い場合もある.
-        sendText("ログイン中");
+        publish(new ConvertProgress("ログイン中"));
 
 
 
@@ -107,8 +106,7 @@ public class Converter extends AbstractCommand implements Callable<Boolean> {
             }
         }
 
-        stopFlagReturn();
-
+        checkStop();
 
         File commentFile;
         if (profile.getCommentSetting().isDownload()) {
@@ -121,13 +119,14 @@ public class Converter extends AbstractCommand implements Callable<Boolean> {
             final String name = pattern.createFileName(movieId, true);
             final File file = new File(profile.getCommentSetting().getDir(), name);
 
-            commentFile = client.getCommentFile(vi, file.getPath(), wbi, profile.getCommentSetting().getLengthRelatedCommentSize(),
+            commentFile = client.getCommentFile(vi, file.getPath(), wbi, profile.getCommentSetting().
+                    getLengthRelatedCommentSize(),
                     profile.getCommentSetting().isDisablePerMinComment());
         } else {
             commentFile = profile.getCommentSetting().getLocalFile();
         }
 
-        stopFlagReturn();
+        checkStop();
 
         File videoFile;
         GetFlvResult vf = null;
@@ -152,8 +151,8 @@ public class Converter extends AbstractCommand implements Callable<Boolean> {
         }
 
         if (!profile.getOutputFileSetting().isConvert()) {
-            sendText("動画・コメントを保存し、変換は行いませんでした。");
-            return true;
+            publish(new ConvertProgress("動画・コメントを保存し、変換は行いませんでした。"));
+            return new ConvertResult(true);
         }
 
         if (!videoFile.isFile()) {
@@ -172,18 +171,12 @@ public class Converter extends AbstractCommand implements Callable<Boolean> {
         final boolean isNotLow = (vf == null) ? true : (vf.getStatus() != Status.GET_LOW);
         File convertedVideoFile = getOutputFileName(vi.getTitleInWatchPage(), isNotLow);
 
-        boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, videoFile,
-                convertedVideoFile, profile.getFfmpeg(), profile.getGeneralSetting()).execute();
-        return res;
-    }
+        return new ConvertResult(true);
 
-    public boolean isConverted() {
-        return getStopFlag().isFinished();
-    }
-
-    @Override
-    public ConvertStopFlag getStopFlag() {
-        return super.getStopFlag();
+        // TODO FFMPEG 実行開始は別タスクとして実装する.
+//        boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, videoFile,
+//                convertedVideoFile, profile.getFfmpeg(), profile.getGeneralSetting()).execute();
+//        return res;
     }
 
     /** @return 何か実行すべき処理があればtrue. */
@@ -275,4 +268,10 @@ public class Converter extends AbstractCommand implements Callable<Boolean> {
         final String name = pattern.createFileName(movieId, isNotLow);
         return new File(dir, name);
     }
+
+    private void checkStop() throws InterruptedException {
+        if (Thread.interrupted()) {
+            throw new InterruptedException("中止要求を受け付けました");
+        }
+    }
 }