From 53d1f196e460f34443a039b1aaf1ac31a12aa926 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 17 Feb 2026 09:07:09 +0200 Subject: [PATCH] suggest configuring a custom wallets directory when opening a wallet from a non-default location --- .../sparrowwallet/sparrow/AppController.java | 25 +++++++++++++++++++ .../com/sparrowwallet/sparrow/io/Config.java | 20 +++++++++++++++ .../com/sparrowwallet/sparrow/io/Storage.java | 11 +++++++- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index 64168bc9..78227648 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -1140,12 +1140,37 @@ public class AppController implements Initializable { AppServices.moveToActiveWindowScreen(window, 800, 450); List files = fileChooser.showOpenMultipleDialog(window); if(files != null) { + configureWalletsDir(files); for(File file : files) { openWalletFile(file, forceSameWindow); } } } + private static void configureWalletsDir(List files) { + List parentDirs = files.stream().map(File::getParentFile).distinct().collect(Collectors.toList()); + if(parentDirs.size() == 1 && !Boolean.FALSE.equals(Config.get().getSuggestChangeWalletsDir())) { + File selectedDir = parentDirs.getFirst(); + boolean sameDir; + try { + sameDir = Files.isSameFile(selectedDir.toPath(), Storage.getWalletsDir().toPath()); + } catch(IOException e) { + sameDir = selectedDir.toPath().normalize().equals(Storage.getWalletsDir().toPath().normalize()); + } + if(!sameDir) { + ConfirmationAlert alert = new ConfirmationAlert("Change wallets directory?", + "Do you want to configure Sparrow to use " + selectedDir + " as the default wallets directory?", ButtonType.NO, ButtonType.YES); + Optional optType = alert.showAndWait(); + if(optType.isPresent() && optType.get() == ButtonType.YES) { + Config.get().setWalletsDir(selectedDir); + Config.get().setSuggestChangeWalletsDir(null); + } else if(alert.isDontAskAgain()) { + Config.get().setSuggestChangeWalletsDir(Boolean.FALSE); + } + } + } + } + public void openWalletFile(File file, boolean forceSameWindow) { try { Storage storage = new Storage(file); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Config.java b/src/main/java/com/sparrowwallet/sparrow/io/Config.java index 2f9e2ad2..51793fdd 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Config.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Config.java @@ -59,6 +59,8 @@ public class Config { private Boolean connectToBroadcast; private Boolean connectToResolve; private Boolean suggestSendToMany; + private Boolean suggestChangeWalletsDir; + private File walletsDir; private List recentWalletFiles; private Integer keyDerivationPeriod; private long dustAttackThreshold = DUST_ATTACK_THRESHOLD_SATS; @@ -406,6 +408,24 @@ public class Config { flush(); } + public Boolean getSuggestChangeWalletsDir() { + return suggestChangeWalletsDir; + } + + public void setSuggestChangeWalletsDir(Boolean suggestChangeWalletsDir) { + this.suggestChangeWalletsDir = suggestChangeWalletsDir; + flush(); + } + + public File getWalletsDir() { + return walletsDir; + } + + public void setWalletsDir(File walletsDir) { + this.walletsDir = walletsDir; + flush(); + } + public List getRecentWalletFiles() { return recentWalletFiles; } diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java index dd7d737d..51147fa0 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java @@ -485,7 +485,16 @@ public class Storage { } public static File getWalletsDir() { - File walletsDir = new File(getSparrowDir(), WALLETS_DIR); + File walletsDir = Config.get().getWalletsDir(); + if(walletsDir != null) { + if(!walletsDir.exists() && (walletsDir.getParentFile() == null || !walletsDir.getParentFile().exists() || !walletsDir.getParentFile().canWrite())) { + log.info("Configured wallets directory " + walletsDir.getAbsolutePath() + " is not reachable, reverting to default"); + walletsDir = null; + } + } + if(walletsDir == null) { + walletsDir = new File(getSparrowDir(), WALLETS_DIR); + } if(!walletsDir.exists()) { createOwnerOnlyDirectory(walletsDir); }