From c981cf32b9a309ae696fc2fffaecb39ddf996270 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 3 May 2022 09:20:30 +0200 Subject: [PATCH] add restart in testnet/mainnet menu command --- .../sparrowwallet/sparrow/AppController.java | 32 +++++++++++++++++++ .../java/com/sparrowwallet/sparrow/Args.java | 22 +++++++++++++ .../com/sparrowwallet/sparrow/app.fxml | 1 + 3 files changed, 55 insertions(+) diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index 7b652880..99e23d3b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -1,5 +1,6 @@ package com.sparrowwallet.sparrow; +import com.beust.jcommander.JCommander; import com.google.common.base.Charsets; import com.google.common.eventbus.Subscribe; import com.google.common.io.ByteSource; @@ -90,6 +91,7 @@ public class AppController implements Initializable { public static final String LOADING_TRANSACTIONS_MESSAGE = "Loading wallet, select Transactions tab to view..."; public static final String CONNECTION_FAILED_PREFIX = "Connection failed: "; public static final String TRYING_ANOTHER_SERVER_MESSAGE = "trying another server..."; + public static final String JPACKAGE_APP_PATH = "jpackage.app-path"; @FXML private MenuItem saveTransaction; @@ -181,6 +183,9 @@ public class AppController implements Initializable { private CheckMenuItem preventSleep; private static final BooleanProperty preventSleepProperty = new SimpleBooleanProperty(); + @FXML + private MenuItem restart; + @FXML private StackPane rootStack; @@ -334,6 +339,8 @@ public class AppController implements Initializable { showLoadingLog.selectedProperty().bindBidirectional(showLoadingLogProperty); preventSleepProperty.set(Config.get().isPreventSleep()); preventSleep.selectedProperty().bindBidirectional(preventSleepProperty); + restart.setText("Restart in " + (Network.get() == Network.MAINNET ? Network.TESTNET.toDisplayString() : Network.MAINNET.toDisplayString())); + restart.setVisible(System.getProperty(JPACKAGE_APP_PATH) != null); saveTransaction.setDisable(true); showTransaction.visibleProperty().bind(Bindings.and(saveTransaction.visibleProperty(), saveTransaction.disableProperty().not())); @@ -839,6 +846,31 @@ public class AppController implements Initializable { AppServices.get().setPreventSleep(item.isSelected()); } + public void restart(ActionEvent event) { + if(System.getProperty(JPACKAGE_APP_PATH) == null) { + throw new IllegalStateException("Property " + JPACKAGE_APP_PATH + " is not present"); + } + + Args args = new Args(); + ProcessHandle.current().info().arguments().ifPresent(argv -> { + JCommander jCommander = JCommander.newBuilder().addObject(args).acceptUnknownOptions(true).build(); + jCommander.parse(argv); + }); + + args.network = (Network.get() == Network.MAINNET ? Network.TESTNET : Network.MAINNET); + + try { + List cmd = new ArrayList<>(); + cmd.add(System.getProperty(JPACKAGE_APP_PATH)); + cmd.addAll(args.toParams()); + final ProcessBuilder builder = new ProcessBuilder(cmd); + builder.start(); + quit(event); + } catch(Exception e) { + log.error("Error restarting application", e); + } + } + public void openFile(File file) { if(isWalletFile(file)) { openWalletFile(file, true); diff --git a/src/main/java/com/sparrowwallet/sparrow/Args.java b/src/main/java/com/sparrowwallet/sparrow/Args.java index fb157343..cda17e3e 100644 --- a/src/main/java/com/sparrowwallet/sparrow/Args.java +++ b/src/main/java/com/sparrowwallet/sparrow/Args.java @@ -4,6 +4,9 @@ import com.beust.jcommander.Parameter; import com.sparrowwallet.drongo.Network; import org.slf4j.event.Level; +import java.util.ArrayList; +import java.util.List; + public class Args { @Parameter(names = { "--dir", "-d" }, description = "Path to Sparrow home folder") public String dir; @@ -16,4 +19,23 @@ public class Args { @Parameter(names = { "--help", "-h" }, description = "Show usage", help = true) public boolean help; + + public List toParams() { + List params = new ArrayList<>(); + + if(dir != null) { + params.add("-d"); + params.add(dir); + } + if(network != null) { + params.add("-n"); + params.add(network.toString()); + } + if(level != null) { + params.add("-l"); + params.add(level.toString()); + } + + return params; + } } diff --git a/src/main/resources/com/sparrowwallet/sparrow/app.fxml b/src/main/resources/com/sparrowwallet/sparrow/app.fxml index d24de4d9..72644711 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/app.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/app.fxml @@ -118,6 +118,7 @@ +