mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-12-23 20:36:44 +00:00
open files or uris from commandline arguments
This commit is contained in:
parent
7cba0de268
commit
e046512e86
3 changed files with 56 additions and 13 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<File> argFiles = new ArrayList<>();
|
||||
private static final List<URI> 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);
|
||||
|
|
Loading…
Reference in a new issue