Files
magpie/internal/uninstall/system_linux.go
T
yetone c2fd48782b Put back what 1f05c311d took out of main by mistake
1f05c311d was committed with its worktree's older tree on top of a
fresh origin/main (git reset --soft), so beside the memory fix it
undid every file 610ae1f54..c1312ac15 changed (45 files: key balance,
import links, project subagents, uninstall, site, LESSONS.md and
more). This commit restores each of them to c1312ac15 byte for byte;
the tree is now c1312ac15 plus the memory fix alone
(git diff --stat c1312ac15 HEAD: the 4 files of the fix).
2026-11-13 12:18:23 +08:00

62 lines
1.6 KiB
Go

package uninstall
import (
"errors"
"os"
"path/filepath"
"strings"
"github.com/yetone/magpie/internal/appdir"
)
// dataHome is where the app's desktop entry, icon and WebKitGTK's data
// are (gui's dataHome).
func dataHome() string {
if x := appdir.Getenv("XDG_DATA_HOME"); filepath.IsAbs(x) {
return x
}
h, _ := os.UserHomeDir()
return filepath.Join(h, ".local", "share")
}
// unregisterScheme takes out the desktop entry the app made for magpie://
// links while it runs exe, or an exe that is gone, and its icon with it.
func unregisterScheme(exe string) error {
_, err := removeEntry("magpie-ccswitch.desktop", exe)
gone, err2 := removeEntry("magpie.desktop", exe)
if gone {
_ = os.Remove(filepath.Join(dataHome(), "icons", "hicolor", "256x256", "apps", "magpie.png"))
}
return errors.Join(err, err2)
}
// removeEntry takes out one of the app's desktop entries while it runs exe,
// or an exe that is gone.
func removeEntry(name, exe string) (bool, error) {
entry := filepath.Join(dataHome(), "applications", name)
b, err := os.ReadFile(entry)
if err != nil {
return false, nil
}
for _, l := range strings.Split(string(b), "\n") {
if run, ok := strings.CutPrefix(l, "Exec="); ok {
run = strings.TrimSuffix(run, " %u")
if run != exe {
if _, err := os.Stat(run); err == nil {
return false, nil // another magpie's
}
}
}
}
return true, os.Remove(entry)
}
// webData is WebKitGTK's folder for the app's pages (named after the
// program, gui's programName).
func webData() []string {
if appdir.Portable() != "" {
return nil
}
return []string{filepath.Join(dataHome(), "magpie")}
}