settings-ariang.js 19 KB

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