2018-11-21 18:01:03 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const path = require('path');
|
2019-01-23 02:49:33 +08:00
|
|
|
const {ELECTRON_APP_ROOT_DIR} = require("./resource_dir");
|
2018-11-21 18:01:03 +08:00
|
|
|
const log = require("./log");
|
|
|
|
const os = require('os');
|
|
|
|
const fs = require('fs');
|
2018-11-21 18:19:33 +08:00
|
|
|
const config = require('./config');
|
2018-11-22 07:24:33 +08:00
|
|
|
const utils = require('./utils');
|
2018-11-21 18:01:03 +08:00
|
|
|
|
|
|
|
const template = `[Desktop Entry]
|
|
|
|
Type=Application
|
|
|
|
Name=Trilium Notes
|
2019-01-23 02:49:33 +08:00
|
|
|
Icon=#APP_ROOT_DIR#/icon.png
|
2018-11-21 18:01:03 +08:00
|
|
|
Exec=#EXE_PATH#
|
|
|
|
Categories=Office
|
|
|
|
Terminal=false
|
|
|
|
`;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Installs .desktop icon into standard ~/.local/share/applications directory.
|
|
|
|
* We overwrite this file during every run as it might have been updated.
|
|
|
|
*/
|
2018-11-21 18:19:33 +08:00
|
|
|
function installLocalAppIcon() {
|
2018-11-22 07:24:33 +08:00
|
|
|
if (!utils.isElectron()
|
|
|
|
|| ["win32", "darwin"].includes(os.platform())
|
|
|
|
|| (config.General && config.General.noDesktopIcon)) {
|
2018-11-21 18:01:03 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const desktopDir = path.resolve(os.homedir(), '.local/share/applications');
|
|
|
|
|
|
|
|
fs.stat(desktopDir, function (err, stats) {
|
|
|
|
if (err) {
|
|
|
|
// Directory doesn't exist so we won't attempt to create the .desktop file
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stats.isDirectory()) {
|
|
|
|
const desktopFilePath = path.resolve(desktopDir, "trilium-notes.desktop");
|
|
|
|
|
|
|
|
fs.writeFile(desktopFilePath, getDesktopFileContent(), function (err) {
|
|
|
|
if (err) {
|
|
|
|
log.error("Desktop icon installation to ~/.local/share/applications failed.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDesktopFileContent() {
|
|
|
|
return template
|
2019-01-23 02:49:33 +08:00
|
|
|
.replace("#APP_ROOT_DIR#", escapePath(ELECTRON_APP_ROOT_DIR))
|
2018-11-21 18:01:03 +08:00
|
|
|
.replace("#EXE_PATH#", escapePath(getExePath()));
|
|
|
|
}
|
|
|
|
|
|
|
|
function escapePath(path) {
|
|
|
|
return path.replace(" ", "\\ ");
|
|
|
|
}
|
|
|
|
|
|
|
|
function getExePath() {
|
2018-11-21 18:19:33 +08:00
|
|
|
return path.resolve(ELECTRON_APP_ROOT_DIR, 'trilium');
|
2018-11-21 18:01:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
installLocalAppIcon
|
|
|
|
};
|