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/ariaNgSettingService.js

261 lines
9 KiB
JavaScript
Raw Normal View History

2016-05-13 18:09:12 +02:00
(function () {
'use strict';
angular.module('ariaNg').factory('ariaNgSettingService', ['$window', '$location', '$filter', '$translate', 'base64', 'amMoment', 'localStorageService', 'ariaNgConstants', 'ariaNgDefaultOptions', 'ariaNgLanguages', function ($window, $location, $filter, $translate, base64, amMoment, localStorageService, ariaNgConstants, ariaNgDefaultOptions, ariaNgLanguages) {
var onFirstVisitCallbacks = [];
2016-11-06 15:41:28 +01:00
var sessionSettings = {
debugMode: false
};
var fireFirstVisitEvent = function () {
if (!angular.isArray(onFirstVisitCallbacks) || onFirstVisitCallbacks.length < 1) {
return;
}
for (var i = 0; i < onFirstVisitCallbacks.length; i++) {
var callback = onFirstVisitCallbacks[i];
callback();
}
};
var getDefaultLanguage = function () {
var browserLang = $window.navigator.browserLanguage ? $window.navigator.browserLanguage : $window.navigator.language;
if (!browserLang) {
return ariaNgDefaultOptions.language;
}
2016-12-10 18:50:25 +01:00
browserLang = browserLang.replace(/\-/g, '_');
if (!ariaNgLanguages[browserLang]) {
return ariaNgDefaultOptions.language;
}
return browserLang;
};
2016-05-16 18:59:27 +02:00
var getDefaultRpcHost = function () {
var currentHost = $location.$$host;
if (currentHost) {
return currentHost;
}
return ariaNgConstants.defaultHost;
};
2016-05-13 18:09:12 +02:00
var setOptions = function (options) {
2016-05-16 18:59:27 +02:00
return localStorageService.set(ariaNgConstants.optionStorageKey, options);
2016-05-13 18:09:12 +02:00
};
var getOptions = function () {
2016-05-16 18:59:27 +02:00
var options = localStorageService.get(ariaNgConstants.optionStorageKey);
2016-05-13 18:09:12 +02:00
if (!options) {
options = angular.extend({}, ariaNgDefaultOptions);
options.language = getDefaultLanguage();
2016-05-13 18:09:12 +02:00
setOptions(options);
fireFirstVisitEvent();
2016-05-13 18:09:12 +02:00
}
return options;
};
var getOption = function (key) {
var options = getOptions();
if (angular.isUndefined(options[key])) {
options[key] = ariaNgDefaultOptions[key];
setOptions(options);
}
return options[key];
2016-05-13 18:09:12 +02:00
};
var setOption = function (key, value) {
var options = getOptions();
options[key] = value;
setOptions(options);
};
return {
getAllOptions: function () {
var options = angular.extend({}, ariaNgDefaultOptions, getOptions());
2016-05-16 18:59:27 +02:00
if (!options.rpcHost) {
options.rpcHost = getDefaultRpcHost();
}
2016-06-04 09:33:40 +02:00
if (options.secret) {
2016-06-25 04:04:26 +02:00
options.secret = base64.decode(options.secret);
2016-06-04 09:33:40 +02:00
}
return options;
2016-05-13 18:09:12 +02:00
},
2016-11-06 15:41:28 +01:00
getAllSessionOptions: function () {
return angular.copy(sessionSettings);
},
2016-05-26 17:52:34 +02:00
applyLanguage: function (lang) {
if (!ariaNgLanguages[lang]) {
return false;
}
$translate.use(lang);
amMoment.changeLocale(lang);
2016-05-31 16:51:12 +02:00
2016-05-26 17:52:34 +02:00
return true;
},
getLanguage: function () {
return getOption('language');
},
setLanguage: function (value) {
2016-05-26 17:52:34 +02:00
if (this.applyLanguage(value)) {
setOption('language', value);
}
2016-05-13 18:09:12 +02:00
},
2016-11-06 15:41:28 +01:00
isEnableDebugMode: function () {
return sessionSettings.debugMode;
},
setDebugMode: function (value) {
sessionSettings.debugMode = value;
},
2016-06-26 15:31:58 +02:00
getTitle: function () {
return getOption('title');
},
getFinalTitle: function (context) {
var title = this.getTitle();
context = angular.extend({
downloadingCount: 0,
waitingCount: 0,
stoppedCount: 0,
downloadSpeed: 0,
uploadSpeed: 0
}, context);
2016-06-26 15:31:58 +02:00
title = title.replace(/\$\{downloading\}/g, $translate.instant('Downloading') + ': ' + context.downloadingCount);
title = title.replace(/\$\{waiting\}/g, $translate.instant('Waiting') + ': ' + context.waitingCount);
title = title.replace(/\$\{stopped\}/g, $translate.instant('Downloaded / Stopped') + ': ' + context.stoppedCount);
title = title.replace(/\$\{downspeed\}/g, $translate.instant('Download') + ': ' + $filter('readableVolumn')(context.downloadSpeed) + '/s');
title = title.replace(/\$\{upspeed\}/g, $translate.instant('Upload') + ': ' + $filter('readableVolumn')(context.uploadSpeed) + '/s');
title = title.replace(/\$\{title\}/g, ariaNgConstants.title);
return title;
},
setTitle: function (value) {
setOption('title', value);
},
getTitleRefreshInterval: function () {
return getOption('titleRefreshInterval');
},
setTitleRefreshInterval: function (value) {
setOption('titleRefreshInterval', Math.max(parseInt(value), 0));
},
2016-06-26 16:52:06 +02:00
getBrowserNotification: function () {
return getOption('browserNotification');
},
setBrowserNotification: function (value) {
setOption('browserNotification', value);
},
2016-05-16 18:59:27 +02:00
getJsonRpcUrl: function () {
var protocol = getOption('protocol');
var rpcHost = getOption('rpcHost');
var rpcPort = getOption('rpcPort');
2016-05-13 18:09:12 +02:00
if (!rpcHost) {
2016-05-16 18:59:27 +02:00
rpcHost = getDefaultRpcHost();
2016-05-13 18:09:12 +02:00
}
2016-05-16 18:59:27 +02:00
return protocol + '://' + rpcHost + ':' + rpcPort + '/jsonrpc';
},
2016-05-16 18:59:27 +02:00
setRpcHost: function (value) {
setOption('rpcHost', value);
},
setRpcPort: function (value) {
2016-05-22 05:23:08 +02:00
setOption('rpcPort', Math.max(parseInt(value), 0));
},
getProtocol: function () {
return getOption('protocol');
},
setProtocol: function (value) {
2016-05-22 05:23:08 +02:00
setOption('protocol', value);
2016-05-13 18:09:12 +02:00
},
isUseWebSocket: function (protocol) {
if (!protocol) {
protocol = this.getProtocol();
}
2016-08-01 19:26:10 +02:00
return protocol === 'ws' || protocol === 'wss';
2016-06-28 18:18:17 +02:00
},
2016-06-04 09:33:40 +02:00
getSecret: function () {
var value = getOption('secret');
2016-06-25 04:04:26 +02:00
return (value ? base64.decode(value) : value);
2016-06-04 09:33:40 +02:00
},
setSecret: function (value) {
if (value) {
2016-06-25 04:04:26 +02:00
value = base64.encode(value);
2016-06-04 09:33:40 +02:00
}
setOption('secret', value);
},
2016-05-13 18:09:12 +02:00
getGlobalStatRefreshInterval: function () {
return getOption('globalStatRefreshInterval');
},
2016-05-17 16:15:28 +02:00
setGlobalStatRefreshInterval: function (value) {
setOption('globalStatRefreshInterval', Math.max(parseInt(value), 0));
},
2016-05-13 18:09:12 +02:00
getDownloadTaskRefreshInterval: function () {
return getOption('downloadTaskRefreshInterval');
},
2016-05-17 16:15:28 +02:00
setDownloadTaskRefreshInterval: function (value) {
setOption('downloadTaskRefreshInterval', Math.max(parseInt(value), 0));
},
2016-05-13 18:09:12 +02:00
getDisplayOrder: function () {
var value = getOption('displayOrder');
2016-05-13 18:09:12 +02:00
if (!value) {
2016-05-25 18:09:00 +02:00
value = 'default:asc';
2016-05-13 18:09:12 +02:00
}
2016-05-13 18:09:12 +02:00
return value;
},
setDisplayOrder: function (value) {
setOption('displayOrder', value);
},
getFileListDisplayOrder: function () {
var value = getOption('fileListDisplayOrder');
if (!value) {
2016-05-25 18:09:00 +02:00
value = 'default:asc';
}
return value;
},
setFileListDisplayOrder: function (value) {
setOption('fileListDisplayOrder', value);
},
getPeerListDisplayOrder: function () {
var value = getOption('peerListDisplayOrder');
if (!value) {
value = 'default:asc';
}
return value;
},
setPeerListDisplayOrder: function (value) {
setOption('peerListDisplayOrder', value);
},
onFirstAccess: function (callback) {
if (!callback) {
return;
}
onFirstVisitCallbacks.push(callback);
2016-05-13 18:09:12 +02:00
}
};
}]);
2016-08-01 16:49:16 +02:00
}());