main-process.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. 'use strict';
  2. const electron = require('electron');
  3. const core = require('../core');
  4. const config = require('../config/config');
  5. const titlebar = require('../components/titlebar');
  6. const menu = require('../components/menu');
  7. const tray = require('../components/tray');
  8. const notification = require('../components/notification');
  9. const http = require('../lib/http');
  10. const websocket = require('../lib/websocket');
  11. const localfs = require('../lib/localfs');
  12. const bittorrent = require('../lib/bittorrent');
  13. const shell = electron.shell;
  14. const nativeTheme = electron.nativeTheme;
  15. const dialog = electron.dialog;
  16. const ipcMain = electron.ipcMain;
  17. ipcMain.on('render-sync-get-runtime-environment', (event) => {
  18. if (!process || !process.versions) {
  19. return null;
  20. }
  21. var versions = process.versions;
  22. event.returnValue = [
  23. {
  24. name: 'Electron',
  25. value: versions.electron
  26. },
  27. {
  28. name: 'Node.js',
  29. value: versions.node
  30. },
  31. {
  32. name: 'Chromium',
  33. value: versions.chrome
  34. },
  35. {
  36. name: 'V8',
  37. value: versions.v8
  38. }
  39. ];
  40. });
  41. ipcMain.on('render-sync-get-global-setting', (event, key) => {
  42. if (key === 'version') {
  43. event.returnValue = core.version;
  44. } else if (key === 'ariaNgVersion') {
  45. event.returnValue = core.ariaNgVersion;
  46. } else if (key === 'buildCommit') {
  47. event.returnValue = core.buildCommit;
  48. } else if (key === 'isDevMode') {
  49. event.returnValue = core.isDevMode;
  50. } else if (key === 'useCustomAppTitle') {
  51. event.returnValue = core.useCustomAppTitle;
  52. } else {
  53. event.returnValue = undefined;
  54. }
  55. });
  56. ipcMain.on('render-set-native-theme', (event, theme) => {
  57. if (theme === 'dark') {
  58. nativeTheme.themeSource = 'dark';
  59. } else if (theme === 'light') {
  60. nativeTheme.themeSource = 'light'
  61. } else {
  62. nativeTheme.themeSource = 'system';
  63. }
  64. });
  65. ipcMain.on('render-set-titlebar-color', (event, titleBarBackgroundColor, titleBarSymbolColor) => {
  66. titlebar.updateWindowTitleBar(titleBarBackgroundColor, titleBarSymbolColor);
  67. });
  68. ipcMain.on('render-reload-native-window', (event) => {
  69. core.mainWindow.reload();
  70. });
  71. ipcMain.on('render-show-textbox-context-menu', (event, context) => {
  72. const contextMenu = menu.getTextboxContentMenu(context);
  73. if (contextMenu) {
  74. contextMenu.popup(core.mainWindow);
  75. }
  76. });
  77. ipcMain.on('render-show-system-notification', (event, context) => {
  78. notification.showNotification(context.title, context.body, context.silent);
  79. });
  80. ipcMain.on('render-update-app-menu-label', (event, labels) => {
  81. menu.setApplicationMenu({
  82. labels: labels
  83. });
  84. menu.setTextboxContextMenuTemplate({
  85. labels: labels
  86. });
  87. });
  88. ipcMain.on('render-update-tray-menu-label', (event, labels) => {
  89. tray.setContextMenu({
  90. labels: labels
  91. });
  92. });
  93. ipcMain.on('render-update-tray-tip', (event, tooltip) => {
  94. tray.setToolTip(tooltip);
  95. });
  96. ipcMain.on('render-sync-get-native-config', (event) => {
  97. event.returnValue = {
  98. defaultPosition: config.defaultPosition,
  99. minimizedToTray: config.minimizedToTray,
  100. execCommandOnStartup: config.execCommandOnStartup,
  101. execCommandArgumentsOnStartup: config.execCommandArgumentsOnStartup,
  102. execDetachedCommandOnStartup: config.execDetachedCommandOnStartup
  103. };
  104. });
  105. ipcMain.on('render-set-native-config-default-position', (event, value) => {
  106. config.defaultPosition = value;
  107. config.save('defaultPosition');
  108. });
  109. ipcMain.on('render-set-native-config-minimized-to-tray', (event, value) => {
  110. config.minimizedToTray = !!value;
  111. config.save('minimizedToTray');
  112. });
  113. ipcMain.on('render-set-native-config-exec-command-on-startup', (event, value) => {
  114. config.execCommandOnStartup = value;
  115. config.save('execCommandOnStartup');
  116. });
  117. ipcMain.on('render-set-native-config-exec-command-arguments-on-startup', (event, value) => {
  118. config.execCommandArgumentsOnStartup = value;
  119. config.save('execCommandArgumentsOnStartup');
  120. });
  121. ipcMain.on('render-set-native-config-exec-detached-command-on-startup', (event, value) => {
  122. config.execDetachedCommandOnStartup = value;
  123. config.save('execDetachedCommandOnStartup');
  124. });
  125. ipcMain.handle('render-get-native-config-last-check-updates-time', (event) => {
  126. return config.lastCheckUpdatesTime;
  127. });
  128. ipcMain.on('render-set-native-config-last-check-updates-time', (event, value) => {
  129. config.lastCheckUpdatesTime = value;
  130. config.save('lastCheckUpdatesTime');
  131. });
  132. ipcMain.handle('render-get-startup-command-process-output', (event, requestContext) => {
  133. return core.startupCommandOutput;
  134. });
  135. ipcMain.handle('render-request-http', (event, requestContext) => {
  136. return http.request(requestContext);
  137. });
  138. ipcMain.on('render-connect-websocket', (event, rpcUrl, options) => {
  139. websocket.connect(rpcUrl, options,
  140. function onOpen(context) {
  141. try {
  142. core.mainWindow.webContents.send('on-main-websocket-open', {
  143. url: context.url
  144. });
  145. } catch (ex) {
  146. // Do Nothing
  147. }
  148. },
  149. function onClose(context) {
  150. try {
  151. core.mainWindow.webContents.send('on-main-websocket-close', {
  152. url: context.url,
  153. autoReconnect: context.autoReconnect
  154. });
  155. } catch (ex) {
  156. // Do Nothing
  157. }
  158. },
  159. function onMessage(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-file-buffer', (event, fullpath) => {
  200. return localfs.getLocalFSFileBuffer(fullpath);
  201. });
  202. ipcMain.handle('render-get-localfs-exists', (event, fullpath) => {
  203. return localfs.isExists(fullpath);
  204. });
  205. ipcMain.on('render-open-local-directory', (event, dir, filename) => {
  206. let fullpath = localfs.getFullPath(dir, filename);
  207. if (localfs.isExists(fullpath)) {
  208. shell.showItemInFolder(fullpath);
  209. } else {
  210. shell.openPath(dir);
  211. }
  212. });
  213. ipcMain.handle('render-show-open-file-dialog', (event, filters) => {
  214. return dialog.showOpenDialog({
  215. properties: ['openFile'],
  216. filters: filters
  217. });
  218. });
  219. ipcMain.on('render-show-dev-tools', (event) => {
  220. core.mainWindow.webContents.openDevTools();
  221. });
  222. ipcMain.on('render-sync-parse-bittorrent-info', (event, data) => {
  223. event.returnValue = bittorrent.parseBittorrentInfo(data);
  224. });