Skip to content

Commit 09600d2

Browse files
author
Léonard Dallot
committed
Implement wallpaper portal
1 parent 3d3b4fe commit 09600d2

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ The list below contains all of the portal interfaces available within the projec
8181
- [x] Settings
8282
- [x] Trash
8383
- [ ] Usb
84-
- [ ] Wallpaper
84+
- [x] Wallpaper
8585

8686

8787
## Used by other projects

wallpaper/setWallpaperFile.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package wallpaper
2+
3+
import (
4+
"os"
5+
6+
"github.com/godbus/dbus/v5"
7+
"github.com/rymdport/portal/internal/apis"
8+
)
9+
10+
// SetWallpaperFile sets wallpaper specified as a local file
11+
func SetWallpaperFile(parentWindow string, file *os.File, options *SetWallpaperOptions) error {
12+
data := dbusDataFromOptions(options)
13+
14+
result, err := apis.Call(setWallpaperFileCallName, parentWindow, dbus.UnixFD(file.Fd()), data)
15+
if err != nil {
16+
return err
17+
}
18+
19+
return readStatusFromResponse(result.(dbus.ObjectPath))
20+
}

wallpaper/setWallpaperURI.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package wallpaper
2+
3+
import (
4+
"net/url"
5+
6+
"github.com/godbus/dbus/v5"
7+
"github.com/rymdport/portal/internal/apis"
8+
)
9+
10+
// SetWallpaperURI sets wallpaper specified as an URI (not a local file)
11+
func SetWallpaperURI(parentWindow string, uri string, options *SetWallpaperOptions) error {
12+
// Parse URI as an URL !
13+
url, err := url.Parse(uri)
14+
if err != nil {
15+
return err
16+
}
17+
18+
data := dbusDataFromOptions(options)
19+
20+
result, err := apis.Call(setWallpaperURICallName, parentWindow, url.String(), data)
21+
if err != nil {
22+
return err
23+
}
24+
25+
return readStatusFromResponse(result.(dbus.ObjectPath))
26+
}

wallpaper/wallpaper.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package wallpaper
2+
3+
import (
4+
"errors"
5+
6+
"github.com/godbus/dbus/v5"
7+
"github.com/rymdport/portal/internal/apis"
8+
"github.com/rymdport/portal/internal/convert"
9+
"github.com/rymdport/portal/internal/request"
10+
)
11+
12+
const (
13+
interfaceName = apis.CallBaseName + ".Wallpaper"
14+
setWallpaperURICallName = interfaceName + ".SetWallpaperURI"
15+
setWallpaperFileCallName = interfaceName + ".SetWallpaperFile"
16+
)
17+
18+
// WallpaperLocation is the type of the parameter SetOn of wallpaper options
19+
type WallpaperLocation uint
20+
21+
const (
22+
Background WallpaperLocation = iota // Set wallpaper of Background
23+
Lockscreen // Set wallpaper of Locksreen
24+
Both // Set wallpaper of both background and lockscreen
25+
)
26+
27+
var wallpaperLocationName = map[WallpaperLocation]string{
28+
Background: "background",
29+
Lockscreen: "lockscreen",
30+
Both: "both",
31+
}
32+
33+
func (l WallpaperLocation) String() string {
34+
return wallpaperLocationName[l]
35+
}
36+
37+
// SetWallpaperOptions contains the options of backgound change
38+
type SetWallpaperOptions struct {
39+
ShowPreview bool // Whether to show a preview of the picture. Note that the portal may decide to show a preview even if this option is not set.
40+
SetOn WallpaperLocation // Where to set the wallpaper. Possible values are Background, Lockscreen, or Both constants
41+
}
42+
43+
func dbusDataFromOptions(options *SetWallpaperOptions) map[string]dbus.Variant {
44+
data := map[string]dbus.Variant{}
45+
if options != nil {
46+
data["show-preview"] = convert.FromBool(options.ShowPreview)
47+
data["set-on"] = convert.FromString(wallpaperLocationName[options.SetOn])
48+
}
49+
return data
50+
}
51+
52+
func readStatusFromResponse(path dbus.ObjectPath) error {
53+
status, _, err := request.OnSignalResponse(path)
54+
if err != nil {
55+
return err
56+
}
57+
switch status {
58+
case request.Cancelled:
59+
return errors.New("operation cancelled by User")
60+
case request.Ended:
61+
return errors.New("operation cancelled by system")
62+
case request.Success:
63+
return nil
64+
default:
65+
return errors.New("unknown status code")
66+
}
67+
return nil
68+
}

0 commit comments

Comments
 (0)