//go:build linux
|
|
package startup
|
|
import (
|
"fmt"
|
"os"
|
"path/filepath"
|
)
|
|
const desktopEntry = `[Desktop Entry]
|
Type=Application
|
Name=PrivateVoice Input
|
Exec=%s
|
Hidden=false
|
X-GNOME-Autostart-enabled=true
|
`
|
|
func autostartPath() string {
|
return autostartPathForName("privatevoice-input.desktop")
|
}
|
|
func autostartPathForName(name string) string {
|
configDir, err := os.UserConfigDir()
|
if err != nil {
|
home, _ := os.UserHomeDir()
|
configDir = filepath.Join(home, ".config")
|
}
|
return filepath.Join(configDir, "autostart", name)
|
}
|
|
func isEnabled() bool {
|
removeLegacyAutostartFiles()
|
_, err := os.Stat(autostartPath())
|
return err == nil
|
}
|
|
func setEnabled(enable bool) error {
|
path := autostartPath()
|
|
if !enable {
|
if err := removeAutostartFile(path); err != nil {
|
return err
|
}
|
removeLegacyAutostartFiles()
|
return nil
|
}
|
|
removeLegacyAutostartFiles()
|
|
exe, err := os.Executable()
|
if err != nil {
|
return err
|
}
|
|
dir := filepath.Dir(path)
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
return err
|
}
|
|
content := fmt.Sprintf(desktopEntry, exe)
|
return os.WriteFile(path, []byte(content), 0644)
|
}
|
|
func removeLegacyAutostartFiles() {
|
_ = removeAutostartFile(autostartPathForName("voicesnap.desktop"))
|
}
|
|
func removeAutostartFile(path string) error {
|
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
return err
|
}
|
return nil
|
}
|