From 8ce3c1a48063a4dafa43bd5bd822fb89e75bc628 Mon Sep 17 00:00:00 2001 From: azivner Date: Wed, 21 Nov 2018 11:01:03 +0100 Subject: [PATCH] generate local .desktop file so it shows among apps in linux desktop environments --- README.md | 5 +-- electron.js | 3 ++ src/services/app_icon.js | 65 ++++++++++++++++++++++++++++++++++++ src/services/resource_dir.js | 4 ++- 4 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 src/services/app_icon.js diff --git a/README.md b/README.md index c4272cbd7..f8a094355 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ See other pictures in [screenshot tour](https://github.com/zadam/trilium/wiki/Sc * Note [attributes](https://github.com/zadam/trilium/wiki/Attributes) can be used for note organization, querying and advanced [scripting](https://github.com/zadam/trilium/wiki/Scripts) * [Synchronization](https://github.com/zadam/trilium/wiki/Synchronization) with self-hosted sync server * Strong [note encryption](https://github.com/zadam/trilium/wiki/Protected-notes) +* [Relation maps](https://github.com/zadam/trilium/wiki/Relation-map) for visualizing notes and their relations * [Scripting](https://github.com/zadam/trilium/wiki/Scripts) - see [Advanced showcases](https://github.com/zadam/trilium/wiki/Advanced-showcases) * Scales well in both usability and performance upwards of 100 000 notes * [Night theme](https://github.com/zadam/trilium/wiki/Themes) @@ -31,10 +32,6 @@ Trilium is provided as either desktop application ([Electron](https://electronjs * If you want to install Trilium on server, follow [this page](https://github.com/zadam/trilium/wiki/Server-installation). * Currently only recent Chrome and Firefox are supported (tested) browsers. -## Status - -Trilium is beta quality software. While it is reasonably feature complete and is tested by its author, it lacks proper testing by more users. It's not yet recommended for daily use, but testing and experimentation is encouraged. - ## Documentation [See wiki for complete list of documentation pages.](https://github.com/zadam/trilium/wiki/) \ No newline at end of file diff --git a/electron.js b/electron.js index 560644cb4..054198ccb 100644 --- a/electron.js +++ b/electron.js @@ -6,6 +6,7 @@ const log = require('./src/services/log'); const cls = require('./src/services/cls'); const url = require("url"); const port = require('./src/services/port'); +const appIconService = require('./src/services/app_icon'); const app = electron.app; const globalShortcut = electron.globalShortcut; @@ -13,6 +14,8 @@ const globalShortcut = electron.globalShortcut; // Adds debug features like hotkeys for triggering dev tools and reload require('electron-debug')(); +appIconService.installLocalAppIcon(); + // Prevent window being garbage collected let mainWindow; diff --git a/src/services/app_icon.js b/src/services/app_icon.js new file mode 100644 index 000000000..ff10e94a5 --- /dev/null +++ b/src/services/app_icon.js @@ -0,0 +1,65 @@ +"use strict"; + +const path = require('path'); +const {APP_PNG_ICON_DIR} = require("./resource_dir"); +const log = require("./log"); +const os = require('os'); +const fs = require('fs'); + +const template = `[Desktop Entry] +Type=Application +Name=Trilium Notes +Icon=#APP_PNG_ICON_DIR#/128x128.png +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. + */ +async function installLocalAppIcon() { + if (["win32", "darwin"].includes(os.platform())) { + 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 + .replace("#APP_PNG_ICON_DIR#", escapePath(APP_PNG_ICON_DIR)) + .replace("#EXE_PATH#", escapePath(getExePath())); +} + +function escapePath(path) { + return path.replace(" ", "\\ "); +} + +function getExePath() { + const appPath = require('electron').app.getAppPath(); + + return path.resolve(appPath, 'trilium'); +} + +module.exports = { + installLocalAppIcon +}; \ No newline at end of file diff --git a/src/services/resource_dir.js b/src/services/resource_dir.js index cce2ed5fe..bf2b3d430 100644 --- a/src/services/resource_dir.js +++ b/src/services/resource_dir.js @@ -5,6 +5,7 @@ const fs = require('fs'); const RESOURCE_DIR = path.resolve(__dirname, "../.."); const DB_INIT_DIR = path.resolve(RESOURCE_DIR, "db"); +const APP_PNG_ICON_DIR = path.resolve(RESOURCE_DIR, "src/public/images/app-icons/png"); if (!fs.existsSync(DB_INIT_DIR)) { log.error("Could not find DB initialization directory: " + DB_INIT_DIR); @@ -21,5 +22,6 @@ if (!fs.existsSync(MIGRATIONS_DIR)) { module.exports = { RESOURCE_DIR, MIGRATIONS_DIR, - DB_INIT_DIR + DB_INIT_DIR, + APP_PNG_ICON_DIR }; \ No newline at end of file