From 75d26de5815b6d4f40ddee98b45ddac2fb0a63d3 Mon Sep 17 00:00:00 2001 From: Hinton Date: Fri, 27 Nov 2020 10:58:47 +0100 Subject: [PATCH] Add support for linux autostart --- src/main/messaging.main.ts | 42 ++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/src/main/messaging.main.ts b/src/main/messaging.main.ts index 338244fe..41925bb1 100644 --- a/src/main/messaging.main.ts +++ b/src/main/messaging.main.ts @@ -1,4 +1,6 @@ import { app, ipcMain } from 'electron'; +import * as fs from 'fs'; +import * as path from 'path'; import { Main } from '../main'; @@ -49,14 +51,10 @@ export class MessagingMain { this.main.trayMain.hideToTray(); break; case 'addOpenAtLogin': - if (process.platform !== 'linux') { - app.setLoginItemSettings({openAtLogin: true}); - } + this.addOpenAtLogin(); break; case 'removeOpenAtLogin': - if (process.platform !== 'linux') { - app.setLoginItemSettings({openAtLogin: false}); - } + this.removeOpenAtLogin(); break; default: break; @@ -88,4 +86,36 @@ export class MessagingMain { lockNowTrayMenuItem.enabled = isAuthenticated && !isLocked; } } + + private addOpenAtLogin() { + if (process.platform === 'linux') { + const data = `[Desktop Entry] + Type=Application + Version=${app.getVersion()} + Name=Bitwarden + Comment=Bitwarden startup script + Exec=${app.getPath('exe')} + StartupNotify=false + Terminal=false`; + + fs.writeFileSync(this.linuxStartupFile(), data); + } else { + app.setLoginItemSettings({openAtLogin: true}); + } + } + + private removeOpenAtLogin() { + if (process.platform === 'linux') { + const file = this.linuxStartupFile(); + if (fs.existsSync(file)) { + fs.unlinkSync(file); + } + } else { + app.setLoginItemSettings({openAtLogin: false}); + } + } + + private linuxStartupFile(): string { + return path.join(app.getPath('home'), '.config', 'autostart', 'bitwarden.desktop'); + } }