From 1c35780d9ec99714edd43efcf15b3214ec20d686 Mon Sep 17 00:00:00 2001 From: Martin Beyer Date: Mon, 29 Mar 2021 19:10:05 +0200 Subject: [PATCH] Cleaned up integrations implementation and added Regex for input --- .../common/settings/VaultSettings.java | 20 +-------- .../settings/VaultSettingsJsonAdapter.java | 12 ------ .../cryptomator/ui/fxapp/FxApplication.java | 41 ++----------------- .../AutoLockVaultOptionsController.java | 17 +++++--- 4 files changed, 16 insertions(+), 74 deletions(-) diff --git a/main/commons/src/main/java/org/cryptomator/common/settings/VaultSettings.java b/main/commons/src/main/java/org/cryptomator/common/settings/VaultSettings.java index faca30b94..5b828380d 100644 --- a/main/commons/src/main/java/org/cryptomator/common/settings/VaultSettings.java +++ b/main/commons/src/main/java/org/cryptomator/common/settings/VaultSettings.java @@ -37,9 +37,6 @@ public class VaultSettings { public static final String DEFAULT_MOUNT_FLAGS = ""; public static final int DEFAULT_FILENAME_LENGTH_LIMIT = -1; public static final WhenUnlocked DEFAULT_ACTION_AFTER_UNLOCK = WhenUnlocked.ASK; - public static final boolean DEFAULT_LOCK_ON_SLEEP = false; - public static final boolean DEFAULT_LOCK_AFTER_IDLETIME = false; - public static final String DEFAULT_LOCK_IDLETIME_IN_MINUTES = "30"; public static final boolean DEFAULT_LOCK_AFTER_TIME = false; public static final String DEFAULT_LOCK_TIME_IN_MINUTES = "30"; @@ -57,9 +54,6 @@ public class VaultSettings { private final StringProperty mountFlags = new SimpleStringProperty(DEFAULT_MOUNT_FLAGS); private final IntegerProperty filenameLengthLimit = new SimpleIntegerProperty(DEFAULT_FILENAME_LENGTH_LIMIT); private final ObjectProperty actionAfterUnlock = new SimpleObjectProperty<>(DEFAULT_ACTION_AFTER_UNLOCK); - private final BooleanProperty lockOnSleep = new SimpleBooleanProperty(DEFAULT_LOCK_ON_SLEEP); - private final BooleanProperty lockAfterIdleTime = new SimpleBooleanProperty(DEFAULT_LOCK_AFTER_IDLETIME); - private final StringProperty lockIdleTimeInMinutes = new SimpleStringProperty(DEFAULT_LOCK_IDLETIME_IN_MINUTES); private final BooleanProperty lockAfterTime = new SimpleBooleanProperty(DEFAULT_LOCK_AFTER_TIME); private final StringProperty lockTimeInMinutes = new SimpleStringProperty(DEFAULT_LOCK_TIME_IN_MINUTES); private final StringBinding mountName; @@ -70,7 +64,7 @@ public class VaultSettings { } Observable[] observables() { - return new Observable[]{path, displayName, winDriveLetter, unlockAfterStartup, revealAfterMount, useCustomMountPath, customMountPath, usesReadOnlyMode, mountFlags, filenameLengthLimit, actionAfterUnlock, lockOnSleep, lockAfterIdleTime, lockIdleTimeInMinutes, lockAfterTime, lockTimeInMinutes}; + return new Observable[]{path, displayName, winDriveLetter, unlockAfterStartup, revealAfterMount, useCustomMountPath, customMountPath, usesReadOnlyMode, mountFlags, filenameLengthLimit, actionAfterUnlock, lockAfterTime, lockTimeInMinutes}; } public static VaultSettings withRandomId() { @@ -171,18 +165,6 @@ public class VaultSettings { return actionAfterUnlock.get(); } - public BooleanProperty lockOnSleep() { - return lockOnSleep; - } - - public BooleanProperty lockAfterIdleTime() { - return lockAfterIdleTime; - } - - public StringProperty lockIdleTimeInMinutes() { - return lockIdleTimeInMinutes; - } - public BooleanProperty lockAfterTime() { return lockAfterTime; } diff --git a/main/commons/src/main/java/org/cryptomator/common/settings/VaultSettingsJsonAdapter.java b/main/commons/src/main/java/org/cryptomator/common/settings/VaultSettingsJsonAdapter.java index 717f2474d..a32956411 100644 --- a/main/commons/src/main/java/org/cryptomator/common/settings/VaultSettingsJsonAdapter.java +++ b/main/commons/src/main/java/org/cryptomator/common/settings/VaultSettingsJsonAdapter.java @@ -31,9 +31,6 @@ class VaultSettingsJsonAdapter { out.name("mountFlags").value(value.mountFlags().get()); out.name("filenameLengthLimit").value(value.filenameLengthLimit().get()); out.name("actionAfterUnlock").value(value.actionAfterUnlock().get().name()); - out.name("lockOnSleep").value(value.lockOnSleep().get()); - out.name("lockAfterIdleTime").value(value.lockAfterIdleTime().get()); - out.name("lockIdleTimeInMinutes").value(value.lockIdleTimeInMinutes().get()); out.name("lockAfterTime").value(value.lockAfterTime().get()); out.name("lockTimeInMinutes").value(value.lockTimeInMinutes().get()); out.endObject(); @@ -53,9 +50,6 @@ class VaultSettingsJsonAdapter { String mountFlags = VaultSettings.DEFAULT_MOUNT_FLAGS; int filenameLengthLimit = VaultSettings.DEFAULT_FILENAME_LENGTH_LIMIT; WhenUnlocked actionAfterUnlock = VaultSettings.DEFAULT_ACTION_AFTER_UNLOCK; - boolean lockOnSleep = VaultSettings.DEFAULT_LOCK_ON_SLEEP; - boolean lockAfterIdleTime = VaultSettings.DEFAULT_LOCK_AFTER_IDLETIME; - String lockIdleTimeInMinutes = VaultSettings.DEFAULT_LOCK_IDLETIME_IN_MINUTES; boolean lockAfterTime = VaultSettings.DEFAULT_LOCK_AFTER_TIME; String lockTimeInMinutes = VaultSettings.DEFAULT_LOCK_TIME_IN_MINUTES; @@ -76,9 +70,6 @@ class VaultSettingsJsonAdapter { case "mountFlags" -> mountFlags = in.nextString(); case "filenameLengthLimit" -> filenameLengthLimit = in.nextInt(); case "actionAfterUnlock" -> actionAfterUnlock = parseActionAfterUnlock(in.nextString()); - case "lockOnSleep" -> lockOnSleep = in.nextBoolean(); - case "lockAfterIdleTime" -> lockAfterIdleTime = in.nextBoolean(); - case "lockIdleTimeInMinutes" -> lockIdleTimeInMinutes = in.nextString(); case "lockAfterTime" -> lockAfterTime = in.nextBoolean(); case "lockTimeInMinutes" -> lockTimeInMinutes = in.nextString(); default -> { @@ -105,9 +96,6 @@ class VaultSettingsJsonAdapter { vaultSettings.mountFlags().set(mountFlags); vaultSettings.filenameLengthLimit().set(filenameLengthLimit); vaultSettings.actionAfterUnlock().set(actionAfterUnlock); - vaultSettings.lockOnSleep().set(lockOnSleep); - vaultSettings.lockAfterIdleTime().set(lockAfterIdleTime); - vaultSettings.lockIdleTimeInMinutes().set(lockIdleTimeInMinutes); vaultSettings.lockAfterTime().set(lockAfterTime); vaultSettings.lockTimeInMinutes().set(lockTimeInMinutes); return vaultSettings; diff --git a/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplication.java b/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplication.java index d49ef2c7e..400760e2d 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplication.java +++ b/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplication.java @@ -15,9 +15,6 @@ import org.cryptomator.common.settings.UiTheme; import org.cryptomator.common.vaults.Vault; import org.cryptomator.common.vaults.VaultListManager; import org.cryptomator.common.vaults.Volume; -//import org.cryptomator.integrations.autolock.AutoLockException; -//import org.cryptomator.integrations.autolock.AutoLockProvider; -//import org.cryptomator.integrations.autolock.SystemState; import org.cryptomator.integrations.tray.TrayIntegrationProvider; import org.cryptomator.integrations.uiappearance.Theme; import org.cryptomator.integrations.uiappearance.UiAppearanceException; @@ -60,12 +57,9 @@ public class FxApplication extends Application { private final BooleanBinding hasVisibleWindows; private final UiAppearanceListener systemInterfaceThemeListener = this::systemInterfaceThemeChanged; private final VaultListManager vaultListManager; - //private final Optional autoLockProvider; @Inject - //FxApplication(Settings settings, Lazy mainWindow, Lazy preferencesWindow, Provider unlockWindowBuilderProvider, Provider lockWindowBuilderProvider, Lazy quitWindow, Optional trayIntegration, Optional appearanceProvider, VaultService vaultService, LicenseHolder licenseHolder, VaultListManager vaultListManager, Optional autoLockProvider) { FxApplication(Settings settings, Lazy mainWindow, Lazy preferencesWindow, Provider unlockWindowBuilderProvider, Provider lockWindowBuilderProvider, Lazy quitWindow, Optional trayIntegration, Optional appearanceProvider, VaultService vaultService, LicenseHolder licenseHolder, VaultListManager vaultListManager) { - this.settings = settings; this.mainWindow = mainWindow; this.preferencesWindow = preferencesWindow; @@ -79,7 +73,6 @@ public class FxApplication extends Application { this.visibleWindows = Stage.getWindows().filtered(Window::isShowing); this.hasVisibleWindows = Bindings.isNotEmpty(visibleWindows); this.vaultListManager = vaultListManager; - //this.autoLockProvider = autoLockProvider; } public void start() { @@ -90,7 +83,6 @@ public class FxApplication extends Application { settings.theme().addListener(this::appThemeChanged); loadSelectedStyleSheet(settings.theme().get()); - //applySystemListener(); } @Override @@ -206,36 +198,11 @@ public class FxApplication extends Application { }); } - /*private void applySystemListener() { - autoLockProvider.ifPresent(autoLockProvider -> { - try { - autoLockProvider.addListener(this::systemInterfaceStateChanged); - } catch (AutoLockException e) { - LOG.error("Failed to listen to changing System Power and Lock states."); - } - }); - } - private void systemInterfaceStateChanged(SystemState systemState) { - vaultListManager.getVaultList().forEach(vault -> { - switch (systemState) { - case SLEEP -> { - if (vault.getVaultSettings().lockOnSleep().get()) { - try { - vault.lock(true); - } catch (Volume.VolumeException e) { - e.printStackTrace(); - } - } - } - } - }); - } - */ - public void checkAutolock(Vault vault, Optional owner){ - if (vault.getVaultSettings().lockAfterTime().get()){ //TODO: this is lock after a fixed amount of minutes - LOG.info("Locking after {} minutes", vault.getVaultSettings().lockTimeInMinutes().get()); + private void checkAutolock(Vault vault, Optional owner){ + if (vault.getVaultSettings().lockAfterTime().get()){ + LOG.info("Locking after {} minutes.", vault.getVaultSettings().lockTimeInMinutes().get()); new java.util.Timer().schedule( new java.util.TimerTask() { @Override @@ -243,7 +210,7 @@ public class FxApplication extends Application { startLockWorkflow(vault, owner); } }, - new Date(System.currentTimeMillis() + Integer.parseInt(vault.getVaultSettings().lockTimeInMinutes().get()) * 60 * 1000) + new Date(System.currentTimeMillis() + (int)(Double.parseDouble(vault.getVaultSettings().lockTimeInMinutes().get()) * 60 * 1000)) ); } } diff --git a/main/ui/src/main/java/org/cryptomator/ui/vaultoptions/AutoLockVaultOptionsController.java b/main/ui/src/main/java/org/cryptomator/ui/vaultoptions/AutoLockVaultOptionsController.java index 722dcab97..21af0c3d9 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/vaultoptions/AutoLockVaultOptionsController.java +++ b/main/ui/src/main/java/org/cryptomator/ui/vaultoptions/AutoLockVaultOptionsController.java @@ -5,6 +5,8 @@ import org.cryptomator.ui.common.FxController; import javax.inject.Inject; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.fxml.FXML; import javafx.scene.control.CheckBox; import javafx.scene.control.TextField; @@ -17,10 +19,7 @@ public class AutoLockVaultOptionsController implements FxController { private final Vault vault; private final Stage window; - public CheckBox lockOnSleepCheckbox; - public CheckBox lockAfterIdleTimeCheckbox; public CheckBox lockAfterTimeCheckbox; - public TextField lockIdleTimeInMinutesTextField; public TextField lockTimeInMinutesTextField; @Inject @@ -31,10 +30,16 @@ public class AutoLockVaultOptionsController implements FxController { @FXML public void initialize() { - lockOnSleepCheckbox.selectedProperty().bindBidirectional(vault.getVaultSettings().lockOnSleep()); - lockAfterIdleTimeCheckbox.selectedProperty().bindBidirectional(vault.getVaultSettings().lockAfterIdleTime()); - lockIdleTimeInMinutesTextField.textProperty().bindBidirectional(vault.getVaultSettings().lockIdleTimeInMinutes()); lockAfterTimeCheckbox.selectedProperty().bindBidirectional(vault.getVaultSettings().lockAfterTime()); lockTimeInMinutesTextField.textProperty().bindBidirectional(vault.getVaultSettings().lockTimeInMinutes()); + //force the field to be a double with the correct decimal point + lockTimeInMinutesTextField.textProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, String oldValue, String newValue) { + if (!newValue.matches("\\d{0,9}([\\.]\\d{0,9})?")) { + lockTimeInMinutesTextField.setText(newValue.replaceAll("[^\\d]", "")); + } + } + }); } }