Skip to content

Commit

Permalink
cleaned code
Browse files Browse the repository at this point in the history
  • Loading branch information
reujab committed Jun 16, 2021
1 parent b5d61c5 commit 62b31f3
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 176 deletions.
10 changes: 0 additions & 10 deletions darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ func SetFromFile(file string) error {
return exec.Command("osascript", "-e", `tell application "System Events" to tell every desktop to set picture to `+strconv.Quote(file)).Run()
}

// SetFromURL downloads `url` and calls SetFromFile.
func SetFromURL(url string) error {
file, err := downloadImage(url)
if err != nil {
return err
}

return SetFromFile(file)
}

func getCacheDir() (string, error) {
usr, err := user.Current()
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/reujab/wallpaper

go 1.16

require (
github.com/smartystreets/goconvey v1.6.4 // indirect
gopkg.in/ini.v1 v1.62.0
gopkg.in/yaml.v2 v2.4.0
)
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
4 changes: 2 additions & 2 deletions kde.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"
)

func parseKDEConfig() (string, error) {
func getKDE() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
Expand Down Expand Up @@ -49,7 +49,7 @@ func parseKDEConfig() (string, error) {
return "", errors.New("kde image not found")
}

func setKDEBackground(uri string) error {
func setKDE(uri string) error {
return exec.Command("qdbus", "org.kde.plasmashell", "/PlasmaShell", "org.kde.PlasmaShell.evaluateScript", `
const monitors = desktops()
for (var i = 0; i < monitors.length; i++) {
Expand Down
124 changes: 5 additions & 119 deletions linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,15 @@
package wallpaper

import (
"io/ioutil"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"strconv"
"strings"

ini "gopkg.in/ini.v1"
yaml "gopkg.in/yaml.v2"
)

// init guesses the current desktop by reading processes if $XDG_CURRENT_DESKTOP was not set.
func init() {
if Desktop != "" {
return
}

files, err := ioutil.ReadDir("/proc")
if err != nil {
return
}

for _, file := range files {
// continue if not pid
_, err := strconv.ParseUint(file.Name(), 10, 64)
if err != nil {
continue
}

// checks to see if process's binary is a recognized window manager
bin, err := os.Readlink("/proc/" + file.Name() + "/exe")
if err != nil {
continue
}

switch path.Base(bin) {
case "i3":
Desktop = "i3"
return
}
}
}

// Get returns the current wallpaper.
func Get() (string, error) {
if isGNOMECompliant() {
Expand All @@ -56,24 +20,15 @@ func Get() (string, error) {

switch Desktop {
case "KDE":
return parseKDEConfig()
return getKDE()
case "X-Cinnamon":
return parseDconf("dconf", "read", "/org/cinnamon/desktop/background/picture-uri")
case "MATE":
return parseDconf("dconf", "read", "/org/mate/desktop/background/picture-filename")
case "XFCE":
desktops, err := getXFCEDesktops()
if err != nil || len(desktops) == 0 {
return "", err
}

output, err := exec.Command("xfconf-query", "--channel", "xfce4-desktop", "--property", desktops[0]).Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
return getXFCE()
case "LXDE":
return parseLXDEConfig()
return getLXDE()
case "Deepin":
return parseDconf("dconf", "read", "/com/deepin/wrap/gnome/desktop/background/picture-uri")
default:
Expand All @@ -89,23 +44,13 @@ func SetFromFile(file string) error {

switch Desktop {
case "KDE":
return setKDEBackground("file://" + file)
return setKDE("file://" + file)
case "X-Cinnamon":
return exec.Command("dconf", "write", "/org/cinnamon/desktop/background/picture-uri", strconv.Quote("file://"+file)).Run()
case "MATE":
return exec.Command("dconf", "write", "/org/mate/desktop/background/picture-filename", strconv.Quote(file)).Run()
case "XFCE":
desktops, err := getXFCEDesktops()
if err != nil {
return err
}
for _, desktop := range desktops {
err := exec.Command("xfconf-query", "--channel", "xfce4-desktop", "--property", desktop, "--set", file).Run()
if err != nil {
return err
}
}
return nil
return setXFCE(file)
case "LXDE":
return exec.Command("pcmanfm", "-w", file).Run()
case "Deepin":
Expand All @@ -120,23 +65,6 @@ func SetFromFile(file string) error {
}
}

// SetFromURL sets wallpaper from a URL.
//
// In GNOME, it sets org.gnome.desktop.background.picture-uri to the URL.
// In other desktops, it downloads the image and calls SetFromFile.
func SetFromURL(url string) error {
switch Desktop {
case "i3":
return exec.Command("feh", "--bg-fill", url).Run()
default:
filename, err := downloadImage(url)
if err != nil {
return err
}
return SetFromFile(filename)
}
}

func getCacheDir() (string, error) {
usr, err := user.Current()
if err != nil {
Expand Down Expand Up @@ -169,48 +97,6 @@ func parseDconf(command string, args ...string) (string, error) {
return removeProtocol(unquoted), nil
}

func parseLXDEConfig() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}

if DesktopSession == "" {
DesktopSession = "LXDE"
}

cfg, err := ini.Load(filepath.Join(usr.HomeDir, ".config/pcmanfm/"+DesktopSession+"/desktop-items-0.conf"))
if err != nil {
return "", err
}

key, err := cfg.Section("*").GetKey("wallpaper")
if err != nil {
return "", err
}
return key.String(), err
}

func isGNOMECompliant() bool {
return strings.Contains(Desktop, "GNOME") || Desktop == "Unity" || Desktop == "Pantheon"
}

func getXFCEDesktops() ([]string, error) {
output, err := exec.Command("xfconf-query", "--channel", "xfce4-desktop", "--list").Output()
if err != nil {
return nil, err
}

lines := strings.Split(strings.Trim(string(output), "\n"), "\n")

i := 0
for _, line := range lines {
if path.Base(line) == "last-image" {
lines[i] = line
i++
}
}
lines = lines[:i]

return lines, nil
}
30 changes: 30 additions & 0 deletions lxde.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package wallpaper

import (
"os/user"
"path/filepath"

ini "gopkg.in/ini.v1"
)

func getLXDE() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}

if DesktopSession == "" {
DesktopSession = "LXDE"
}

cfg, err := ini.Load(filepath.Join(usr.HomeDir, ".config/pcmanfm/"+DesktopSession+"/desktop-items-0.conf"))
if err != nil {
return "", err
}

key, err := cfg.Section("*").GetKey("wallpaper")
if err != nil {
return "", err
}
return key.String(), err
}
46 changes: 11 additions & 35 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package wallpaper

import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"
)

// Desktop contains the current desktop environment on Linux.
// Empty string on all other operating systems.
var Desktop = os.Getenv("XDG_CURRENT_DESKTOP")

// DesktopSession is used by LXDE on Linux.
var DesktopSession = os.Getenv("DESKTOP_SESSION")

// ErrUnsupportedDE is thrown when Desktop is not a supported desktop environment.
Expand All @@ -29,7 +28,12 @@ func downloadImage(url string) (string, error) {
return "", errors.New("non-200 status code")
}

file, err := prepareFile()
cacheDir, err := getCacheDir()
if err != nil {
return "", err
}

file, err := os.Create(filepath.Join(cacheDir, "wallpaper"))
if err != nil {
return "", err
}
Expand All @@ -47,40 +51,12 @@ func downloadImage(url string) (string, error) {
return file.Name(), nil
}

func prepareFile() (*os.File, error) {
cacheDir, err := getCacheDir()
// SetFromURL downloads the image to a cache directory and calls SetFromFile.
func SetFromURL(url string) error {
file, err := downloadImage(url)
if err != nil {
return nil, err
}

wallpaperDir := filepath.Join(cacheDir, "wallpaper")
if err = recreateDir(wallpaperDir); err != nil {
return nil, err
}

filename := filepath.Join(wallpaperDir, fmt.Sprint(time.Now().UnixNano()))
file, err := os.Create(filename)
if err != nil {
return nil, err
}
return file, nil
}

func recreateDir(dirName string) error {
if err := os.RemoveAll(dirName); err != nil {
return err
}
if err := ensureDir(dirName); err != nil {
return err
}
return nil
}

func ensureDir(dirName string) error {
err := os.MkdirAll(dirName, os.ModePerm)
if err == nil || os.IsExist(err) {
return nil
} else {
return err
}
return SetFromFile(file)
}
10 changes: 0 additions & 10 deletions windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@ func SetFromFile(filename string) error {
return nil
}

// SetFromURL downloads url and calls SetFromFile.
func SetFromURL(url string) error {
file, err := downloadImage(url)
if err != nil {
return err
}

return SetFromFile(file)
}

func getCacheDir() (string, error) {
return os.TempDir(), nil
}
Loading

0 comments on commit 62b31f3

Please sign in to comment.