From 44195290632700deb03faef1448b75d62569f2f9 Mon Sep 17 00:00:00 2001 From: thatguywiththatname Date: Fri, 10 Apr 2020 20:31:52 +0100 Subject: [PATCH] Read current config when starting, check appropriate buttons. Fixes #1 --- config/config.go | 36 ++++++++++++++++++++++++++++++------ main.go | 22 ++++++++++++++++++++-- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/config/config.go b/config/config.go index 8e20c8b..c4b5ea2 100644 --- a/config/config.go +++ b/config/config.go @@ -5,6 +5,7 @@ import ( "github.com/getlantern/systray" "io/ioutil" "os" + "strings" "tawesoft.co.uk/go/dialog" ) @@ -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 { @@ -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 } diff --git a/main.go b/main.go index ca88851..bf4b74b 100644 --- a/main.go +++ b/main.go @@ -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() { @@ -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 @@ -46,8 +59,13 @@ func onReady() { systray.Quit() } }() + } func onExit() { // No cleanup needed } + +func main() { + systray.Run(onReady, onExit) +}