2016-05-13 18:09:12 +02:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
2016-07-13 17:35:23 +02:00
|
|
|
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) {
|
2016-07-22 18:17:47 +02:00
|
|
|
var onFirstVisitCallbacks = [];
|
2016-11-06 15:41:28 +01:00
|
|
|
var sessionSettings = {
|
|
|
|
debugMode: false
|
|
|
|
};
|
2016-07-22 18:17:47 +02:00
|
|
|
|
|
|
|
var fireFirstVisitEvent = function () {
|
|
|
|
if (!angular.isArray(onFirstVisitCallbacks) || onFirstVisitCallbacks.length < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < onFirstVisitCallbacks.length; i++) {
|
|
|
|
var callback = onFirstVisitCallbacks[i];
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-13 17:35:23 +02:00
|
|
|
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, '_');
|
2016-07-13 17:35:23 +02:00
|
|
|
|
|
|
|
if (!ariaNgLanguages[browserLang]) {
|
|
|
|
return ariaNgDefaultOptions.language;
|
|
|
|
}
|
|
|
|
|
|
|
|
return browserLang;
|
|
|
|
};
|
|
|
|
|
2016-05-16 18:59:27 +02:00
|
|
|
var getDefaultRpcHost = function () {
|
2016-08-01 16:18:00 +02:00
|
|
|
var currentHost = $location.$$host;
|
|
|
|
|
|
|
|
if (currentHost) {
|
|
|
|
return currentHost;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ariaNgConstants.defaultHost;
|
2016-05-16 17:41:39 +02:00
|
|
|
};
|
|
|
|
|
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);
|
2016-07-13 17:35:23 +02:00
|
|
|
options.language = getDefaultLanguage();
|
|
|
|
|
2016-05-13 18:09:12 +02:00
|
|
|
setOptions(options);
|
2016-07-22 18:17:47 +02:00
|
|
|
fireFirstVisitEvent();
|
2016-05-13 18:09:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
};
|
|
|
|
|
|
|
|
var getOption = function (key) {
|
2016-05-16 15:32:15 +02:00
|
|
|
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 {
|
2016-05-16 17:41:39 +02:00
|
|
|
getAllOptions: function () {
|
|
|
|
var options = angular.extend({}, ariaNgDefaultOptions, getOptions());
|
|
|
|
|
2016-05-16 18:59:27 +02:00
|
|
|
if (!options.rpcHost) {
|
|
|
|
options.rpcHost = getDefaultRpcHost();
|
2016-05-16 17:41:39 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-05-16 17:41:39 +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;
|
|
|
|
},
|
2016-05-16 17:41:39 +02:00
|
|
|
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();
|
|
|
|
|
2016-06-27 17:59:22 +02:00
|
|
|
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);
|
|
|
|
},
|
2016-06-27 17:59:22 +02:00
|
|
|
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-12-31 16:02:05 +01:00
|
|
|
var rpcInterface = getOption('rpcInterface');
|
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-12-31 16:02:05 +01:00
|
|
|
return protocol + '://' + rpcHost + ':' + rpcPort + '/' + rpcInterface;
|
2016-05-16 15:28:23 +02:00
|
|
|
},
|
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));
|
2016-05-16 17:41:39 +02:00
|
|
|
},
|
2016-12-31 16:02:05 +01:00
|
|
|
setRpcInterface: function (value) {
|
|
|
|
setOption('rpcInterface', value);
|
|
|
|
},
|
2016-05-16 15:28:23 +02:00
|
|
|
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
|
|
|
},
|
2016-12-24 15:31:31 +01:00
|
|
|
getHttpMethod: function () {
|
|
|
|
return getOption('httpMethod');
|
|
|
|
},
|
|
|
|
setHttpMethod: function (value) {
|
|
|
|
setOption('httpMethod', value);
|
|
|
|
},
|
2016-06-29 17:30:01 +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-16 15:28:23 +02:00
|
|
|
|
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-16 15:28:23 +02:00
|
|
|
|
2016-05-13 18:09:12 +02:00
|
|
|
return value;
|
|
|
|
},
|
|
|
|
setDisplayOrder: function (value) {
|
|
|
|
setOption('displayOrder', value);
|
2016-05-25 17:33:06 +02:00
|
|
|
},
|
|
|
|
getFileListDisplayOrder: function () {
|
|
|
|
var value = getOption('fileListDisplayOrder');
|
|
|
|
|
|
|
|
if (!value) {
|
2016-05-25 18:09:00 +02:00
|
|
|
value = 'default:asc';
|
2016-05-25 17:33:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
setFileListDisplayOrder: function (value) {
|
|
|
|
setOption('fileListDisplayOrder', value);
|
2016-06-11 16:24:49 +02:00
|
|
|
},
|
|
|
|
getPeerListDisplayOrder: function () {
|
|
|
|
var value = getOption('peerListDisplayOrder');
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
value = 'default:asc';
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
setPeerListDisplayOrder: function (value) {
|
|
|
|
setOption('peerListDisplayOrder', value);
|
2016-07-22 18:17:47 +02:00
|
|
|
},
|
|
|
|
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
|
|
|
}());
|