ariaNgNativeElectronService.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. (function () {
  2. 'use strict';
  3. angular.module('ariaNg').factory('ariaNgNativeElectronService', ['$q', 'ariaNgLogService', 'ariaNgLocalizationService', function ($q, ariaNgLogService, ariaNgLocalizationService) {
  4. var electron = angular.isFunction(window.nodeRequire) ? nodeRequire('electron') : {};
  5. var ipcRenderer = electron.ipcRenderer || {};
  6. var onMainProcessEvent = function (channel, callback) {
  7. ipcRenderer.on && ipcRenderer.on(channel, callback);
  8. };
  9. var removeMainProcessEvent = function (channel, callback) {
  10. ipcRenderer.removeListener && ipcRenderer.removeListener(channel, callback);
  11. };
  12. var invokeMainProcessMethod = function (channel, ...args) {
  13. ipcRenderer.send && ipcRenderer.send(channel, ...args);
  14. };
  15. var invokeMainProcessMethodSync = function (channel, ...args) {
  16. if (!ipcRenderer.sendSync) {
  17. return null;
  18. }
  19. return ipcRenderer.sendSync(channel, ...args);
  20. };
  21. var invokeMainProcessMethodAsync = function (channel, ...args) {
  22. if (!ipcRenderer.invoke) {
  23. return null;
  24. }
  25. return ipcRenderer.invoke(channel, ...args);
  26. };
  27. onMainProcessEvent('on-main-log-debug', function (event, msg, obj) {
  28. ariaNgLogService.debug(msg, obj);
  29. });
  30. onMainProcessEvent('on-main-log-info', function (event, msg, obj) {
  31. ariaNgLogService.info(msg, obj);
  32. });
  33. onMainProcessEvent('on-main-log-warn', function (event, msg, obj) {
  34. ariaNgLogService.warn(msg, obj);
  35. });
  36. onMainProcessEvent('on-main-log-error', function (event, msg, obj) {
  37. ariaNgLogService.error(msg, obj);
  38. });
  39. invokeMainProcessMethod('on-render-electron-service-inited');
  40. return {
  41. getRuntimeEnvironment: function () {
  42. return invokeMainProcessMethodSync('render-sync-get-runtime-environment');
  43. },
  44. getVersion: function() {
  45. return invokeMainProcessMethodSync('render-sync-get-global-setting', 'version');
  46. },
  47. getAriaNgVersion: function() {
  48. return invokeMainProcessMethodSync('render-sync-get-global-setting', 'ariaNgVersion');
  49. },
  50. isDevMode: function () {
  51. return invokeMainProcessMethodSync('render-sync-get-global-setting', 'isDevMode');
  52. },
  53. useCustomAppTitle: function () {
  54. return invokeMainProcessMethodSync('render-sync-get-global-setting', 'useCustomAppTitle');
  55. },
  56. getWindowMaximizedAsync: function (callback) {
  57. return invokeMainProcessMethodAsync('render-get-native-window-maximized')
  58. .then(function onReceive(maximized) {
  59. if (callback) {
  60. callback(maximized);
  61. }
  62. });
  63. },
  64. minimizeWindow: function () {
  65. invokeMainProcessMethod('render-minimize-native-window');
  66. },
  67. maximizeOrRestoreWindow: function () {
  68. invokeMainProcessMethod('render-maximize-or-restore-native-window');
  69. },
  70. reload: function () {
  71. invokeMainProcessMethod('render-reload-native-window');
  72. },
  73. exitApp: function () {
  74. invokeMainProcessMethod('render-exit-native-app');
  75. },
  76. showTextboxContextMenu: function (context) {
  77. invokeMainProcessMethod('render-show-textbox-context-menu', context);
  78. },
  79. showSystemNotification: function (context) {
  80. invokeMainProcessMethod('render-show-system-notification', context);
  81. },
  82. setApplicationMenu: function () {
  83. invokeMainProcessMethod('render-update-app-menu-label', {
  84. AboutAriaNgNative: ariaNgLocalizationService.getLocalizedText('menu.AboutAriaNgNative'),
  85. Services: ariaNgLocalizationService.getLocalizedText('menu.Services'),
  86. HideAriaNgNative: ariaNgLocalizationService.getLocalizedText('menu.HideAriaNgNative'),
  87. HideOthers: ariaNgLocalizationService.getLocalizedText('menu.HideOthers'),
  88. ShowAll: ariaNgLocalizationService.getLocalizedText('menu.ShowAll'),
  89. QuitAriaNgNative: ariaNgLocalizationService.getLocalizedText('menu.QuitAriaNgNative'),
  90. Edit: ariaNgLocalizationService.getLocalizedText('menu.Edit'),
  91. Undo: ariaNgLocalizationService.getLocalizedText('menu.Undo'),
  92. Redo: ariaNgLocalizationService.getLocalizedText('menu.Redo'),
  93. Cut: ariaNgLocalizationService.getLocalizedText('menu.Cut'),
  94. Copy: ariaNgLocalizationService.getLocalizedText('menu.Copy'),
  95. Paste: ariaNgLocalizationService.getLocalizedText('menu.Paste'),
  96. Delete: ariaNgLocalizationService.getLocalizedText('menu.Delete'),
  97. SelectAll: ariaNgLocalizationService.getLocalizedText('menu.SelectAll'),
  98. Window: ariaNgLocalizationService.getLocalizedText('menu.Window'),
  99. Minimize: ariaNgLocalizationService.getLocalizedText('menu.Minimize'),
  100. Zoom: ariaNgLocalizationService.getLocalizedText('menu.Zoom'),
  101. BringAllToFront: ariaNgLocalizationService.getLocalizedText('menu.BringAllToFront')
  102. });
  103. },
  104. setTrayMenu: function () {
  105. invokeMainProcessMethod('render-update-tray-menu-label', {
  106. ShowAriaNgNative: ariaNgLocalizationService.getLocalizedText('tray.ShowAriaNgNative'),
  107. Exit: ariaNgLocalizationService.getLocalizedText('tray.Exit')
  108. });
  109. },
  110. setSystemNotificationTemplate: function () {
  111. invokeMainProcessMethod('render-update-system-notification-templates', {
  112. onDownloadComplete: {
  113. title: ariaNgLocalizationService.getLocalizedText('Download Completed')
  114. },
  115. onBtDownloadComplete: {
  116. title: ariaNgLocalizationService.getLocalizedText('BT Download Completed')
  117. },
  118. onDownloadError: {
  119. title: ariaNgLocalizationService.getLocalizedText('Download Error')
  120. },
  121. });
  122. },
  123. setTrayToolTip: function (value) {
  124. invokeMainProcessMethod('render-update-tray-tip', value);
  125. },
  126. setMainWindowLanguage: function () {
  127. this.setApplicationMenu();
  128. this.setTrayMenu();
  129. this.setSystemNotificationTemplate();
  130. },
  131. getNativeConfig: function () {
  132. var config = invokeMainProcessMethodSync('render-sync-get-native-config');
  133. var cfg = {};
  134. for (var key in config) {
  135. if (!config.hasOwnProperty(key)) {
  136. continue;
  137. }
  138. cfg[key] = angular.copy(config[key]);
  139. }
  140. return cfg;
  141. },
  142. setDefaultPosition: function (value) {
  143. invokeMainProcessMethod('render-set-native-config-default-position', value);
  144. },
  145. setMinimizedToTray: function (value) {
  146. invokeMainProcessMethod('render-set-native-config-minimized-to-tray', value);
  147. },
  148. setNotificationSound: function (value) {
  149. invokeMainProcessMethod('render-set-native-config-notification-sound', value);
  150. },
  151. setExecCommandOnStartup: function (value) {
  152. invokeMainProcessMethod('render-set-native-config-exec-command-on-startup', value);
  153. },
  154. setExecCommandArgumentsOnStartup: function (value) {
  155. invokeMainProcessMethod('render-set-native-config-exec-command-arguments-on-startup', value);
  156. },
  157. setExecDetachedCommandOnStartup: function (value) {
  158. invokeMainProcessMethod('render-set-native-config-exec-detached-command-on-startup', value);
  159. },
  160. getLastCheckUpdatesTimeAsync: function (callback) {
  161. return invokeMainProcessMethodAsync('render-get-native-config-last-check-updates-time')
  162. .then(function onReceive(lastCheckUpdatesTime) {
  163. if (callback) {
  164. callback(lastCheckUpdatesTime);
  165. }
  166. });
  167. },
  168. setLastCheckUpdatesTime: function (value) {
  169. invokeMainProcessMethod('render-set-native-config-last-check-updates-time', value);
  170. },
  171. getStartupCommandOutputAsync: function () {
  172. return invokeMainProcessMethodAsync('render-get-startup-command-process-output');
  173. },
  174. requestHttp: function (requestContext) {
  175. var deferred = $q.defer();
  176. invokeMainProcessMethodAsync('render-request-http', requestContext)
  177. .then(function onReceive(result) {
  178. if (result && result.success) {
  179. deferred.resolve(result.response);
  180. } else {
  181. deferred.reject(result.response);
  182. }
  183. }).catch(function onError() {
  184. deferred.reject({});
  185. });
  186. return deferred.promise;
  187. },
  188. createWebSocketClient: function (rpcUrl, options) {
  189. var WebSocketClient = function (rpcUrl, options) {
  190. var openCallback = null;
  191. var closeCallback = null;
  192. var messageCallback = null;
  193. Object.defineProperty(WebSocketClient.prototype, 'readyState', {
  194. get: function get() {
  195. return invokeMainProcessMethodSync('render-get-websocket-readystate');
  196. },
  197. set: function set() {
  198. throw new Error('The \"readyState\" property is readonly.');
  199. }
  200. });
  201. this.send = function (request) {
  202. invokeMainProcessMethod('render-send-websocket-message', {
  203. url: rpcUrl,
  204. data: request
  205. });
  206. };
  207. this.reconnect = function () {
  208. invokeMainProcessMethod('render-reconnect-websocket', rpcUrl, options);
  209. };
  210. this.onOpen = function (callback) {
  211. openCallback = callback;
  212. };
  213. this.onClose = function (callback) {
  214. closeCallback = callback;
  215. };
  216. this.onMessage = function (callback) {
  217. messageCallback = callback;
  218. };
  219. onMainProcessEvent('on-main-websocket-open', function (event, e) {
  220. if (e.url !== rpcUrl) {
  221. ariaNgLogService.debug('[ariaNgNativeElectronService.websocket.onOpen] event dropped, because rpc url not equals, excepted url: ' + rpcUrl + ", actual url: " + e.url);
  222. return;
  223. }
  224. if (angular.isFunction(openCallback)) {
  225. openCallback(e);
  226. }
  227. });
  228. onMainProcessEvent('on-main-websocket-close', function (event, e) {
  229. if (e.url !== rpcUrl) {
  230. ariaNgLogService.debug('[ariaNgNativeElectronService.websocket.onClose] event dropped, because rpc url not equals, excepted url: ' + rpcUrl + ", actual url: " + e.url);
  231. return;
  232. }
  233. if (angular.isFunction(closeCallback)) {
  234. closeCallback(e);
  235. }
  236. });
  237. onMainProcessEvent('on-main-websocket-message', function (event, message) {
  238. if (message.url !== rpcUrl) {
  239. ariaNgLogService.debug('[ariaNgNativeElectronService.websocket.onMessage] event dropped, because rpc url not equals, excepted url: ' + rpcUrl + ", actual url: " + message.url, message.data);
  240. return;
  241. }
  242. if (angular.isFunction(messageCallback)) {
  243. messageCallback(message);
  244. }
  245. });
  246. invokeMainProcessMethod('render-connect-websocket', rpcUrl, options);
  247. };
  248. return new WebSocketClient(rpcUrl, options);
  249. },
  250. openProjectLink: function () {
  251. invokeMainProcessMethod('render-open-external-url', 'https://github.com/mayswind/AriaNg-Native');
  252. },
  253. openProjectReleaseLink: function () {
  254. invokeMainProcessMethod('render-open-external-url', 'https://github.com/mayswind/AriaNg-Native/releases');
  255. },
  256. readPackageFile: function (path) {
  257. return invokeMainProcessMethodSync('render-sync-get-package-file-content', path);
  258. },
  259. getLocalFSExistsAsync: function (fullpath, callback) {
  260. return invokeMainProcessMethodAsync('render-get-localfs-exists', fullpath)
  261. .then(function onReceive(exists) {
  262. if (callback) {
  263. callback(exists);
  264. }
  265. });
  266. },
  267. openFileInDirectory: function (dir, filename) {
  268. invokeMainProcessMethod('render-open-local-directory', dir, filename);
  269. },
  270. showOpenFileDialogAsync: function (filters, callback) {
  271. return invokeMainProcessMethodAsync('render-show-open-file-dialog', filters)
  272. .then(function onReceive(result) {
  273. if (callback) {
  274. callback({
  275. canceled: result.canceled,
  276. filePaths: result.filePaths
  277. });
  278. }
  279. });
  280. },
  281. parseBittorrentInfo: function (data) {
  282. var info = angular.copy(invokeMainProcessMethodSync('render-sync-parse-bittorrent-info', data));
  283. info.type = 'bittorrent';
  284. ariaNgLogService.debug('[ariaNgNativeElectronService.parseBittorrentInfo] bittorrent info', info);
  285. return info;
  286. },
  287. notifyMainProcessViewLoaded: function (locationPath) {
  288. invokeMainProcessMethod('on-render-view-content-loaded', locationPath);
  289. },
  290. notifyMainProcessorNewDropFile: function (message) {
  291. invokeMainProcessMethod('on-render-new-drop-file', message);
  292. },
  293. notifyMainProcessorNewDropText: function (message) {
  294. invokeMainProcessMethod('on-render-new-drop-text', message);
  295. },
  296. onMainWindowMaximize: function (callback) {
  297. onMainProcessEvent('on-main-window-maximized', callback);
  298. },
  299. onMainWindowUnmaximize: function (callback) {
  300. onMainProcessEvent('on-main-window-unmaximized', callback);
  301. },
  302. onMainProcessShowError: function (callback) {
  303. onMainProcessEvent('on-main-show-error', callback);
  304. },
  305. onMainProcessNavigateTo: function (callback) {
  306. onMainProcessEvent('on-main-navigate-to', callback);
  307. },
  308. onMainProcessNewTaskFromFile: function (callback) {
  309. onMainProcessEvent('on-main-new-task-from-file', callback);
  310. },
  311. onMainProcessNewTaskFromText: function (callback) {
  312. onMainProcessEvent('on-main-new-task-from-text', callback);
  313. },
  314. removeMainProcessNewTaskFromFileCallback: function (callback) {
  315. removeMainProcessEvent('on-main-new-task-from-file', callback);
  316. },
  317. removeMainProcessNewTaskFromTextCallback: function (callback) {
  318. removeMainProcessEvent('on-main-new-task-from-text', callback);
  319. }
  320. };
  321. }]);
  322. }());