From ad3e940a165db1d997946d3ac05174e8a3fba0ac Mon Sep 17 00:00:00 2001 From: Sergii Pylypenko Date: Thu, 5 Dec 2024 15:44:57 +0200 Subject: [PATCH] Use an absolute filename for the file save dialog on Linux This fixes #6912. On Gnome the save dialog works with any filename, on KDE Plasma the save dialog opens with an empty filename input field, unless the application uses an absolute file path instead of only the filename. The default save location is the Downloads directory in the user's home directory. --- app/main.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/app/main.ts b/app/main.ts index 22bca044a07..57a49026148 100644 --- a/app/main.ts +++ b/app/main.ts @@ -1,7 +1,7 @@ // Copyright 2017 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only -import { join, normalize, extname, dirname, basename } from 'path'; +import { join, normalize, extname, dirname, basename, isAbsolute } from 'path'; import { pathToFileURL } from 'url'; import * as os from 'os'; import { chmod, realpath, writeFile } from 'fs-extra'; @@ -3021,6 +3021,30 @@ ipc.handle('show-save-dialog', async (_event, { defaultPath }) => { return { canceled: true }; } + if ( + process.platform === 'linux' && + defaultPath && + !isAbsolute(defaultPath) + ) { + // On Linux the defaultPath should be an absolute path, otherwise the save dialog will have an empty filename on KDE/Plasma + let downloadsPath = ''; + try { + downloadsPath = app.getPath('downloads'); + getLogger().info( + 'show-save-dialog: saving to user downloads directory: ' + downloadsPath + ); + } catch (e) { + // If we cannot get Downloads path, fall back to the user's home directory + downloadsPath = app.getPath('home'); + getLogger().info( + 'show-save-dialog: saving to user home directory: ' + downloadsPath + ); + } + if (downloadsPath) { + defaultPath = join(downloadsPath, defaultPath) + } + } + const { canceled, filePath: selectedFilePath } = await dialog.showSaveDialog( mainWindow, {