From c281687910a32897fec148408388b5b88146a0dd Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Fri, 22 Nov 2019 13:43:40 +0100 Subject: [PATCH] added copy/print buttons to recovery key dialog --- .../CreateNewVaultRecoveryKeyController.java | 15 ++++- .../ui/controls/FontAwesome5Icon.java | 2 + .../RecoveryKeyDisplayController.java | 66 +++++++++++++++++++ .../fxml/addvault_new_recoverykey.fxml | 14 +++- .../resources/fxml/recoverykey_display.fxml | 20 +++++- .../main/resources/i18n/strings.properties | 2 + 6 files changed, 112 insertions(+), 7 deletions(-) diff --git a/main/ui/src/main/java/org/cryptomator/ui/addvaultwizard/CreateNewVaultRecoveryKeyController.java b/main/ui/src/main/java/org/cryptomator/ui/addvaultwizard/CreateNewVaultRecoveryKeyController.java index 6249a9a81..5d2c31558 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/addvaultwizard/CreateNewVaultRecoveryKeyController.java +++ b/main/ui/src/main/java/org/cryptomator/ui/addvaultwizard/CreateNewVaultRecoveryKeyController.java @@ -1,10 +1,12 @@ package org.cryptomator.ui.addvaultwizard; import dagger.Lazy; +import javafx.beans.property.ReadOnlyBooleanProperty; +import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.StringProperty; import javafx.fxml.FXML; import javafx.print.PageLayout; -import javafx.print.PrintQuality; +import javafx.print.Printer; import javafx.print.PrinterJob; import javafx.scene.Scene; import javafx.scene.control.TextArea; @@ -33,6 +35,7 @@ public class CreateNewVaultRecoveryKeyController implements FxController { private final Lazy successScene; private final StringProperty recoveryKeyProperty; private final StringProperty vaultName; + private final ReadOnlyBooleanProperty printerSupported; public TextArea textarea; @Inject @@ -41,11 +44,13 @@ public class CreateNewVaultRecoveryKeyController implements FxController { this.successScene = successScene; this.recoveryKeyProperty = recoveryKey; this.vaultName = vaultName; + this.printerSupported = new SimpleBooleanProperty(Printer.getDefaultPrinter() != null); } @FXML public void printRecoveryKey() { // TODO localize + PrinterJob job = PrinterJob.createPrinterJob(); if (job != null && job.showPrintDialog(window)) { PageLayout pageLayout = job.getJobSettings().getPageLayout(); @@ -89,6 +94,14 @@ public class CreateNewVaultRecoveryKeyController implements FxController { /* Getter/Setter */ + public ReadOnlyBooleanProperty printerSupportedProperty() { + return printerSupported; + } + + public boolean isPrinterSupported() { + return printerSupported.get(); + } + public String getRecoveryKey() { return recoveryKeyProperty.get(); } diff --git a/main/ui/src/main/java/org/cryptomator/ui/controls/FontAwesome5Icon.java b/main/ui/src/main/java/org/cryptomator/ui/controls/FontAwesome5Icon.java index d592876d6..742d29438 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/controls/FontAwesome5Icon.java +++ b/main/ui/src/main/java/org/cryptomator/ui/controls/FontAwesome5Icon.java @@ -9,6 +9,7 @@ public enum FontAwesome5Icon { CHECK("\uF00C"), // COG("\uF013"), // COGS("\uF085"), // + COPY("\uF0C5"), // EXCLAMATION("\uF12A"), EXCLAMATION_CIRCLE("\uF06A"), // EXCLAMATION_TRIANGLE("\uF071"), // @@ -24,6 +25,7 @@ public enum FontAwesome5Icon { LOCK_ALT("\uF30D"), // LOCK_OPEN_ALT("\uF3C2"), // PLUS("\uF067"), // + PRINT("\uF02F"), // QUESTION("\uF128"), // SPARKLES("\uF890"), // SPINNER("\uF110"), // diff --git a/main/ui/src/main/java/org/cryptomator/ui/recoverykey/RecoveryKeyDisplayController.java b/main/ui/src/main/java/org/cryptomator/ui/recoverykey/RecoveryKeyDisplayController.java index 2d96d786f..06b5c8a6a 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/recoverykey/RecoveryKeyDisplayController.java +++ b/main/ui/src/main/java/org/cryptomator/ui/recoverykey/RecoveryKeyDisplayController.java @@ -1,26 +1,84 @@ package org.cryptomator.ui.recoverykey; +import javafx.beans.property.ReadOnlyBooleanProperty; import javafx.beans.property.ReadOnlyStringProperty; +import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.StringProperty; import javafx.fxml.FXML; +import javafx.print.PageLayout; +import javafx.print.Printer; +import javafx.print.PrinterJob; +import javafx.scene.input.Clipboard; +import javafx.scene.input.ClipboardContent; +import javafx.scene.text.Font; +import javafx.scene.text.FontSmoothingType; +import javafx.scene.text.FontWeight; +import javafx.scene.text.Text; +import javafx.scene.text.TextFlow; import javafx.stage.Stage; import org.cryptomator.common.vaults.Vault; import org.cryptomator.ui.common.FxController; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.inject.Inject; @RecoveryKeyScoped public class RecoveryKeyDisplayController implements FxController { + + private static final Logger LOG = LoggerFactory.getLogger(RecoveryKeyDisplayController.class); private final Stage window; private final Vault vault; private final StringProperty recoveryKeyProperty; + private final ReadOnlyBooleanProperty printerSupported; @Inject public RecoveryKeyDisplayController(@RecoveryKeyWindow Stage window, @RecoveryKeyWindow Vault vault, @RecoveryKeyWindow StringProperty recoveryKey) { this.window = window; this.vault = vault; this.recoveryKeyProperty = recoveryKey; + this.printerSupported = new SimpleBooleanProperty(Printer.getDefaultPrinter() != null); + } + + @FXML + public void printRecoveryKey() { + // TODO localize + + PrinterJob job = PrinterJob.createPrinterJob(); + if (job != null && job.showPrintDialog(window)) { + PageLayout pageLayout = job.getJobSettings().getPageLayout(); + + Text heading = new Text("Cryptomator Recovery Key\n" + vault.getDisplayableName() + "\n"); + heading.setFont(Font.font("serif", FontWeight.BOLD, 20)); + heading.setFontSmoothingType(FontSmoothingType.LCD); + + Text key = new Text(recoveryKeyProperty.get()); + key.setFont(Font.font("serif", FontWeight.NORMAL, 16)); + key.setFontSmoothingType(FontSmoothingType.GRAY); + + TextFlow textFlow = new TextFlow(); + textFlow.setPrefSize(pageLayout.getPrintableWidth(), pageLayout.getPrintableHeight()); + textFlow.getChildren().addAll(heading, key); + textFlow.setLineSpacing(6); + + if (job.printPage(textFlow)) { + LOG.info("Recovery key printed."); + job.endJob(); + } else { + LOG.warn("Printing recovery key failed."); + } + } else { + LOG.info("Printing recovery key canceled by user."); + } + } + + @FXML + public void copyRecoveryKey() { + ClipboardContent clipboardContent = new ClipboardContent(); + clipboardContent.putString(recoveryKeyProperty.get()); + Clipboard.getSystemClipboard().setContent(clipboardContent); + LOG.info("Recovery key copied to clipboard."); } @FXML @@ -34,6 +92,14 @@ public class RecoveryKeyDisplayController implements FxController { return vault; } + public ReadOnlyBooleanProperty printerSupportedProperty() { + return printerSupported; + } + + public boolean isPrinterSupported() { + return printerSupported.get(); + } + public ReadOnlyStringProperty recoveryKeyProperty() { return recoveryKeyProperty; } diff --git a/main/ui/src/main/resources/fxml/addvault_new_recoverykey.fxml b/main/ui/src/main/resources/fxml/addvault_new_recoverykey.fxml index af7d3a9d8..9c2a2622e 100644 --- a/main/ui/src/main/resources/fxml/addvault_new_recoverykey.fxml +++ b/main/ui/src/main/resources/fxml/addvault_new_recoverykey.fxml @@ -7,6 +7,7 @@ +