From e78155396d4f1aade4847e407e6de2f1215912f8 Mon Sep 17 00:00:00 2001 From: Martin Beyer Date: Wed, 8 Jul 2020 14:01:10 +0200 Subject: [PATCH] Implementing #1251 --- .../org/cryptomator/common/Environment.java | 3 ++ .../ui/preferences/AutoStartModule.java | 5 +-- .../ui/preferences/AutoStartStrategy.java | 7 ++++ .../ui/preferences/AutoStartWinStrategy.java | 33 ++++++++++++++++++- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/main/commons/src/main/java/org/cryptomator/common/Environment.java b/main/commons/src/main/java/org/cryptomator/common/Environment.java index 890840b7d..54e29df8b 100644 --- a/main/commons/src/main/java/org/cryptomator/common/Environment.java +++ b/main/commons/src/main/java/org/cryptomator/common/Environment.java @@ -40,6 +40,7 @@ public class Environment { LOG.debug("cryptomator.mountPointsDir: {}", System.getProperty("cryptomator.mountPointsDir")); LOG.debug("cryptomator.minPwLength: {}", System.getProperty("cryptomator.minPwLength")); LOG.debug("cryptomator.buildNumber: {}", System.getProperty("cryptomator.buildNumber")); + LOG.debug("cryptomator.binaryPath: {}", System.getProperty("cryptomator.binaryPath")); } public boolean useCustomLogbackConfig() { @@ -74,6 +75,8 @@ public class Environment { return getInt("cryptomator.minPwLength", DEFAULT_MIN_PW_LENGTH); } + public Optional getBinaryPath() { return getPath("cryptomator.binaryPath"); } + private int getInt(String propertyName, int defaultValue) { String value = System.getProperty(propertyName); try { 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 index d006d8681..1909b6af2 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartModule.java +++ b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartModule.java @@ -3,6 +3,7 @@ package org.cryptomator.ui.preferences; import dagger.Module; import dagger.Provides; import org.apache.commons.lang3.SystemUtils; +import org.cryptomator.common.Environment; import org.cryptomator.jni.MacFunctions; import java.util.Optional; @@ -12,12 +13,12 @@ abstract class AutoStartModule { @Provides @PreferencesScoped - public static Optional provideAutoStartStrategy(Optional macFunctions) { + public static Optional provideAutoStartStrategy(Optional macFunctions, Environment env) { if (SystemUtils.IS_OS_MAC_OSX && macFunctions.isPresent()) { return Optional.of(new AutoStartMacStrategy(macFunctions.get())); } else if (SystemUtils.IS_OS_WINDOWS) { Optional exeName = ProcessHandle.current().info().command(); - return exeName.map(AutoStartWinStrategy::new); + return exeName.map(x -> new AutoStartWinStrategy(x, env)); } 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 index 99b21b4cd..10a431e1f 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartStrategy.java +++ b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartStrategy.java @@ -21,4 +21,11 @@ public interface AutoStartStrategy { } } + class TogglingAutoStartWithPowershellFailedException extends TogglingAutoStartFailedException { + + public TogglingAutoStartWithPowershellFailedException(String message, Throwable cause) { + super(message, cause); + } + + } } diff --git a/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartWinStrategy.java b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartWinStrategy.java index f4a8b0578..3ffebfc18 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartWinStrategy.java +++ b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartWinStrategy.java @@ -1,5 +1,6 @@ package org.cryptomator.ui.preferences; +import org.cryptomator.common.Environment; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,9 +16,12 @@ class AutoStartWinStrategy implements AutoStartStrategy { private static final String HKCU_AUTOSTART_KEY = "\"HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\""; private static final String AUTOSTART_VALUE = "Cryptomator"; private final String exePath; + private static final String WINDOWS_START_MENU_FOLDER = "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs"; + private Environment env; - public AutoStartWinStrategy(String exePath) { + public AutoStartWinStrategy(String exePath, Environment env) { this.exePath = exePath; + this.env = env; } @Override @@ -47,9 +51,11 @@ class AutoStartWinStrategy implements AutoStartStrategy { if (finishedInTime) { LOG.debug("Added {} to registry key {}.", AUTOSTART_VALUE, HKCU_AUTOSTART_KEY); } else { + addShortcutOfAppToAutostartFolder(); throw new TogglingAutoStartFailedException("Adding registry value failed."); } } catch (IOException e) { + addShortcutOfAppToAutostartFolder(); throw new TogglingAutoStartFailedException("Adding registry value failed. " + command, e); } } @@ -66,9 +72,11 @@ class AutoStartWinStrategy implements AutoStartStrategy { if (finishedInTime) { LOG.debug("Removed {} from registry key {}.", AUTOSTART_VALUE, HKCU_AUTOSTART_KEY); } else { + removeShortcutOfAppFromAutostartFolder(); throw new TogglingAutoStartFailedException("Removing registry value failed."); } } catch (IOException e) { + removeShortcutOfAppFromAutostartFolder(); throw new TogglingAutoStartFailedException("Removing registry value failed. " + command, e); } } @@ -88,4 +96,27 @@ class AutoStartWinStrategy implements AutoStartStrategy { return finishedInTime; } + private void addShortcutOfAppToAutostartFolder() throws TogglingAutoStartWithPowershellFailedException{ + String startmenueDirectory = System.getProperty("user.home") + WINDOWS_START_MENU_FOLDER + "\\Cryptomator.lnk"; + String cryptomator = env.getBinaryPath().get().toString(); + String createShortcutCommand = "$s=(New-Object -COM WScript.Shell).CreateShortcut('" + startmenueDirectory + "');$s.TargetPath='" + cryptomator + "';$s.Save();"; + ProcessBuilder shortcutAdd = new ProcessBuilder("cmd", "/c", "Start powershell " + createShortcutCommand); + try { + shortcutAdd.start(); + } catch (IOException e) { + throw new TogglingAutoStartWithPowershellFailedException("Adding shortcut to autostart folder failed.", e); + } + } + + private void removeShortcutOfAppFromAutostartFolder() throws TogglingAutoStartWithPowershellFailedException{ + String startmenueDirectory = System.getProperty("user.home") + WINDOWS_START_MENU_FOLDER + "\\Cryptomator.lnk"; + ProcessBuilder shortcutRemove = new ProcessBuilder("cmd", "/c del \"" + startmenueDirectory + "\""); + try { + shortcutRemove.start(); + } catch (IOException e) { + throw new TogglingAutoStartWithPowershellFailedException("Removing shortcut from autostart folder failed.", e); + } + } + + }