Skip to content

Commit 53d1f19

Browse files
committed
suggest configuring a custom wallets directory when opening a wallet from a non-default location
1 parent 64b8da1 commit 53d1f19

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/main/java/com/sparrowwallet/sparrow/AppController.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,12 +1140,37 @@ public void openWallet(boolean forceSameWindow) {
11401140
AppServices.moveToActiveWindowScreen(window, 800, 450);
11411141
List<File> files = fileChooser.showOpenMultipleDialog(window);
11421142
if(files != null) {
1143+
configureWalletsDir(files);
11431144
for(File file : files) {
11441145
openWalletFile(file, forceSameWindow);
11451146
}
11461147
}
11471148
}
11481149

1150+
private static void configureWalletsDir(List<File> files) {
1151+
List<File> parentDirs = files.stream().map(File::getParentFile).distinct().collect(Collectors.toList());
1152+
if(parentDirs.size() == 1 && !Boolean.FALSE.equals(Config.get().getSuggestChangeWalletsDir())) {
1153+
File selectedDir = parentDirs.getFirst();
1154+
boolean sameDir;
1155+
try {
1156+
sameDir = Files.isSameFile(selectedDir.toPath(), Storage.getWalletsDir().toPath());
1157+
} catch(IOException e) {
1158+
sameDir = selectedDir.toPath().normalize().equals(Storage.getWalletsDir().toPath().normalize());
1159+
}
1160+
if(!sameDir) {
1161+
ConfirmationAlert alert = new ConfirmationAlert("Change wallets directory?",
1162+
"Do you want to configure Sparrow to use " + selectedDir + " as the default wallets directory?", ButtonType.NO, ButtonType.YES);
1163+
Optional<ButtonType> optType = alert.showAndWait();
1164+
if(optType.isPresent() && optType.get() == ButtonType.YES) {
1165+
Config.get().setWalletsDir(selectedDir);
1166+
Config.get().setSuggestChangeWalletsDir(null);
1167+
} else if(alert.isDontAskAgain()) {
1168+
Config.get().setSuggestChangeWalletsDir(Boolean.FALSE);
1169+
}
1170+
}
1171+
}
1172+
}
1173+
11491174
public void openWalletFile(File file, boolean forceSameWindow) {
11501175
try {
11511176
Storage storage = new Storage(file);

src/main/java/com/sparrowwallet/sparrow/io/Config.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class Config {
5959
private Boolean connectToBroadcast;
6060
private Boolean connectToResolve;
6161
private Boolean suggestSendToMany;
62+
private Boolean suggestChangeWalletsDir;
63+
private File walletsDir;
6264
private List<File> recentWalletFiles;
6365
private Integer keyDerivationPeriod;
6466
private long dustAttackThreshold = DUST_ATTACK_THRESHOLD_SATS;
@@ -406,6 +408,24 @@ public void setSuggestSendToMany(Boolean suggestSendToMany) {
406408
flush();
407409
}
408410

411+
public Boolean getSuggestChangeWalletsDir() {
412+
return suggestChangeWalletsDir;
413+
}
414+
415+
public void setSuggestChangeWalletsDir(Boolean suggestChangeWalletsDir) {
416+
this.suggestChangeWalletsDir = suggestChangeWalletsDir;
417+
flush();
418+
}
419+
420+
public File getWalletsDir() {
421+
return walletsDir;
422+
}
423+
424+
public void setWalletsDir(File walletsDir) {
425+
this.walletsDir = walletsDir;
426+
flush();
427+
}
428+
409429
public List<File> getRecentWalletFiles() {
410430
return recentWalletFiles;
411431
}

src/main/java/com/sparrowwallet/sparrow/io/Storage.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,16 @@ public static File getWalletsBackupDir() {
485485
}
486486

487487
public static File getWalletsDir() {
488-
File walletsDir = new File(getSparrowDir(), WALLETS_DIR);
488+
File walletsDir = Config.get().getWalletsDir();
489+
if(walletsDir != null) {
490+
if(!walletsDir.exists() && (walletsDir.getParentFile() == null || !walletsDir.getParentFile().exists() || !walletsDir.getParentFile().canWrite())) {
491+
log.info("Configured wallets directory " + walletsDir.getAbsolutePath() + " is not reachable, reverting to default");
492+
walletsDir = null;
493+
}
494+
}
495+
if(walletsDir == null) {
496+
walletsDir = new File(getSparrowDir(), WALLETS_DIR);
497+
}
489498
if(!walletsDir.exists()) {
490499
createOwnerOnlyDirectory(walletsDir);
491500
}

0 commit comments

Comments
 (0)