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