Skip to content

A full-featured download manager.

refactor: main index to launcher

Dr_rOotff38d4ea5cde
diff --git a/src/main/Launcher.js b/src/main/Launcher.js
new file mode 100644
index 00000000..8d5066fc
--- /dev/null
+++ b/src/main/Launcher.js
@@ -0,0 +1,145 @@
+import { EventEmitter } from 'events'
+import { app } from 'electron'
+import is from 'electron-is'
+
+import ExceptionHandler from './core/ExceptionHandler'
+import logger from './core/Logger'
+import Application from './Application'
+
+const EMPTY_STRING = ''
+
+export default class Launcher extends EventEmitter {
+ constructor () {
+ super()
+ this.url = EMPTY_STRING
+ this.file = EMPTY_STRING
+
+ this.makeSingleInstance(() => {
+ this.init()
+ })
+ }
+
+ makeSingleInstance (callback) {
+ // Mac App Store Sandboxed App not support requestSingleInstanceLock
+ if (is.mas()) {
+ callback()
+ return
+ }
+
+ const gotSingleLock = app.requestSingleInstanceLock()
+
+ if (!gotSingleLock) {
+ app.quit()
+ } else {
+ app.on('second-instance', (event, commandLine, workingDirectory) => {
+ global.application.showPage('index')
+ })
+
+ callback()
+ }
+ }
+
+ init () {
+ this.exceptionHandler = new ExceptionHandler()
+
+ this.handleAppEvents()
+ }
+
+ handleAppEvents () {
+ this.handleOpenUrl()
+ this.handleOpenFile()
+
+ this.handelAppReady()
+ this.handleAllWindowClosed()
+ this.handleAppWillQuit()
+ }
+
+ /**
+ * handleOpenUrl
+ * "name": "Motrix Protocol",
+ * "schemes": ["mo", "motrix"]
+ */
+ handleOpenUrl () {
+ if (is.mas()) {
+ return
+ }
+ app.on('open-url', (event, url) => {
+ logger.info(`[Motrix] open-url path: ${url}`)
+ event.preventDefault()
+ this.url = url
+ if (this.url && global.application && global.application.isReady) {
+ global.application.handleProtocol(this.url)
+ this.url = EMPTY_STRING
+ }
+ })
+ }
+
+ /**
+ * handleOpenFile [WIP]
+ * handle open torrent file
+ */
+ handleOpenFile () {
+ // macOS
+ if (is.macOS()) {
+ app.on('open-file', (event, path) => {
+ logger.info(`[Motrix] open-file path: ${path}`)
+ event.preventDefault()
+ this.file = path
+ this.sendFileToApplication()
+ })
+ } else if (process.argv.length > 0) { // Windows, Linux
+ this.file = process.argv[1]
+ this.sendFileToApplication()
+ }
+ }
+
+ sendFileToApplication () {
+ if (this.file && global.application && global.application.isReady) {
+ global.application.handleFileAssociation(this.file)
+ this.file = EMPTY_STRING
+ }
+ }
+
+ handelAppReady () {
+ app.on('ready', () => {
+ global.application = new Application()
+
+ global.application.start('index')
+
+ global.application.on('ready', () => {
+ if (this.url) {
+ global.application.handleProtocol(this.url)
+ }
+
+ if (this.file) {
+ global.application.handleFileAssociation(this.file)
+ }
+ })
+ })
+
+ app.on('activate', () => {
+ if (global.application) {
+ logger.info('[Motrix] activate')
+ global.application.showPage('index')
+ }
+ })
+ }
+
+ handleAllWindowClosed () {
+ app.on('window-all-closed', function () {
+ // On OS X it's common NOT to close app even if all windows are closed
+ if (process.platform !== 'darwin') {
+ app.quit()
+ }
+ })
+ }
+
+ handleAppWillQuit () {
+ app.on('will-quit', () => {
+ logger.info('[Motrix] will-quit')
+ if (global.application) {
+ global.application.stop()
+ }
+ })
+ }
+}
diff --git a/src/main/core/Logger.js b/src/main/core/Logger.js
index 8929766b..2c632922 100644
--- a/src/main/core/Logger.js
+++ b/src/main/core/Logger.js
@@ -3,5 +3,6 @@ import logger from 'electron-log'
logger.transports.file.level = is.production() ? 'warn' : 'info'
logger.info('Logger init')
+logger.warn('[Motrix] Logger init')
export default logger
diff --git a/src/main/index.js b/src/main/index.js
index c6b92f45..8ce43df0 100644
--- a/src/main/index.js
+++ b/src/main/index.js
@@ -1,8 +1,7 @@
import { app } from 'electron'
import is from 'electron-is'
-import logger from './core/Logger'
-import Application from './Application'
+import Launcher from './Launcher'
if (process.env.NODE_ENV !== 'development') {
global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
@@ -12,67 +11,8 @@ if (process.env.NODE_ENV !== 'development') {
* Fix Windows notification func
* appId defined in .electron-vue/webpack.main.config.js
*/
-if (process.platform === 'win32') {
+if (is.windows()) {
app.setAppUserModelId(appId)
}
-function _init () {
- let openURL = null
- if (!is.mas()) {
- app.on('open-url', (event, url) => {
- logger.info(`You arrived from: ${url}`)
- event.preventDefault()
- openURL = url
- if (global.application) {
- global.application.handleProtocol(openURL)
- }
- })
- }
-
- app.on('ready', () => {
- global.application = new Application()
- global.application.start('index')
-
- if (openURL) {
- global.application.handleProtocol(openURL)
- }
- })
-
- app.on('will-quit', () => {
- logger.warn('will-quit')
- global.application && global.application.stop()
- })
-
- // Quit when all windows are closed.
- app.on('window-all-closed', function () {
- // On OS X it's common NOT to close app even if all windows are closed
- if (process.platform !== 'darwin') {
- app.quit()
- }
- })
-
- app.on('activate', () => {
- global.application.showPage('index')
- })
-}
-
-function init () {
- // Mac App Store Sandboxed App Not support requestSingleInstanceLock
- if (is.mas()) {
- _init()
- } else {
- const gotSingleLock = app.requestSingleInstanceLock()
-
- if (!gotSingleLock) {
- app.quit()
- } else {
- app.on('second-instance', (event, commandLine, workingDirectory) => {
- global.application.showPage('index')
- })
-
- _init()
- }
- }
-}
-
-init()
+global.launcher = new Launcher()