settings-ariang.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. (function () {
  2. 'use strict';
  3. angular.module('ariaNg').controller('AriaNgSettingsController', ['$rootScope', '$scope', '$routeParams', '$window', '$interval', '$timeout', '$filter', 'clipboard', 'ariaNgLanguages', 'ariaNgCommonService', 'ariaNgNotificationService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgFileService', 'ariaNgSettingService', 'ariaNgMonitorService', 'ariaNgTitleService', 'aria2SettingService', 'ariaNgVersionService', 'ariaNgNativeElectronService', function ($rootScope, $scope, $routeParams, $window, $interval, $timeout, $filter, clipboard, ariaNgLanguages, ariaNgCommonService, ariaNgNotificationService, ariaNgLocalizationService, ariaNgLogService, ariaNgFileService, ariaNgSettingService, ariaNgMonitorService, ariaNgTitleService, aria2SettingService, ariaNgVersionService, ariaNgNativeElectronService) {
  4. var extendType = $routeParams.extendType;
  5. var lastRefreshPageNotification = null;
  6. var getFinalTitle = function () {
  7. return ariaNgTitleService.getFinalTitleByGlobalStat({
  8. globalStat: ariaNgMonitorService.getCurrentGlobalStat(),
  9. currentRpcProfile: getCurrentRPCProfile()
  10. });
  11. };
  12. var getCurrentRPCProfile = function () {
  13. if (!$scope.context || !$scope.context.rpcSettings || $scope.context.rpcSettings.length < 1) {
  14. return null;
  15. }
  16. for (var i = 0; i < $scope.context.rpcSettings.length; i++) {
  17. var rpcSetting = $scope.context.rpcSettings[i];
  18. if (rpcSetting.isDefault) {
  19. return rpcSetting;
  20. }
  21. }
  22. return null;
  23. };
  24. var getNativeSettings = function () {
  25. var originalConfig = ariaNgNativeElectronService.getNativeConfig();
  26. var config = {};
  27. config.defaultPosition = originalConfig.defaultPosition || 'last-position';
  28. if (!originalConfig.minimizedToTray) {
  29. config.afterMainWindowClosed = 'exit-application';
  30. } else {
  31. config.afterMainWindowClosed = 'minimize-to-tray';
  32. }
  33. return config;
  34. };
  35. var setNeedRefreshPage = function () {
  36. if (lastRefreshPageNotification) {
  37. return;
  38. }
  39. lastRefreshPageNotification = ariaNgNotificationService.notifyInPage('', 'Configuration has been modified, please reload the page for the changes to take effect.', {
  40. delay: false,
  41. type: 'info',
  42. templateUrl: 'views/notification-reloadable.html',
  43. onClose: function () {
  44. lastRefreshPageNotification = null;
  45. }
  46. });
  47. };
  48. $scope.context = {
  49. currentTab: 'global',
  50. ariaNgNativeVersion: ariaNgNativeElectronService.getVersion(),
  51. ariaNgVersion: ariaNgNativeElectronService.getAriaNgVersion(),
  52. isCurrentLatestVersion: false,
  53. runtimeEnvironment: ariaNgNativeElectronService.getRuntimeEnvironment(),
  54. runtimeEnvironmentCollapsed: true,
  55. languages: ariaNgLanguages,
  56. titlePreview: getFinalTitle(),
  57. availableTime: ariaNgCommonService.getTimeOptions([1000, 2000, 3000, 5000, 10000, 30000, 60000], true),
  58. trueFalseOptions: [{name: 'Enabled', value: true}, {name: 'Disabled', value: false}],
  59. showRpcSecret: false,
  60. isInsecureProtocolDisabled: ariaNgSettingService.isInsecureProtocolDisabled(),
  61. settings: ariaNgSettingService.getAllOptions(),
  62. nativeSettings: getNativeSettings(),
  63. sessionSettings: ariaNgSettingService.getAllSessionOptions(),
  64. rpcSettings: ariaNgSettingService.getAllRpcSettings(),
  65. isSupportReconnect: aria2SettingService.canReconnect(),
  66. isSupportBlob: ariaNgFileService.isSupportBlob(),
  67. isSupportDarkMode: ariaNgSettingService.isBrowserSupportDarkMode(),
  68. importSettings: null,
  69. exportSettings: null,
  70. exportSettingsCopied: false
  71. };
  72. $scope.context.titlePreview = getFinalTitle();
  73. $scope.context.showDebugMode = $scope.context.sessionSettings.debugMode || extendType === 'debug';
  74. $scope.changeGlobalTab = function () {
  75. $scope.context.currentTab = 'global';
  76. };
  77. $scope.isCurrentGlobalTab = function () {
  78. return $scope.context.currentTab === 'global';
  79. };
  80. $scope.changeRpcTab = function (rpcIndex) {
  81. $scope.context.currentTab = 'rpc' + rpcIndex;
  82. };
  83. $scope.isCurrentRpcTab = function (rpcIndex) {
  84. return $scope.context.currentTab === 'rpc' + rpcIndex;
  85. };
  86. $scope.getCurrentRpcTabIndex = function () {
  87. if ($scope.isCurrentGlobalTab()) {
  88. return -1;
  89. }
  90. return parseInt($scope.context.currentTab.substring(3));
  91. };
  92. $scope.checkUpdate = function () {
  93. return ariaNgVersionService.getTheLatestVersion()
  94. .then(function onSuccess(response) {
  95. ariaNgLogService.debug('[AriaNgSettingsController.checkUpdate] latest version info', response);
  96. if (!response || !response.data || !response.data.tag_name) {
  97. ariaNgLogService.warn('[AriaNgSettingsController.checkUpdate] data format of latest version is invalid', response);
  98. ariaNgCommonService.showError('Failed to get latest version!');
  99. return;
  100. }
  101. var latestVersion = response.data.tag_name;
  102. if (ariaNgVersionService.compareVersion($scope.context.ariaNgNativeVersion, latestVersion) >= 0) {
  103. ariaNgCommonService.showInfo('Check Update', 'You have installed the latest version!');
  104. $scope.context.isCurrentLatestVersion = true;
  105. } else {
  106. ariaNgNativeElectronService.openProjectReleaseLink();
  107. }
  108. }).catch(function onError(response) {
  109. ariaNgLogService.error('[AriaNgSettingsController.checkUpdate] failed to get latest version', response);
  110. ariaNgCommonService.showError('Failed to get latest version!');
  111. });
  112. };
  113. $scope.updateTitlePreview = function () {
  114. $scope.context.titlePreview = getFinalTitle();
  115. };
  116. $rootScope.swipeActions.extendLeftSwipe = function () {
  117. var tabIndex = -1;
  118. if (!$scope.isCurrentGlobalTab()) {
  119. tabIndex = parseInt($scope.getCurrentRpcTabIndex());
  120. }
  121. if (tabIndex < $scope.context.rpcSettings.length - 1) {
  122. $scope.changeRpcTab(tabIndex + 1);
  123. return true;
  124. } else {
  125. return false;
  126. }
  127. };
  128. $rootScope.swipeActions.extendRightSwipe = function () {
  129. var tabIndex = -1;
  130. if (!$scope.isCurrentGlobalTab()) {
  131. tabIndex = parseInt($scope.getCurrentRpcTabIndex());
  132. }
  133. if (tabIndex > 0) {
  134. $scope.changeRpcTab(tabIndex - 1);
  135. return true;
  136. } else if (tabIndex === 0) {
  137. $scope.changeGlobalTab();
  138. return true;
  139. } else {
  140. return false;
  141. }
  142. };
  143. $scope.isSupportNotification = function () {
  144. return ariaNgNotificationService.isSupportBrowserNotification() &&
  145. ariaNgSettingService.isCurrentRpcUseWebSocket($scope.context.settings.protocol);
  146. };
  147. $scope.setLanguage = function (value) {
  148. if (ariaNgSettingService.setLanguage(value)) {
  149. ariaNgLocalizationService.applyLanguage(value);
  150. }
  151. $scope.updateTitlePreview();
  152. };
  153. $scope.setTheme = function (value) {
  154. ariaNgSettingService.setTheme(value);
  155. $rootScope.setTheme(value);
  156. };
  157. $scope.setDebugMode = function (value) {
  158. ariaNgSettingService.setDebugMode(value);
  159. };
  160. $scope.setTitle = function (value) {
  161. ariaNgSettingService.setTitle(value);
  162. };
  163. $scope.setEnableBrowserNotification = function (value) {
  164. ariaNgSettingService.setBrowserNotification(value);
  165. if (value && !ariaNgNotificationService.hasBrowserPermission()) {
  166. ariaNgNotificationService.requestBrowserPermission(function (result) {
  167. if (!result.granted) {
  168. $scope.context.settings.browserNotification = false;
  169. ariaNgCommonService.showError('You have disabled notification in your browser. You should change your browser\'s settings before you enable this function.');
  170. }
  171. });
  172. }
  173. };
  174. $scope.setWebSocketReconnectInterval = function (value) {
  175. setNeedRefreshPage();
  176. ariaNgSettingService.setWebSocketReconnectInterval(value);
  177. };
  178. $scope.setTitleRefreshInterval = function (value) {
  179. setNeedRefreshPage();
  180. ariaNgSettingService.setTitleRefreshInterval(value);
  181. };
  182. $scope.setGlobalStatRefreshInterval = function (value) {
  183. setNeedRefreshPage();
  184. ariaNgSettingService.setGlobalStatRefreshInterval(value);
  185. };
  186. $scope.setDownloadTaskRefreshInterval = function (value) {
  187. setNeedRefreshPage();
  188. ariaNgSettingService.setDownloadTaskRefreshInterval(value);
  189. };
  190. $scope.setRPCListDisplayOrder = function (value) {
  191. setNeedRefreshPage();
  192. ariaNgSettingService.setRPCListDisplayOrder(value);
  193. };
  194. $scope.setKeyboardShortcuts = function (value) {
  195. ariaNgSettingService.setKeyboardShortcuts(value);
  196. };
  197. $scope.setSwipeGesture = function (value) {
  198. ariaNgSettingService.setSwipeGesture(value);
  199. };
  200. $scope.setDragAndDropTasks = function (value) {
  201. ariaNgSettingService.setDragAndDropTasks(value);
  202. };
  203. $scope.setAfterCreatingNewTask = function (value) {
  204. ariaNgSettingService.setAfterCreatingNewTask(value);
  205. };
  206. $scope.setRemoveOldTaskAfterRetrying = function (value) {
  207. ariaNgSettingService.setRemoveOldTaskAfterRetrying(value);
  208. };
  209. $scope.setConfirmTaskRemoval = function (value) {
  210. ariaNgSettingService.setConfirmTaskRemoval(value);
  211. };
  212. $scope.setIncludePrefixWhenCopyingFromTaskDetails = function (value) {
  213. ariaNgSettingService.setIncludePrefixWhenCopyingFromTaskDetails(value);
  214. };
  215. $scope.setShowPiecesInfoInTaskDetailPage = function (value) {
  216. ariaNgSettingService.setShowPiecesInfoInTaskDetailPage(value);
  217. };
  218. $scope.setAfterRetryingTask = function (value) {
  219. ariaNgSettingService.setAfterRetryingTask(value);
  220. };
  221. $scope.setDefaultPosition = function (value) {
  222. ariaNgNativeElectronService.setDefaultPosition(value);
  223. }
  224. $scope.setAfterMainWindowClosed = function (value) {
  225. if (value === 'minimize-to-tray') {
  226. ariaNgNativeElectronService.setMinimizedToTray(true);
  227. } else if (value === 'exit-application') {
  228. ariaNgNativeElectronService.setMinimizedToTray(false);
  229. }
  230. };
  231. $scope.showImportSettingsModal = function () {
  232. $scope.context.importSettings = null;
  233. angular.element('#import-settings-modal').modal();
  234. };
  235. $('#import-settings-modal').on('hide.bs.modal', function (e) {
  236. $scope.context.importSettings = null;
  237. });
  238. $scope.openAriaNgConfigFile = function () {
  239. ariaNgFileService.openFileContent({
  240. scope: $scope,
  241. fileFilter: '.json',
  242. fileType: 'text'
  243. }, function (result) {
  244. $scope.context.importSettings = result.content;
  245. }, function (error) {
  246. ariaNgCommonService.showError(error);
  247. }, angular.element('#import-file-holder'));
  248. };
  249. $scope.importSettings = function (settings) {
  250. var settingsObj = null;
  251. try {
  252. settingsObj = JSON.parse(settings);
  253. } catch (e) {
  254. ariaNgLogService.error('[AriaNgSettingsController.importSettings] parse settings json error', e);
  255. ariaNgCommonService.showError('Invalid settings data format!');
  256. return;
  257. }
  258. if (!angular.isObject(settingsObj) || angular.isArray(settingsObj)) {
  259. ariaNgLogService.error('[AriaNgSettingsController.importSettings] settings json is not object');
  260. ariaNgCommonService.showError('Invalid settings data format!');
  261. return;
  262. }
  263. if (settingsObj) {
  264. ariaNgCommonService.confirm('Confirm Import', 'Are you sure you want to import all settings?', 'warning', function () {
  265. ariaNgSettingService.importAllOptions(settingsObj);
  266. $window.location.reload();
  267. });
  268. }
  269. };
  270. $scope.showExportSettingsModal = function () {
  271. $scope.context.exportSettings = $filter('json')(ariaNgSettingService.exportAllOptions());
  272. $scope.context.exportSettingsCopied = false;
  273. angular.element('#export-settings-modal').modal();
  274. };
  275. $('#export-settings-modal').on('hide.bs.modal', function (e) {
  276. $scope.context.exportSettings = null;
  277. $scope.context.exportSettingsCopied = false;
  278. });
  279. $scope.copyExportSettings = function () {
  280. clipboard.copyText($scope.context.exportSettings, {
  281. container: angular.element('#export-settings-modal')[0]
  282. });
  283. $scope.context.exportSettingsCopied = true;
  284. };
  285. $scope.addNewRpcSetting = function () {
  286. setNeedRefreshPage();
  287. var newRpcSetting = ariaNgSettingService.addNewRpcSetting();
  288. $scope.context.rpcSettings.push(newRpcSetting);
  289. $scope.changeRpcTab($scope.context.rpcSettings.length - 1);
  290. };
  291. $scope.updateRpcSetting = function (setting, field) {
  292. setNeedRefreshPage();
  293. ariaNgSettingService.updateRpcSetting(setting, field);
  294. };
  295. $scope.removeRpcSetting = function (setting) {
  296. var rpcName = (setting.rpcAlias ? setting.rpcAlias : setting.rpcHost + ':' + setting.rpcPort);
  297. ariaNgCommonService.confirm('Confirm Remove', 'Are you sure you want to remove rpc setting "{rpcName}"?', 'warning', function () {
  298. setNeedRefreshPage();
  299. var currentIndex = $scope.getCurrentRpcTabIndex();
  300. var index = $scope.context.rpcSettings.indexOf(setting);
  301. ariaNgSettingService.removeRpcSetting(setting);
  302. $scope.context.rpcSettings.splice(index, 1);
  303. if (currentIndex >= $scope.context.rpcSettings.length) {
  304. $scope.changeRpcTab($scope.context.rpcSettings.length - 1);
  305. } else if (currentIndex <= 0 || currentIndex <= index) {
  306. ; // Do Nothing
  307. } else { // currentIndex > index
  308. $scope.changeRpcTab(currentIndex - 1);
  309. }
  310. }, false, {
  311. textParams: {
  312. rpcName: rpcName
  313. }
  314. });
  315. };
  316. $scope.setDefaultRpcSetting = function (setting) {
  317. if (setting.isDefault) {
  318. return;
  319. }
  320. ariaNgSettingService.setDefaultRpcSetting(setting);
  321. $window.location.reload();
  322. };
  323. $scope.resetSettings = function () {
  324. ariaNgCommonService.confirm('Confirm Reset', 'Are you sure you want to reset all settings?', 'warning', function () {
  325. ariaNgSettingService.resetSettings();
  326. $window.location.reload();
  327. });
  328. };
  329. $scope.clearHistory = function () {
  330. ariaNgCommonService.confirm('Confirm Clear', 'Are you sure you want to clear all settings history?', 'warning', function () {
  331. aria2SettingService.clearSettingsHistorys();
  332. $window.location.reload();
  333. });
  334. };
  335. $scope.reloadApp = function () {
  336. ariaNgNativeElectronService.reload();
  337. };
  338. angular.element('[data-toggle="popover"]').popover();
  339. $rootScope.loadPromise = $timeout(function () {}, 100);
  340. }]);
  341. }());