From cc1517f1b805111470ad78b73e0924dcc03e5f61 Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Mon, 6 Jan 2020 12:37:36 +0100 Subject: [PATCH] Added checkbox for auto start on macOS (#418) --- main/pom.xml | 2 +- .../ui/preferences/AutoStartMacStrategy.java | 39 +++++++++++++++++++ .../ui/preferences/AutoStartModule.java | 26 +++++++++++++ .../ui/preferences/AutoStartStrategy.java | 10 +++++ .../GeneralPreferencesController.java | 27 ++++++++++++- .../ui/preferences/PreferencesModule.java | 2 +- .../resources/fxml/preferences_general.fxml | 2 + .../main/resources/i18n/strings.properties | 1 + 8 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartMacStrategy.java create mode 100644 main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartModule.java create mode 100644 main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartStrategy.java diff --git a/main/pom.xml b/main/pom.xml index fc263cd3a..1df55e0d1 100644 --- a/main/pom.xml +++ b/main/pom.xml @@ -25,7 +25,7 @@ 1.9.0-rc2 - 2.2.1 + 2.2.2 1.2.1 1.1.11 1.0.10 diff --git a/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartMacStrategy.java b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartMacStrategy.java new file mode 100644 index 000000000..3c5205316 --- /dev/null +++ b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartMacStrategy.java @@ -0,0 +1,39 @@ +package org.cryptomator.ui.preferences; + +import org.cryptomator.jni.MacFunctions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class AutoStartMacStrategy implements AutoStartStrategy { + + private static final Logger LOG = LoggerFactory.getLogger(AutoStartMacStrategy.class); + + private final MacFunctions macFunctions; + + public AutoStartMacStrategy(MacFunctions macFunctions) { + this.macFunctions = macFunctions; + } + + @Override + public boolean isAutoStartEnabled() { + return macFunctions.launchServices().isLoginItemEnabled(); + } + + @Override + public void enableAutoStart() { + if (macFunctions.launchServices().enableLoginItem()) { + LOG.debug("Added login item."); + } else { + LOG.warn("Adding login item failed."); + } + } + + @Override + public void disableAutoStart() { + if (macFunctions.launchServices().disableLoginItem()) { + LOG.debug("Removed login item."); + } else { + LOG.warn("Removing login item failed."); + } + } +} diff --git a/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartModule.java b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartModule.java new file mode 100644 index 000000000..839043973 --- /dev/null +++ b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartModule.java @@ -0,0 +1,26 @@ +package org.cryptomator.ui.preferences; + +import dagger.Module; +import dagger.Provides; +import org.apache.commons.lang3.SystemUtils; +import org.cryptomator.jni.MacFunctions; + +import java.util.Optional; + +@Module +abstract class AutoStartModule { + + @Provides + @PreferencesScoped + public static Optional provideAutoStartStrategy(Optional macFunctions) { + if (SystemUtils.IS_OS_MAC_OSX && macFunctions.isPresent()) { + return Optional.of(new AutoStartMacStrategy(macFunctions.get())); + } else if (SystemUtils.IS_OS_WINDOWS) { + // TODO: add windows support + return Optional.empty(); + } else { + return Optional.empty(); + } + } + +} diff --git a/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartStrategy.java b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartStrategy.java new file mode 100644 index 000000000..543804795 --- /dev/null +++ b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartStrategy.java @@ -0,0 +1,10 @@ +package org.cryptomator.ui.preferences; + +public interface AutoStartStrategy { + + boolean isAutoStartEnabled(); + + void enableAutoStart(); + + void disableAutoStart(); +} diff --git a/main/ui/src/main/java/org/cryptomator/ui/preferences/GeneralPreferencesController.java b/main/ui/src/main/java/org/cryptomator/ui/preferences/GeneralPreferencesController.java index 6866e5266..9a7985901 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/preferences/GeneralPreferencesController.java +++ b/main/ui/src/main/java/org/cryptomator/ui/preferences/GeneralPreferencesController.java @@ -17,6 +17,7 @@ import org.slf4j.LoggerFactory; import javax.inject.Inject; import javax.inject.Named; +import java.util.Optional; @PreferencesScoped public class GeneralPreferencesController implements FxController { @@ -25,17 +26,20 @@ public class GeneralPreferencesController implements FxController { private final Settings settings; private final boolean trayMenuSupported; + private final Optional autoStartStrategy; public ChoiceBox themeChoiceBox; public CheckBox startHiddenCheckbox; public CheckBox debugModeCheckbox; + public CheckBox autoStartCheckbox; public ToggleGroup nodeOrientation; public RadioButton nodeOrientationLtr; public RadioButton nodeOrientationRtl; @Inject - GeneralPreferencesController(Settings settings, @Named("trayMenuSupported") boolean trayMenuSupported) { + GeneralPreferencesController(Settings settings, @Named("trayMenuSupported") boolean trayMenuSupported, Optional autoStartStrategy) { this.settings = settings; this.trayMenuSupported = trayMenuSupported; + this.autoStartStrategy = autoStartStrategy; } @FXML @@ -48,6 +52,9 @@ public class GeneralPreferencesController implements FxController { debugModeCheckbox.selectedProperty().bindBidirectional(settings.debugMode()); + autoStartCheckbox.setSelected(this.isAutoStartEnabled()); + autoStartCheckbox.selectedProperty().addListener(this::toggleAutoStart); + nodeOrientationLtr.setSelected(settings.userInterfaceOrientation().get() == NodeOrientation.LEFT_TO_RIGHT); nodeOrientationRtl.setSelected(settings.userInterfaceOrientation().get() == NodeOrientation.RIGHT_TO_LEFT); nodeOrientation.selectedToggleProperty().addListener(this::toggleNodeOrientation); @@ -57,6 +64,24 @@ public class GeneralPreferencesController implements FxController { return this.trayMenuSupported; } + public boolean isAutoStartSupported() { + return autoStartStrategy.isPresent(); + } + + private boolean isAutoStartEnabled() { + return autoStartStrategy.map(AutoStartStrategy::isAutoStartEnabled).orElse(false); + } + + private void toggleAutoStart(@SuppressWarnings("unused") ObservableValue observable, @SuppressWarnings("unused") boolean oldValue, boolean newValue) { + autoStartStrategy.ifPresent(autoStart -> { + if (newValue) { + autoStart.enableAutoStart(); + } else { + autoStart.disableAutoStart(); + } + }); + } + private void toggleNodeOrientation(@SuppressWarnings("unused") ObservableValue observable, @SuppressWarnings("unused") Toggle oldValue, Toggle newValue) { if (nodeOrientationLtr.equals(newValue)) { settings.userInterfaceOrientation().set(NodeOrientation.LEFT_TO_RIGHT); diff --git a/main/ui/src/main/java/org/cryptomator/ui/preferences/PreferencesModule.java b/main/ui/src/main/java/org/cryptomator/ui/preferences/PreferencesModule.java index 981d9c87f..8b76364ef 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/preferences/PreferencesModule.java +++ b/main/ui/src/main/java/org/cryptomator/ui/preferences/PreferencesModule.java @@ -22,7 +22,7 @@ import java.util.Map; import java.util.Optional; import java.util.ResourceBundle; -@Module +@Module(includes = {AutoStartModule.class}) abstract class PreferencesModule { @Provides diff --git a/main/ui/src/main/resources/fxml/preferences_general.fxml b/main/ui/src/main/resources/fxml/preferences_general.fxml index f5ee40489..a34c9e2ed 100644 --- a/main/ui/src/main/resources/fxml/preferences_general.fxml +++ b/main/ui/src/main/resources/fxml/preferences_general.fxml @@ -33,5 +33,7 @@ + + diff --git a/main/ui/src/main/resources/i18n/strings.properties b/main/ui/src/main/resources/i18n/strings.properties index a401f6fdf..05184f9de 100644 --- a/main/ui/src/main/resources/i18n/strings.properties +++ b/main/ui/src/main/resources/i18n/strings.properties @@ -203,3 +203,4 @@ passwordStrength.messageLabel.4=Very strong # Quit quit.prompt=Quit application? There are unlocked vaults. quit.lockAndQuit=Lock and Quit +preferences.general.autoStart=Launch Cryptomator on system start