Skip to content

Commit

Permalink
Read current config when starting, check appropriate buttons. Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
psidex committed Apr 10, 2020
1 parent caeb7b9 commit 4419529
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
36 changes: 30 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/getlantern/systray"
"io/ioutil"
"os"
"strings"
"tawesoft.co.uk/go/dialog"
)

Expand All @@ -17,14 +18,17 @@ type EApoConfig struct {
MenuItem *systray.MenuItem
}

func fatalError(errMsg string) {
dialog.Alert("EACS Fatal Error", errMsg)
os.Exit(1)
}

func CreateConfigSlice() []*EApoConfig {
var configSlice []*EApoConfig

files, err := ioutil.ReadDir(configFileDir)
if err != nil {
errMsg := fmt.Sprintf("Cannot read config file directory: %s", configFileDir)
dialog.Alert("EACS Config Error", errMsg)
os.Exit(1)
fatalError("Cannot read EACS config file directory")
}

for _, file := range files {
Expand All @@ -51,8 +55,28 @@ func WriteConfigToMaster(configSlice []*EApoConfig) {

err := ioutil.WriteFile(configFileMaster, completeData, 0644)
if err != nil {
errMsg := fmt.Sprintf("Cannot write to master config file: %s", configFileMaster)
dialog.Alert("EACS Config Error", errMsg)
os.Exit(1)
fatalError("Cannot write to master config file")
}
}

func ReadConfigFromMaster() []string {
var currentConfigFileNames []string

includes, err := ioutil.ReadFile(configFileMaster)
if err != nil {
fatalError("Cannot read from master config file")
}

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

for _, line := range lines {

parts := strings.Split(line, "\\")
fileName := parts[len(parts)-1]

currentConfigFileNames = append(currentConfigFileNames, fileName)

}

return currentConfigFileNames
}
22 changes: 20 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ import (
"strings"
)

func main() {
systray.Run(onReady, onExit)
// Find takes a slice and looks for an element in it. If found it will return true, else false.
// https://golangcode.com/check-if-element-exists-in-slice/
func find(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}

func onReady() {
Expand All @@ -17,12 +24,18 @@ func onReady() {
systray.SetTooltip("Equalizer APO Config Switcher")

configSlice := config.CreateConfigSlice()
currentConfigFileNames := config.ReadConfigFromMaster()

for _, configStruct := range configSlice {
configName := strings.Replace(configStruct.FileName, ".txt", "", 1)
btn := systray.AddMenuItem(configName, "Activate / Deactivate this config")
configStruct.MenuItem = btn

// If this config is already in the config master file
if find(currentConfigFileNames, configStruct.FileName) {
btn.Check()
}

go func() {
for {
<-btn.ClickedCh
Expand All @@ -46,8 +59,13 @@ func onReady() {
systray.Quit()
}
}()

}

func onExit() {
// No cleanup needed
}

func main() {
systray.Run(onReady, onExit)
}

0 comments on commit 4419529

Please sign in to comment.