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/app/scripts/services/aria2SettingService.js

159 lines
6.1 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) {
if (status == 'active' && isBittorrent) {
2016-06-06 16:34:55 +02:00
return {
readwrite: aria2TaskAvailableOptions.activeBtTaskOptions,
readonly: aria2TaskAvailableOptions.activeTaskReadonlyOptions
};
2016-06-01 18:02:10 +02:00
} else if (status == 'active' && !isBittorrent) {
2016-06-06 16:34:55 +02:00
return {
readwrite: aria2TaskAvailableOptions.activeNormalTaskOptions,
readonly: aria2TaskAvailableOptions.activeTaskReadonlyOptions
};
2016-06-01 18:02:10 +02:00
} else if ((status == 'waiting' || status == 'paused') && isBittorrent) {
2016-06-06 16:34:55 +02:00
return {
readwrite: aria2TaskAvailableOptions.inactiveBtTaskOptions,
readonly: []
};
2016-06-01 18:02:10 +02:00
} else if ((status == 'waiting' || status == 'paused') && !isBittorrent) {
2016-06-06 16:34:55 +02:00
return {
readwrite: aria2TaskAvailableOptions.inactiveNormalTaskOptions,
readonly: []
};
2016-06-01 18:02:10 +02:00
} else {
return false;
}
},
2016-06-06 16:34:55 +02:00
getSpecifiedOptions: function (keys, readonly) {
2016-05-31 16:51:12 +02:00
var options = [];
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
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-06-06 16:34:55 +02:00
if (!!readonly) {
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-05-31 16:51:12 +02:00
callback: function (result) {
if (!callback) {
return;
}
var stat = processStatResult(result);
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
}
};
}]);
})();