This repository has been archived on 2022-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
AriaNg/src/scripts/services/aria2SettingService.js

187 lines
6.6 KiB
JavaScript
Raw Normal View History

2016-05-31 16:51:12 +02:00
(function () {
'use strict';
2016-06-06 16:34:55 +02:00
angular.module('ariaNg').factory('aria2SettingService', ['aria2AllOptions', 'aria2GlobalAvailableOptions', 'aria2TaskAvailableOptions', 'ariaNgCommonService', 'aria2RpcService', function (aria2AllOptions, aria2GlobalAvailableOptions, aria2TaskAvailableOptions, ariaNgCommonService, aria2RpcService) {
2016-05-31 16:51:12 +02:00
var processStatResult = function (stat) {
if (!stat) {
return stat;
}
2016-06-01 18:02:10 +02:00
2016-05-31 16:51:12 +02:00
var activeCount = parseInt(stat.numActive);
var waitingCount = parseInt(stat.numWaiting);
var totalRunningCount = activeCount + waitingCount;
stat.totalRunningCount = totalRunningCount;
2016-06-01 18:02:10 +02:00
2016-05-31 16:51:12 +02:00
return stat;
};
2016-06-01 18:02:10 +02:00
2016-05-31 16:51:12 +02:00
return {
2016-06-01 18:02:10 +02:00
getAvailableGlobalOptionsKeys: function (type) {
2016-05-31 16:51:12 +02:00
if (type == 'basic') {
return aria2GlobalAvailableOptions.basicOptions;
} else if (type == 'http-ftp-sftp') {
return aria2GlobalAvailableOptions.httpFtpSFtpOptions;
} else if (type == 'http') {
return aria2GlobalAvailableOptions.httpOptions;
} else if (type == 'ftp-sftp') {
return aria2GlobalAvailableOptions.ftpSFtpOptions;
} else if (type == 'bt') {
return aria2GlobalAvailableOptions.btOptions;
} else if (type == 'metalink') {
return aria2GlobalAvailableOptions.metalinkOptions;
} else if (type == 'rpc') {
return aria2GlobalAvailableOptions.rpcOptions;
} else if (type == 'advanced') {
return aria2GlobalAvailableOptions.advancedOptions;
} else {
return false;
}
},
2016-06-01 18:02:10 +02:00
getAvailableTaskOptionKeys: function (status, isBittorrent) {
2016-07-03 10:15:47 +02:00
var allOptions = aria2TaskAvailableOptions.taskOptions;
var availableOptions = [];
for (var i = 0; i < allOptions.length; i++) {
var option = allOptions[i];
var optionKey = {
key: option.key
2016-06-06 16:34:55 +02:00
};
2016-07-03 10:15:47 +02:00
if (option.newOnly) {
continue;
}
if (option.httpOnly && isBittorrent) {
continue;
} else if (option.btOnly && !isBittorrent) {
continue;
}
if (option.activeReadonly && status == 'active') {
optionKey.readonly = true;
}
availableOptions.push(optionKey);
2016-06-01 18:02:10 +02:00
}
2016-07-03 10:15:47 +02:00
return availableOptions;
2016-06-01 18:02:10 +02:00
},
getNewTaskOptionKeys: function (isBittorrent) {
2016-07-03 10:15:47 +02:00
var allOptions = aria2TaskAvailableOptions.taskOptions;
var availableOptions = [];
for (var i = 0; i < allOptions.length; i++) {
var option = allOptions[i];
var optionKey = {
key: option.key
};
availableOptions.push(optionKey);
}
return availableOptions;
},
2016-07-03 10:15:47 +02:00
getSpecifiedOptions: function (keys) {
2016-05-31 16:51:12 +02:00
var options = [];
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
2016-07-03 10:15:47 +02:00
var readonly = false;
if (angular.isObject(key)) {
var optionKey = key;
key = optionKey.key;
readonly = !!optionKey.readonly;
}
2016-05-31 16:51:12 +02:00
var option = aria2AllOptions[key];
if (!option) {
continue;
}
option = angular.extend({
key: key,
nameKey: 'options.' + key + '.name',
descriptionKey: 'options.' + key + '.description'
}, option);
2016-06-05 18:49:48 +02:00
if (option.type == 'boolean') {
option.options = ['true', 'false'];
}
2016-06-10 13:23:16 +02:00
2016-07-03 10:15:47 +02:00
if (readonly) {
2016-06-06 16:34:55 +02:00
option.readonly = true;
}
2016-06-05 18:49:48 +02:00
if (option.options) {
var availableOptions = [];
for (var j = 0; j < option.options.length; j++) {
availableOptions.push({
name: 'options.' + option.options[j],
value: option.options[j]
});
}
option.options = availableOptions;
}
2016-05-31 16:51:12 +02:00
options.push(option);
}
return options;
},
2016-06-10 13:23:16 +02:00
getGlobalOption: function (callback, silent) {
2016-05-31 16:51:12 +02:00
return aria2RpcService.getGlobalOption({
2016-06-10 13:23:16 +02:00
silent: !!silent,
2016-05-31 16:51:12 +02:00
callback: callback
});
},
2016-06-10 13:23:16 +02:00
setGlobalOption: function (key, value, callback, silent) {
2016-05-31 16:51:12 +02:00
var data = {};
data[key] = value;
return aria2RpcService.changeGlobalOption({
options: data,
2016-06-10 13:23:16 +02:00
silent: !!silent,
2016-05-31 16:51:12 +02:00
callback: callback
});
},
2016-06-10 13:23:16 +02:00
getAria2Status: function (callback, silent) {
2016-05-31 16:51:12 +02:00
return aria2RpcService.getVersion({
2016-06-10 13:23:16 +02:00
silent: !!silent,
2016-05-31 16:51:12 +02:00
callback: callback
})
},
2016-06-04 09:33:40 +02:00
getGlobalStat: function (callback, silent) {
2016-05-31 16:51:12 +02:00
return aria2RpcService.getGlobalStat({
2016-06-04 09:33:40 +02:00
silent: !!silent,
2016-07-03 11:53:25 +02:00
callback: function (response) {
2016-05-31 16:51:12 +02:00
if (!callback) {
return;
}
2016-07-03 11:53:25 +02:00
var stat = processStatResult(response);
2016-05-31 16:51:12 +02:00
callback(stat);
}
});
},
2016-06-10 13:23:16 +02:00
saveSession: function (callback, silent) {
return aria2RpcService.saveSession({
2016-06-10 13:23:16 +02:00
silent: !!silent,
callback: callback
})
},
2016-06-10 13:23:16 +02:00
shutdown: function (callback, silent) {
return aria2RpcService.shutdown({
2016-06-10 13:23:16 +02:00
silent: !!silent,
callback: callback
})
2016-05-31 16:51:12 +02:00
}
};
}]);
2016-08-01 16:49:16 +02:00
}());