-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystemuri_linux.go
80 lines (65 loc) · 1.92 KB
/
systemuri_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//go:build linux
package systemuri
import (
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"strings"
)
func registerURLHandler(name string, scheme string, applicationPath string, argumentsPattern string) error {
if applicationPath == "" {
return fmt.Errorf("applicationPath is empty")
}
exec := applicationPath + " " + strings.Replace(argumentsPattern, "%s", "%u", -1)
desktopFileContent := fmt.Sprintf(`[Desktop Entry]
Version=1.0
Name=%s
Exec=%s
Terminal=false
Type=Application
NoDisplay=true
MimeType=x-scheme-handler/%s;
`, name, exec, scheme)
usr, err := user.Current()
if err != nil {
return fmt.Errorf("failed to get current user: %w", err)
}
xdgDataHome := os.Getenv("XDG_DATA_HOME")
if xdgDataHome == "" {
xdgDataHome = filepath.Join(usr.HomeDir, ".local", "share")
}
applicationsDir := filepath.Join(xdgDataHome, "applications")
err = os.MkdirAll(applicationsDir, 0755)
if err != nil {
return fmt.Errorf("failed to create applications directory: %w", err)
}
desktopFilePath := filepath.Join(applicationsDir, fmt.Sprintf("%s-url-handler.desktop", scheme))
err = os.WriteFile(desktopFilePath, []byte(desktopFileContent), 0644)
if err != nil {
return fmt.Errorf("failed to create .desktop file: %w", err)
}
return nil
}
func unregisterURLHandler(scheme string) error {
usr, err := user.Current()
if err != nil {
return fmt.Errorf("failed to get current user: %w", err)
}
xdgDataHome := os.Getenv("XDG_DATA_HOME")
if xdgDataHome == "" {
xdgDataHome = filepath.Join(usr.HomeDir, ".local", "share")
}
applicationsDir := filepath.Join(xdgDataHome, "applications")
desktopFilePath := filepath.Join(applicationsDir, fmt.Sprintf("%s-url-handler.desktop", scheme))
err = os.Remove(desktopFilePath)
if err != nil {
return fmt.Errorf("failed to remove .desktop file: %w", err)
}
return nil
}
func unregisterURLHandlerByPath(applicationPath string) error {
log.Println("Not yet implemented")
return nil
}