From 74e3b8f6147240643bceb831f542cf8310b1ed90 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 11 Dec 2016 00:25:19 +0800 Subject: [PATCH] support restart task --- src/langs/zh_CN.txt | 4 + src/scripts/config/defaultLanguage.js | 4 + src/scripts/controllers/main.js | 37 +++++++-- src/scripts/services/aria2TaskService.js | 96 +++++++++++++++++++++++- src/views/list.html | 1 + 5 files changed, 133 insertions(+), 9 deletions(-) diff --git a/src/langs/zh_CN.txt b/src/langs/zh_CN.txt index ce0362a..e85fc77 100644 --- a/src/langs/zh_CN.txt +++ b/src/langs/zh_CN.txt @@ -15,6 +15,7 @@ RPC=RPC New=新建 Start=开始任务 Pause=暂停任务 +Restart=重试 Delete=删除任务 Select All=全选 Select None=不选 @@ -96,6 +97,9 @@ Speed=速度 No Data=无数据 No connected peers=没有连接到其他节点 Failed to change some tasks state.=修改一些任务状态时失败. +Confirm Restart=确认重试 +Are you sure you want to restart this task? AriaNg will create a same task after clicking OK.=您是否要重试这个任务? 点击 "确定" 后, AriaNg 将会创建一个相同的任务. +Failed to restart this task.=该任务重试失败. Confirm Remove=确认删除 Are you sure you want to remove the selected task?=您是否要删除选中的任务? Failed to remove some task(s).=删除一些任务时失败. diff --git a/src/scripts/config/defaultLanguage.js b/src/scripts/config/defaultLanguage.js index 5013ade..8646390 100644 --- a/src/scripts/config/defaultLanguage.js +++ b/src/scripts/config/defaultLanguage.js @@ -19,6 +19,7 @@ 'New': 'New', 'Start': 'Start', 'Pause': 'Pause', + 'Restart': 'Restart', 'Delete': 'Delete', 'Select All': 'Select All', 'Select None': 'Select None', @@ -100,6 +101,9 @@ 'No Data': 'No Data', 'No connected peers': 'No connected peers', 'Failed to change some tasks state.': 'Failed to change some tasks state.', + 'Confirm Restart': 'Confirm Restart', + 'Are you sure you want to restart this task? AriaNg will create a same task after clicking OK.': 'Are you sure you want to restart this task? AriaNg will create a same task after clicking OK.', + 'Failed to restart this task.': 'Failed to restart this task.', 'Confirm Remove': 'Confirm Remove', 'Are you sure you want to remove the selected task?': 'Are you sure you want to remove the selected task?', 'Failed to remove some task(s).': 'Failed to remove some task(s).', diff --git a/src/scripts/controllers/main.js b/src/scripts/controllers/main.js index 44cd091..c4d3579 100644 --- a/src/scripts/controllers/main.js +++ b/src/scripts/controllers/main.js @@ -119,13 +119,13 @@ refreshGlobalStat(true); if (!response.hasError && state === 'start') { - if ($location.path() === '/waiting') { + if ($location.path() !== '/downloading') { $location.path('/downloading'); } else { $route.reload(); } } else if (!response.hasError && state === 'pause') { - if ($location.path() === '/downloading') { + if ($location.path() !== '/waiting') { $location.path('/waiting'); } else { $route.reload(); @@ -134,6 +134,27 @@ }, (gids.length > 1)); }; + $scope.restart = function (task) { + ariaNgCommonService.confirm('Confirm Restart', 'Are you sure you want to restart this task? AriaNg will create a same task after clicking OK.', 'info', function () { + $rootScope.loadPromise = aria2TaskService.restartTask(task.gid, function (response) { + if (!response.success) { + ariaNgCommonService.showError('Failed to restart this task.'); + return; + } + + refreshGlobalStat(true); + + if (response.success) { + if ($location.path() !== '/downloading') { + $location.path('/downloading'); + } else { + $route.reload(); + } + } + }, false); + }); + }; + $scope.removeTasks = function () { var tasks = $rootScope.taskContext.getSelectedTasks(); @@ -154,10 +175,10 @@ refreshGlobalStat(true); if (!response.hasError) { - if ($location.path() === '/stopped') { - $route.reload(); - } else { + if ($location.path() !== '/stopped') { $location.path('/stopped'); + } else { + $route.reload(); } } }, (tasks.length > 1)); @@ -173,10 +194,10 @@ refreshGlobalStat(true); - if ($location.path() === '/stopped') { - $route.reload(); - } else { + if ($location.path() !== '/stopped') { $location.path('/stopped'); + } else { + $route.reload(); } }); }); diff --git a/src/scripts/services/aria2TaskService.js b/src/scripts/services/aria2TaskService.js index 8138b0a..44733fe 100644 --- a/src/scripts/services/aria2TaskService.js +++ b/src/scripts/services/aria2TaskService.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('aria2TaskService', ['$q', '$translate', 'bittorrentPeeridService', 'aria2Errors', 'aria2RpcService', 'ariaNgCommonService', function ($q, $translate, bittorrentPeeridService, aria2Errors, aria2RpcService, ariaNgCommonService) { + angular.module('ariaNg').factory('aria2TaskService', ['$q', '$translate', 'bittorrentPeeridService', 'aria2Errors', 'aria2RpcService', 'ariaNgCommonService', 'ariaNgLogService', function ($q, $translate, bittorrentPeeridService, aria2Errors, aria2RpcService, ariaNgCommonService, ariaNgLogService) { var getFileName = function (file) { if (!file) { return ''; @@ -423,6 +423,100 @@ callback: callback }); }, + restartTask: function (gid, callback, silent) { + var deferred = $q.defer(); + + var methods = [ + aria2RpcService.tellStatus({gid: gid}, true), + aria2RpcService.getOption({gid: gid}, true) + ]; + + var task = null, options = null; + + aria2RpcService.multicall({ + methods: methods, + silent: !!silent, + callback: function (response) { + if (!callback) { + ariaNgLogService.warn("[aria2TaskService.restartTask] callback is null"); + return; + } + + if (!response.success) { + ariaNgLogService.warn("[aria2TaskService.restartTask] response is not success"); + deferred.reject(response); + callback(response); + return; + } + + if (response.data.length > 0) { + task = response.data[0][0]; + } + + if (response.data.length > 1) { + options = response.data[1][0]; + } + + if (!task || !options || !task.files || task.files.length != 1 || task.bittorrent) { + if (!task) { + ariaNgLogService.warn("[aria2TaskService.restartTask] task is null"); + } + + if (!options) { + ariaNgLogService.warn("[aria2TaskService.restartTask] options is null"); + } + + if (!task.files) { + ariaNgLogService.warn("[aria2TaskService.restartTask] task file is null"); + } + + if (task.files.length != 1) { + ariaNgLogService.warn("[aria2TaskService.restartTask] task file length is not equal 1"); + } + + if (task.bittorrent) { + ariaNgLogService.warn("[aria2TaskService.restartTask] task is bittorrent"); + } + + deferred.reject(gid); + callback({ + success: false + }); + return; + } + + var file = task.files[0]; + var urls = []; + + for (var i = 0; i < file.uris.length; i++) { + var uriObj = file.uris[i]; + urls.push(uriObj.uri); + } + + aria2RpcService.addUri({ + task: { + urls: urls, + options: options + }, + pauseOnAdded: false, + silent: !!silent, + callback: function (response) { + if (!response.success) { + ariaNgLogService.warn("[aria2TaskService.restartTask] addUri response is not success"); + deferred.reject(response); + callback(response); + return; + } + + deferred.resolve(response); + callback(response); + } + }); + } + }); + + return deferred.promise; + }, removeTasks: function (tasks, callback, silent) { var runningTaskGids = []; var stoppedTaskGids = []; diff --git a/src/views/list.html b/src/views/list.html index dd2876f..97c3986 100644 --- a/src/views/list.html +++ b/src/views/list.html @@ -46,6 +46,7 @@ + Restart