2016-05-13 18:09:12 +02:00
( function ( ) {
'use strict' ;
2018-11-21 13:46:21 +01:00
angular . module ( 'ariaNg' ) . controller ( 'MainController' , [ '$rootScope' , '$scope' , '$route' , '$window' , '$location' , '$document' , '$interval' , 'clipboard' , 'ariaNgBuildConfiguration' , 'aria2RpcErrors' , 'ariaNgCommonService' , 'ariaNgNotificationService' , 'ariaNgLocalizationService' , 'ariaNgSettingService' , 'ariaNgMonitorService' , 'ariaNgTitleService' , 'aria2TaskService' , 'aria2SettingService' , function ( $rootScope , $scope , $route , $window , $location , $document , $interval , clipboard , ariaNgBuildConfiguration , aria2RpcErrors , ariaNgCommonService , ariaNgNotificationService , ariaNgLocalizationService , ariaNgSettingService , ariaNgMonitorService , ariaNgTitleService , aria2TaskService , aria2SettingService ) {
2016-06-27 17:59:22 +02:00
var pageTitleRefreshPromise = null ;
2016-05-17 16:15:28 +02:00
var globalStatRefreshPromise = null ;
2016-06-27 17:59:22 +02:00
var refreshPageTitle = function ( ) {
2019-02-21 14:36:50 +01:00
$document [ 0 ] . title = ariaNgTitleService . getFinalTitleByGlobalStat ( {
globalStat : $scope . globalStat ,
currentRpcProfile : getCurrentRPCProfile ( )
} ) ;
2016-06-27 17:59:22 +02:00
} ;
var refreshGlobalStat = function ( silent , callback ) {
2016-06-10 13:23:16 +02:00
return aria2SettingService . getGlobalStat ( function ( response ) {
2016-08-01 16:49:16 +02:00
if ( ! response . success && response . data . message === aria2RpcErrors . Unauthorized . message ) {
2016-06-10 13:23:16 +02:00
$interval . cancel ( globalStatRefreshPromise ) ;
return ;
}
2016-06-10 19:25:09 +02:00
2016-06-10 13:23:16 +02:00
if ( response . success ) {
$scope . globalStat = response . data ;
2016-06-10 19:25:09 +02:00
ariaNgMonitorService . recordGlobalStat ( response . data ) ;
2016-06-10 13:23:16 +02:00
}
2016-06-27 17:59:22 +02:00
if ( callback ) {
callback ( response ) ;
}
2016-06-04 09:33:40 +02:00
} , silent ) ;
2016-05-17 16:15:28 +02:00
} ;
2016-06-27 17:59:22 +02:00
2019-02-21 14:36:50 +01:00
var getCurrentRPCProfile = function ( ) {
if ( ! $scope . rpcSettings || $scope . rpcSettings . length < 1 ) {
return null ;
}
for ( var i = 0 ; i < $scope . rpcSettings . length ; i ++ ) {
var rpcSetting = $scope . rpcSettings [ i ] ;
if ( rpcSetting . isDefault ) {
return rpcSetting ;
}
}
return null ;
} ;
2016-06-26 16:52:06 +02:00
if ( ariaNgSettingService . getBrowserNotification ( ) ) {
ariaNgNotificationService . requestBrowserPermission ( ) ;
}
2016-05-17 16:15:28 +02:00
2018-11-21 13:46:21 +01:00
$scope . ariaNgVersion = ariaNgBuildConfiguration . buildVersion ;
2016-06-10 19:25:09 +02:00
$scope . globalStatusContext = {
2016-12-04 16:36:12 +01:00
isEnabled : ariaNgSettingService . getGlobalStatRefreshInterval ( ) > 0 ,
2016-06-10 19:25:09 +02:00
data : ariaNgMonitorService . getGlobalStatsData ( )
} ;
2018-04-05 18:58:14 +02:00
$scope . enableDebugMode = function ( ) {
return ariaNgSettingService . isEnableDebugMode ( ) ;
} ;
2016-06-10 19:25:09 +02:00
2017-05-14 16:45:35 +02:00
$scope . quickSettingContext = null ;
2017-03-19 16:36:14 +01:00
$scope . rpcSettings = ariaNgSettingService . getAllRpcSettings ( ) ;
2016-05-31 16:51:12 +02:00
$scope . isTaskSelected = function ( ) {
return $rootScope . taskContext . getSelectedTaskIds ( ) . length > 0 ;
} ;
2016-05-17 16:15:28 +02:00
2017-10-22 14:03:43 +02:00
$scope . isSingleUrlTaskSelected = function ( ) {
2019-03-27 17:41:10 +01:00
var selectedTasks = $rootScope . taskContext . getSelectedTasks ( ) ;
if ( selectedTasks . length !== 1 ) {
return false ;
}
return ! ! selectedTasks [ 0 ] . singleUrl ;
} ;
$scope . isSingleBittorrentHasInfoHashTaskSelected = function ( ) {
var selectedTasks = $rootScope . taskContext . getSelectedTasks ( ) ;
2017-10-22 14:03:43 +02:00
2019-03-27 17:41:10 +01:00
if ( selectedTasks . length !== 1 ) {
2017-10-22 14:03:43 +02:00
return false ;
}
2019-03-27 17:41:10 +01:00
return ! ! selectedTasks [ 0 ] . bittorrent && ! ! selectedTasks [ 0 ] . infoHash ;
2017-10-22 14:03:43 +02:00
} ;
2016-06-04 10:52:24 +02:00
$scope . isSpecifiedTaskSelected = function ( ) {
2016-05-31 16:51:12 +02:00
var selectedTasks = $rootScope . taskContext . getSelectedTasks ( ) ;
2016-05-30 19:26:41 +02:00
2016-05-31 16:51:12 +02:00
if ( selectedTasks . length < 1 ) {
return false ;
2016-05-30 19:26:41 +02:00
}
2016-05-31 16:51:12 +02:00
for ( var i = 0 ; i < selectedTasks . length ; i ++ ) {
2016-06-04 10:52:24 +02:00
for ( var j = 0 ; j < arguments . length ; j ++ ) {
2016-08-01 16:49:16 +02:00
if ( selectedTasks [ i ] . status === arguments [ j ] ) {
2016-06-04 10:52:24 +02:00
return true ;
}
}
}
return false ;
} ;
$scope . isSpecifiedTaskShowing = function ( ) {
var tasks = $rootScope . taskContext . list ;
if ( tasks . length < 1 ) {
return false ;
}
for ( var i = 0 ; i < tasks . length ; i ++ ) {
for ( var j = 0 ; j < arguments . length ; j ++ ) {
2016-08-01 16:49:16 +02:00
if ( tasks [ i ] . status === arguments [ j ] ) {
2016-06-04 10:52:24 +02:00
return true ;
}
2016-05-30 19:26:41 +02:00
}
2016-05-31 16:51:12 +02:00
}
return false ;
2016-05-30 19:26:41 +02:00
} ;
2016-05-31 16:51:12 +02:00
$scope . changeTasksState = function ( state ) {
2016-05-30 19:26:41 +02:00
var gids = $rootScope . taskContext . getSelectedTaskIds ( ) ;
if ( ! gids || gids . length < 1 ) {
return ;
}
2016-05-31 16:51:12 +02:00
var invoke = null ;
2016-08-01 16:49:16 +02:00
if ( state === 'start' ) {
2016-06-01 16:05:36 +02:00
invoke = aria2TaskService . startTasks ;
2016-08-01 16:49:16 +02:00
} else if ( state === 'pause' ) {
2016-06-01 16:05:36 +02:00
invoke = aria2TaskService . pauseTasks ;
2016-05-31 16:51:12 +02:00
} else {
return ;
}
2016-06-10 13:23:16 +02:00
$rootScope . loadPromise = invoke ( gids , function ( response ) {
if ( response . hasError && gids . length > 1 ) {
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService . showError ( 'Failed to change some tasks state.' ) ;
2016-06-10 13:23:16 +02:00
}
if ( ! response . hasSuccess ) {
return ;
}
2016-06-06 16:56:33 +02:00
refreshGlobalStat ( true ) ;
2016-06-10 13:23:16 +02:00
2016-08-01 16:49:16 +02:00
if ( ! response . hasError && state === 'start' ) {
2016-12-10 18:10:56 +01:00
if ( $location . path ( ) === '/waiting' ) {
2016-06-10 13:23:16 +02:00
$location . path ( '/downloading' ) ;
} else {
$route . reload ( ) ;
}
2016-08-01 16:49:16 +02:00
} else if ( ! response . hasError && state === 'pause' ) {
2016-12-10 18:10:56 +01:00
if ( $location . path ( ) === '/downloading' ) {
2016-06-10 13:23:16 +02:00
$location . path ( '/waiting' ) ;
} else {
$route . reload ( ) ;
}
}
} , ( gids . length > 1 ) ) ;
2016-05-30 19:26:41 +02:00
} ;
2018-11-11 13:53:56 +01:00
$scope . retryTask = function ( task ) {
2018-11-11 15:23:11 +01:00
ariaNgLocalizationService . confirm ( 'Confirm Retry' , 'Are you sure you want to retry the selected task? AriaNg will create same task after clicking OK.' , 'info' , function ( ) {
2018-11-11 13:53:56 +01:00
$rootScope . loadPromise = aria2TaskService . retryTask ( task . gid , function ( response ) {
2016-12-10 17:25:19 +01:00
if ( ! response . success ) {
2018-11-11 13:53:56 +01:00
ariaNgLocalizationService . showError ( 'Failed to retry this task.' ) ;
2016-12-10 17:25:19 +01:00
return ;
}
refreshGlobalStat ( true ) ;
2018-11-11 13:53:56 +01:00
var actionAfterRetryingTask = ariaNgSettingService . getAfterRetryingTask ( ) ;
2018-10-21 17:22:05 +02:00
if ( response . success && response . data ) {
2018-11-11 13:53:56 +01:00
if ( actionAfterRetryingTask === 'task-list-downloading' ) {
2018-10-21 17:22:05 +02:00
if ( $location . path ( ) !== '/downloading' ) {
$location . path ( '/downloading' ) ;
} else {
$route . reload ( ) ;
}
2018-11-11 13:53:56 +01:00
} else if ( actionAfterRetryingTask === 'task-detail' ) {
2018-10-21 17:22:05 +02:00
$location . path ( '/task/detail/' + response . data ) ;
2016-12-10 17:25:19 +01:00
} else {
$route . reload ( ) ;
}
}
} , false ) ;
} ) ;
} ;
2018-11-11 15:23:11 +01:00
$scope . isTaskRetryable = function ( task ) {
return task && task . status === 'error' && task . errorDescription && ! task . bittorrent ;
} ;
$scope . isSelectedTaskRetryable = function ( ) {
var selectedTasks = $rootScope . taskContext . getSelectedTasks ( ) ;
if ( selectedTasks . length < 1 ) {
return false ;
}
for ( var i = 0 ; i < selectedTasks . length ; i ++ ) {
if ( ! $scope . isTaskRetryable ( selectedTasks [ i ] ) ) {
return false ;
}
}
return true ;
} ;
$scope . retryTasks = function ( ) {
var tasks = $rootScope . taskContext . getSelectedTasks ( ) ;
if ( ! tasks || tasks . length < 1 ) {
return ;
} else if ( tasks . length === 1 ) {
return $scope . retryTask ( tasks [ 0 ] ) ;
}
var retryableTasks = [ ] ;
var skipCount = 0 ;
for ( var i = 0 ; i < tasks . length ; i ++ ) {
if ( $scope . isTaskRetryable ( tasks [ i ] ) ) {
retryableTasks . push ( tasks [ i ] ) ;
} else {
skipCount ++ ;
}
}
ariaNgLocalizationService . confirm ( 'Confirm Retry' , 'Are you sure you want to retry the selected task? AriaNg will create same task after clicking OK.' , 'info' , function ( ) {
$rootScope . loadPromise = aria2TaskService . retryTasks ( retryableTasks , function ( response ) {
refreshGlobalStat ( true ) ;
ariaNgLocalizationService . showInfo ( 'Operation Result' , '{{successCount}} tasks have been retried and {{failedCount}} tasks are failed.' , function ( ) {
var actionAfterRetryingTask = ariaNgSettingService . getAfterRetryingTask ( ) ;
if ( response . hasSuccess ) {
if ( actionAfterRetryingTask === 'task-list-downloading' ) {
if ( $location . path ( ) !== '/downloading' ) {
$location . path ( '/downloading' ) ;
} else {
$route . reload ( ) ;
}
} else {
$route . reload ( ) ;
}
}
} , {
textParams : {
successCount : response . successCount ,
failedCount : response . failedCount ,
skipCount : skipCount
}
} ) ;
} , false ) ;
} , true ) ;
} ;
2016-05-31 16:51:12 +02:00
$scope . removeTasks = function ( ) {
2016-06-04 10:52:24 +02:00
var tasks = $rootScope . taskContext . getSelectedTasks ( ) ;
2016-05-30 19:26:41 +02:00
2016-06-04 10:52:24 +02:00
if ( ! tasks || tasks . length < 1 ) {
2016-05-30 19:26:41 +02:00
return ;
}
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService . confirm ( 'Confirm Remove' , 'Are you sure you want to remove the selected task?' , 'warning' , function ( ) {
2016-06-10 13:23:16 +02:00
$rootScope . loadPromise = aria2TaskService . removeTasks ( tasks , function ( response ) {
if ( response . hasError && tasks . length > 1 ) {
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService . showError ( 'Failed to remove some task(s).' ) ;
2016-06-10 13:23:16 +02:00
}
if ( ! response . hasSuccess ) {
return ;
}
2016-06-06 16:56:33 +02:00
refreshGlobalStat ( true ) ;
2016-06-10 13:23:16 +02:00
if ( ! response . hasError ) {
2016-12-10 17:25:19 +01:00
if ( $location . path ( ) !== '/stopped' ) {
2016-06-10 13:23:16 +02:00
$location . path ( '/stopped' ) ;
2016-12-10 17:25:19 +01:00
} else {
$route . reload ( ) ;
2016-06-10 13:23:16 +02:00
}
2016-06-04 11:10:54 +02:00
}
2016-06-10 13:23:16 +02:00
} , ( tasks . length > 1 ) ) ;
2016-05-30 19:26:41 +02:00
} ) ;
} ;
2016-06-04 10:52:24 +02:00
$scope . clearStoppedTasks = function ( ) {
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService . confirm ( 'Confirm Clear' , 'Are you sure you want to clear stopped tasks?' , 'warning' , function ( ) {
2016-06-10 13:23:16 +02:00
$rootScope . loadPromise = aria2TaskService . clearStoppedTasks ( function ( response ) {
if ( ! response . success ) {
return ;
}
2016-06-06 16:56:33 +02:00
refreshGlobalStat ( true ) ;
2016-06-10 13:23:16 +02:00
2016-12-10 17:25:19 +01:00
if ( $location . path ( ) !== '/stopped' ) {
2016-06-04 11:10:54 +02:00
$location . path ( '/stopped' ) ;
2016-12-10 17:25:19 +01:00
} else {
$route . reload ( ) ;
2016-06-04 11:10:54 +02:00
}
2016-06-04 10:52:24 +02:00
} ) ;
2016-05-30 19:26:41 +02:00
} ) ;
} ;
2018-11-10 15:25:32 +01:00
$scope . isAllTasksSelected = function ( ) {
return $rootScope . taskContext . isAllSelected ( ) ;
} ;
2016-05-29 17:27:47 +02:00
$scope . selectAllTasks = function ( ) {
2016-05-30 16:34:15 +02:00
$rootScope . taskContext . selectAll ( ) ;
2016-05-29 17:27:47 +02:00
} ;
2017-10-22 14:03:43 +02:00
$scope . copySelectedOneTaskDownloadLink = function ( ) {
2019-03-27 17:41:10 +01:00
var selectedTasks = $rootScope . taskContext . getSelectedTasks ( ) ;
if ( selectedTasks . length === 1 ) {
clipboard . copyText ( selectedTasks [ 0 ] . singleUrl ) ;
}
} ;
$scope . copySelectedOneTaskMagnetLink = function ( ) {
var selectedTasks = $rootScope . taskContext . getSelectedTasks ( ) ;
2017-10-22 14:03:43 +02:00
2019-03-27 17:41:10 +01:00
if ( selectedTasks . length === 1 ) {
clipboard . copyText ( 'magnet:?xt=urn:btih:' + selectedTasks [ 0 ] . infoHash ) ;
2017-10-22 14:03:43 +02:00
}
} ;
2016-05-22 05:13:05 +02:00
$scope . changeDisplayOrder = function ( type , autoSetReverse ) {
2016-05-31 16:51:12 +02:00
var oldType = ariaNgCommonService . parseOrderType ( ariaNgSettingService . getDisplayOrder ( ) ) ;
var newType = ariaNgCommonService . parseOrderType ( type ) ;
2016-05-22 05:13:05 +02:00
2016-08-01 16:49:16 +02:00
if ( autoSetReverse && newType . type === oldType . type ) {
2016-05-22 05:13:05 +02:00
newType . reverse = ! oldType . reverse ;
}
ariaNgSettingService . setDisplayOrder ( newType . getValue ( ) ) ;
2016-05-17 16:15:28 +02:00
} ;
2016-05-25 18:09:00 +02:00
$scope . isSetDisplayOrder = function ( type ) {
2016-05-31 16:51:12 +02:00
var orderType = ariaNgCommonService . parseOrderType ( ariaNgSettingService . getDisplayOrder ( ) ) ;
var targetType = ariaNgCommonService . parseOrderType ( type ) ;
2016-05-22 05:13:05 +02:00
2016-05-25 18:09:00 +02:00
return orderType . equals ( targetType ) ;
2016-05-17 16:15:28 +02:00
} ;
2017-05-14 16:45:35 +02:00
$scope . showQuickSettingDialog = function ( type , title ) {
$scope . quickSettingContext = {
type : type ,
title : title
} ;
} ;
2017-03-19 16:36:14 +01:00
$scope . switchRpcSetting = function ( setting ) {
if ( setting . isDefault ) {
return ;
}
ariaNgSettingService . setDefaultRpcSetting ( setting ) ;
$window . location . reload ( ) ;
} ;
2016-06-27 17:59:22 +02:00
if ( ariaNgSettingService . getTitleRefreshInterval ( ) > 0 ) {
pageTitleRefreshPromise = $interval ( function ( ) {
refreshPageTitle ( ) ;
} , ariaNgSettingService . getTitleRefreshInterval ( ) ) ;
}
2016-05-17 16:15:28 +02:00
if ( ariaNgSettingService . getGlobalStatRefreshInterval ( ) > 0 ) {
globalStatRefreshPromise = $interval ( function ( ) {
2016-06-04 09:33:40 +02:00
refreshGlobalStat ( true ) ;
2016-05-17 16:15:28 +02:00
} , ariaNgSettingService . getGlobalStatRefreshInterval ( ) ) ;
}
$scope . $on ( '$destroy' , function ( ) {
2016-06-27 17:59:22 +02:00
if ( pageTitleRefreshPromise ) {
$interval . cancel ( pageTitleRefreshPromise ) ;
}
2016-05-17 16:15:28 +02:00
if ( globalStatRefreshPromise ) {
$interval . cancel ( globalStatRefreshPromise ) ;
}
} ) ;
2016-05-31 16:51:12 +02:00
2016-06-27 17:59:22 +02:00
refreshGlobalStat ( true , function ( ) {
refreshPageTitle ( ) ;
} ) ;
2016-05-13 18:09:12 +02:00
} ] ) ;
2016-08-01 16:49:16 +02:00
} ( ) ) ;