main-process.js 7.4 KB

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