open files or uris from commandline arguments

This commit is contained in:
Craig Raw 2021-05-10 11:44:06 +02:00
parent 7cba0de268
commit e046512e86
3 changed files with 56 additions and 13 deletions

View file

@ -170,11 +170,7 @@ public class AppController implements Initializable {
boolean success = false; boolean success = false;
if(db.hasFiles()) { if(db.hasFiles()) {
for(File file : db.getFiles()) { for(File file : db.getFiles()) {
if(isWalletFile(file)) { openFile(file);
openWalletFile(file, true);
} else {
openTransactionFile(file);
}
} }
success = true; success = true;
} }
@ -687,6 +683,14 @@ public class AppController implements Initializable {
EventManager.get().post(new BitcoinUnitChangedEvent(unit)); 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) { private boolean isWalletFile(File file) {
FileType fileType = IOUtils.getFileType(file); FileType fileType = IOUtils.getFileType(file);
return FileType.JSON.equals(fileType) || FileType.BINARY.equals(fileType); return FileType.JSON.equals(fileType) || FileType.BINARY.equals(fileType);

View file

@ -45,6 +45,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.awt.*; import java.awt.*;
import java.awt.desktop.OpenURIEvent;
import java.awt.desktop.OpenURIHandler;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; 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) { public AppServices(MainApp application) {
this.application = application; this.application = application;
EventManager.get().register(this); 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() { public static void addURIHandlers() {
try { try {
if(Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.APP_OPEN_URI)) { if(Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.APP_OPEN_URI)) {
Desktop.getDesktop().setOpenURIHandler(event -> { Desktop.getDesktop().setOpenURIHandler(openURIHandler);
URI uri = event.getURI();
if("bitcoin".equals(uri.getScheme())) {
Platform.runLater(() -> openBitcoinUri(uri));
} else if("aopp".equals(uri.getScheme())) {
Platform.runLater(() -> openAddressOwnershipProof(uri));
}
});
} }
} catch(Exception e) { } catch(Exception e) {
log.error("Could not add URI handler", e); log.error("Could not add URI handler", e);

View file

@ -25,6 +25,8 @@ import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler; import org.slf4j.bridge.SLF4JBridgeHandler;
import java.io.File; import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -36,6 +38,9 @@ public class MainApp extends Application {
private Stage mainStage; private Stage mainStage;
private static final List<File> argFiles = new ArrayList<>();
private static final List<URI> argUris = new ArrayList<>();
@Override @Override
public void init() throws Exception { public void init() throws Exception {
Thread.setDefaultUncaughtExceptionHandler((t, e) -> LoggerFactory.getLogger(MainApp.class).error("Exception in thread \"" + t.getName() + "\"", e)); 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(); AppServices.get().start();
} }
@ -162,6 +175,24 @@ public class MainApp extends Application {
getLogger().info("Using " + Network.get() + " configuration"); 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.removeHandlersForRootLogger();
SLF4JBridgeHandler.install(); SLF4JBridgeHandler.install();
com.sun.javafx.application.LauncherImpl.launchApplication(MainApp.class, MainAppPreloader.class, argv); com.sun.javafx.application.LauncherImpl.launchApplication(MainApp.class, MainAppPreloader.class, argv);