From 62350fe8b7952aa41ee7a62c485c0c2126dbf0fa Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Sun, 14 Aug 2022 21:33:38 +0200 Subject: [PATCH] Make the code fit for using the unix socket and save settings --- cmd/backive_ui/main.go | 16 ++-------------- go.mod | 5 ----- go.sum | 7 ------- ui/ui_main.go | 36 ++++++++++++++++++++++++++++++++---- ui_handler.go | 21 ++++++++++++++++++--- 5 files changed, 52 insertions(+), 33 deletions(-) diff --git a/cmd/backive_ui/main.go b/cmd/backive_ui/main.go index d4188c4..e0aa958 100644 --- a/cmd/backive_ui/main.go +++ b/cmd/backive_ui/main.go @@ -1,8 +1,6 @@ package main import ( - "time" - "fyne.io/fyne/v2/app" "github.com/qwc/backive" @@ -10,23 +8,13 @@ import ( ) var ( - config backive.Configuration - database backive.Database + config backive.Configuration ) func main() { - database.Load() config.Load() - backive.Init(config, database) app := app.NewWithID("Backive UI") - backiveui.Init(app, nil, config, database) - go func() { - for { - backiveui.NotificationRun() - time.Sleep(time.Second) - } - }() - + backiveui.Init(app, nil, config) app.Run() } diff --git a/go.mod b/go.mod index 967b4e9..346801e 100644 --- a/go.mod +++ b/go.mod @@ -21,13 +21,11 @@ require ( github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff // indirect github.com/gopherjs/gopherjs v0.0.0-20211219123610-ec9572f70e60 // indirect github.com/hashicorp/hcl v1.0.0 // indirect - github.com/hexdigest/gounit v0.0.0-20180817093830-f1874d3307cb // indirect github.com/kr/text v0.2.0 // indirect github.com/magiconair/properties v1.8.6 // indirect github.com/mitchellh/mapstructure v1.4.3 // indirect github.com/pelletier/go-toml v1.9.4 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/shibukawa/configdir v0.0.0-20170330084843-e180dbdc8da0 // indirect github.com/spf13/afero v1.8.2 // indirect github.com/spf13/cast v1.4.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect @@ -39,12 +37,9 @@ require ( github.com/yuin/goldmark v1.4.0 // indirect golang.org/x/image v0.0.0-20200430140353-33d19683fad8 // indirect golang.org/x/mobile v0.0.0-20211207041440-4e6c2922fdee // indirect - golang.org/x/mod v0.4.2 // indirect golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect golang.org/x/sys v0.0.0-20220317061510-51cd9980dadf // indirect golang.org/x/text v0.3.7 // indirect - golang.org/x/tools v0.1.8-0.20211022200916-316ba0b74098 // indirect - golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/ini.v1 v1.66.4 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index 174236f..b0fc555 100644 --- a/go.sum +++ b/go.sum @@ -264,8 +264,6 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p github.com/hashicorp/memberlist v0.3.0/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE= github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= github.com/hashicorp/serf v0.9.6/go.mod h1:TXZNMjZQijwlDvp+r0b63xZ45H7JmCmgg4gpTwn9UV4= -github.com/hexdigest/gounit v0.0.0-20180817093830-f1874d3307cb h1:n/9MDDIvjvPY8fTNWozjyeN4UajDZX3R/X7OuEKgquw= -github.com/hexdigest/gounit v0.0.0-20180817093830-f1874d3307cb/go.mod h1:MrMFZVYn+mNMWR7SsVxvf5L373FZy4+EDS3pBm7D9Kk= github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= @@ -369,8 +367,6 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/sagikazarmark/crypt v0.4.0/go.mod h1:ALv2SRj7GxYV4HO9elxH9nS6M9gW+xDNxqmyJ6RfDFM= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= -github.com/shibukawa/configdir v0.0.0-20170330084843-e180dbdc8da0 h1:Xuk8ma/ibJ1fOy4Ee11vHhUFHQNpHhrBneOCNHVXS5w= -github.com/shibukawa/configdir v0.0.0-20170330084843-e180dbdc8da0/go.mod h1:7AwjWCpdPhkSmNAgUv5C7EJ4AbmjEB3r047r3DXWu3Y= github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk= github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= @@ -488,7 +484,6 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -708,12 +703,10 @@ golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= -golang.org/x/tools v0.1.8-0.20211022200916-316ba0b74098 h1:YuekqPskqwCCPM79F1X5Dhv4ezTCj+Ki1oNwiafxkA0= golang.org/x/tools v0.1.8-0.20211022200916-316ba0b74098/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= diff --git a/ui/ui_main.go b/ui/ui_main.go index 8168c3c..dcf62fa 100644 --- a/ui/ui_main.go +++ b/ui/ui_main.go @@ -3,7 +3,10 @@ package ui import ( "encoding/json" "fmt" + "log" "net" + "os" + "path" "strconv" "time" @@ -13,6 +16,9 @@ import ( "github.com/qwc/backive" ) +var mockOsWriteFile = os.WriteFile +var mockOsReadFile = os.ReadFile + type UISettings struct { hideUntil time.Time globalLevel int @@ -22,19 +28,20 @@ var ( app fyne.App window fyne.Window config backive.Configuration - db backive.Database doNotShowUntil time.Time = time.Unix(0, 0) c net.Conn uisettings UISettings messageLevel int + apphomedir string ) -func Init(a fyne.App, w fyne.Window, conf backive.Configuration, d backive.Database) { +func Init(a fyne.App, w fyne.Window, conf backive.Configuration) { app = a a.SetIcon(theme.FyneLogo()) makeTray(app) config = conf - db = d + apphomedir, _ := os.UserHomeDir() + apphomedir += string(os.PathSeparator) + ".config" + string(os.PathSeparator) + "backive" + string(os.PathSeparator) + "ui.json" go PollConnection() } @@ -111,15 +118,36 @@ func SetMessageLevel(level int) { } else { uisettings.globalLevel = level messageLevel = 0 + SaveSettings() } } func SaveSettings() { // save internal settings to file in homedir + jsonstr, merr := json.Marshal(uisettings) + if merr != nil { + panic(merr) + } + log.Printf("Writing database output to file: %s", jsonstr) + saveDir, _ := path.Split(apphomedir) + backive.CreateDirectoryIfNotExists(saveDir) + err := mockOsWriteFile(apphomedir, []byte(jsonstr), 0644) + if err != nil { + panic(err) + } } func LoadSettings() { // load settings + if _, err := os.Stat(apphomedir); err == nil { + data, rferr := mockOsReadFile(apphomedir) + if rferr != nil { + panic(rferr) + } + json.Unmarshal(data, &uisettings) + } /*else if os.IsNotExist(err) { + // no data + }*/ } func makeTray(app fyne.App) { @@ -145,7 +173,7 @@ func makeTray(app fyne.App) { levelMenu.ChildMenu = fyne.NewMenu( "", fyne.NewMenuItem( - "Only problems and tasks finished (resets to default with restart)", + "Only problems and tasks finished (resets to previous with restart)", func() { SetMessageLevel(10) }, ), fyne.NewMenuItem( diff --git a/ui_handler.go b/ui_handler.go index 48262e7..f92dbc0 100644 --- a/ui_handler.go +++ b/ui_handler.go @@ -8,6 +8,19 @@ import ( "path" ) +// MsgLevel type for setting the message level +type MsgLevel int + +// Constants for setting the MsgLevel +const ( + ERROR MsgLevel = iota * 10 + FINISH + REMIND + INFO + DEBUG +) + +// UIHandler internal data struct type UIHandler struct { ls net.Listener client net.Conn @@ -18,6 +31,7 @@ var mockUIAccept = func(uh *UIHandler) (net.Conn, error) { return uh.ls.Accept() } +// Init initializing the UIHandler func (uh *UIHandler) Init(socketPath string) error { log.Println("Initializing UIHandler") var err error @@ -30,6 +44,7 @@ func (uh *UIHandler) Init(socketPath string) error { return nil } +// Listen starts the Unix socket listener func (uh *UIHandler) Listen() { log.Println("Running UIHandler loop") func() { @@ -43,6 +58,7 @@ func (uh *UIHandler) Listen() { }() } +// DisplayMessage is the method to use inside the service to display messages, with intended level func (uh *UIHandler) DisplayMessage(header string, message string, level int) error { if uh.client != nil { var data map[string]interface{} @@ -56,8 +72,7 @@ func (uh *UIHandler) DisplayMessage(header string, message string, level int) er } uh.client.Write(b) return nil - } else { - log.Println("No UI client available, msg did not get delivered.") - return fmt.Errorf("No UI client available, msg not delivered.") } + log.Println("No UI client available, msg did not get delivered.") + return fmt.Errorf("No UI client available, msg not delivered") }