2021-04-24 05:56:56 -04:00
|
|
|
import './errorHandler'
|
2020-04-24 23:34:04 -04:00
|
|
|
import {
|
|
|
|
app,
|
|
|
|
globalShortcut,
|
2020-09-30 05:32:35 -04:00
|
|
|
protocol,
|
2023-04-05 01:02:17 -04:00
|
|
|
Notification,
|
2023-04-05 10:55:35 -04:00
|
|
|
Menu,
|
|
|
|
dialog
|
2020-04-24 23:34:04 -04:00
|
|
|
} from 'electron'
|
|
|
|
import {
|
2021-04-24 00:13:58 -04:00
|
|
|
createProtocol
|
2020-04-24 23:34:04 -04:00
|
|
|
} from 'vue-cli-plugin-electron-builder/lib'
|
2021-04-24 00:13:58 -04:00
|
|
|
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
|
2020-04-24 23:34:04 -04:00
|
|
|
import beforeOpen from '~/main/utils/beforeOpen'
|
|
|
|
import ipcList from '~/main/events/ipcList'
|
|
|
|
import busEventList from '~/main/events/busEventList'
|
2022-10-30 04:08:29 -04:00
|
|
|
import { IRemoteNoticeTriggerHook, IWindowList } from '#/types/enum'
|
2020-04-24 23:34:04 -04:00
|
|
|
import windowManager from 'apis/app/window/windowManager'
|
|
|
|
import {
|
2021-07-26 12:15:11 -04:00
|
|
|
updateShortKeyFromVersion212,
|
|
|
|
migrateGalleryFromVersion230
|
2020-04-24 23:34:04 -04:00
|
|
|
} from '~/main/migrate'
|
|
|
|
import {
|
|
|
|
uploadChoosedFiles,
|
|
|
|
uploadClipboardFiles
|
|
|
|
} from 'apis/app/uploader/apis'
|
|
|
|
import {
|
|
|
|
createTray
|
|
|
|
} from 'apis/app/system'
|
|
|
|
import server from '~/main/server/index'
|
|
|
|
import updateChecker from '~/main/utils/updateChecker'
|
|
|
|
import shortKeyHandler from 'apis/app/shortKey/shortKeyHandler'
|
|
|
|
import { getUploadFiles } from '~/main/utils/handleArgv'
|
2021-07-26 12:15:11 -04:00
|
|
|
import db, { GalleryDB } from '~/main/apis/core/datastore'
|
2020-04-24 23:34:04 -04:00
|
|
|
import bus from '@core/bus'
|
2021-04-23 12:44:49 -04:00
|
|
|
import logger from 'apis/core/picgo/logger'
|
2021-08-01 05:02:54 -04:00
|
|
|
import picgo from 'apis/core/picgo'
|
2022-01-09 21:20:56 -05:00
|
|
|
import fixPath from './fixPath'
|
2023-02-15 10:36:47 -05:00
|
|
|
import { clearTempFolder } from '../manage/utils/common'
|
2022-02-20 08:30:22 -05:00
|
|
|
import { initI18n } from '~/main/utils/handleI18n'
|
2022-10-30 04:08:29 -04:00
|
|
|
import { remoteNoticeHandler } from 'apis/app/remoteNotice'
|
2023-02-15 10:36:47 -05:00
|
|
|
import { manageIpcList } from '../manage/events/ipcList'
|
|
|
|
import getManageApi from '../manage/Main'
|
|
|
|
import UpDownTaskQueue from '../manage/datastore/upDownTaskQueue'
|
2023-04-05 01:02:17 -04:00
|
|
|
import { T } from '~/main/i18n'
|
2023-04-05 10:55:35 -04:00
|
|
|
import { UpdateInfo, autoUpdater } from 'electron-updater'
|
2020-04-24 23:34:04 -04:00
|
|
|
const isDevelopment = process.env.NODE_ENV !== 'production'
|
2021-04-23 12:44:49 -04:00
|
|
|
|
|
|
|
const handleStartUpFiles = (argv: string[], cwd: string) => {
|
|
|
|
const files = getUploadFiles(argv, cwd, logger)
|
|
|
|
if (files === null || files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动
|
|
|
|
if (files === null) {
|
|
|
|
logger.info('cli -> uploading file from clipboard')
|
|
|
|
uploadClipboardFiles()
|
|
|
|
} else {
|
|
|
|
logger.info('cli -> uploading files from cli', ...files.map(item => item.path))
|
|
|
|
const win = windowManager.getAvailableWindow()
|
|
|
|
uploadChoosedFiles(win.webContents, files)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 10:55:35 -04:00
|
|
|
autoUpdater.setFeedURL({
|
|
|
|
provider: 'generic',
|
|
|
|
url: 'https://release.piclist.cn/latest',
|
|
|
|
channel: 'latest'
|
|
|
|
})
|
|
|
|
|
|
|
|
autoUpdater.autoDownload = false
|
|
|
|
|
|
|
|
autoUpdater.on('update-available', (info: UpdateInfo) => {
|
|
|
|
dialog.showMessageBox({
|
|
|
|
type: 'info',
|
|
|
|
title: T('FIND_NEW_VERSION'),
|
|
|
|
buttons: ['Yes', 'No'],
|
|
|
|
message: T('TIPS_FIND_NEW_VERSION', {
|
|
|
|
v: info.version
|
|
|
|
})
|
|
|
|
}).then((result) => {
|
|
|
|
if (result.response === 0) {
|
|
|
|
autoUpdater.downloadUpdate()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
autoUpdater.on('update-downloaded', () => {
|
|
|
|
dialog.showMessageBox({
|
|
|
|
type: 'info',
|
|
|
|
title: T('UPDATE_DOWNLOADED'),
|
|
|
|
buttons: ['Yes', 'No'],
|
|
|
|
message: T('TIPS_UPDATE_DOWNLOADED')
|
|
|
|
}).then((result) => {
|
|
|
|
if (result.response === 0) {
|
|
|
|
autoUpdater.quitAndInstall()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
autoUpdater.on('error', (err) => {
|
|
|
|
dialog.showErrorBox('error', err.message)
|
|
|
|
})
|
|
|
|
|
2020-04-24 23:34:04 -04:00
|
|
|
class LifeCycle {
|
2021-08-01 05:02:54 -04:00
|
|
|
private async beforeReady () {
|
2020-04-24 23:34:04 -04:00
|
|
|
protocol.registerSchemesAsPrivileged([{ scheme: 'picgo', privileges: { secure: true, standard: true } }])
|
2022-01-08 21:11:14 -05:00
|
|
|
// fix the $PATH in macOS & linux
|
2020-04-24 23:34:04 -04:00
|
|
|
fixPath()
|
|
|
|
beforeOpen()
|
2022-02-20 08:30:22 -05:00
|
|
|
initI18n()
|
2020-04-24 23:34:04 -04:00
|
|
|
ipcList.listen()
|
2023-02-15 10:36:47 -05:00
|
|
|
getManageApi()
|
|
|
|
UpDownTaskQueue.getInstance()
|
|
|
|
manageIpcList.listen()
|
2020-04-24 23:34:04 -04:00
|
|
|
busEventList.listen()
|
|
|
|
updateShortKeyFromVersion212(db, db.get('settings.shortKey'))
|
2021-08-01 05:02:54 -04:00
|
|
|
await migrateGalleryFromVersion230(db, GalleryDB.getInstance(), picgo)
|
2020-04-24 23:34:04 -04:00
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2020-04-24 23:34:04 -04:00
|
|
|
private onReady () {
|
2021-08-01 05:02:54 -04:00
|
|
|
const readyFunction = async () => {
|
2021-08-01 02:50:25 -04:00
|
|
|
console.log('on ready')
|
2020-04-24 23:34:04 -04:00
|
|
|
createProtocol('picgo')
|
|
|
|
if (isDevelopment && !process.env.IS_TEST) {
|
|
|
|
// Install Vue Devtools
|
|
|
|
try {
|
2021-04-24 00:13:58 -04:00
|
|
|
await installExtension(VUEJS_DEVTOOLS)
|
2022-01-04 10:40:28 -05:00
|
|
|
} catch (e: any) {
|
2020-04-24 23:34:04 -04:00
|
|
|
console.error('Vue Devtools failed to install:', e.toString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
windowManager.create(IWindowList.TRAY_WINDOW)
|
|
|
|
windowManager.create(IWindowList.SETTING_WINDOW)
|
2023-04-05 01:02:17 -04:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
app.dock.setMenu(
|
|
|
|
Menu.buildFromTemplate([
|
|
|
|
{
|
|
|
|
label: T('OPEN_MAIN_WINDOW'),
|
|
|
|
click () {
|
|
|
|
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)
|
|
|
|
settingWindow!.show()
|
|
|
|
settingWindow!.focus()
|
|
|
|
if (windowManager.has(IWindowList.MINI_WINDOW)) {
|
|
|
|
windowManager.get(IWindowList.MINI_WINDOW)!.hide()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
])
|
|
|
|
)
|
|
|
|
}
|
2020-12-28 20:59:50 -05:00
|
|
|
createTray()
|
2020-04-24 23:34:04 -04:00
|
|
|
db.set('needReload', false)
|
|
|
|
updateChecker()
|
2023-04-05 10:55:35 -04:00
|
|
|
autoUpdater.checkForUpdatesAndNotify()
|
2020-04-24 23:34:04 -04:00
|
|
|
// 不需要阻塞
|
|
|
|
process.nextTick(() => {
|
|
|
|
shortKeyHandler.init()
|
|
|
|
})
|
|
|
|
server.startup()
|
|
|
|
if (process.env.NODE_ENV !== 'development') {
|
2021-04-23 12:44:49 -04:00
|
|
|
handleStartUpFiles(process.argv, process.cwd())
|
2020-04-24 23:34:04 -04:00
|
|
|
}
|
2020-09-30 05:32:35 -04:00
|
|
|
|
2021-08-20 22:42:52 -04:00
|
|
|
if (global.notificationList && global.notificationList?.length > 0) {
|
|
|
|
while (global.notificationList?.length) {
|
2020-09-30 05:32:35 -04:00
|
|
|
const option = global.notificationList.pop()
|
|
|
|
const notice = new Notification(option!)
|
|
|
|
notice.show()
|
|
|
|
}
|
|
|
|
}
|
2022-10-30 04:08:29 -04:00
|
|
|
await remoteNoticeHandler.init()
|
|
|
|
remoteNoticeHandler.triggerHook(IRemoteNoticeTriggerHook.APP_START)
|
2021-08-01 05:02:54 -04:00
|
|
|
}
|
2023-01-06 04:21:27 -05:00
|
|
|
app.whenReady().then(readyFunction)
|
2020-04-24 23:34:04 -04:00
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2020-04-24 23:34:04 -04:00
|
|
|
private onRunning () {
|
|
|
|
app.on('second-instance', (event, commandLine, workingDirectory) => {
|
2021-04-23 12:44:49 -04:00
|
|
|
logger.info('detect second instance')
|
|
|
|
const result = handleStartUpFiles(commandLine, workingDirectory)
|
|
|
|
if (!result) {
|
2020-04-24 23:34:04 -04:00
|
|
|
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
|
|
|
|
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)!
|
|
|
|
if (settingWindow.isMinimized()) {
|
|
|
|
settingWindow.restore()
|
|
|
|
}
|
|
|
|
settingWindow.focus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
app.on('activate', () => {
|
|
|
|
createProtocol('picgo')
|
|
|
|
if (!windowManager.has(IWindowList.TRAY_WINDOW)) {
|
|
|
|
windowManager.create(IWindowList.TRAY_WINDOW)
|
|
|
|
}
|
|
|
|
if (!windowManager.has(IWindowList.SETTING_WINDOW)) {
|
|
|
|
windowManager.create(IWindowList.SETTING_WINDOW)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
app.setLoginItemSettings({
|
|
|
|
openAtLogin: db.get('settings.autoStart') || false
|
|
|
|
})
|
|
|
|
if (process.platform === 'win32') {
|
2023-02-15 10:36:47 -05:00
|
|
|
app.setAppUserModelId('com.kuingsmile.piclist')
|
2020-04-24 23:34:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.XDG_CURRENT_DESKTOP && process.env.XDG_CURRENT_DESKTOP.includes('Unity')) {
|
|
|
|
process.env.XDG_CURRENT_DESKTOP = 'Unity'
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2020-04-24 23:34:04 -04:00
|
|
|
private onQuit () {
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('will-quit', () => {
|
2023-02-15 10:36:47 -05:00
|
|
|
UpDownTaskQueue.getInstance().persist()
|
|
|
|
clearTempFolder()
|
2020-04-24 23:34:04 -04:00
|
|
|
globalShortcut.unregisterAll()
|
|
|
|
bus.removeAllListeners()
|
|
|
|
server.shutdown()
|
|
|
|
})
|
|
|
|
// Exit cleanly on request from parent process in development mode.
|
|
|
|
if (isDevelopment) {
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
process.on('message', data => {
|
|
|
|
if (data === 'graceful-exit') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
process.on('SIGTERM', () => {
|
|
|
|
app.quit()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2021-07-26 12:15:11 -04:00
|
|
|
async launchApp () {
|
2020-04-24 23:34:04 -04:00
|
|
|
const gotTheLock = app.requestSingleInstanceLock()
|
|
|
|
if (!gotTheLock) {
|
|
|
|
app.quit()
|
|
|
|
} else {
|
2021-08-01 05:02:54 -04:00
|
|
|
await this.beforeReady()
|
2020-04-24 23:34:04 -04:00
|
|
|
this.onReady()
|
|
|
|
this.onRunning()
|
|
|
|
this.onQuit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const bootstrap = new LifeCycle()
|
|
|
|
|
|
|
|
export {
|
|
|
|
bootstrap
|
|
|
|
}
|