|
| 1 | +package org.cryptomator.linux.autostart; |
| 2 | + |
| 3 | +import org.cryptomator.integrations.autostart.AutoStartProvider; |
| 4 | +import org.cryptomator.integrations.autostart.ToggleAutoStartFailedException; |
| 5 | +import org.cryptomator.integrations.common.CheckAvailability; |
| 6 | +import org.cryptomator.integrations.common.OperatingSystem; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.nio.file.StandardOpenOption; |
| 14 | +import java.util.Objects; |
| 15 | + |
| 16 | +/** |
| 17 | + * Enables autostart for Linux desktop environments following the freedesktop standard. |
| 18 | + * <p> |
| 19 | + * This service is based on <a href=https://specifications.freedesktop.org/autostart-spec/autostart-spec-0.5.html>version 0.5 of the freedesktop autostart-spec</a>. |
| 20 | + */ |
| 21 | +@CheckAvailability |
| 22 | +@OperatingSystem(OperatingSystem.Value.LINUX) |
| 23 | +public class FreedesktopAutoStartService implements AutoStartProvider { |
| 24 | + |
| 25 | + private static final Logger LOG = LoggerFactory.getLogger(FreedesktopAutoStartService.class); |
| 26 | + private static final String CMD_PROPERTY = "cryptomator.integrationsLinux.autoStartCmd"; |
| 27 | + private static final String AUTOSTART_FILENAME = "Cryptomator.desktop"; |
| 28 | + private static final String CONTENT_TEMPLATE = """ |
| 29 | + [Desktop Entry] |
| 30 | + Type=Application |
| 31 | + Exec=%s |
| 32 | + Hidden=false |
| 33 | + NoDisplay=false |
| 34 | + X-GNOME-Autostart-enabled=true |
| 35 | + Name=Cryptomator |
| 36 | + Comment=Created with %s |
| 37 | + """; |
| 38 | + |
| 39 | + private final Path autostartFile; |
| 40 | + private final String content; |
| 41 | + |
| 42 | + public FreedesktopAutoStartService() { |
| 43 | + var xdgConfigDirString = Objects.requireNonNullElse(System.getenv("XDG_CONFIG_HOME"), System.getProperty("user.home") + "/.config"); |
| 44 | + this.autostartFile = Path.of(xdgConfigDirString, "autostart", AUTOSTART_FILENAME); |
| 45 | + |
| 46 | + var execValue = System.getProperty(CMD_PROPERTY); |
| 47 | + if (execValue == null) { |
| 48 | + LOG.debug("Property {} not set, using command path", CMD_PROPERTY); |
| 49 | + execValue = ProcessHandle.current().info().command().orElse(""); |
| 50 | + } |
| 51 | + this.content = CONTENT_TEMPLATE.formatted(execValue, this.getClass().getName()); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void enable() throws ToggleAutoStartFailedException { |
| 56 | + try { |
| 57 | + Files.writeString(autostartFile, content, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); |
| 58 | + } catch (IOException e) { |
| 59 | + throw new ToggleAutoStartFailedException("Failed to activate Cryptomator autostart for GNOME desktop environment.", e); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void disable() throws ToggleAutoStartFailedException { |
| 65 | + try { |
| 66 | + Files.deleteIfExists(autostartFile); |
| 67 | + } catch (IOException e) { |
| 68 | + throw new ToggleAutoStartFailedException("Failed to deactivate Cryptomator autostart for GNOME desktop environment.", e); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean isEnabled() { |
| 74 | + return Files.exists(autostartFile); |
| 75 | + } |
| 76 | + |
| 77 | + @CheckAvailability |
| 78 | + public boolean isSupported() { |
| 79 | + //TODO: might need to research which Desktop Environments support this |
| 80 | + return Files.exists(autostartFile.getParent()); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments