main-process.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. 'use strict';
  2. const electron = require('electron');
  3. const core = require('../core');
  4. const config = require('../config/config');
  5. const menu = require('../components/menu');
  6. const tray = require('../components/tray');
  7. const http = require('../lib/http');
  8. const websocket = require('../lib/websocket');
  9. const localfs = require('../lib/localfs');
  10. const bittorrent = require('../lib/bittorrent');
  11. const shell = electron.shell;
  12. const dialog = electron.dialog;
  13. const ipcMain = electron.ipcMain;
  14. ipcMain.on('render-sync-get-runtime-environment', (event) => {
  15. if (!process || !process.versions) {
  16. return null;
  17. }
  18. var versions = process.versions;
  19. event.returnValue = [
  20. {
  21. name: 'Electron',
  22. value: versions.electron
  23. },
  24. {
  25. name: 'Node.js',
  26. value: versions.node
  27. },
  28. {
  29. name: 'Chrome',
  30. value: versions.chrome
  31. },
  32. {
  33. name: 'V8',
  34. value: versions.v8
  35. }
  36. ];
  37. });
  38. ipcMain.on('render-sync-get-global-setting', (event, key) => {
  39. event.returnValue = global.settings[key];
  40. });
  41. ipcMain.handle('render-get-native-window-maximized', (event) => {
  42. return core.mainWindow.isMaximized();
  43. });
  44. ipcMain.on('render-minimize-native-window', (event) => {
  45. core.mainWindow.minimize();
  46. });
  47. ipcMain.on('render-maximize-or-restore-native-window', (event) => {
  48. if (!core.mainWindow.isMaximized()) {
  49. core.mainWindow.maximize();
  50. } else {
  51. core.mainWindow.unmaximize();
  52. }
  53. });
  54. ipcMain.on('render-reload-native-window', (event) => {
  55. core.mainWindow.reload();
  56. });
  57. ipcMain.on('render-exit-native-app', (event) => {
  58. core.mainWindow.close();
  59. });
  60. ipcMain.on('render-show-textbox-context-menu', (event, context) => {
  61. const contextMenu = menu.getTextboxContentMenu(context);
  62. if (contextMenu) {
  63. contextMenu.popup(core.mainWindow);
  64. }
  65. });
  66. ipcMain.on('render-update-app-menu-label', (event, labels) => {
  67. menu.setApplicationMenu({
  68. labels: labels
  69. });
  70. menu.setTextboxContextMenuTemplate({
  71. labels: labels
  72. });
  73. });
  74. ipcMain.on('render-update-tray-menu-label', (event, labels) => {
  75. tray.setContextMenu({
  76. labels: labels
  77. });
  78. });
  79. ipcMain.on('render-update-tray-tip', (event, tooltip) => {
  80. tray.setToolTip(tooltip);
  81. });
  82. ipcMain.on('render-sync-get-native-config', (event) => {
  83. event.returnValue = {
  84. defaultPosition: config.defaultPosition,
  85. minimizedToTray: config.minimizedToTray,
  86. execCommandOnStartup: config.execCommandOnStartup,
  87. execCommandArgumentsOnStartup: config.execCommandArgumentsOnStartup,
  88. execDetachedCommandOnStartup: config.execDetachedCommandOnStartup
  89. };
  90. });
  91. ipcMain.on('render-set-native-config-default-position', (event, value) => {
  92. config.defaultPosition = value;
  93. config.save('defaultPosition');
  94. });
  95. ipcMain.on('render-set-native-config-minimized-to-tray', (event, value) => {
  96. config.minimizedToTray = !!value;
  97. config.save('minimizedToTray');
  98. });
  99. ipcMain.on('render-set-native-config-exec-command-on-startup', (event, value) => {
  100. config.execCommandOnStartup = value;
  101. config.save('execCommandOnStartup');
  102. });
  103. ipcMain.on('render-set-native-config-exec-command-arguments-on-startup', (event, value) => {
  104. config.execCommandArgumentsOnStartup = value;
  105. config.save('execCommandArgumentsOnStartup');
  106. });
  107. ipcMain.on('render-set-native-config-exec-detached-command-on-startup', (event, value) => {
  108. config.execDetachedCommandOnStartup = value;
  109. config.save('execDetachedCommandOnStartup');
  110. });
  111. ipcMain.handle('render-get-native-config-last-check-updates-time', (event) => {
  112. return config.lastCheckUpdatesTime;
  113. });
  114. ipcMain.on('render-set-native-config-last-check-updates-time', (event, value) => {
  115. config.lastCheckUpdatesTime = value;
  116. config.save('lastCheckUpdatesTime');
  117. });
  118. ipcMain.handle('render-get-startup-command-process-output', (event, requestContext) => {
  119. return core.startupCommandOutput;
  120. });
  121. ipcMain.handle('render-request-http', (event, requestContext) => {
  122. return http.request(requestContext);
  123. });
  124. ipcMain.on('render-connect-websocket', (event, rpcUrl, options) => {
  125. websocket.connect(rpcUrl, options,
  126. function onOpen(context) {
  127. core.mainWindow.webContents.send('on-main-websocket-open', {
  128. url: context.url
  129. });
  130. },
  131. function onClose(context) {
  132. core.mainWindow.webContents.send('on-main-websocket-close', {
  133. url: context.url,
  134. autoReconnect: context.autoReconnect
  135. });
  136. },
  137. function onMessage(context) {
  138. core.mainWindow.webContents.send('on-main-websocket-message', {
  139. success: context.success,
  140. url: context.url,
  141. data: context.message
  142. });
  143. }
  144. );
  145. });
  146. ipcMain.on('render-reconnect-websocket', (event, rpcUrl, options) => {
  147. websocket.reconnect(rpcUrl, options);
  148. });
  149. ipcMain.on('render-send-websocket-message', (event, requestContext) => {
  150. websocket.send(requestContext)
  151. .catch(function () {
  152. core.mainWindow.webContents.send('on-main-websocket-message', {
  153. success: false,
  154. url: requestContext.url,
  155. request: requestContext.data,
  156. data: null
  157. });
  158. });
  159. });
  160. ipcMain.on('render-get-websocket-readystate', (event) => {
  161. event.returnValue = websocket.getReadyState();
  162. });
  163. ipcMain.on('render-open-external-url', (event, url) => {
  164. shell.openExternal(url);
  165. });
  166. ipcMain.on('render-sync-get-package-file-content', (event, path) => {
  167. event.returnValue = localfs.readPackageFile(path);
  168. });
  169. ipcMain.handle('render-get-localfs-exists', (event, fullpath) => {
  170. return localfs.isExists(fullpath);
  171. });
  172. ipcMain.on('render-open-local-directory', (event, dir, filename) => {
  173. let fullpath = localfs.getFullPath(dir, filename);
  174. if (localfs.isExists(fullpath)) {
  175. shell.showItemInFolder(fullpath);
  176. } else {
  177. shell.openItem(dir);
  178. }
  179. });
  180. ipcMain.handle('render-show-open-file-dialog', (event, filters) => {
  181. return dialog.showOpenDialog({
  182. properties: ['openFile'],
  183. filters: filters
  184. });
  185. });
  186. ipcMain.on('render-sync-parse-bittorrent-info', (event, data) => {
  187. event.returnValue = bittorrent.parseBittorrentInfo(data);
  188. });