Implementing #1251

This commit is contained in:
Martin Beyer
2020-07-08 14:01:10 +02:00
parent d3063c8117
commit e78155396d
4 changed files with 45 additions and 3 deletions

View File

@@ -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<Path> getBinaryPath() { return getPath("cryptomator.binaryPath"); }
private int getInt(String propertyName, int defaultValue) {
String value = System.getProperty(propertyName);
try {

View File

@@ -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<AutoStartStrategy> provideAutoStartStrategy(Optional<MacFunctions> macFunctions) {
public static Optional<AutoStartStrategy> provideAutoStartStrategy(Optional<MacFunctions> 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<String> exeName = ProcessHandle.current().info().command();
return exeName.map(AutoStartWinStrategy::new);
return exeName.map(x -> new AutoStartWinStrategy(x, env));
} else {
return Optional.empty();
}

View File

@@ -21,4 +21,11 @@ public interface AutoStartStrategy {
}
}
class TogglingAutoStartWithPowershellFailedException extends TogglingAutoStartFailedException {
public TogglingAutoStartWithPowershellFailedException(String message, Throwable cause) {
super(message, cause);
}
}
}

View File

@@ -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);
}
}
}