Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
* Set a wallpaper immediately when running settimed
* Add "verbose" toggles
* Let "xml2stw" take a filename
  • Loading branch information
xyproto committed Feb 13, 2019
1 parent 598adc3 commit 0026570
Show file tree
Hide file tree
Showing 29 changed files with 872 additions and 413 deletions.
44 changes: 24 additions & 20 deletions cinnamon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,55 @@ type Cinnamon struct {
hasDconf bool
hasGsettings bool
hasChecked bool
verbose bool
}

func (s *Cinnamon) Name() string {
func (c *Cinnamon) Name() string {
return "Cinnamon"
}

func (s *Cinnamon) ExecutablesExists() bool {
s.hasDconf = which("dconf") != ""
s.hasGsettings = which("gsettings") != ""
s.hasChecked = true
return s.hasDconf || s.hasGsettings
func (c *Cinnamon) ExecutablesExists() bool {
c.hasDconf = which("dconf") != ""
c.hasGsettings = which("gsettings") != ""
c.hasChecked = true
return c.hasDconf || c.hasGsettings
}

func (s *Cinnamon) Running() bool {
// TODO: To test
func (c *Cinnamon) Running() bool {
return (containsE("GDMSESSION", "cinnamon") || containsE("XDG_SESSION_DESKTOP", "cinnamon") || containsE("XDG_CURRENT_DESKTOP", "cinnamon"))
}

func (s *Cinnamon) SetMode(mode string) {
s.mode = mode
func (c *Cinnamon) SetMode(mode string) {
c.mode = mode
}

func (c *Cinnamon) SetVerbose(verbose bool) {
c.verbose = verbose
}

// SetWallpaper sets the desktop wallpaper, given an image filename.
// The image must exist and be readable.
func (s *Cinnamon) SetWallpaper(imageFilename string) error {
func (c *Cinnamon) SetWallpaper(imageFilename string) error {
// Check if dconf or gsettings are there, if we haven't already checked
if !s.hasChecked {
s.ExecutablesExists()
if !c.hasChecked {
c.ExecutablesExists()
}
// Set the desktop wallpaper picture mode
mode := defaultMode
if s.mode != "" {
mode = s.mode
if c.mode != "" {
mode = c.mode
}
// Change the background with either dconf or gsettings
if s.hasDconf {
if c.hasDconf {
// use dconf
if err := run("dconf write /org/cinnamon/desktop/background/picture-filename \"'" + imageFilename + "'\""); err != nil {
if err := run("dconf write /org/cinnamon/desktop/background/picture-filename \"'"+imageFilename+"'\"", c.verbose); err != nil {
return err
}
return run("dconf write /org/cinnamon/desktop/background/picture-options \"'" + mode + "'\"")
return run("dconf write /org/cinnamon/desktop/background/picture-options \"'"+mode+"'\"", c.verbose)
}
// use gsettings
if err := run("gsettings set org.cinnamon.background picture-filename '" + imageFilename + "'"); err != nil {
if err := run("gsettings set org.cinnamon.background picture-filename '"+imageFilename+"'", c.verbose); err != nil {
return err
}
return run("gsettings set org.cinnamon.background picture-options '" + mode + "'")
return run("gsettings set org.cinnamon.background picture-options '"+mode+"'", c.verbose)
}
37 changes: 22 additions & 15 deletions cmd/lscollections/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"text/tabwriter"
)

// has checks if the given string slice contains the given string
func has(sl []string, s string) bool {
for _, e := range sl {
if e == s {
Expand All @@ -20,8 +21,15 @@ func has(sl []string, s string) bool {
func main() {
alsoPrintPath := len(os.Args) > 1 && os.Args[1] == "-l"

// Find all wallpapers
searchResults, err := monitor.FindWallpapers()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}

if !alsoPrintPath {
for _, name := range monitor.FindCollectionNames() {
for _, name := range searchResults.CollectionNames() {
fmt.Println(name)
}
return
Expand All @@ -30,18 +38,15 @@ func main() {
// Prepare to write text in columns
w := tabwriter.NewWriter(os.Stdout, 0, 0, 10, ' ', tabwriter.AlignRight)

// Find all wallpapers
wallpapers, gnomeWallpapers, simpleTimedWallpapers := monitor.FindWallpapers()
var collectionNames []string

// Output all wallpaper collection names and paths (these are directories
// with files of varying resolutions)
for _, wp := range wallpapers {
var collectionNames []string
for _, wp := range searchResults.Wallpapers() {
if wp.PartOfCollection {
name := wp.CollectionName
dir := filepath.Dir(wp.Path) + "/"
if alsoPrintPath || !has(collectionNames, name) {
fmt.Fprintf(w, "%s\t(%s)\t\t%s\n", name, "wallpaper collection", dir)
if alsoPrintPath && !has(collectionNames, name) {
fmt.Fprintf(w, "%s\t%s\t\t%s\n", name, "Wallpaper Collection", dir)
collectionNames = append(collectionNames, wp.CollectionName)
}
}
Expand All @@ -51,21 +56,23 @@ func main() {
// several wallpaper images.

// Output all Simple Timed Wallpaper names and paths.
for _, stw := range simpleTimedWallpapers {
collectionNames = []string{}
for _, stw := range searchResults.SimpleTimedWallpapers() {
name := stw.Name
path := stw.Path
if alsoPrintPath || !has(collectionNames, name) {
fmt.Fprintf(w, "%s\t(%s)\t\t%s\n", name, "simple timed wallpaper", path)
if alsoPrintPath && !has(collectionNames, name) {
fmt.Fprintf(w, "%s\t%s\t\t%s\n", name, "Simple Timed Wallpaper", path)
collectionNames = append(collectionNames, name)
}
}

// Output all GNOME timed wallpaper names and paths.
for _, gw := range gnomeWallpapers {
name := gw.CollectionName
collectionNames = []string{}
for _, gw := range searchResults.GnomeTimedWallpapers() {
name := gw.Name
path := gw.Path
if alsoPrintPath || !has(collectionNames, name) {
fmt.Fprintf(w, "%s\t(%s)\t\t%s\n", name, "GNOME timed wallpaper", path)
if alsoPrintPath && !has(collectionNames, name) {
fmt.Fprintf(w, "%s\t%s\t\t%s\n", name, "GNOME Timed Wallpaper", path)
collectionNames = append(collectionNames, name)
}
}
Expand Down
16 changes: 10 additions & 6 deletions cmd/lstimed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,27 @@ func main() {
// Prepare to write text in columns
w := tabwriter.NewWriter(os.Stdout, 0, 0, 10, ' ', tabwriter.AlignRight)

_, gnomeWallpapers, simpleTimedWallpapers := monitor.FindWallpapers()
for _, stw := range simpleTimedWallpapers {
searchResults, err := monitor.FindWallpapers()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}

for _, stw := range searchResults.SimpleTimedWallpapers() {
if alsoPrintPath {
numEvents := len(stw.Statics) + len(stw.Transitions)
fmt.Fprintf(w, "%s\t%s\t\tevents: %d\n", stw.Name, stw.Path, numEvents)
} else {
fmt.Fprintf(w, "%s\n", stw.Name)
}
}
for _, gw := range gnomeWallpapers {
for _, gw := range searchResults.GnomeTimedWallpapers() {
if alsoPrintPath {
numEvents := len(gw.Config.Statics) + len(gw.Config.Transitions)
fmt.Fprintf(w, "%s\t%s\t\tevents: %d\n", gw.CollectionName, gw.Path, numEvents)
fmt.Fprintf(w, "%s\t%s\t\tevents: %d\n", gw.Name, gw.Path, numEvents)
} else {
fmt.Fprintf(w, "%s\n", gw.CollectionName)
fmt.Fprintf(w, "%s\n", gw.Name)
}
}

w.Flush()
}
9 changes: 7 additions & 2 deletions cmd/lswallpaper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package main
import (
"fmt"
"github.com/xyproto/monitor"
"os"
)

func main() {
wallpapers, _, _ := monitor.FindWallpapers()
for _, wp := range wallpapers {
searchResults, err := monitor.FindWallpapers()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
for _, wp := range searchResults.Wallpapers() {
fmt.Println(wp)
}
}
46 changes: 23 additions & 23 deletions cmd/setcollection/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"github.com/xyproto/monitor"
)

func setWallpaper(wallpapers []*monitor.Wallpaper) error {
// Select the wallpaper that is closest to the current monitor resolution and set that as the wallpaper
func SelectAndSetWallpaper(wallpapers []*monitor.Wallpaper) error {
// Gather a slice of filenames
var filenames []string
for _, wp := range wallpapers {
Expand Down Expand Up @@ -50,42 +51,41 @@ func main() {
fmt.Printf("Setting wallpaper collection \"%s\"\n", collectionName)

fmt.Print("Searching for wallpapers...")
wallpapers, gnomeWallpapers, simpleTimedWallpapers := monitor.FindWallpapers()
if len(wallpapers) == 0 && len(gnomeWallpapers) == 0 && len(simpleTimedWallpapers) == 0 {
searchResults, err := monitor.FindWallpapers()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}

wallpapers := searchResults.Wallpapers()
gnomeTimedWallpapers := searchResults.GnomeTimedWallpapers()
simpleTimedWallpapers := searchResults.SimpleTimedWallpapers()

if searchResults.Empty() {
fmt.Fprintln(os.Stderr, "Could not find any wallpapers on the system.")
os.Exit(1)
} else {
fmt.Println("ok")
}

fmt.Print("Filtering wallpapers by collection name...")
wallpapers = monitor.FilterWallpapers(collectionName, wallpapers)
gnomeWallpapers = monitor.FilterGnomeWallpapers(collectionName, gnomeWallpapers)
simpleTimedWallpapers = monitor.FilterSimpleTimedWallpapers(collectionName, simpleTimedWallpapers)
wallpapers = searchResults.WallpapersByName(collectionName)
gnomeTimedWallpapers = searchResults.GnomeTimedWallpapersByName(collectionName)
simpleTimedWallpapers = searchResults.SimpleTimedWallpapersByName(collectionName)
fmt.Println("ok")

if len(wallpapers) == 0 && len(gnomeWallpapers) == 0 && len(simpleTimedWallpapers) == 0 {
fmt.Fprintln(os.Stderr, "No such collection: "+collectionName)
if len(wallpapers) == 0 && (len(gnomeTimedWallpapers) > 0 || len(simpleTimedWallpapers) > 0) {
fmt.Fprintln(os.Stderr, "Timed wallpapers are not supported by this utility.")
os.Exit(1)
}

// wallpapers and gnomeWallpapers are now filtered to only contain elements with matching collection names

if len(wallpapers) > 0 && len(gnomeWallpapers) > 0 {
fmt.Fprintln(os.Stderr, "Found both a wallpaper collection and a GNOME timed background after filtering by collection name.")
if len(wallpapers) == 0 {
fmt.Fprintln(os.Stderr, "No such collection: "+collectionName)
os.Exit(1)
}
if len(wallpapers) > 0 && len(gnomeWallpapers) == 0 {
err := setWallpaper(wallpapers)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
} else if len(wallpapers) == 0 && len(gnomeWallpapers) == 1 {
fmt.Fprintln(os.Stderr, "Timed wallpapers are not supported by this utility")
os.Exit(1)
} else if len(wallpapers) == 0 && len(gnomeWallpapers) > 1 {
fmt.Fprintln(os.Stderr, "Found several GNOME timed backgrounds, with the same collection name!")

if err = SelectAndSetWallpaper(wallpapers); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Loading

0 comments on commit 0026570

Please sign in to comment.