From 6d4a4dc4a31db9fc6e2f2330d0a5fdb5c5719d80 Mon Sep 17 00:00:00 2001 From: David Gerber Date: Sun, 19 Jan 2025 22:44:42 +0100 Subject: [PATCH] Fix sound settings path on Linux Also let you actually select another path if the current sound directory doesn't exist. --- .../settings/SettingsSoundController.java | 21 +++++++++++++++---- .../xeres/ui/support/sound/SoundSettings.java | 13 +++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/ui/src/main/java/io/xeres/ui/controller/settings/SettingsSoundController.java b/ui/src/main/java/io/xeres/ui/controller/settings/SettingsSoundController.java index 6f5f607d..09f72cc6 100644 --- a/ui/src/main/java/io/xeres/ui/controller/settings/SettingsSoundController.java +++ b/ui/src/main/java/io/xeres/ui/controller/settings/SettingsSoundController.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 by David Gerber - https://zapek.com + * Copyright (c) 2024-2025 by David Gerber - https://zapek.com * * This file is part of Xeres. * @@ -91,11 +91,11 @@ public class SettingsSoundController implements SettingsController private final SoundSettings soundSettings; private final SoundService soundService; - public SettingsSoundController(ResourceBundle bundle, SoundSettings soundSettings, SoundService soundService, SoundService soundService1) + public SettingsSoundController(ResourceBundle bundle, SoundSettings soundSettings, SoundService soundService) { this.bundle = bundle; this.soundSettings = soundSettings; - this.soundService = soundService1; + this.soundService = soundService; } @Override @@ -121,7 +121,7 @@ private void initializeSoundPath(CheckBox checkbox, TextField path, Button pathS if (!path.getText().isEmpty()) { fileChooser.setInitialFileName(path.getText()); - fileChooser.setInitialDirectory(Path.of(path.getText()).getParent().toFile()); + setInitialDirectoryIfExists(fileChooser, path.getText()); } var selectedFile = fileChooser.showOpenDialog(UiUtils.getWindow(event)); if (selectedFile != null && selectedFile.isFile()) @@ -132,6 +132,19 @@ private void initializeSoundPath(CheckBox checkbox, TextField path, Button pathS playButton.setOnAction(actionEvent -> soundService.play(path.getText())); } + private static void setInitialDirectoryIfExists(FileChooser fileChooser, String path) + { + var parent = Path.of(path).getParent(); + if (parent != null) + { + var file = parent.toFile(); + if (file.exists() && file.isDirectory()) + { + fileChooser.setInitialDirectory(file); + } + } + } + @Override public void onLoad(Settings settings) { diff --git a/ui/src/main/java/io/xeres/ui/support/sound/SoundSettings.java b/ui/src/main/java/io/xeres/ui/support/sound/SoundSettings.java index dd414912..f11cf3a7 100644 --- a/ui/src/main/java/io/xeres/ui/support/sound/SoundSettings.java +++ b/ui/src/main/java/io/xeres/ui/support/sound/SoundSettings.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 by David Gerber - https://zapek.com + * Copyright (c) 2024-2025 by David Gerber - https://zapek.com * * This file is part of Xeres. * @@ -20,6 +20,7 @@ package io.xeres.ui.support.sound; import io.xeres.ui.support.preference.PreferenceUtils; +import org.apache.commons.lang3.SystemUtils; import org.springframework.stereotype.Service; import static io.xeres.ui.support.preference.PreferenceUtils.SOUND; @@ -149,10 +150,12 @@ private void loadIfNeeded() friendEnabled = node.getBoolean(ENABLE_FRIEND, false); downloadEnabled = node.getBoolean(ENABLE_DOWNLOAD, false); - messageFile = node.get(MESSAGE_FILE, "app/sounds/message-notification-190034.mp3"); - highlightFile = node.get(HIGHLIGHT_FILE, "app/sounds/notification-4-126507.mp3"); - friendFile = node.get(FRIEND_FILE, "app/sounds/notification-20-270145.mp3"); - downloadFile = node.get(DOWNLOAD_FILE, "app/sounds/achive-sound-132273.mp3"); + var prefixPath = SystemUtils.IS_OS_LINUX ? "../lib/" : ""; + + messageFile = node.get(MESSAGE_FILE, prefixPath + "app/sounds/message-notification-190034.mp3"); + highlightFile = node.get(HIGHLIGHT_FILE, prefixPath + "app/sounds/notification-4-126507.mp3"); + friendFile = node.get(FRIEND_FILE, prefixPath + "app/sounds/notification-20-270145.mp3"); + downloadFile = node.get(DOWNLOAD_FILE, prefixPath + "app/sounds/achive-sound-132273.mp3"); loaded = true; }