actions.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. const Settings = require('../../settings');
  2. import logger from '../../utils/logger';
  3. import swarmNode from '../../swarmNode';
  4. import { SwarmState } from './reducer';
  5. const swarmLog = logger.create('swarm');
  6. export function syncFlags(argv) {
  7. return { type: '[MAIN]:CLI_FLAGS:SYNC', payload: { cliFlags: argv } };
  8. }
  9. export function syncBuildConfig(key, value) {
  10. return {
  11. type: '[MAIN]:BUILD_CONFIG:SYNC',
  12. payload: { [key]: value }
  13. };
  14. }
  15. export function setLanguage(lang, browserWindow) {
  16. return dispatch => {
  17. dispatch({ type: '[MAIN]:SET_LANGUAGE:START' });
  18. dispatch(setLanguageOnMain(lang));
  19. dispatch(setLanguageOnClient(lang, browserWindow));
  20. dispatch(setAcceptLanguageHeader(lang, browserWindow));
  21. dispatch(resetMenu(lang));
  22. dispatch({ type: '[MAIN]:SET_LANGUAGE:FINISH' });
  23. };
  24. }
  25. export function setLanguageOnMain(lang) {
  26. return dispatch => {
  27. dispatch({ type: '[MAIN]:SET_LANGUAGE_ON_MAIN:START' });
  28. try {
  29. i18n.changeLanguage(lang.substr(0, 5));
  30. dispatch({
  31. type: '[MAIN]:SET_LANGUAGE_ON_MAIN:SUCCESS',
  32. payload: { i18n: lang }
  33. });
  34. } catch (error) {
  35. dispatch({ type: '[MAIN]:SET_LANGUAGE_ON_MAIN:FAILURE', error });
  36. }
  37. };
  38. }
  39. export function setLanguageOnClient(lang, browserWindow) {
  40. return dispatch => {
  41. dispatch({ type: '[MAIN]:SET_LANGUAGE_ON_CLIENT:START' });
  42. try {
  43. browserWindow.webContents.executeJavaScript(
  44. `TAPi18n.setLanguage("${lang}");`
  45. );
  46. dispatch({
  47. type: '[MAIN]:SET_LANGUAGE_ON_CLIENT:SUCCESS',
  48. payload: { i18n: lang }
  49. });
  50. } catch (error) {
  51. dispatch({ type: '[MAIN]:SET_LANGUAGE_ON_CLIENT:FAILURE', error });
  52. }
  53. };
  54. }
  55. export function setAcceptLanguageHeader(lang, browserWindow) {
  56. return dispatch => {
  57. dispatch({ type: '[MAIN]:SET_ACCEPT_LANGUAGE_HEADER:START' });
  58. try {
  59. const session = browserWindow.webContents.session;
  60. session.setUserAgent(session.getUserAgent(), lang);
  61. dispatch({ type: '[MAIN]:SET_ACCEPT_LANGUAGE_HEADER:SUCCESS' });
  62. } catch (error) {
  63. dispatch({ type: '[MAIN]:SET_ACCEPT_LANGUAGE_HEADER:FAILURE', error });
  64. }
  65. };
  66. }
  67. export function resetMenu(lang) {
  68. return dispatch => {
  69. dispatch({ type: '[MAIN]:RESET_MENU:START' });
  70. try {
  71. if (lang) {
  72. Settings.language = lang;
  73. }
  74. const appMenu = require('../../menuItems');
  75. appMenu(global.webviews);
  76. dispatch({ type: '[MAIN]:RESET_MENU:SUCCESS' });
  77. } catch (error) {
  78. dispatch({ type: '[MAIN]:RESET_MENU:FAILURE', error });
  79. }
  80. };
  81. }
  82. export function getLanguage(event) {
  83. return (dispatch, getState) => {
  84. dispatch({ type: '[MAIN]:GET_LANGUAGE:START' });
  85. try {
  86. const i18n = getState().settings.i18n;
  87. event.returnValue = i18n;
  88. dispatch({ type: '[MAIN]:GET_LANGUAGE:SUCCESS', payload: { i18n } });
  89. } catch (error) {
  90. dispatch({ type: '[MAIN]:GET_LANGUAGE:FAILURE', error });
  91. }
  92. };
  93. }
  94. export function toggleSwarm(event) {
  95. return (dispatch, getState) => {
  96. if (
  97. [SwarmState.Enabled, SwarmState.Enabling].includes(
  98. getState().settings.swarmState
  99. )
  100. ) {
  101. dispatch({ type: '[MAIN]:SWARM:STOP' });
  102. try {
  103. swarmNode.on('stopping', () => {
  104. swarmLog.info('Stopping Swarm');
  105. dispatch({ type: '[MAIN]:SWARM:DISABLING' });
  106. });
  107. swarmNode.on('stopped', () => {
  108. swarmLog.info('Swarm stopped');
  109. dispatch({ type: '[MAIN]:SWARM:DISABLED' });
  110. dispatch(resetMenu());
  111. });
  112. swarmNode.stop();
  113. if (getState().settings.swarmEnableOnStart) {
  114. Settings.enableSwarmOnStart = false;
  115. dispatch({ type: '[MAIN]:SWARM:DISABLE_ON_START' });
  116. }
  117. } catch (error) {
  118. dispatch({ type: '[MAIN]:SWARM:FAILURE', error });
  119. swarmLog.error(error);
  120. }
  121. } else {
  122. dispatch({ type: '[MAIN]:SWARM:START' });
  123. try {
  124. swarmNode.on('starting', () => {
  125. swarmLog.info('Starting Swarm');
  126. dispatch({ type: '[MAIN]:SWARM:ENABLING' });
  127. });
  128. swarmNode.on('downloadProgress', progress => {
  129. swarmLog.info(
  130. `Downloading Swarm binary: ${(progress * 100).toFixed(1)}%`
  131. );
  132. });
  133. swarmNode.on('started', () => {
  134. swarmLog.info('Swarm started');
  135. dispatch({ type: '[MAIN]:SWARM:ENABLED' });
  136. dispatch(resetMenu());
  137. });
  138. swarmNode.init();
  139. if (!getState().settings.swarmEnableOnStart) {
  140. Settings.enableSwarmOnStart = true;
  141. dispatch({ type: '[MAIN]:SWARM:ENABLE_ON_START' });
  142. }
  143. } catch (error) {
  144. dispatch({ type: '[MAIN]:SWARM:FAILURE', error });
  145. swarmLog.error(error);
  146. }
  147. }
  148. };
  149. }
  150. export function setSwarmEnableOnStart() {
  151. return { type: '[MAIN]:SWARM:ENABLE_ON_START' };
  152. }