|
| 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