2022-03-07 23:24:02 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2022-06-07 21:20:16 +02:00
|
|
|
"encoding/json"
|
2022-03-12 23:08:57 +01:00
|
|
|
"fmt"
|
2022-06-07 21:20:16 +02:00
|
|
|
"net"
|
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-06-08 10:04:28 +02:00
|
|
|
type UISettings struct {
|
|
|
|
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
|
|
|
|
db backive.Database
|
|
|
|
doNotShowUntil time.Time = time.Unix(0, 0)
|
2022-06-08 09:48:26 +02:00
|
|
|
c net.Conn
|
2022-06-08 10:04:28 +02:00
|
|
|
uisettings UISettings
|
|
|
|
messageLevel int
|
2022-03-07 23:24:02 +01:00
|
|
|
)
|
|
|
|
|
2022-06-07 21:20:16 +02:00
|
|
|
func Init(a fyne.App, w fyne.Window, conf backive.Configuration, d backive.Database) {
|
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-03-07 23:24:02 +01:00
|
|
|
db = d
|
2022-06-07 21:20:16 +02:00
|
|
|
go PollConnection()
|
2022-03-07 23:24:02 +01:00
|
|
|
}
|
2022-06-07 21:20:16 +02:00
|
|
|
|
|
|
|
func PollConnection() {
|
|
|
|
var err error
|
|
|
|
for {
|
|
|
|
if c == nil {
|
|
|
|
c, err = net.Dial("unix", config.Settings.UIUnixSocketLocation)
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("Connection already established")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
// ignore
|
|
|
|
err = nil
|
|
|
|
// sleep a while and then retry
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 22:19:15 +02:00
|
|
|
func NotificationRun() {
|
2022-06-07 21:20:16 +02:00
|
|
|
if c != nil {
|
|
|
|
b := make([]byte, 2048)
|
|
|
|
i, err := c.Read(b)
|
|
|
|
if err == nil && i > 0 {
|
|
|
|
var data map[string]string
|
|
|
|
err = json.Unmarshal(b, &data)
|
|
|
|
if err == nil {
|
|
|
|
ShowNotification(data)
|
|
|
|
}
|
|
|
|
// else ignore and try to read again
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
// we just try again and discard the error
|
|
|
|
err = nil
|
|
|
|
}
|
2022-04-18 22:13:06 +02:00
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
if level <= 10 {
|
|
|
|
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-06-08 09:48:26 +02:00
|
|
|
func SetHideUntil(until time.Time) {
|
2022-06-08 10:04:28 +02:00
|
|
|
uisettings.hideUntil = until
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SaveSettings() {
|
|
|
|
// save internal settings to file in homedir
|
|
|
|
}
|
2022-06-08 09:48:26 +02:00
|
|
|
|
2022-06-08 10:04:28 +02:00
|
|
|
func LoadSettings() {
|
|
|
|
// load settings
|
2022-06-08 09:48:26 +02:00
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
"Hide Reminders for",
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
hideReminders.ChildMenu = fyne.NewMenu(
|
|
|
|
"",
|
|
|
|
fyne.NewMenuItem("Hide reminder notifications for 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-06-08 09:48:26 +02:00
|
|
|
fyne.NewMenuItem("Hide reminder notifications for 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(
|
|
|
|
"Only problems and tasks finished (resets to default with restart)",
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|