Skip to content

Commit

Permalink
Merge 81d34da into 230b575
Browse files Browse the repository at this point in the history
  • Loading branch information
AntiD2ta committed Jan 24, 2020
2 parents 230b575 + 81d34da commit 094455d
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@ darwin

# Auto-generates go files.

*.pb.go
*.pb.go
*.log
24 changes: 22 additions & 2 deletions src/coin/skycoin/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@ import (
"strings"

local "github.com/fibercrypto/fibercryptowallet/src/main"
"github.com/fibercrypto/fibercryptowallet/src/util/logging"
)

const (
LocalWallet = "local"
RemoteWallet = "remote"
SectionName = "skycoin"
SettingPathToLog = "log"
SettingPathToNode = "node"
SettingPathToWalletSource = "walletSource"
)

var (
sectionManager *local.SectionManager
log = logging.MustGetLogger("Skycoin Config")
)

func getMultiPlatformUserDirectory() string {
usr, err := user.Current()
if err != nil {
//TODO: Log error
log.WithError(err).Error()
return ""
}
return filepath.Join(usr.HomeDir, string(os.PathSeparator), ".skycoin", string(os.PathSeparator), "wallets")
Expand All @@ -53,7 +56,21 @@ func RegisterConfig() error {

wltOpt := local.NewOption(string(wltSrc.id), []string{SettingPathToWalletSource}, false, string(wltSrcBytes))

sectionManager = cm.RegisterSection(SectionName, []*local.Option{nodeOpt, wltOpt})
level := map[string]string{"level": "warn"}
levelBytes, err := json.Marshal(level)
if err != nil {
return err
}
logLevelOpt := local.NewOption(SettingPathToLog, []string{}, false, string(levelBytes))

output := map[string]string{"output": "none"}
outputBytes, err := json.Marshal(output)
if err != nil {
return err
}
logOutputOpt := local.NewOption(SettingPathToLog, []string{}, false, string(outputBytes))

sectionManager = cm.RegisterSection(SectionName, []*local.Option{nodeOpt, wltOpt, logLevelOpt, logOutputOpt})
return nil
}

Expand All @@ -71,12 +88,14 @@ func GetDataRefreshTimeout() uint64 {
sm := cm.GetSectionManager("global")
value, err := sm.GetValue("cache", nil)
if err != nil {
log.WithError(err).Warn("Couldn't get cache value option for saved settings")
return 0
}

keyValue := make(map[string]string)
err = json.Unmarshal([]byte(value), &keyValue)
if err != nil {
log.WithError(err).Warn("Couldn't unmarshal from options")
return 0
}
strVal, ok := keyValue["lifeTime"]
Expand All @@ -85,6 +104,7 @@ func GetDataRefreshTimeout() uint64 {
}
val, err := strconv.ParseUint(strVal, 10, 64)
if err != nil {
log.WithError(err).Warn("Couldn't parse %s to int", strVal)
return 0
}
return val
Expand Down
35 changes: 31 additions & 4 deletions src/coin/skycoin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package skycoin //nolint goimports
import (
"encoding/json"

skylog "github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/fibercrypto/fibercryptowallet/src/coin/skycoin/config"
sky "github.com/fibercrypto/fibercryptowallet/src/coin/skycoin/models"
"github.com/fibercrypto/fibercryptowallet/src/core"
Expand All @@ -20,15 +21,41 @@ func init() {
if err != nil {
logSkycoin.Warn("Couldn't register Skycoin configuration")
}
nodeStr, err := config.GetOption(config.SettingPathToNode)

logSettingStr, err := config.GetOption(config.SettingPathToLog)
if err != nil {
logSkycoin.Warn("Couldn't get log options")
}
logSetting := make(map[string]string)
err = json.Unmarshal([]byte(logSettingStr), &logSetting)
if err != nil {
logSkycoin.Warn("Couldn't get node options")
logSkycoin.WithError(err).Warn("Couldn't unmarshal from options")
}
level, err := logging.LevelFromString(logSetting["level"])
if err != nil {
logSkycoin.Warn("Couldn't get level from logging")
logSkycoin.WithError(err).WithField("string", logSetting["level"]).Error()
} else {
logging.SetLevel(level)
}
writer, err := logging.GetOutputWriter(logSetting["output"])
if err != nil {
logSkycoin.WithError(err).Error("Error opening file: ", logSetting["output"])
} else {
logging.SetOutputTo(writer)
skylog.SetOutputTo(writer)
}

nodeSettingStr, err := config.GetOption(config.SettingPathToNode)
if err != nil {
logSkycoin.Warn("Couldn't get node settings")
}
node := make(map[string]string)
err = json.Unmarshal([]byte(nodeStr), &node)
err = json.Unmarshal([]byte(nodeSettingStr), &node)
if err != nil {
logSkycoin.Warn("Couldn't unmarshal from options")
logSkycoin.WithError(err).Warn("Couldn't unmarshal from options")
}

err = core.GetMultiPool().CreateSection(sky.PoolSection, sky.NewSkycoinConnectionFactory(node["address"]))
if err != nil {
logSkycoin.Warn("Couldn't create section for Skycoin")
Expand Down
6 changes: 5 additions & 1 deletion src/main/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,18 @@ func (cm *ConfigManager) RegisterSection(name string, options []*Option) *Sectio
defer cm.setting.Sync()

for _, opt := range options {
depthLevel := 0
for _, sect := range opt.sectionPath {
cm.setting.BeginGroup(sect)
defer cm.setting.EndGroup()
depthLevel++
}
if !opt.optional && !cm.setting.Contains(opt.name) {
cm.setting.SetValue(opt.name, qtcore.NewQVariant1(opt._default))

}
for i := 0; i < depthLevel; i++ {
cm.setting.EndGroup()
}
}

return cm.sections[name]
Expand Down
41 changes: 33 additions & 8 deletions src/models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import (

local "github.com/fibercrypto/fibercryptowallet/src/main"

"github.com/therecipe/qt/qml"

skylog "github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/fibercrypto/fibercryptowallet/src/util/logging"
qtcore "github.com/therecipe/qt/core"
"github.com/therecipe/qt/qml"
)

var log = logging.MustGetLogger("Config Model")

type ConfigManager struct {
qtcore.QObject
configManager *local.ConfigManager
Expand Down Expand Up @@ -75,6 +78,8 @@ func (cm *ConfigManager) getDefaultValue(path string) string {
return cm.GetSection(section).getDefaultValue(optName, optPath, name)
}

//TODO: Exported type ConfigSection should have comment or be unexported (linter suggestion)
// ConfigSection <description>
type ConfigSection struct {
qtcore.QObject
sm *local.SectionManager
Expand All @@ -96,7 +101,7 @@ func (cs *ConfigSection) getPaths() [][]string {
func (cs *ConfigSection) getOptions(path []string) []*KeyValueStorage {
opts, err := cs.sm.GetValues(path)
if err != nil {
//log error
log.WithError(err).Warn("Couldn't get values from options")
return nil
}

Expand All @@ -115,7 +120,7 @@ func (cs *ConfigSection) getOptions(path []string) []*KeyValueStorage {
func (cs *ConfigSection) getOption(name string, path []string) *KeyValueStorage {
opt, err := cs.sm.GetValue(name, path)
if err != nil {
//log error
log.WithError(err).Warn("Couldn't get value %s from saved settings", name)
return nil
}

Expand All @@ -129,24 +134,42 @@ func (cs *ConfigSection) getOption(name string, path []string) *KeyValueStorage
func (cs *ConfigSection) saveOptionValue(opt string, path []string, name string, value string) {
optV := cs.getOption(opt, path)
if optV == nil {
//log error
log.Warn("Couldn't get option %s from saved settings when saving its new value", opt)
return
}

optV.setValue(name, value)
data, err := json.Marshal(optV.keyValues)
if err != nil {
//log error
log.WithError(err).Error("Couldn't marshal values")
return
}
cs.sm.Save(opt, path, string(data))

if opt == "log" {
if name == "level" {
level, err := logging.LevelFromString(value)
if err != nil {
log.WithError(err).Warn("%s is not a correct log level", value)
} else {
logging.SetLevel(level)
}
} else if name == "output" {
writer, err := logging.GetOutputWriter(value)
if err != nil {
log.WithError(err).Warn("%s is not a correct output entry or path", value)
} else {
logging.SetOutputTo(writer)
skylog.SetOutputTo(writer)
}
}
}
}

func (cs *ConfigSection) getValue(opt string, path []string, name string) string {
optV := cs.getOption(opt, path)
if optV == nil {
//log error
log.Warn("Couldn't get value %s from saved settings", opt)
return ""
}
return optV.getValue(name)
Expand All @@ -161,6 +184,8 @@ func (cs *ConfigSection) getDefaultValue(opt string, path []string, name string)
return val
}

//TODO: Exported type KeyValueStorage should have comment or be unexported (linter suggestion)
// KeyValueStorage <description>
type KeyValueStorage struct {
qtcore.QObject
_ func() []string `slot:"getKeys"`
Expand Down Expand Up @@ -192,7 +217,7 @@ func (kv *KeyValueStorage) getKeys() []string {
func (kv *KeyValueStorage) getValue(key string) string {
val, ok := kv.keyValues[key]
if !ok {
//log error
log.Warn("Incorrect key %s", key)
return ""
}
return val
Expand Down
65 changes: 61 additions & 4 deletions src/ui/Settings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Page {
readonly property string defaultWalletPath: configManager.getDefaultValue("skycoin/walletSource/1/Source")
readonly property bool defaultIsLocalWalletEnv: configManager.getDefaultValue("skycoin/walletSource/1/SourceType") === "local"
readonly property string defaultNodeUrl: configManager.getDefaultValue("skycoin/node/address")
readonly property string defaultLogLevel: configManager.getDefaultValue("skycoin/log/level")
readonly property string defaultLogOutput: configManager.getDefaultValue("skycoin/log/output")
readonly property var defaultCacheLifeTime: configManager.getDefaultValue("global/cache/lifeTime")

// These are the saved settings, must be applied when the settings are opened or when
Expand All @@ -30,14 +32,24 @@ Page {
property string savedWalletPath: configManager.getValue("skycoin/walletSource/1/Source")
property bool savedIsLocalWalletEnv: configManager.getValue("skycoin/walletSource/1/SourceType") === "local"
property url savedNodeUrl: configManager.getValue("skycoin/node/address")
property string savedLogLevel: configManager.getValue("skycoin/log/level")
property string savedLogOutput: configManager.getValue("skycoin/log/output")
property var savedLifeTime: configManager.getValue("global/cache/lifeTime")

// QtObject{
// id: logLevel
// property string modifier
// property string old
// }

// These are the properties that are actually set, so they are aliases of the respective
// control's properties
property alias walletPath: textFieldWalletPath.text
property alias isLocalWalletEnv: switchLocalWalletEnv.checked
property alias nodeUrl: textFieldNodeUrl.text
property alias cacheLifeTime: textFieldCacheLifeTime.text
property alias logLevel: textFieldLogLevel.text
property alias logOutput: textFieldLogOutput.text

Component.onCompleted: {
loadSavedSettings()
Expand All @@ -47,7 +59,8 @@ Page {
configManager.setValue("skycoin/walletSource/1/Source", walletPath)
configManager.setValue("skycoin/walletSource/1/SourceType", isLocalWalletEnv ? "local" : "remote")
configManager.setValue("skycoin/node/address", nodeUrl)
configManager.setValue("global/cache/lifeTime", cacheLifeTime)
configManager.setValue("skycoin/log/level", logLevel)
configManager.setValue("skycoin/log/output", logOutput)
loadSavedSettings()
}

Expand All @@ -56,6 +69,8 @@ Page {
isLocalWalletEnv = savedIsLocalWalletEnv = configManager.getValue("skycoin/walletSource/1/SourceType") === "local"
nodeUrl = savedNodeUrl = configManager.getValue("skycoin/node/address")
cacheLifeTime = savedLifeTime = configManager.getValue("global/cache/lifeTime")
logLevel = savedLogLevel = configManager.getValue("skycoin/log/level")
logOutput = savedLogOutput = configManager.getValue("skycoin/log/output")

updateFooterButtonsStatus()
}
Expand All @@ -65,13 +80,15 @@ Page {
isLocalWalletEnv = defaultIsLocalWalletEnv
nodeUrl = defaultNodeUrl
cacheLifeTime = defaultCacheLifeTime
logLevel = defaultLogLevel
logOutput = defaultLogOutput

saveCurrentSettings()
}

function updateFooterButtonsStatus() {
var configChanged = (walletPath !== savedWalletPath || isLocalWalletEnv !== savedIsLocalWalletEnv || nodeUrl != savedNodeUrl || cacheLifeTime != savedLifeTime)
var noDefaultConfig = (walletPath !== defaultWalletPath || isLocalWalletEnv !== defaultIsLocalWalletEnv || nodeUrl !== defaultNodeUrl || cacheLifeTime || defaultCacheLifeTime)
var configChanged = (walletPath !== savedWalletPath || isLocalWalletEnv !== savedIsLocalWalletEnv || nodeUrl != savedNodeUrl || cacheLifeTime != savedLifeTime || logLevel != savedLogLevel || logOutput != savedLogOutput)
var noDefaultConfig = (walletPath !== defaultWalletPath || isLocalWalletEnv !== defaultIsLocalWalletEnv || nodeUrl !== defaultNodeUrl || cacheLifeTime || defaultCacheLifeTime || logLevel !== defaultLogLevel || logOutput != defaultLogOutput)
footer.standardButton(Dialog.Apply).enabled = configChanged
footer.standardButton(Dialog.Discard).enabled = configChanged
footer.standardButton(Dialog.RestoreDefaults).enabled = noDefaultConfig
Expand Down Expand Up @@ -288,7 +305,47 @@ Page {
}

} // GroupBox (addressBook setting)
} // ColumnLayout

GroupBox{
Layout.fillWidth: true
title: qsTr("Log level")

RowLayout{

anchors.fill: parent
TextField {
id: textFieldLogLevel

selectByMouse: true

placeholderText: qsTr("Log level")
onTextChanged: {
updateFooterButtonsStatus();
}
}
}
}

GroupBox{
Layout.fillWidth: true
title: qsTr("Log output")

RowLayout{

anchors.fill: parent
TextField {
id: textFieldLogOutput

selectByMouse: true

placeholderText: qsTr("Log output")
onTextChanged: {
updateFooterButtonsStatus();
}
}
}
}
}

ButtonGroup {
property int select:checkedButton.pos
Expand Down
1 change: 0 additions & 1 deletion src/ui/TransactionDetails.qml
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ Item {
Layout.alignment: Qt.AlignTop
Layout.fillWidth: true
Layout.fillHeight: true
Component.onCompleted: console.log("LALALALA", width, height, contentWidth, contentHeight)

ListView {
id: listViewOutputs
Expand Down
Loading

0 comments on commit 094455d

Please sign in to comment.