@asombrado_a (X, 2122995130097713493) has CC Switch and magpie both installed, and a ccswitch://v1/import link opened magpie. magpie claims magpie:// only since03314b25a(v0.1.4455), but v0.1.3981 to v0.1.4454 claimed CC Switch's scheme too, and what they registered stays: - macOS: Launch Services keeps the old Info.plist's ccswitch claim after the bundle is rewritten in place (seen on mote below); - Windows: HKCU\Software\Classes\ccswitch running magpie.exe; - Linux: magpie-ccswitch.desktop as the x-scheme-handler/ccswitch default. The owner's call in03314b25astands: magpie doesn't read CC Switch's links. A ccswitch:// link that reaches magpie anyway is now handed on: - cli.run, before anything is read, moved or started (Windows, Linux: `magpie ccswitch://…`), and the Mac's URL event in gui.Run call ccswitchlink.Forward; - macOS: `open -b com.ccswitch.desktop <link>`, then `lsregister -f` on magpie's own bundle, which reads its current Info.plist and drops the claim; - Windows: the handler's program when it isn't this magpie, else %LOCALAPPDATA%\Programs\CC Switch\cc-switch.exe (where CC Switch's per-user MSI installs it); when the handler ran this magpie the key is rewritten as that MSI writes it ("URL:com.ccswitch.desktop protocol", URL Protocol, DefaultIcon, shell\open\command "exe" "%1"); - Linux: CC Switch's .desktop entry in the XDG data dirs (its first start's cc-switch-handler.desktop with %u first, else the package's "CC Switch.desktop", whose Exec has no %u, so the link is appended) is run, and a default that is magpie-ccswitch.desktop moves to it; magpie's entry is removed while it runs this magpie or a gone binary. Any other default is left alone. - No CC Switch: ErrNotInstalled, nothing opened, the claim kept, so magpie opens such a link as it does now (it says it reads magpie:// links only). Existing users: nothing changes until a ccswitch:// link reaches magpie. The first one is opened in CC Switch and the claim goes back, so later links go to CC Switch directly. CC Switch's facts come from farion1231/cc-switch (tauri.conf.json's deep-link schemes ["ccswitch"], wix per-user-main.wxs, lib.rs's register_all) and its v4.0.35 assets: macOS.zip's Info.plist (com.ccswitch.desktop, CFBundleURLSchemes ccswitch), the portable zip's cc-switch.exe, the .deb's "CC Switch.desktop" and /usr/bin/cc-switch. Run (this tree ond74754970, then again where named after a rebase on589e4ba6d, which touches none of these files): - mote: go test -count=1 ./internal/ccswitchlink ./internal/cli ./internal/gui ok; go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go build ok; gofmt -l empty. - wsl-run (magpie-desktop) and win-run (xiaomi-gpu): go test -count=1 -v ./internal/ccswitchlink ok (Windows writes a test key of its own, Software\Classes\magpie-test-ccswitch-<pid>, removed after). - Full suite on mica (go test -count=1 -timeout 45m ./..., ond74754970): 101 packages ok; reds, all filed and none in a package this touches: internal/dirent's watch tests (#7031; red alone on a fresh origin/main589e4ba6dtoo), TestGatewayModeListsNoOwnSessions (#7064) and TestWSLStoppedDistroKept (#7071), both ok alone on mica. - After the rebase on589e4ba6d: go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go build, gofmt -l on mote ok; ./internal/ccswitchlink and ./internal/gui ok on mote; ./internal/cli ok on mica. On mote three cli tests reach the 127.0.0.1:3425 an ssh port forward holds there (#6223). - Real, macOS (mote, in ~/runs, cleaned up with lsregister -u): a test bundle registered with the old ccswitch claim kept it 5s after its Info.plist was rewritten without it; the built Forward then opened the real CC Switch v4.0.35 app and the dump showed the claim gone. Without CC Switch registered it returned ErrNotInstalled. - Real, Linux (magpie-desktop, sandbox HOME and XDG dirs): with magpie-ccswitch.desktop as the real xdg-mime default, Forward ran cc-switch-handler.desktop's Exec with the link and the default was cc-switch-handler.desktop after. - Broken by hand: no lsregister call (darwin), no xdg-mime default (linux) and no handBack (windows) each turned their test red. - GUI suite: not run; no page, app.js or i18n change. Not tried: a real CC Switch install on Windows (the box's real HKCU ccswitch key is left alone; the test uses its own key), and a real CC Switch .deb or AppImage on Linux (its entry was faked with the package's own bytes).
233 lines
5.6 KiB
Go
233 lines
5.6 KiB
Go
package ccswitchlink
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
const mime = "x-scheme-handler/" + Scheme
|
|
|
|
// magpieEntry is the desktop entry magpie v0.1.3981 to v0.1.4454 wrote for
|
|
// ccswitch:// and made the default when it had none.
|
|
const magpieEntry = "magpie-ccswitch.desktop"
|
|
|
|
// executable is this program's path; tests stand in for it.
|
|
var executable = os.Executable
|
|
|
|
// start starts CC Switch; tests stand in for it.
|
|
var start = func(argv []string) error { return proc.StartReaped(proc.Command(argv[0], argv[1:]...)) }
|
|
|
|
// forward runs the Exec of CC Switch's desktop entry with the link. Then,
|
|
// when the desktop's default for ccswitch:// is magpie's entry, that entry
|
|
// goes and CC Switch's is made the default.
|
|
func forward(link string) error {
|
|
id, exec := entry()
|
|
if id == "" {
|
|
return ErrNotInstalled
|
|
}
|
|
argv := expand(exec, link)
|
|
if len(argv) == 0 {
|
|
return ErrNotInstalled
|
|
}
|
|
if err := start(argv); err != nil {
|
|
return err
|
|
}
|
|
out, _ := proc.Command("xdg-mime", "query", "default", mime).Output()
|
|
if strings.TrimSpace(string(out)) != magpieEntry {
|
|
return nil
|
|
}
|
|
dir := filepath.Join(dataHome(), "applications")
|
|
if ours(filepath.Join(dir, magpieEntry)) {
|
|
_ = os.Remove(filepath.Join(dir, magpieEntry))
|
|
_ = proc.Command("update-desktop-database", dir).Run()
|
|
}
|
|
return proc.Command("xdg-mime", "default", id, mime).Run()
|
|
}
|
|
|
|
// dataHome is $XDG_DATA_HOME, or ~/.local/share without it.
|
|
func dataHome() string {
|
|
if d := appdir.Getenv("XDG_DATA_HOME"); d != "" {
|
|
return d
|
|
}
|
|
home, _ := os.UserHomeDir()
|
|
return filepath.Join(home, ".local", "share")
|
|
}
|
|
|
|
// dataDirs are the folders desktop entries are looked up in, the user's
|
|
// first.
|
|
func dataDirs() []string {
|
|
dirs := []string{dataHome()}
|
|
sys := appdir.Getenv("XDG_DATA_DIRS")
|
|
if sys == "" {
|
|
sys = "/usr/local/share:/usr/share"
|
|
}
|
|
for _, d := range filepath.SplitList(sys) {
|
|
if d != "" && !slices.Contains(dirs, d) {
|
|
dirs = append(dirs, d)
|
|
}
|
|
}
|
|
return dirs
|
|
}
|
|
|
|
// entry is the id and Exec of the desktop entry that handles ccswitch://
|
|
// other than magpie's: one whose Exec takes a URL (cc-switch-handler.desktop)
|
|
// before one that doesn't ("CC Switch.desktop" of the package, whose Exec
|
|
// is then given the link as its last argument). An entry earlier in the
|
|
// data folders hides one of the same id later.
|
|
func entry() (id, exec string) {
|
|
seen := map[string]bool{}
|
|
var plain, plainExec string
|
|
for _, d := range dataDirs() {
|
|
files, _ := filepath.Glob(filepath.Join(d, "applications", "*.desktop"))
|
|
for _, f := range files {
|
|
name := filepath.Base(f)
|
|
if seen[name] {
|
|
continue
|
|
}
|
|
seen[name] = true
|
|
if strings.HasPrefix(name, "magpie") {
|
|
continue
|
|
}
|
|
x, ok := handles(f)
|
|
if !ok {
|
|
continue
|
|
}
|
|
if strings.Contains(x, "%u") || strings.Contains(x, "%U") {
|
|
return name, x
|
|
}
|
|
if plain == "" {
|
|
plain, plainExec = name, x
|
|
}
|
|
}
|
|
}
|
|
return plain, plainExec
|
|
}
|
|
|
|
// handles is the Exec of a desktop entry that lists ccswitch:// in its
|
|
// MimeType and isn't hidden.
|
|
func handles(path string) (string, bool) {
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
var exec string
|
|
claims, in := false, false
|
|
for _, l := range strings.Split(string(b), "\n") {
|
|
l = strings.TrimSpace(l)
|
|
if strings.HasPrefix(l, "[") {
|
|
in = l == "[Desktop Entry]"
|
|
continue
|
|
}
|
|
if !in {
|
|
continue
|
|
}
|
|
k, v, ok := strings.Cut(l, "=")
|
|
if !ok {
|
|
continue
|
|
}
|
|
switch strings.TrimSpace(k) {
|
|
case "Exec":
|
|
exec = strings.TrimSpace(v)
|
|
case "MimeType":
|
|
claims = slices.Contains(strings.Split(v, ";"), mime)
|
|
case "Hidden":
|
|
if strings.TrimSpace(v) == "true" {
|
|
return "", false
|
|
}
|
|
}
|
|
}
|
|
return exec, claims && exec != ""
|
|
}
|
|
|
|
// expand is an Exec line as arguments, with the link for %u, %U, %f or
|
|
// %F, or as the last argument when it has none of them. Other field codes
|
|
// go, and %% is a %.
|
|
func expand(exec, link string) []string {
|
|
var argv []string
|
|
put := false
|
|
for _, w := range words(exec) {
|
|
switch w {
|
|
case "%u", "%U", "%f", "%F":
|
|
argv = append(argv, link)
|
|
put = true
|
|
continue
|
|
case "%i", "%c", "%k":
|
|
continue
|
|
}
|
|
var b strings.Builder
|
|
for i := 0; i < len(w); i++ {
|
|
if w[i] == '%' && i+1 < len(w) {
|
|
i++
|
|
if w[i] == '%' {
|
|
b.WriteByte('%')
|
|
}
|
|
continue
|
|
}
|
|
b.WriteByte(w[i])
|
|
}
|
|
argv = append(argv, b.String())
|
|
}
|
|
if len(argv) > 0 && !put {
|
|
argv = append(argv, link)
|
|
}
|
|
return argv
|
|
}
|
|
|
|
// words splits an Exec line as the Desktop Entry spec quotes it: double
|
|
// quotes around an argument, a backslash before ", `, $ or \ inside them.
|
|
func words(s string) []string {
|
|
var out []string
|
|
var b strings.Builder
|
|
quoted, have := false, false
|
|
for i := 0; i < len(s); i++ {
|
|
c := s[i]
|
|
switch {
|
|
case quoted && c == '\\' && i+1 < len(s):
|
|
i++
|
|
b.WriteByte(s[i])
|
|
case c == '"':
|
|
quoted = !quoted
|
|
have = true
|
|
case !quoted && (c == ' ' || c == '\t'):
|
|
if have {
|
|
out = append(out, b.String())
|
|
b.Reset()
|
|
have = false
|
|
}
|
|
default:
|
|
b.WriteByte(c)
|
|
have = true
|
|
}
|
|
}
|
|
if have {
|
|
out = append(out, b.String())
|
|
}
|
|
return out
|
|
}
|
|
|
|
// ours reports whether a desktop entry runs this executable, or one that
|
|
// is gone: magpie's own, not another magpie's.
|
|
func ours(path string) bool {
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
self, _ := executable()
|
|
for _, l := range strings.Split(string(b), "\n") {
|
|
if x, ok := strings.CutPrefix(l, "Exec="); ok {
|
|
run := strings.TrimSuffix(x, " %u")
|
|
if run == self {
|
|
return true
|
|
}
|
|
_, err := os.Stat(run)
|
|
return os.IsNotExist(err)
|
|
}
|
|
}
|
|
return false
|
|
}
|