Skip to content

A full-featured download manager.

feat: auto launch manager

Dr_rOot1876587973f1
diff --git a/src/main/Application.js b/src/main/Application.js
index 256a3e4e..8b67dda5 100644
--- a/src/main/Application.js
+++ b/src/main/Application.js
@@ -8,6 +8,7 @@ import logger from './core/Logger'
import ConfigManager from './core/ConfigManager'
import { setupLocaleManager } from '@/ui/Locale'
import Engine from './core/Engine'
+import AutoLaunchManager from './core/AutoLaunchManager'
import UpdateManager from './core/UpdateManager'
import EnergyManager from './core/EnergyManager'
import ProtocolManager from './core/ProtocolManager'
@@ -49,6 +50,8 @@ export default class Application extends EventEmitter {
this.trayManager = new TrayManager()
+ this.autoLaunchManager = new AutoLaunchManager()
+
this.initThemeManager()
this.energyManager = new EnergyManager()
@@ -58,6 +61,7 @@ export default class Application extends EventEmitter {
this.initProtocolManager()
this.handleCommands()
+
this.handleIpcMessages()
}
@@ -270,6 +274,19 @@ export default class Application extends EventEmitter {
app.exit()
})
+ this.on('application:open-at-login', (openAtLogin) => {
+ console.log('application:open-at-login===>', openAtLogin)
+ if (is.linux()) {
+ return
+ }
+
+ if (openAtLogin) {
+ this.autoLaunchManager.enable()
+ } else {
+ this.autoLaunchManager.disable()
+ }
+ })
+
this.on('application:show', (page) => {
this.show(page)
})
diff --git a/src/main/Launcher.js b/src/main/Launcher.js
index 347a12fa..d007a2a0 100644
--- a/src/main/Launcher.js
+++ b/src/main/Launcher.js
@@ -5,7 +5,11 @@ import is from 'electron-is'
import ExceptionHandler from './core/ExceptionHandler'
import logger from './core/Logger'
import Application from './Application'
-import { parseArgvAsUrl, parseArgvAsFile } from './utils'
+import {
+ splitArgv,
+ parseArgvAsUrl,
+ parseArgvAsFile
+} from './utils'
const EMPTY_STRING = ''
@@ -33,9 +37,9 @@ export default class Launcher extends EventEmitter {
app.quit()
} else {
app.on('second-instance', (event, argv, workingDirectory) => {
- logger.warn('second-instance====>', event, argv, workingDirectory)
+ logger.warn('second-instance====>', argv, workingDirectory)
global.application.showPage('index')
- if (!is.macOS() && argv.length > 1) { // Windows, Linux
+ if (!is.macOS() && argv.length > 1) {
this.handleAppLaunchArgv(argv)
}
})
@@ -47,6 +51,16 @@ export default class Launcher extends EventEmitter {
init () {
this.exceptionHandler = new ExceptionHandler()
+ this.openedAtLogin = is.macOS()
+ ? app.getLoginItemSettings().wasOpenedAtLogin
+ : false
+
+ if (process.argv.length > 1) {
+ this.handleAppLaunchArgv(process.argv)
+ }
+
+ logger.warn('openedAtLogin===>', this.openedAtLogin)
+
this.handleAppEvents()
}
@@ -60,11 +74,12 @@ export default class Launcher extends EventEmitter {
/**
* handleOpenUrl
+ * Event 'open-url' macOS only
* "name": "Motrix Protocol",
* "schemes": ["mo", "motrix"]
*/
handleOpenUrl () {
- if (is.mas()) {
+ if (is.mas() || !is.macOS()) {
return
}
app.on('open-url', (event, url) => {
@@ -77,30 +92,44 @@ export default class Launcher extends EventEmitter {
/**
* handleOpenFile
+ * Event 'open-file' macOS only
* handle open torrent file
*/
handleOpenFile () {
- // macOS
- if (is.macOS()) {
- app.on('open-file', (event, path) => {
- logger.info(`[Motrix] open-file: ${path}`)
- event.preventDefault()
- this.file = path
- this.sendFileToApplication()
- })
- } else if (process.argv.length > 1) { // Windows, Linux
- this.handleAppLaunchArgv(process.argv)
+ if (!is.macOS()) {
+ return
}
+ app.on('open-file', (event, path) => {
+ logger.info(`[Motrix] open-file: ${path}`)
+ event.preventDefault()
+ this.file = path
+ this.sendFileToApplication()
+ })
}
+ /**
+ * handleAppLaunchArgv
+ * For Windows, Linux
+ * @param {array} argv
+ */
handleAppLaunchArgv (argv) {
- const file = parseArgvAsFile(argv)
+ logger.info('handleAppLaunchArgv===>', argv)
+
+ // args: array, extra: map
+ const { args, extra } = splitArgv(argv)
+ logger.info('splitArgv.args===>', args)
+ logger.info('splitArgv.extra===>', extra)
+ if (extra['--opened-at-login'] === '1') {
+ this.openedAtLogin = true
+ }
+
+ const file = parseArgvAsFile(args)
if (file) {
this.file = file
this.sendFileToApplication()
}
- const url = parseArgvAsUrl(argv)
+ const url = parseArgvAsUrl(args)
if (url) {
this.url = url
this.sendUrlToApplication()
@@ -125,7 +154,10 @@ export default class Launcher extends EventEmitter {
app.on('ready', () => {
global.application = new Application()
- global.application.start('index')
+ const { openedAtLogin } = this
+ global.application.start('index', {
+ openedAtLogin
+ })
global.application.on('ready', () => {
this.sendUrlToApplication()
diff --git a/src/main/core/AutoLaunchManager.js b/src/main/core/AutoLaunchManager.js
new file mode 100644
index 00000000..777269bd
--- /dev/null
+++ b/src/main/core/AutoLaunchManager.js
@@ -0,0 +1,30 @@
+import { app } from 'electron'
+
+export default class AutoLaunchManager {
+ enable () {
+ return new Promise((resolve, reject) => {
+ app.setLoginItemSettings({
+ openAtLogin: true,
+ // For Windows
+ args: [
+ '--opened-at-login=1'
+ ]
+ })
+ resolve()
+ })
+ }
+
+ disable () {
+ return new Promise((resolve, reject) => {
+ app.setLoginItemSettings({ openAtLogin: false })
+ resolve()
+ })
+ }
+
+ isEnabled () {
+ return new Promise((resolve, reject) => {
+ const enabled = app.getLoginItemSettings().openAtLogin
+ resolve(enabled)
+ })
+ }
+}
diff --git a/src/main/utils/index.js b/src/main/utils/index.js
index eae545cd..6cc1d68a 100644
--- a/src/main/utils/index.js
+++ b/src/main/utils/index.js
@@ -65,22 +65,47 @@ export function moveAppToApplicationsFolder (errorMsg = '') {
})
}
+export function splitArgv (argv) {
+ const args = []
+ const extra = {}
+ for (const arg of argv) {
+ if (arg.startsWith('--')) {
+ const kv = arg.split('=')
+ const key = kv[0]
+ const value = kv[1] || '1'
+ extra[key] = value
+ continue
+ }
+ args.push(arg)
+ }
+ return { args, extra }
+}
+
export function parseArgvAsUrl (argv) {
let arg = argv[1]
if (!arg) {
return
}
+ if (checkIsSupportedSchema(arg)) {
+ return arg
+ }
+}
+
+export function checkIsSupportedSchema (url = '') {
+ const str = url.toLowerCase()
if (
- arg.toLowerCase().startsWith('mo:') ||
- arg.toLowerCase().startsWith('motrix:') ||
- arg.toLowerCase().startsWith('http:') ||
- arg.toLowerCase().startsWith('https:') ||
- arg.toLowerCase().startsWith('ftp:') ||
- arg.toLowerCase().startsWith('magnet:') ||
- arg.toLowerCase().startsWith('thunder:')
+ str.startsWith('mo:') ||
+ str.startsWith('motrix:') ||
+ str.startsWith('http:') ||
+ str.startsWith('https:') ||
+ str.startsWith('ftp:') ||
+ str.startsWith('magnet:') ||
+ str.startsWith('thunder:')
) {
- return arg
+ return true
+ } else {
+ return false
}
}