Files
magpie/internal/gui/scheme_linux.go
T
yetone 4bd3f3a4b5 import: magpie://import links add a provider after the user confirms
A vendor or relay can hand out a ready-made provider as a link. The app
registers the magpie scheme (Info.plist on macOS, HKCU on Windows, a
.desktop handler on Linux), a second launch hands the link to the running
one, and the window shows what the link would add, with the hosts the
prompts and key would go to, before anything is saved. `magpie import`
does the same in a terminal.
2026-09-24 12:17:45 +08:00

47 lines
1.3 KiB
Go

package gui
import (
"os"
"os/exec"
"path/filepath"
"strings"
)
// registerScheme makes magpie:// links open this executable: a desktop
// entry that claims the scheme, then the desktop's default for it. An
// entry that already runs this executable and claims it is left alone.
func registerScheme() error {
exe, err := os.Executable()
if err != nil {
return err
}
data := os.Getenv("XDG_DATA_HOME")
if data == "" {
home, _ := os.UserHomeDir()
data = filepath.Join(home, ".local", "share")
}
dir := filepath.Join(data, "applications")
path := filepath.Join(dir, "magpie.desktop")
entry := `[Desktop Entry]
Type=Application
Name=magpie
Comment=Every agent's model. One place.
Exec=` + exe + ` %u
Icon=magpie
Categories=Development;Utility;
MimeType=x-scheme-handler/magpie;
Terminal=false
`
if old, err := os.ReadFile(path); err == nil && strings.Contains(string(old), "Exec="+exe+" %u") && strings.Contains(string(old), "x-scheme-handler/magpie") {
return nil
}
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
if err := os.WriteFile(path, []byte(entry), 0o644); err != nil {
return err
}
_ = exec.Command("update-desktop-database", dir).Run()
return exec.Command("xdg-mime", "default", "magpie.desktop", "x-scheme-handler/magpie").Run()
}