From f3a7d583a5d0c7dce770c3c4ae34574433c22e94 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Sun, 9 Aug 2020 15:26:38 +0200 Subject: [PATCH] initial setup and drag file improvements --- .../com/sparrowwallet/sparrow/AppController.java | 16 +++++++++++++++- .../java/com/sparrowwallet/sparrow/MainApp.java | 10 ++++++++-- .../com/sparrowwallet/sparrow/io/IOUtils.java | 16 ++++++++++++++-- .../sparrow/preferences/PreferencesDialog.java | 14 +++++++++++++- 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index bd2f9ec1..eb199f03 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -146,7 +146,11 @@ public class AppController implements Initializable { boolean success = false; if(db.hasFiles()) { for(File file : db.getFiles()) { - openTransactionFile(file); + if(isWalletFile(file)) { + openWalletFile(file); + } else { + openTransactionFile(file); + } } success = true; } @@ -181,6 +185,11 @@ public class AppController implements Initializable { if(walletAdded || walletRemoved) { EventManager.get().post(new OpenWalletsEvent(getOpenWallets())); } + + if(tabs.getTabs().isEmpty()) { + Stage tabStage = (Stage)tabs.getScene().getWindow(); + tabStage.setTitle("Sparrow"); + } } }); @@ -583,6 +592,11 @@ public class AppController implements Initializable { EventManager.get().post(new BitcoinUnitChangedEvent(unit)); } + private boolean isWalletFile(File file) { + FileType fileType = IOUtils.getFileType(file); + return FileType.JSON.equals(fileType) || FileType.BINARY.equals(fileType); + } + public void newWallet(ActionEvent event) { WalletNameDialog dlg = new WalletNameDialog(); Optional walletName = dlg.showAndWait(); diff --git a/src/main/java/com/sparrowwallet/sparrow/MainApp.java b/src/main/java/com/sparrowwallet/sparrow/MainApp.java index 269bfb2c..1d14b2d7 100644 --- a/src/main/java/com/sparrowwallet/sparrow/MainApp.java +++ b/src/main/java/com/sparrowwallet/sparrow/MainApp.java @@ -31,6 +31,7 @@ public class MainApp extends Application { GlyphFontRegistry.register(new FontAwesome5()); GlyphFontRegistry.register(new FontAwesome5Brands()); + boolean createNewWallet = false; Mode mode = Config.get().getMode(); if(mode == null) { WelcomeDialog welcomeDialog = new WelcomeDialog(getHostServices()); @@ -40,8 +41,9 @@ public class MainApp extends Application { Config.get().setMode(mode); if(mode.equals(Mode.ONLINE)) { - PreferencesDialog preferencesDialog = new PreferencesDialog(PreferenceGroup.SERVER); - preferencesDialog.showAndWait(); + PreferencesDialog preferencesDialog = new PreferencesDialog(PreferenceGroup.SERVER, true); + Optional optNewWallet = preferencesDialog.showAndWait(); + createNewWallet = optNewWallet.isPresent() && optNewWallet.get(); } } } @@ -64,6 +66,10 @@ public class MainApp extends Application { stage.show(); + if(createNewWallet) { + appController.newWallet(null); + } + List recentWalletFiles = Config.get().getRecentWalletFiles(); if(recentWalletFiles != null) { for(File walletFile : recentWalletFiles) { diff --git a/src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java b/src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java index a885a8d6..839c9aab 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java @@ -1,7 +1,6 @@ package com.sparrowwallet.sparrow.io; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.nio.file.Files; public class IOUtils { @@ -9,6 +8,19 @@ public class IOUtils { try { String type = Files.probeContentType(file.toPath()); if (type == null) { + if(file.getName().toLowerCase().endsWith("txn") || file.getName().toLowerCase().endsWith("psbt")) { + return FileType.TEXT; + } + + if(file.exists()) { + try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) { + String line = br.readLine(); + if(line.startsWith("01000000") || line.startsWith("cHNid")) { + return FileType.TEXT; + } + } + } + return FileType.BINARY; } else if (type.equals("application/json")) { return FileType.JSON; diff --git a/src/main/java/com/sparrowwallet/sparrow/preferences/PreferencesDialog.java b/src/main/java/com/sparrowwallet/sparrow/preferences/PreferencesDialog.java index 8464b8b0..dc5d5969 100644 --- a/src/main/java/com/sparrowwallet/sparrow/preferences/PreferencesDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/preferences/PreferencesDialog.java @@ -11,12 +11,16 @@ import org.controlsfx.tools.Borders; import java.io.IOException; -public class PreferencesDialog extends Dialog { +public class PreferencesDialog extends Dialog { public PreferencesDialog() { this(null); } public PreferencesDialog(PreferenceGroup initialGroup) { + this(initialGroup, false); + } + + public PreferencesDialog(PreferenceGroup initialGroup, boolean initialSetup) { final DialogPane dialogPane = getDialogPane(); try { @@ -32,8 +36,16 @@ public class PreferencesDialog extends Dialog { final ButtonType closeButtonType = new javafx.scene.control.ButtonType("Close", ButtonBar.ButtonData.CANCEL_CLOSE); dialogPane.getButtonTypes().addAll(closeButtonType); + + final ButtonType newWalletButtonType = new javafx.scene.control.ButtonType("Create New Wallet", ButtonBar.ButtonData.OK_DONE); + if(initialSetup) { + dialogPane.getButtonTypes().addAll(newWalletButtonType); + } + dialogPane.setPrefWidth(650); dialogPane.setPrefHeight(500); + + setResultConverter(dialogButton -> dialogButton == newWalletButtonType ? Boolean.TRUE : null); } catch(IOException e) { throw new RuntimeException(e); }