From e046512e8656120d252faacb580228fb61f5fcb5 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Mon, 10 May 2021 11:44:06 +0200 Subject: [PATCH] open files or uris from commandline arguments --- .../sparrowwallet/sparrow/AppController.java | 14 ++++++--- .../sparrowwallet/sparrow/AppServices.java | 24 +++++++++----- .../com/sparrowwallet/sparrow/MainApp.java | 31 +++++++++++++++++++ 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index 677737c5..f041ccc8 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -170,11 +170,7 @@ public class AppController implements Initializable { boolean success = false; if(db.hasFiles()) { for(File file : db.getFiles()) { - if(isWalletFile(file)) { - openWalletFile(file, true); - } else { - openTransactionFile(file); - } + openFile(file); } success = true; } @@ -687,6 +683,14 @@ public class AppController implements Initializable { EventManager.get().post(new BitcoinUnitChangedEvent(unit)); } + public void openFile(File file) { + if(isWalletFile(file)) { + openWalletFile(file, true); + } else { + openTransactionFile(file); + } + } + private boolean isWalletFile(File file) { FileType fileType = IOUtils.getFileType(file); return FileType.JSON.equals(fileType) || FileType.BINARY.equals(fileType); diff --git a/src/main/java/com/sparrowwallet/sparrow/AppServices.java b/src/main/java/com/sparrowwallet/sparrow/AppServices.java index cfc3324c..3037011c 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppServices.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppServices.java @@ -45,6 +45,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.awt.*; +import java.awt.desktop.OpenURIEvent; +import java.awt.desktop.OpenURIHandler; import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; @@ -121,6 +123,15 @@ public class AppServices { } }; + private static final OpenURIHandler openURIHandler = event -> { + URI uri = event.getURI(); + if("bitcoin".equals(uri.getScheme())) { + Platform.runLater(() -> openBitcoinUri(uri)); + } else if("aopp".equals(uri.getScheme())) { + Platform.runLater(() -> openAddressOwnershipProof(uri)); + } + }; + public AppServices(MainApp application) { this.application = application; EventManager.get().register(this); @@ -600,17 +611,14 @@ public class AppServices { } } + public static void handleURI(URI uri) { + openURIHandler.openURI(new OpenURIEvent(uri)); + } + public static void addURIHandlers() { try { if(Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.APP_OPEN_URI)) { - Desktop.getDesktop().setOpenURIHandler(event -> { - URI uri = event.getURI(); - if("bitcoin".equals(uri.getScheme())) { - Platform.runLater(() -> openBitcoinUri(uri)); - } else if("aopp".equals(uri.getScheme())) { - Platform.runLater(() -> openAddressOwnershipProof(uri)); - } - }); + Desktop.getDesktop().setOpenURIHandler(openURIHandler); } } catch(Exception e) { log.error("Could not add URI handler", e); diff --git a/src/main/java/com/sparrowwallet/sparrow/MainApp.java b/src/main/java/com/sparrowwallet/sparrow/MainApp.java index 2f7b4b91..e49c37a9 100644 --- a/src/main/java/com/sparrowwallet/sparrow/MainApp.java +++ b/src/main/java/com/sparrowwallet/sparrow/MainApp.java @@ -25,6 +25,8 @@ import org.slf4j.LoggerFactory; import org.slf4j.bridge.SLF4JBridgeHandler; import java.io.File; +import java.net.URI; +import java.net.URISyntaxException; import java.util.*; import java.util.stream.Collectors; @@ -36,6 +38,9 @@ public class MainApp extends Application { private Stage mainStage; + private static final List argFiles = new ArrayList<>(); + private static final List argUris = new ArrayList<>(); + @Override public void init() throws Exception { Thread.setDefaultUncaughtExceptionHandler((t, e) -> LoggerFactory.getLogger(MainApp.class).error("Exception in thread \"" + t.getName() + "\"", e)); @@ -108,6 +113,14 @@ public class MainApp extends Application { } } + for(File argFile : argFiles) { + appController.openFile(argFile); + } + + for(URI argUri : argUris) { + AppServices.handleURI(argUri); + } + AppServices.get().start(); } @@ -162,6 +175,24 @@ public class MainApp extends Application { getLogger().info("Using " + Network.get() + " configuration"); } + if(!jCommander.getUnknownOptions().isEmpty()) { + for(String fileUri : jCommander.getUnknownOptions()) { + try { + File file = new File(fileUri); + if(file.exists()) { + argFiles.add(file); + continue; + } + URI uri = new URI(fileUri); + argUris.add(uri); + } catch(URISyntaxException e) { + getLogger().warn("Could not parse " + fileUri + " as a valid file or URI"); + } catch(Exception e) { + //ignore + } + } + } + SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install(); com.sun.javafx.application.LauncherImpl.launchApplication(MainApp.class, MainAppPreloader.class, argv);