settings-ariang.js 23 KB

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