2022-03-07 23:24:02 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2022-09-02 13:22:05 +02:00
|
|
|
"bytes"
|
2022-06-07 21:20:16 +02:00
|
|
|
"encoding/json"
|
2022-08-14 21:33:38 +02:00
|
|
|
"log"
|
2022-06-07 21:20:16 +02:00
|
|
|
"net"
|
2022-08-14 21:33:38 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
2022-06-08 09:48:26 +02:00
|
|
|
"strconv"
|
2022-04-04 22:19:15 +02:00
|
|
|
"time"
|
2022-03-12 23:08:57 +01:00
|
|
|
|
2022-03-07 23:24:02 +01:00
|
|
|
"fyne.io/fyne/v2"
|
2022-03-17 20:56:24 +01:00
|
|
|
"fyne.io/fyne/v2/driver/desktop"
|
2022-03-17 21:01:26 +01:00
|
|
|
"fyne.io/fyne/v2/theme"
|
2022-03-07 23:24:02 +01:00
|
|
|
"github.com/qwc/backive"
|
|
|
|
)
|
|
|
|
|
2022-08-14 21:33:38 +02:00
|
|
|
var mockOsWriteFile = os.WriteFile
|
|
|
|
var mockOsReadFile = os.ReadFile
|
|
|
|
|
2022-08-14 21:40:37 +02:00
|
|
|
type uiSettings struct {
|
2022-06-08 10:04:28 +02:00
|
|
|
hideUntil time.Time
|
|
|
|
globalLevel int
|
|
|
|
}
|
|
|
|
|
2022-03-07 23:24:02 +01:00
|
|
|
var (
|
2022-04-18 22:13:06 +02:00
|
|
|
app fyne.App
|
|
|
|
window fyne.Window
|
|
|
|
config backive.Configuration
|
|
|
|
doNotShowUntil time.Time = time.Unix(0, 0)
|
2022-06-08 09:48:26 +02:00
|
|
|
c net.Conn
|
2022-08-14 21:40:37 +02:00
|
|
|
uisettings uiSettings
|
2022-06-08 10:04:28 +02:00
|
|
|
messageLevel int
|
2022-08-14 21:33:38 +02:00
|
|
|
apphomedir string
|
2022-03-07 23:24:02 +01:00
|
|
|
)
|
|
|
|
|
2022-08-14 21:40:37 +02:00
|
|
|
// Init the fyne application
|
2022-08-14 21:33:38 +02:00
|
|
|
func Init(a fyne.App, w fyne.Window, conf backive.Configuration) {
|
2022-03-07 23:24:02 +01:00
|
|
|
app = a
|
2022-03-17 20:56:24 +01:00
|
|
|
a.SetIcon(theme.FyneLogo())
|
|
|
|
makeTray(app)
|
2022-06-07 21:20:16 +02:00
|
|
|
config = conf
|
2022-08-14 21:33:38 +02:00
|
|
|
apphomedir, _ := os.UserHomeDir()
|
2022-09-02 13:22:05 +02:00
|
|
|
apphomedir = path.Join(apphomedir, ".config", "backive", "ui.json")
|
2022-08-14 22:55:57 +02:00
|
|
|
LoadSettings()
|
2022-06-07 21:20:16 +02:00
|
|
|
go PollConnection()
|
2022-09-02 13:22:05 +02:00
|
|
|
log.Println("UI started")
|
2022-03-07 23:24:02 +01:00
|
|
|
}
|
2022-06-07 21:20:16 +02:00
|
|
|
|
2022-08-14 21:40:37 +02:00
|
|
|
// PollConnection polls in an endless loop the connection
|
2022-06-07 21:20:16 +02:00
|
|
|
func PollConnection() {
|
|
|
|
var err error
|
2022-09-02 13:22:05 +02:00
|
|
|
// Loop for connection and receiving data
|
2022-06-07 21:20:16 +02:00
|
|
|
for {
|
2022-09-02 13:22:05 +02:00
|
|
|
// connect when client 'c' is nil
|
2022-06-07 21:20:16 +02:00
|
|
|
if c == nil {
|
2022-09-02 13:22:05 +02:00
|
|
|
log.Println("Creating connection")
|
2022-06-07 21:20:16 +02:00
|
|
|
c, err = net.Dial("unix", config.Settings.UIUnixSocketLocation)
|
2022-10-10 22:55:20 +02:00
|
|
|
} // else already connected
|
2022-09-02 13:22:05 +02:00
|
|
|
// receive msgs
|
2022-10-10 23:11:02 +02:00
|
|
|
if c != nil && err == nil {
|
2022-09-02 13:22:05 +02:00
|
|
|
data := make([]byte, 2048)
|
2022-10-10 22:55:20 +02:00
|
|
|
nr, err := c.Read(data)
|
|
|
|
log.Printf("Read %d bytes...", nr)
|
|
|
|
if err != nil {
|
2022-10-10 23:11:02 +02:00
|
|
|
log.Printf("Error reading data from socket: %s", err)
|
|
|
|
c = nil
|
2022-09-02 13:22:05 +02:00
|
|
|
}
|
2022-10-10 23:11:02 +02:00
|
|
|
if err == nil {
|
|
|
|
sdata := string(bytes.Trim(data, "\x00"))
|
|
|
|
var message map[string]string
|
|
|
|
log.Printf("Reading JSON: %s", sdata)
|
|
|
|
errjson := json.Unmarshal([]byte(sdata), &message)
|
|
|
|
if errjson != nil {
|
|
|
|
err = errjson
|
|
|
|
log.Printf("Error parsing json: %s", errjson)
|
|
|
|
c = nil
|
|
|
|
}
|
|
|
|
if errjson == nil {
|
|
|
|
ShowNotification(message)
|
|
|
|
}
|
2022-09-02 13:22:05 +02:00
|
|
|
}
|
2022-10-10 23:11:02 +02:00
|
|
|
}
|
|
|
|
// handle error on connection
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error during communication: %s", err)
|
|
|
|
// ignore
|
|
|
|
err = nil
|
|
|
|
c = nil
|
|
|
|
// sleep a while and then retry
|
|
|
|
time.Sleep(10 * time.Second)
|
2022-09-02 13:22:05 +02:00
|
|
|
}
|
2022-06-07 21:20:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:37 +02:00
|
|
|
// ShowNotification shows a single notification
|
2022-06-07 21:20:16 +02:00
|
|
|
func ShowNotification(data map[string]string) {
|
|
|
|
if ShallShow(data) {
|
2022-04-18 22:13:06 +02:00
|
|
|
app.SendNotification(
|
|
|
|
fyne.NewNotification(
|
2022-06-07 21:20:16 +02:00
|
|
|
data["header"],
|
|
|
|
data["message"],
|
2022-04-18 22:13:06 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:37 +02:00
|
|
|
// ShallShow checks if a message should be shown by level
|
2022-06-07 21:20:16 +02:00
|
|
|
func ShallShow(data map[string]string) bool {
|
2022-06-08 09:48:26 +02:00
|
|
|
level, err := strconv.ParseUint(data["level"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-10-10 22:55:20 +02:00
|
|
|
if level <= 30 {
|
2022-06-08 09:48:26 +02:00
|
|
|
return true
|
|
|
|
}
|
2022-06-08 10:04:28 +02:00
|
|
|
if int(level) <= uisettings.globalLevel && messageLevel > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if int(level) <= uisettings.globalLevel {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2022-04-04 22:19:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:37 +02:00
|
|
|
// SetHideUntil sets the time until messages should be hidden
|
2022-06-08 09:48:26 +02:00
|
|
|
func SetHideUntil(until time.Time) {
|
2022-06-08 10:04:28 +02:00
|
|
|
uisettings.hideUntil = until
|
2022-08-14 22:55:57 +02:00
|
|
|
SaveSettings()
|
2022-06-08 09:48:26 +02:00
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:37 +02:00
|
|
|
// SetMessageLevel does exactly that.
|
2022-06-08 09:48:26 +02:00
|
|
|
func SetMessageLevel(level int) {
|
2022-06-08 10:04:28 +02:00
|
|
|
if level <= 10 {
|
|
|
|
messageLevel = level
|
|
|
|
} else {
|
|
|
|
uisettings.globalLevel = level
|
|
|
|
messageLevel = 0
|
2022-08-14 21:33:38 +02:00
|
|
|
SaveSettings()
|
2022-06-08 10:04:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:37 +02:00
|
|
|
// SaveSettings stores the settings in $HOME/.config/backive/ui.json
|
2022-06-08 10:04:28 +02:00
|
|
|
func SaveSettings() {
|
|
|
|
// save internal settings to file in homedir
|
2022-08-14 21:33:38 +02:00
|
|
|
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)
|
|
|
|
}
|
2022-09-02 13:22:05 +02:00
|
|
|
log.Println("Saved Settings.")
|
2022-06-08 10:04:28 +02:00
|
|
|
}
|
2022-06-08 09:48:26 +02:00
|
|
|
|
2022-08-14 21:40:37 +02:00
|
|
|
// LoadSettings loads the settings from the place where SaveSettings stored them.
|
2022-06-08 10:04:28 +02:00
|
|
|
func LoadSettings() {
|
|
|
|
// load settings
|
2022-08-14 21:33:38 +02:00
|
|
|
if _, err := os.Stat(apphomedir); err == nil {
|
|
|
|
data, rferr := mockOsReadFile(apphomedir)
|
|
|
|
if rferr != nil {
|
|
|
|
panic(rferr)
|
|
|
|
}
|
|
|
|
json.Unmarshal(data, &uisettings)
|
2022-09-02 13:22:05 +02:00
|
|
|
log.Println("Loaded Settings.")
|
2022-08-14 21:33:38 +02:00
|
|
|
} /*else if os.IsNotExist(err) {
|
|
|
|
// no data
|
|
|
|
}*/
|
2022-06-08 09:48:26 +02:00
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:37 +02:00
|
|
|
// makeTray creates the tray menus needed.
|
2022-03-17 20:56:24 +01:00
|
|
|
func makeTray(app fyne.App) {
|
|
|
|
if desk, ok := app.(desktop.App); ok {
|
2022-06-08 09:48:26 +02:00
|
|
|
hideReminders := fyne.NewMenuItem(
|
2022-08-14 22:06:09 +02:00
|
|
|
"Hide reminders for",
|
2022-06-08 09:48:26 +02:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
hideReminders.ChildMenu = fyne.NewMenu(
|
|
|
|
"",
|
2022-08-14 22:06:09 +02:00
|
|
|
fyne.NewMenuItem("today", func() {
|
2022-04-18 22:13:06 +02:00
|
|
|
doNotShowUntil = time.Now().AddDate(0, 0, 1)
|
2022-06-08 09:48:26 +02:00
|
|
|
SetHideUntil(doNotShowUntil)
|
2022-03-21 22:41:24 +01:00
|
|
|
}),
|
2022-08-14 22:06:09 +02:00
|
|
|
fyne.NewMenuItem("a hour", func() {
|
2022-04-18 22:13:06 +02:00
|
|
|
doNotShowUntil = time.Now().Add(time.Hour)
|
2022-06-08 09:48:26 +02:00
|
|
|
SetHideUntil(doNotShowUntil)
|
2022-03-21 22:41:24 +01:00
|
|
|
}),
|
|
|
|
)
|
2022-06-08 09:48:26 +02:00
|
|
|
levelMenu := fyne.NewMenuItem(
|
|
|
|
"Set notification level", nil,
|
|
|
|
)
|
|
|
|
levelMenu.ChildMenu = fyne.NewMenu(
|
|
|
|
"",
|
|
|
|
fyne.NewMenuItem(
|
2022-08-14 21:33:38 +02:00
|
|
|
"Only problems and tasks finished (resets to previous with restart)",
|
2022-06-08 09:48:26 +02:00
|
|
|
func() { SetMessageLevel(10) },
|
|
|
|
),
|
|
|
|
fyne.NewMenuItem(
|
|
|
|
"+ Reminders (default)",
|
|
|
|
func() { SetMessageLevel(20) },
|
|
|
|
),
|
|
|
|
fyne.NewMenuItem(
|
|
|
|
"+ Informational messages",
|
|
|
|
func() { SetMessageLevel(30) },
|
|
|
|
),
|
|
|
|
fyne.NewMenuItem(
|
|
|
|
"+ Verbose/Debug messages",
|
|
|
|
func() { SetMessageLevel(40) },
|
|
|
|
),
|
|
|
|
)
|
|
|
|
menu := fyne.NewMenu(
|
|
|
|
"backive",
|
|
|
|
hideReminders,
|
|
|
|
levelMenu,
|
|
|
|
)
|
2022-03-17 20:56:24 +01:00
|
|
|
desk.SetSystemTrayMenu(menu)
|
|
|
|
}
|
|
|
|
}
|