blob: 763862178e07dbc16f77f82f9156f6e410c82c59 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
import { app } from 'electron';
import fs from 'fs';
import path from 'path';
import log from '../shared/logging';
import { getDesktopEntries } from './linux-desktop-entry';
const DESKTOP_FILE_NAME = 'mullvad-vpn.desktop';
export function getOpenAtLogin() {
if (process.platform === 'linux') {
try {
const autostartDir = path.join(app.getPath('appData'), 'autostart');
const autostartFilePath = path.join(autostartDir, DESKTOP_FILE_NAME);
fs.accessSync(autostartFilePath);
return true;
} catch (e) {
const error = e as Error;
log.error(`Failed to check autostart file: ${error.message}`);
return false;
}
} else {
return app.getLoginItemSettings().openAtLogin;
}
}
export async function setOpenAtLogin(openAtLogin: boolean) {
if (process.platform === 'linux') {
try {
const desktopFilePath = await getDesktopEntryPath();
const autostartDir = path.join(app.getPath('appData'), 'autostart');
const autostartFilePath = path.join(autostartDir, DESKTOP_FILE_NAME);
if (openAtLogin) {
await createDirIfNecessary(autostartDir);
await fs.promises.symlink(desktopFilePath, autostartFilePath);
} else {
await fs.promises.unlink(autostartFilePath);
}
} catch (e) {
const error = e as Error;
log.error(`Failed to set auto-start: ${error.message}`);
}
} else {
app.setLoginItemSettings({ openAtLogin });
}
}
async function getDesktopEntryPath(): Promise<string> {
const entries = await getDesktopEntries();
const entry = entries.find((entry) => path.parse(entry).base === DESKTOP_FILE_NAME);
if (entry) {
return entry;
} else {
throw new Error(`Couldn't find ${DESKTOP_FILE_NAME}`);
}
}
const createDirIfNecessary = async (directory: string) => {
let stat;
try {
stat = await fs.promises.stat(directory);
} catch {
// Path doesn't exist, so it has to be created
return fs.promises.mkdir(directory);
}
// Is there a file instead of a directory?
if (!stat.isDirectory()) {
// Try to remove existing file and replace it with a new directory
try {
await fs.promises.unlink(directory);
} catch (e) {
const error = e as Error;
log.error(`Failed to remove path before creating a directory for it: ${error.message}`);
}
return fs.promises.mkdir(directory);
}
};
|