index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. const remoteMain = require('@electron/remote/main')
  2. remoteMain.initialize()
  3. // Requirements
  4. const { app, BrowserWindow, ipcMain, Menu, shell } = require('electron')
  5. const autoUpdater = require('electron-updater').autoUpdater
  6. const ejse = require('ejs-electron')
  7. const fs = require('fs')
  8. const isDev = require('./app/assets/js/isdev')
  9. const path = require('path')
  10. const semver = require('semver')
  11. const { pathToFileURL } = require('url')
  12. const { AZURE_CLIENT_ID, MSFT_OPCODE, MSFT_REPLY_TYPE, MSFT_ERROR, SHELL_OPCODE } = require('./app/assets/js/ipcconstants')
  13. // Setup auto updater.
  14. function initAutoUpdater(event, data) {
  15. if(data){
  16. autoUpdater.allowPrerelease = true
  17. } else {
  18. // Defaults to true if application version contains prerelease components (e.g. 0.12.1-alpha.1)
  19. // autoUpdater.allowPrerelease = true
  20. }
  21. if(isDev){
  22. autoUpdater.autoInstallOnAppQuit = false
  23. autoUpdater.updateConfigPath = path.join(__dirname, 'dev-app-update.yml')
  24. }
  25. if(process.platform === 'darwin'){
  26. autoUpdater.autoDownload = false
  27. }
  28. autoUpdater.on('update-available', (info) => {
  29. event.sender.send('autoUpdateNotification', 'update-available', info)
  30. })
  31. autoUpdater.on('update-downloaded', (info) => {
  32. event.sender.send('autoUpdateNotification', 'update-downloaded', info)
  33. })
  34. autoUpdater.on('update-not-available', (info) => {
  35. event.sender.send('autoUpdateNotification', 'update-not-available', info)
  36. })
  37. autoUpdater.on('checking-for-update', () => {
  38. event.sender.send('autoUpdateNotification', 'checking-for-update')
  39. })
  40. autoUpdater.on('error', (err) => {
  41. event.sender.send('autoUpdateNotification', 'realerror', err)
  42. })
  43. }
  44. // Open channel to listen for update actions.
  45. ipcMain.on('autoUpdateAction', (event, arg, data) => {
  46. switch(arg){
  47. case 'initAutoUpdater':
  48. console.log('Initializing auto updater.')
  49. initAutoUpdater(event, data)
  50. event.sender.send('autoUpdateNotification', 'ready')
  51. break
  52. case 'checkForUpdate':
  53. autoUpdater.checkForUpdates()
  54. .catch(err => {
  55. event.sender.send('autoUpdateNotification', 'realerror', err)
  56. })
  57. break
  58. case 'allowPrereleaseChange':
  59. if(!data){
  60. const preRelComp = semver.prerelease(app.getVersion())
  61. if(preRelComp != null && preRelComp.length > 0){
  62. autoUpdater.allowPrerelease = true
  63. } else {
  64. autoUpdater.allowPrerelease = data
  65. }
  66. } else {
  67. autoUpdater.allowPrerelease = data
  68. }
  69. break
  70. case 'installUpdateNow':
  71. autoUpdater.quitAndInstall()
  72. break
  73. default:
  74. console.log('Unknown argument', arg)
  75. break
  76. }
  77. })
  78. // Redirect distribution index event from preloader to renderer.
  79. ipcMain.on('distributionIndexDone', (event, res) => {
  80. event.sender.send('distributionIndexDone', res)
  81. })
  82. // Handle trash item.
  83. ipcMain.handle(SHELL_OPCODE.TRASH_ITEM, async (event, ...args) => {
  84. try {
  85. await shell.trashItem(args[0])
  86. return {
  87. result: true
  88. }
  89. } catch(error) {
  90. return {
  91. result: false,
  92. error: error
  93. }
  94. }
  95. })
  96. // Disable hardware acceleration.
  97. // https://electronjs.org/docs/tutorial/offscreen-rendering
  98. app.disableHardwareAcceleration()
  99. const REDIRECT_URI_PREFIX = 'https://login.microsoftonline.com/common/oauth2/nativeclient?'
  100. // Microsoft Auth Login
  101. let msftAuthWindow
  102. let msftAuthSuccess
  103. let msftAuthViewSuccess
  104. let msftAuthViewOnClose
  105. ipcMain.on(MSFT_OPCODE.OPEN_LOGIN, (ipcEvent, ...arguments_) => {
  106. if (msftAuthWindow) {
  107. ipcEvent.reply(MSFT_OPCODE.REPLY_LOGIN, MSFT_REPLY_TYPE.ERROR, MSFT_ERROR.ALREADY_OPEN, msftAuthViewOnClose)
  108. return
  109. }
  110. msftAuthSuccess = false
  111. msftAuthViewSuccess = arguments_[0]
  112. msftAuthViewOnClose = arguments_[1]
  113. msftAuthWindow = new BrowserWindow({
  114. title: 'Microsoft Login',
  115. backgroundColor: '#222222',
  116. width: 520,
  117. height: 600,
  118. frame: true,
  119. icon: getPlatformIcon('SealCircle')
  120. })
  121. msftAuthWindow.on('closed', () => {
  122. msftAuthWindow = undefined
  123. })
  124. msftAuthWindow.on('close', () => {
  125. if(!msftAuthSuccess) {
  126. ipcEvent.reply(MSFT_OPCODE.REPLY_LOGIN, MSFT_REPLY_TYPE.ERROR, MSFT_ERROR.NOT_FINISHED, msftAuthViewOnClose)
  127. }
  128. })
  129. msftAuthWindow.webContents.on('did-navigate', (_, uri) => {
  130. if (uri.startsWith(REDIRECT_URI_PREFIX)) {
  131. let queries = uri.substring(REDIRECT_URI_PREFIX.length).split('#', 1).toString().split('&')
  132. let queryMap = {}
  133. queries.forEach(query => {
  134. const [name, value] = query.split('=')
  135. queryMap[name] = decodeURI(value)
  136. })
  137. ipcEvent.reply(MSFT_OPCODE.REPLY_LOGIN, MSFT_REPLY_TYPE.SUCCESS, queryMap, msftAuthViewSuccess)
  138. msftAuthSuccess = true
  139. msftAuthWindow.close()
  140. msftAuthWindow = null
  141. }
  142. })
  143. msftAuthWindow.removeMenu()
  144. msftAuthWindow.loadURL(`https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?prompt=select_account&client_id=${AZURE_CLIENT_ID}&response_type=code&scope=XboxLive.signin%20offline_access&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient`)
  145. })
  146. // Microsoft Auth Logout
  147. let msftLogoutWindow
  148. let msftLogoutSuccess
  149. let msftLogoutSuccessSent
  150. ipcMain.on(MSFT_OPCODE.OPEN_LOGOUT, (ipcEvent, uuid, isLastAccount) => {
  151. if (msftLogoutWindow) {
  152. ipcEvent.reply(MSFT_OPCODE.REPLY_LOGOUT, MSFT_REPLY_TYPE.ERROR, MSFT_ERROR.ALREADY_OPEN)
  153. return
  154. }
  155. msftLogoutSuccess = false
  156. msftLogoutSuccessSent = false
  157. msftLogoutWindow = new BrowserWindow({
  158. title: 'Microsoft Logout',
  159. backgroundColor: '#222222',
  160. width: 520,
  161. height: 600,
  162. frame: true,
  163. icon: getPlatformIcon('SealCircle')
  164. })
  165. msftLogoutWindow.on('closed', () => {
  166. msftLogoutWindow = undefined
  167. })
  168. msftLogoutWindow.on('close', () => {
  169. if(!msftLogoutSuccess) {
  170. ipcEvent.reply(MSFT_OPCODE.REPLY_LOGOUT, MSFT_REPLY_TYPE.ERROR, MSFT_ERROR.NOT_FINISHED)
  171. } else if(!msftLogoutSuccessSent) {
  172. msftLogoutSuccessSent = true
  173. ipcEvent.reply(MSFT_OPCODE.REPLY_LOGOUT, MSFT_REPLY_TYPE.SUCCESS, uuid, isLastAccount)
  174. }
  175. })
  176. msftLogoutWindow.webContents.on('did-navigate', (_, uri) => {
  177. if(uri.startsWith('https://login.microsoftonline.com/common/oauth2/v2.0/logoutsession')) {
  178. msftLogoutSuccess = true
  179. setTimeout(() => {
  180. if(!msftLogoutSuccessSent) {
  181. msftLogoutSuccessSent = true
  182. ipcEvent.reply(MSFT_OPCODE.REPLY_LOGOUT, MSFT_REPLY_TYPE.SUCCESS, uuid, isLastAccount)
  183. }
  184. if(msftLogoutWindow) {
  185. msftLogoutWindow.close()
  186. msftLogoutWindow = null
  187. }
  188. }, 5000)
  189. }
  190. })
  191. msftLogoutWindow.removeMenu()
  192. msftLogoutWindow.loadURL('https://login.microsoftonline.com/common/oauth2/v2.0/logout')
  193. })
  194. // Keep a global reference of the window object, if you don't, the window will
  195. // be closed automatically when the JavaScript object is garbage collected.
  196. let win
  197. function createWindow() {
  198. win = new BrowserWindow({
  199. width: 980,
  200. height: 552,
  201. icon: getPlatformIcon('SealCircle'),
  202. frame: false,
  203. webPreferences: {
  204. preload: path.join(__dirname, 'app', 'assets', 'js', 'preloader.js'),
  205. nodeIntegration: true,
  206. contextIsolation: false
  207. },
  208. backgroundColor: '#171614'
  209. })
  210. remoteMain.enable(win.webContents)
  211. ejse.data('bkid', Math.floor((Math.random() * fs.readdirSync(path.join(__dirname, 'app', 'assets', 'images', 'backgrounds')).length)))
  212. win.loadURL(pathToFileURL(path.join(__dirname, 'app', 'app.ejs')).toString())
  213. /*win.once('ready-to-show', () => {
  214. win.show()
  215. })*/
  216. win.removeMenu()
  217. win.resizable = true
  218. win.on('closed', () => {
  219. win = null
  220. })
  221. }
  222. function createMenu() {
  223. if(process.platform === 'darwin') {
  224. // Extend default included application menu to continue support for quit keyboard shortcut
  225. let applicationSubMenu = {
  226. label: 'Application',
  227. submenu: [{
  228. label: 'About Application',
  229. selector: 'orderFrontStandardAboutPanel:'
  230. }, {
  231. type: 'separator'
  232. }, {
  233. label: 'Quit',
  234. accelerator: 'Command+Q',
  235. click: () => {
  236. app.quit()
  237. }
  238. }]
  239. }
  240. // New edit menu adds support for text-editing keyboard shortcuts
  241. let editSubMenu = {
  242. label: 'Edit',
  243. submenu: [{
  244. label: 'Undo',
  245. accelerator: 'CmdOrCtrl+Z',
  246. selector: 'undo:'
  247. }, {
  248. label: 'Redo',
  249. accelerator: 'Shift+CmdOrCtrl+Z',
  250. selector: 'redo:'
  251. }, {
  252. type: 'separator'
  253. }, {
  254. label: 'Cut',
  255. accelerator: 'CmdOrCtrl+X',
  256. selector: 'cut:'
  257. }, {
  258. label: 'Copy',
  259. accelerator: 'CmdOrCtrl+C',
  260. selector: 'copy:'
  261. }, {
  262. label: 'Paste',
  263. accelerator: 'CmdOrCtrl+V',
  264. selector: 'paste:'
  265. }, {
  266. label: 'Select All',
  267. accelerator: 'CmdOrCtrl+A',
  268. selector: 'selectAll:'
  269. }]
  270. }
  271. // Bundle submenus into a single template and build a menu object with it
  272. let menuTemplate = [applicationSubMenu, editSubMenu]
  273. let menuObject = Menu.buildFromTemplate(menuTemplate)
  274. // Assign it to the application
  275. Menu.setApplicationMenu(menuObject)
  276. }
  277. }
  278. function getPlatformIcon(filename){
  279. let ext
  280. switch(process.platform) {
  281. case 'win32':
  282. ext = 'ico'
  283. break
  284. case 'darwin':
  285. case 'linux':
  286. default:
  287. ext = 'png'
  288. break
  289. }
  290. return path.join(__dirname, 'app', 'assets', 'images', `${filename}.${ext}`)
  291. }
  292. app.on('ready', createWindow)
  293. app.on('ready', createMenu)
  294. app.on('window-all-closed', () => {
  295. // On macOS it is common for applications and their menu bar
  296. // to stay active until the user quits explicitly with Cmd + Q
  297. if (process.platform !== 'darwin') {
  298. app.quit()
  299. }
  300. })
  301. app.on('activate', () => {
  302. // On macOS it's common to re-create a window in the app when the
  303. // dock icon is clicked and there are no other windows open.
  304. if (win === null) {
  305. createWindow()
  306. }
  307. })