2020-05-26 18:21:27 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cwtch.im/cwtch/storage/v1"
|
|
|
|
"cwtch.im/ui/go/the"
|
|
|
|
"encoding/json"
|
|
|
|
"git.openprivacy.ca/openprivacy/log"
|
|
|
|
"github.com/therecipe/qt/core"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
const GlobalSettingsFilename = "ui.globals"
|
|
|
|
const saltFile = "SALT"
|
|
|
|
|
|
|
|
type GlobalSettings struct {
|
2020-10-26 20:29:58 +00:00
|
|
|
Zoom float32
|
|
|
|
Locale string
|
|
|
|
Theme string
|
|
|
|
PreviousPid int64
|
|
|
|
ExperimentsEnabled bool
|
|
|
|
Experiments map[string]bool
|
2020-05-26 18:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultGlobalSettings = GlobalSettings{
|
2020-11-20 19:06:41 +00:00
|
|
|
Zoom: 1.9,
|
2020-05-26 18:21:27 +00:00
|
|
|
Locale: "en",
|
|
|
|
Theme: "light",
|
2020-10-21 23:58:57 +00:00
|
|
|
PreviousPid: -1,
|
2020-10-26 20:29:58 +00:00
|
|
|
ExperimentsEnabled: false,
|
|
|
|
Experiments: make(map[string]bool),
|
2020-05-26 18:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func InitGlobalSettingsFile(directory string, password string) error {
|
|
|
|
var key [32]byte
|
|
|
|
salt, err := ioutil.ReadFile(path.Join(directory, saltFile))
|
|
|
|
if err != nil {
|
|
|
|
var newSalt [128]byte
|
2020-06-23 23:42:51 +00:00
|
|
|
key, newSalt, err = v1.CreateKeySalt(password)
|
2020-05-26 18:21:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
os.Mkdir(directory, 0700)
|
|
|
|
err := ioutil.WriteFile(path.Join(directory, saltFile), newSalt[:], 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2020-06-23 23:42:51 +00:00
|
|
|
key = v1.CreateKey(password, salt)
|
2020-05-26 18:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
the.GlobalSettingsFile = v1.NewFileStore(directory, GlobalSettingsFilename, key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-23 20:50:06 +00:00
|
|
|
func ReadGlobalSettings() (*GlobalSettings, bool) {
|
2020-05-26 18:21:27 +00:00
|
|
|
settings := DefaultGlobalSettings
|
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
settings.Zoom = 2.9
|
|
|
|
}
|
|
|
|
settings.Locale = core.QLocale_System().Name()
|
|
|
|
|
|
|
|
settingsBytes, err := the.GlobalSettingsFile.Read()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not read global ui settings: %v\n", err)
|
2020-11-23 20:50:06 +00:00
|
|
|
return &settings, true //firstTime = true
|
2020-05-26 18:21:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 19:06:41 +00:00
|
|
|
|
2020-05-26 18:21:27 +00:00
|
|
|
err = json.Unmarshal(settingsBytes, &settings)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not parse global ui settings: %v\n", err)
|
2020-11-23 20:50:06 +00:00
|
|
|
return &settings, true //firstTime = true
|
2020-05-26 18:21:27 +00:00
|
|
|
}
|
2020-11-20 19:06:41 +00:00
|
|
|
|
|
|
|
log.Debugf("Settings: %v", settings)
|
2020-11-23 20:50:06 +00:00
|
|
|
return &settings, false
|
2020-05-26 18:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func WriteGlobalSettings(globalSettings *GlobalSettings) {
|
|
|
|
bytes, _ := json.Marshal(globalSettings)
|
|
|
|
err := the.GlobalSettingsFile.Write(bytes)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not write global ui settings: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|