diff --git a/internal/agent/applink_other.go b/internal/agent/applink_other.go index 79f32ff76..9744ef21e 100644 --- a/internal/agent/applink_other.go +++ b/internal/agent/applink_other.go @@ -14,7 +14,7 @@ import ( ) // systemLinkHandler asks xdg-mime which desktop entry opens scheme's -// links, as xdg-open does. No xdg-mime, or a failed query, is unknown, +// 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) { @@ -32,8 +32,8 @@ func systemLinkHandler(scheme string) (string, bool, error) { if err != nil { return "", false, err } - if strings.TrimSpace(string(out)) != "" { - return "", true, nil + if name := strings.TrimSpace(string(out)); name != "" { + return desktopHandler(name, xdgDataDirs()) } if kde { return "", false, errors.New("xdg-mime: no answer under KDE") diff --git a/internal/agent/desktop_handler_test.go b/internal/agent/desktop_handler_test.go new file mode 100644 index 000000000..11d11bbf2 --- /dev/null +++ b/internal/agent/desktop_handler_test.go @@ -0,0 +1,57 @@ +package agent + +import ( + "os" + "path/filepath" + "testing" +) + +// geddy (11-03, Omarchy): xdg-mime named cindy.desktop for cindy://, +// but no such entry was there, so xdg-open fell back to the browser and +// a blank Chrome page opened instead of Cindy. The handler is only +// found when its entry and the entry's program are there. +func TestDesktopHandlerNeedsItsEntryAndProgram(t *testing.T) { + home, sys := t.TempDir(), t.TempDir() + prog := filepath.Join(t.TempDir(), "Cindy.AppImage") + if err := os.WriteFile(prog, []byte("#!/bin/sh\n"), 0o755); err != nil { + t.Fatal(err) + } + write := func(dir, rel, exec string) { + p := filepath.Join(dir, "applications", rel) + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(p, []byte("[Desktop Entry]\nType=Application\nName=Cindy\nExec="+exec+"\n"), 0o644); err != nil { + t.Fatal(err) + } + } + write(sys, "cindy.desktop", `"`+prog+`" %U`) + write(home, "gone.desktop", filepath.Join(t.TempDir(), "moved", "Cindy.AppImage")+" %u") + write(sys, "vendor/cindy.desktop", prog+" %u") + dirs := []string{home, sys} + + exe, found, err := desktopHandler("cindy.desktop", dirs) + if err != nil || !found || exe != prog || gone(exe) { + t.Errorf("an entry there: exe %q found %v err %v, want %q", exe, found, err, prog) + } + exe, found, err = desktopHandler("vendor-cindy.desktop", dirs) + if err != nil || !found || exe != prog { + t.Errorf("an entry in a subfolder: exe %q found %v err %v, want %q", exe, found, err, prog) + } + exe, found, err = desktopHandler("missing.desktop", dirs) + if err != nil || !found || !gone(exe) { + t.Errorf("no entry: exe %q found %v err %v, want a path that isn't there", exe, found, err) + } + exe, found, err = desktopHandler("gone.desktop", dirs) + if err != nil || !found || !gone(exe) { + t.Errorf("program moved: exe %q found %v err %v, want its old path", exe, found, err) + } + // and OpenAppLink then says so instead of handing the link to the browser + old := linkHandler + t.Cleanup(func() { linkHandler = old }) + linkHandler = func(string) (string, bool, error) { return desktopHandler("missing.desktop", dirs) } + opened := false + if err := OpenAppLink(CindyLink("http://127.0.0.1:3425"), func(string) error { opened = true; return nil }); err == nil || opened { + t.Errorf("no entry: err %v opened %v, want an error and nothing opened", err, opened) + } +} diff --git a/internal/agent/lody.go b/internal/agent/lody.go index 737cc53fd..6fc53b915 100644 --- a/internal/agent/lody.go +++ b/internal/agent/lody.go @@ -3,6 +3,8 @@ package agent import ( "encoding/base64" "encoding/json" + "errors" + "io/fs" "net/url" "os" "os/exec" @@ -178,22 +180,7 @@ func lodyCommand(link string) []string { if lodyGOOS == "darwin" || lodyGOOS == "windows" { return nil } - data := appdir.Getenv("XDG_DATA_HOME") - if data == "" { - if home, err := os.UserHomeDir(); err == nil { - data = filepath.Join(home, ".local", "share") - } - } - dirs := []string{data} - sys := os.Getenv("XDG_DATA_DIRS") - if sys == "" { - sys = "/usr/local/share:/usr/share" - } - for _, d := range filepath.SplitList(sys) { - if filepath.IsAbs(d) { - dirs = append(dirs, d) - } - } + dirs := xdgDataDirs() for _, name := range lodyEntries { // the first folder with an entry of that name has the one that // counts, as for xdg-open @@ -220,6 +207,48 @@ func lodyCommand(link string) []string { // dropped, and the URL put last when Exec takes none. It is nil with no // Exec, or with no program there to run. func desktopExec(entry, link string) []string { + argv := desktopArgs(entry, link) + if len(argv) == 0 { + return nil + } + if !filepath.IsAbs(argv[0]) { + p, err := proc.LookPath(argv[0]) + if err != nil { + return nil + } + argv[0] = p + } else if st, err := os.Stat(argv[0]); err != nil || st.IsDir() || st.Mode()&0111 == 0 { + // an AppImage moved or deleted since Lody wrote its entry + return nil + } + return argv +} + +// xdgDataDirs are the folders whose applications/ hold desktop entries, +// the user's first, as xdg-open reads them. +func xdgDataDirs() []string { + data := appdir.Getenv("XDG_DATA_HOME") + if data == "" { + if home, err := os.UserHomeDir(); err == nil { + data = filepath.Join(home, ".local", "share") + } + } + dirs := []string{data} + sys := os.Getenv("XDG_DATA_DIRS") + if sys == "" { + sys = "/usr/local/share:/usr/share" + } + for _, d := range filepath.SplitList(sys) { + if filepath.IsAbs(d) { + dirs = append(dirs, d) + } + } + return dirs +} + +// desktopArgs is the command line a desktop entry's Exec gives for link, +// its program not looked for yet; nil with no Exec. +func desktopArgs(entry, link string) []string { line := "" in := false for _, l := range strings.Split(entry, "\n") { @@ -273,15 +302,64 @@ func desktopExec(entry, link string) []string { if !gaveURL { argv = append(argv, link) } - if !filepath.IsAbs(argv[0]) { - p, err := proc.LookPath(argv[0]) - if err != nil { - return nil - } - argv[0] = p - } else if st, err := os.Stat(argv[0]); err != nil || st.IsDir() || st.Mode()&0111 == 0 { - // an AppImage moved or deleted since Lody wrote its entry - return nil - } return argv } + +// desktopHandler is the program the desktop entry name runs, looked for +// in dirs' applications/ as xdg-open does. xdg-mime names an entry that +// was written to mimeapps.list, which may never have been made, or whose +// program has moved since: Electron's setAsDefaultProtocolClient writes +// the name, and an AppImage that wasn't integrated has no entry. xdg-open +// then falls back to the browser, which opened a blank page instead of +// Cindy on Omarchy (geddy, 11-03). With no entry, exe is where the user's +// own would be, which isn't there; with its program gone, that program. +// An entry that can't be read is unknown. +func desktopHandler(name string, dirs []string) (exe string, found bool, err error) { + if !strings.HasSuffix(name, ".desktop") || strings.ContainsAny(name, "/\\") { + return "", true, nil + } + for _, dir := range dirs { + if dir == "" { + continue + } + for _, rel := range entryPaths(name) { + path := filepath.Join(dir, "applications", rel) + b, rerr := os.ReadFile(path) + if errors.Is(rerr, fs.ErrNotExist) { + continue + } + if rerr != nil { + return "", false, rerr + } + argv := desktopArgs(string(b), "") + if len(argv) == 0 { + // no Exec: xdg-open passes it by, as with none + return path, true, nil + } + if filepath.IsAbs(argv[0]) { + return argv[0], true, nil + } + if p, lerr := proc.LookPath(argv[0]); lerr == nil { + return p, true, nil + } + return "", false, nil + } + } + if len(dirs) == 0 || dirs[0] == "" { + return "", false, nil + } + return filepath.Join(dirs[0], "applications", name), true, nil +} + +// entryPaths are where under applications/ an entry's id can be: the +// id itself, or with its dashes standing for subfolders (kde-foo.desktop +// as kde/foo.desktop), as the Desktop Entry Specification names them. +func entryPaths(id string) []string { + paths := []string{id} + for i := 0; i < len(id); i++ { + if id[i] == '-' { + paths = append(paths, filepath.Join(strings.ReplaceAll(id[:i], "-", "/"), id[i+1:])) + } + } + return paths +}