summaryrefslogtreecommitdiffhomepage
path: root/gui/src/main
diff options
context:
space:
mode:
authorOskar Nyberg <oskar@mullvad.net>2021-04-19 16:17:19 +0200
committerOskar Nyberg <oskar@mullvad.net>2021-04-20 09:54:24 +0200
commitc4f227fcbc3832a520eebc9e518a51ca7c599b9c (patch)
treea7529e934b77f488b17b76b8f129d87f02970fcb /gui/src/main
parent635c3cbc166a57b9a00c622eb62572566430cb38 (diff)
downloadmullvadvpn-c4f227fcbc3832a520eebc9e518a51ca7c599b9c.tar.xz
mullvadvpn-c4f227fcbc3832a520eebc9e518a51ca7c599b9c.zip
Find app desktop entry by searching in XDG_DATA_DIRS
Diffstat (limited to 'gui/src/main')
-rw-r--r--gui/src/main/autostart.ts35
1 files changed, 20 insertions, 15 deletions
diff --git a/gui/src/main/autostart.ts b/gui/src/main/autostart.ts
index f5369b6723..8c77ba3b6a 100644
--- a/gui/src/main/autostart.ts
+++ b/gui/src/main/autostart.ts
@@ -1,16 +1,11 @@
import { app } from 'electron';
-import * as fs from 'fs';
-import * as path from 'path';
-import { promisify } from 'util';
+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';
-const mkdirAsync = promisify(fs.mkdir);
-const statAsync = promisify(fs.stat);
-const symlinkAsync = promisify(fs.symlink);
-const unlinkAsync = promisify(fs.unlink);
-
export function getOpenAtLogin() {
if (process.platform === 'linux') {
try {
@@ -32,15 +27,15 @@ export function getOpenAtLogin() {
export async function setOpenAtLogin(openAtLogin: boolean) {
if (process.platform === 'linux') {
try {
- const desktopFilePath = path.join('/usr/share/applications', DESKTOP_FILE_NAME);
+ 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 symlinkAsync(desktopFilePath, autostartFilePath);
+ await fs.promises.symlink(desktopFilePath, autostartFilePath);
} else {
- await unlinkAsync(autostartFilePath);
+ await fs.promises.unlink(autostartFilePath);
}
} catch (error) {
log.error(`Failed to set auto-start: ${error.message}`);
@@ -50,24 +45,34 @@ export async function setOpenAtLogin(openAtLogin: boolean) {
}
}
+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 statAsync(directory);
+ stat = await fs.promises.stat(directory);
} catch (error) {
// Path doesn't exist, so it has to be created
- return mkdirAsync(directory);
+ 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 unlinkAsync(directory);
+ await fs.promises.unlink(directory);
} catch (error) {
log.error(`Failed to remove path before creating a directory for it: ${error.message}`);
}
- return mkdirAsync(directory);
+ return fs.promises.mkdir(directory);
}
};