forked from reujab/wallpaper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkde.go
91 lines (79 loc) · 1.68 KB
/
kde.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
81
82
83
84
85
86
87
88
89
90
91
//+build linux
package wallpaper
import (
"bufio"
"errors"
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"
)
func getKDE() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
filename := filepath.Join(usr.HomeDir, ".config", "plasma-org.kde.plasma.desktop-appletsrc")
if err != nil {
return "", err
}
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if len(line) >= 6 && line[:6] == "Image=" {
return strings.TrimSpace(removeProtocol(line[6:])), nil
}
}
if scanner.Err() != nil {
return "", scanner.Err()
}
err = file.Close()
if err != nil {
return "", err
}
return "", errors.New("kde image not found")
}
func setKDE(path string) error {
return evalKDE(`
for (const desktop of desktops()) {
desktop.currentConfigGroup = ["Wallpaper", "org.kde.image", "General"]
desktop.writeConfig("Image", ` + strconv.Quote("file://"+path) + `)
}
`)
}
func setKDEMode(mode Mode) error {
return evalKDE(`
for (const desktop of desktops()) {
desktop.currentConfigGroup = ["Wallpaper", "org.kde.image", "General"]
desktop.writeConfig("FillMode", ` + mode.getKDEString() + `)
}
`)
}
func evalKDE(script string) error {
return exec.Command("qdbus", "org.kde.plasmashell", "/PlasmaShell", "org.kde.PlasmaShell.evaluateScript", script).Run()
}
func (mode Mode) getKDEString() string {
switch mode {
case Center:
return "6"
case Crop:
return "2"
case Fit:
return "1"
case Span:
return "2"
case Stretch:
return "0"
case Tile:
return "3"
default:
panic("invalid walllpaper mode")
}
}