schemehandler

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package schemehandler registers a program as the user's handler of URL schemes, hands each scheme back to the handler it replaced, and forwards a URL to that handler. On Linux it writes a user desktop entry and sets the x-scheme-handler defaults through xdg-mime, on the host from inside a Flatpak; on Windows it writes the scheme keys under HKCU\Software\Classes. Remembering each scheme's previous Owner.ID between runs is the caller's job.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupported = errors.New("schemehandler: URL scheme registration is not supported on this system")

ErrUnsupported is returned by every call on a system that is neither Linux nor Windows.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Exe is the program a URL is handed to, as its first argument.
	Exe string
	// Schemes are every scheme the program may claim, without "://". The desktop entry lists them all while any is
	// claimed, and Restore hands them all back.
	Schemes []string
	// DesktopID is the entry's file name, "org.example.App.desktop". An entry whose name starts with the id without
	// its extension is the app's own (a profile shortcut, say), never an AppImage integrator's.
	DesktopID string
	// Entry is the rest of the desktop entry.
	Entry Entry
	// Packaged means a package installed DesktopID system-wide: no user entry is written or looked for, and that
	// entry is the handler. Inside a Flatpak it is always true.
	Packaged bool
	// Run replaces the program runner on Linux; nil runs the real program. It gets every program the handler starts,
	// as name and args exactly as exec would take them (an absolute "/usr/bin/xdg-mime", a bare
	// "update-desktop-database", the saved owner's own program for Forward; inside a Flatpak "flatpak-spawn" with
	// "--host" first), and returns the program's standard output. A non-nil error is a failed run. Windows ignores it.
	Run func(name string, args ...string) (string, error)
}

Config is what to register. Exe and Schemes apply everywhere; the rest is Linux only.

type Entry

type Entry struct {
	Name, Comment, Icon  string
	Categories, Keywords []string
	StartupWMClass       string
	// MimeTypes are listed after the schemes whether or not any scheme is claimed, such as the app's own file type.
	MimeTypes []string
}

Entry is the desktop entry's keys besides Exec and MimeType. An empty Comment, Icon, StartupWMClass or list is left out.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler is one Config's registration on this system.

func New

func New(cfg Config) (*Handler, error)

New returns the Handler for cfg. It reads HOME, XDG_DATA_HOME and XDG_DATA_DIRS once.

func (*Handler) DropDefault

func (h *Handler) DropDefault(mimes ...string) error

DropDefault removes the lines that make the handler's entry the default for mimes from the user's mimeapps.list, following a symlink to its target: xdg-mime cannot unset a default.

func (*Handler) EntryProgram

func (h *Handler) EntryProgram() (string, error)

EntryProgram is the program the user entry runs, read back from the quoted Exec this package writes, or "" when the entry holds none. It is fs.ErrNotExist when there is no user entry, which is how a caller tells that another copy of the app (a second AppImage) owns the entry: the program differs from Exe and still exists.

func (*Handler) Forward

func (h *Handler) Forward(link, previous string) error

Forward runs the desktop entry previous (an Owner.ID) on link, which stays one argument. Inside a Flatpak the host's gio launches it.

func (*Handler) Owner

func (h *Handler) Owner(scheme string) (Owner, error)

Owner is the current owner of scheme.

func (*Handler) Register

func (h *Handler) Register(schemes ...string) error

Register writes the user entry listing every scheme, or yields to an AppImage integrator's entry for Exe, then makes that entry the default for each of schemes.

func (*Handler) Release

func (h *Handler) Release(schemes []string, previous map[string]string) error

Release hands back only schemes, as Restore does, and leaves the rest claimed; the entry still lists every scheme.

func (*Handler) RemoveEntry

func (h *Handler) RemoveEntry() error

RemoveEntry deletes the user entry, for an uninstall. Call Restore first to hand the schemes back.

func (*Handler) Restore

func (h *Handler) Restore(previous map[string]string) error

Restore rewrites the user entry without the schemes and hands every Config scheme back to its ID in previous, or drops the default when it has none.

func (*Handler) SetDefault

func (h *Handler) SetDefault(mimes ...string) error

SetDefault makes the handler's entry the default for each MIME type, such as the app's own file type or a scheme that is never handed back.

func (*Handler) WriteEntry

func (h *Handler) WriteEntry() error

WriteEntry writes the user entry unless it already reads as it should, keeping whether it lists the schemes, or yields to an integrator's entry. A Packaged Config has no user entry.

type Owner

type Owner struct {
	ID   string
	Name string
	Mine bool
}

Owner is the program the system opens a scheme's URLs with. ID is what Restore and Release take back: a desktop entry id on Linux, the saved open command on Windows. Name is the program's display name or path. Mine is whether it is this Config's program. An empty ID means no program owns the scheme.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL