settings-ariang.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. (function () {
  2. 'use strict';
  3. angular.module('ariaNg').controller('AriaNgSettingsController', ['$rootScope', '$scope', '$routeParams', '$window', '$interval', '$timeout', '$filter', 'clipboard', 'ariaNgLanguages', 'ariaNgCommonService', 'ariaNgKeyboardService', 'ariaNgNotificationService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgFileService', 'ariaNgSettingService', 'ariaNgMonitorService', 'ariaNgTitleService', 'aria2SettingService', 'ariaNgVersionService', 'ariaNgNativeElectronService', function ($rootScope, $scope, $routeParams, $window, $interval, $timeout, $filter, clipboard, ariaNgLanguages, ariaNgCommonService, ariaNgKeyboardService, 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. isMacKeyboardLike: ariaNgKeyboardService.isMacKeyboardLike(),
  66. isSupportReconnect: aria2SettingService.canReconnect(),
  67. isSupportBlob: ariaNgFileService.isSupportBlob(),
  68. isSupportDarkMode: ariaNgSettingService.isBrowserSupportDarkMode(),
  69. importSettings: null,
  70. exportSettings: null,
  71. exportSettingsCopied: false
  72. };
  73. $scope.context.titlePreview = getFinalTitle();
  74. $scope.context.showDebugMode = false; // AriaNg Native does not allow to disable debug mode when current is in debug mode
  75. $scope.changeGlobalTab = function () {
  76. $scope.context.currentTab = 'global';
  77. };
  78. $scope.isCurrentGlobalTab = function () {
  79. return $scope.context.currentTab === 'global';
  80. };
  81. $scope.changeRpcTab = function (rpcIndex) {
  82. $scope.context.currentTab = 'rpc' + rpcIndex;
  83. };
  84. $scope.isCurrentRpcTab = function (rpcIndex) {
  85. return $scope.context.currentTab === 'rpc' + rpcIndex;
  86. };
  87. $scope.getCurrentRpcTabIndex = function () {
  88. if ($scope.isCurrentGlobalTab()) {
  89. return -1;
  90. }
  91. return parseInt($scope.context.currentTab.substring(3));
  92. };
  93. $scope.checkUpdate = function () {
  94. return ariaNgVersionService.getTheLatestVersion()
  95. .then(function onSuccess(response) {
  96. ariaNgLogService.debug('[AriaNgSettingsController.checkUpdate] latest version info', response);
  97. if (!response || !response.data || !response.data.tag_name) {
  98. ariaNgLogService.warn('[AriaNgSettingsController.checkUpdate] data format of latest version is invalid', response);
  99. ariaNgCommonService.showError('Failed to get latest version!');
  100. return;
  101. }
  102. var latestVersion = response.data.tag_name;
  103. if (ariaNgVersionService.compareVersion($scope.context.ariaNgNativeVersion, latestVersion) >= 0) {
  104. ariaNgCommonService.showInfo('Check Update', 'You have installed the latest version!');
  105. $scope.context.isCurrentLatestVersion = true;
  106. } else {
  107. ariaNgNativeElectronService.openProjectReleaseLink();
  108. }
  109. }).catch(function onError(response) {
  110. ariaNgLogService.error('[AriaNgSettingsController.checkUpdate] failed to get latest version', response);
  111. ariaNgCommonService.showError('Failed to get latest version!');
  112. });
  113. };
  114. $scope.updateTitlePreview = function () {
  115. $scope.context.titlePreview = getFinalTitle();
  116. };
  117. $rootScope.swipeActions.extendLeftSwipe = function () {
  118. var tabIndex = -1;
  119. if (!$scope.isCurrentGlobalTab()) {
  120. tabIndex = parseInt($scope.getCurrentRpcTabIndex());
  121. }
  122. if (tabIndex < $scope.context.rpcSettings.length - 1) {
  123. $scope.changeRpcTab(tabIndex + 1);
  124. return true;
  125. } else {
  126. return false;
  127. }
  128. };
  129. $rootScope.swipeActions.extendRightSwipe = function () {
  130. var tabIndex = -1;
  131. if (!$scope.isCurrentGlobalTab()) {
  132. tabIndex = parseInt($scope.getCurrentRpcTabIndex());
  133. }
  134. if (tabIndex > 0) {
  135. $scope.changeRpcTab(tabIndex - 1);
  136. return true;
  137. } else if (tabIndex === 0) {
  138. $scope.changeGlobalTab();
  139. return true;
  140. } else {
  141. return false;
  142. }
  143. };
  144. $scope.isSupportNotification = function () {
  145. return ariaNgNotificationService.isSupportBrowserNotification() &&
  146. ariaNgSettingService.isCurrentRpcUseWebSocket($scope.context.settings.protocol);
  147. };
  148. $scope.setLanguage = function (value) {
  149. if (ariaNgSettingService.setLanguage(value)) {
  150. ariaNgLocalizationService.applyLanguage(value);
  151. }
  152. $scope.updateTitlePreview();
  153. };
  154. $scope.setTheme = function (value) {
  155. ariaNgSettingService.setTheme(value);
  156. $rootScope.setTheme(value);
  157. };
  158. $scope.setDebugMode = function (value) {
  159. ariaNgSettingService.setDebugMode(value);
  160. };
  161. $scope.setTitle = function (value) {
  162. ariaNgSettingService.setTitle(value);
  163. };
  164. $scope.setEnableBrowserNotification = function (value) {
  165. ariaNgSettingService.setBrowserNotification(value);
  166. if (value && !ariaNgNotificationService.hasBrowserPermission()) {
  167. ariaNgNotificationService.requestBrowserPermission(function (result) {
  168. if (!result.granted) {
  169. $scope.context.settings.browserNotification = false;
  170. ariaNgCommonService.showError('You have disabled notification in your browser. You should change your browser\'s settings before you enable this function.');
  171. }
  172. });
  173. }
  174. };
  175. $scope.setWebSocketReconnectInterval = function (value) {
  176. setNeedRefreshPage();
  177. ariaNgSettingService.setWebSocketReconnectInterval(value);
  178. };
  179. $scope.setTitleRefreshInterval = function (value) {
  180. setNeedRefreshPage();
  181. ariaNgSettingService.setTitleRefreshInterval(value);
  182. };
  183. $scope.setGlobalStatRefreshInterval = function (value) {
  184. setNeedRefreshPage();
  185. ariaNgSettingService.setGlobalStatRefreshInterval(value);
  186. };
  187. $scope.setDownloadTaskRefreshInterval = function (value) {
  188. setNeedRefreshPage();
  189. ariaNgSettingService.setDownloadTaskRefreshInterval(value);
  190. };
  191. $scope.setRPCListDisplayOrder = function (value) {
  192. setNeedRefreshPage();
  193. ariaNgSettingService.setRPCListDisplayOrder(value);
  194. };
  195. $scope.setKeyboardShortcuts = function (value) {
  196. ariaNgSettingService.setKeyboardShortcuts(value);
  197. };
  198. $scope.setSwipeGesture = function (value) {
  199. ariaNgSettingService.setSwipeGesture(value);
  200. };
  201. $scope.setDragAndDropTasks = function (value) {
  202. ariaNgSettingService.setDragAndDropTasks(value);
  203. };
  204. $scope.setAfterCreatingNewTask = function (value) {
  205. ariaNgSettingService.setAfterCreatingNewTask(value);
  206. };
  207. $scope.setRemoveOldTaskAfterRetrying = function (value) {
  208. ariaNgSettingService.setRemoveOldTaskAfterRetrying(value);
  209. };
  210. $scope.setConfirmTaskRemoval = function (value) {
  211. ariaNgSettingService.setConfirmTaskRemoval(value);
  212. };
  213. $scope.setIncludePrefixWhenCopyingFromTaskDetails = function (value) {
  214. ariaNgSettingService.setIncludePrefixWhenCopyingFromTaskDetails(value);
  215. };
  216. $scope.setShowPiecesInfoInTaskDetailPage = function (value) {
  217. ariaNgSettingService.setShowPiecesInfoInTaskDetailPage(value);
  218. };
  219. $scope.setAfterRetryingTask = function (value) {
  220. ariaNgSettingService.setAfterRetryingTask(value);
  221. };
  222. $scope.setDefaultPosition = function (value) {
  223. ariaNgNativeElectronService.setDefaultPosition(value);
  224. }
  225. $scope.setAfterMainWindowClosed = function (value) {
  226. if (value === 'minimize-to-tray') {
  227. ariaNgNativeElectronService.setMinimizedToTray(true);
  228. } else if (value === 'exit-application') {
  229. ariaNgNativeElectronService.setMinimizedToTray(false);
  230. }
  231. };
  232. $scope.showImportSettingsModal = function () {
  233. $scope.context.importSettings = null;
  234. angular.element('#import-settings-modal').modal();
  235. };
  236. $('#import-settings-modal').on('hide.bs.modal', function (e) {
  237. $scope.context.importSettings = null;
  238. });
  239. $scope.openAriaNgConfigFile = function () {
  240. ariaNgFileService.openFileContent({
  241. scope: $scope,
  242. fileFilter: '.json',
  243. fileType: 'text'
  244. }, function (result) {
  245. $scope.context.importSettings = result.content;
  246. }, function (error) {
  247. ariaNgCommonService.showError(error);
  248. }, angular.element('#import-file-holder'));
  249. };
  250. $scope.importSettings = function (settings) {
  251. var settingsObj = null;
  252. try {
  253. settingsObj = JSON.parse(settings);
  254. } catch (e) {
  255. ariaNgLogService.error('[AriaNgSettingsController.importSettings] parse settings json error', e);
  256. ariaNgCommonService.showError('Invalid settings data format!');
  257. return;
  258. }
  259. if (!angular.isObject(settingsObj) || angular.isArray(settingsObj)) {
  260. ariaNgLogService.error('[AriaNgSettingsController.importSettings] settings json is not object');
  261. ariaNgCommonService.showError('Invalid settings data format!');
  262. return;
  263. }
  264. if (settingsObj) {
  265. ariaNgCommonService.confirm('Confirm Import', 'Are you sure you want to import all settings?', 'warning', function () {
  266. ariaNgSettingService.importAllOptions(settingsObj);
  267. $window.location.reload();
  268. });
  269. }
  270. };
  271. $scope.showExportSettingsModal = function () {
  272. $scope.context.exportSettings = $filter('json')(ariaNgSettingService.exportAllOptions());
  273. $scope.context.exportSettingsCopied = false;
  274. angular.element('#export-settings-modal').modal();
  275. };
  276. $('#export-settings-modal').on('hide.bs.modal', function (e) {
  277. $scope.context.exportSettings = null;
  278. $scope.context.exportSettingsCopied = false;
  279. });
  280. $scope.copyExportSettings = function () {
  281. clipboard.copyText($scope.context.exportSettings, {
  282. container: angular.element('#export-settings-modal')[0]
  283. });
  284. $scope.context.exportSettingsCopied = true;
  285. };
  286. $scope.addNewRpcSetting = function () {
  287. setNeedRefreshPage();
  288. var newRpcSetting = ariaNgSettingService.addNewRpcSetting();
  289. $scope.context.rpcSettings.push(newRpcSetting);
  290. $scope.changeRpcTab($scope.context.rpcSettings.length - 1);
  291. };
  292. $scope.updateRpcSetting = function (setting, field) {
  293. setNeedRefreshPage();
  294. ariaNgSettingService.updateRpcSetting(setting, field);
  295. };
  296. $scope.removeRpcSetting = function (setting) {
  297. var rpcName = (setting.rpcAlias ? setting.rpcAlias : setting.rpcHost + ':' + setting.rpcPort);
  298. ariaNgCommonService.confirm('Confirm Remove', 'Are you sure you want to remove rpc setting "{rpcName}"?', 'warning', function () {
  299. setNeedRefreshPage();
  300. var currentIndex = $scope.getCurrentRpcTabIndex();
  301. var index = $scope.context.rpcSettings.indexOf(setting);
  302. ariaNgSettingService.removeRpcSetting(setting);
  303. $scope.context.rpcSettings.splice(index, 1);
  304. if (currentIndex >= $scope.context.rpcSettings.length) {
  305. $scope.changeRpcTab($scope.context.rpcSettings.length - 1);
  306. } else if (currentIndex <= 0 || currentIndex <= index) {
  307. ; // Do Nothing
  308. } else { // currentIndex > index
  309. $scope.changeRpcTab(currentIndex - 1);
  310. }
  311. }, false, {
  312. textParams: {
  313. rpcName: rpcName
  314. }
  315. });
  316. };
  317. $scope.setDefaultRpcSetting = function (setting) {
  318. if (setting.isDefault) {
  319. return;
  320. }
  321. ariaNgSettingService.setDefaultRpcSetting(setting);
  322. $window.location.reload();
  323. };
  324. $scope.resetSettings = function () {
  325. ariaNgCommonService.confirm('Confirm Reset', 'Are you sure you want to reset all settings?', 'warning', function () {
  326. ariaNgSettingService.resetSettings();
  327. $window.location.reload();
  328. });
  329. };
  330. $scope.clearHistory = function () {
  331. ariaNgCommonService.confirm('Confirm Clear', 'Are you sure you want to clear all settings history?', 'warning', function () {
  332. aria2SettingService.clearSettingsHistorys();
  333. $window.location.reload();
  334. });
  335. };
  336. $scope.reloadApp = function () {
  337. ariaNgNativeElectronService.reload();
  338. };
  339. angular.element('[data-toggle="popover"]').popover();
  340. $rootScope.loadPromise = $timeout(function () {}, 100);
  341. }]);
  342. }());