Added a persistent config file for preferences

This commit is contained in:
diamondburned (Forefront) 2020-06-19 23:55:39 -07:00
parent 06f64e7a97
commit 05f6feaca2
4 changed files with 97 additions and 2 deletions

View File

@ -1,7 +1,11 @@
// Package config provides the repository for configuration and preferences.
package config
import "sort"
import (
"sort"
)
const ConfigFile = "config.json"
// List of config sections.
type Section uint8
@ -41,3 +45,11 @@ func AppearanceAdd(name string, value EntryValue) {
})
sortSection(Appearance)
}
func Save() error {
return MarshalToFile(ConfigFile, Sections)
}
func Restore() error {
return UnmarshalFromFile(ConfigFile, &Sections)
}

View File

@ -0,0 +1,67 @@
package config
import (
"encoding/json"
"log"
"os"
"path/filepath"
"github.com/pkg/errors"
)
var DirPath string
func init() {
// Load the config dir:
d, err := os.UserConfigDir()
if err != nil {
log.Fatalln("Failed to get config dir:", err)
}
// Fill Path:
DirPath = filepath.Join(d, "cchat-gtk")
// Ensure it exists:
if err := os.Mkdir(DirPath, 0755|os.ModeDir); err != nil && !os.IsExist(err) {
log.Fatalln("Failed to make config dir:", err)
}
}
func MarshalToFile(file string, from interface{}) error {
file = filepath.Join(DirPath, file)
f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_SYNC|os.O_TRUNC, 0644)
if err != nil {
return errors.Wrap(err, "Failed to open file")
}
defer f.Close()
enc := json.NewEncoder(f)
enc.SetIndent("", "\t")
if err := enc.Encode(from); err != nil {
return errors.Wrap(err, "Failed to marshal given struct")
}
return nil
}
func UnmarshalFromFile(file string, to interface{}) error {
file = filepath.Join(DirPath, file)
f, err := os.OpenFile(file, os.O_RDONLY, 0644)
if err != nil {
// Ignore does not exist error, leave struct as it is.
if os.IsNotExist(err) {
return nil
}
return err
}
defer f.Close()
if err := json.NewDecoder(f).Decode(to); err != nil {
return errors.Wrap(err, "Failed to unmarshal to given struct")
}
return nil
}

View File

@ -2,8 +2,10 @@ package preferences
import (
"github.com/diamondburned/cchat-gtk/internal/gts"
"github.com/diamondburned/cchat-gtk/internal/log"
"github.com/diamondburned/cchat-gtk/internal/ui/config"
"github.com/gotk3/gotk3/gtk"
"github.com/pkg/errors"
)
type Dialog struct {
@ -79,5 +81,12 @@ func NewPreferenceDialog() *Dialog {
}
func SpawnPreferenceDialog() {
NewPreferenceDialog().Show()
p := NewPreferenceDialog()
p.Connect("destroy", func() {
// On close, save the settings.
if err := config.Save(); err != nil {
log.Error(errors.Wrap(err, "Failed to save settings"))
}
})
p.Show()
}

View File

@ -4,7 +4,9 @@ import (
"github.com/diamondburned/cchat-gtk/internal/gts"
"github.com/diamondburned/cchat-gtk/internal/log"
"github.com/diamondburned/cchat-gtk/internal/ui"
"github.com/diamondburned/cchat-gtk/internal/ui/config"
"github.com/diamondburned/cchat/services"
"github.com/pkg/errors"
_ "github.com/diamondburned/cchat-discord"
_ "github.com/diamondburned/cchat-mock"
@ -29,6 +31,11 @@ func main() {
app.AddService(srvc)
}
// Restore the configs.
if err := config.Restore(); err != nil {
log.Error(errors.Wrap(err, "Failed to restore config"))
}
return app
})