Files
magpie/internal/agent/applink_other.go
T
yetone 4cd01c964a agent: a Linux link handler counts only when its desktop entry and program are there (geddy)
geddy (X, 2117406492832010548) added magpie to Cindy on Omarchy: a blank
Chrome tab opened and magpie said it was handed to Cindy. On Linux,
systemLinkHandler took any answer from `xdg-mime query default
x-scheme-handler/cindy` as "found". xdg-utils 1.2.1 (Arch) checks that the
entry exists only for mimeapps.list; a stale line in mimeinfo.cache or
defaults.list comes back as a name with nothing behind it. xdg-open then
can't find the entry and falls back to $BROWSER / chromium, which is the
blank tab.

Now the name xdg-mime gives is looked up in every XDG data dir's
applications/ (with the dash-as-subfolder variants), and its Exec program
must be there (absolute and present, or on PATH). With no entry, the gone
path goes to OpenAppLink, which returns NoLinkHandlerError ("Cindy's links
(cindy://) open …/applications/cindy.desktop, which isn't there") instead
of opening anything. A read error other than not-exist is returned as
unknown. desktopExec (Lody) now shares the same parse (desktopArgs) and
the XDG dir list.

What was run:
- TestDesktopHandlerNeedsItsEntryAndProgram (new) and -run
  'Desktop|OpenAppLink|Lody|CommandProgram' on mote (macOS) and Linux
  (wsl-run, magpie-desktop, Go 1.26.8): ok.
- Real xdg-mime on Linux in a temp HOME, XDG_CURRENT_DESKTOP=Hyprland, a
  stale `x-scheme-handler/cindy=cindy.desktop;` in mimeinfo.cache and no
  entry: origin/main said found and handed the link to xdg-open; this
  change returns the error above.
- Broken by hand: returning found with no entry turns the new test red.
- gofmt -l clean; GOOS=linux go vet -tags nogui and GOOS=windows go vet
  of ./internal/agent on mote: ok.
- go test ./... on Linux: every package ok except reds that fail the same
  at base c86fa2a34 on that box: 7 internal/agent install-detection
  tests (#4033; TestHowInstalled fails at base too with -count=3),
  TestCodexPoolSaysTheClientsVersion (#4807), TestCCListingKeepsItsNames
  (#4925). Root, internal/cli and internal/gui need GTK4 there, so they
  ran again with -tags nogui: all three ok.
- Rebased on 1a92a97ca (5 commits, internal/agent's Claude Desktop
  models among them): internal/agent on Linux again, only #4033's
  install-detection reds.

Not tried: a real Omarchy VM (its Hyper-V adapter had no IP, ssh was
refused) and a real Cindy install on Linux.
2026-11-03 09:50:31 +08:00

43 lines
1.3 KiB
Go

//go:build !windows && !(darwin && cgo)
package agent
import (
"context"
"errors"
"runtime"
"strings"
"time"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/proc"
)
// systemLinkHandler asks xdg-mime which desktop entry opens scheme's
// links, as xdg-open does, and looks for that entry and its program. No xdg-mime, or a failed query, is unknown,
// and so is KDE's empty answer: kde-open reads KDE's own service cache,
// which can know a handler xdg-mime's lookup doesn't.
func systemLinkHandler(scheme string) (string, bool, error) {
if runtime.GOOS == "darwin" {
return "", false, errors.New("no launch services without cgo")
}
kde := strings.Contains(strings.ToUpper(appdir.Getenv("XDG_CURRENT_DESKTOP")), "KDE")
tool := proc.FindTool("xdg-mime")
if tool == "" {
return "", false, errors.New("no xdg-mime")
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
out, err := proc.CommandContext(ctx, tool, "query", "default", "x-scheme-handler/"+scheme).Output()
if err != nil {
return "", false, err
}
if name := strings.TrimSpace(string(out)); name != "" {
return desktopHandler(name, xdgDataDirs())
}
if kde {
return "", false, errors.New("xdg-mime: no answer under KDE")
}
return "", false, nil
}