2022-03-07 23:24:02 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2022-03-12 23:08:57 +01:00
|
|
|
"fmt"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-03-07 23:24:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func Init(a fyne.App, w fyne.Window, c backive.Configuration, d backive.Database) {
|
|
|
|
app = a
|
2022-03-17 20:56:24 +01:00
|
|
|
a.SetIcon(theme.FyneLogo())
|
|
|
|
makeTray(app)
|
2022-03-07 23:24:02 +01:00
|
|
|
config = c
|
|
|
|
db = d
|
|
|
|
}
|
2022-04-04 22:19:15 +02:00
|
|
|
func NotificationRun() {
|
2022-04-18 22:13:06 +02:00
|
|
|
if doNotShowUntil == time.Unix(0, 0) || time.Now().After(doNotShowUntil) {
|
|
|
|
ShowNotification()
|
|
|
|
if doNotShowUntil != time.Unix(0, 0) {
|
|
|
|
doNotShowUntil = time.Unix(0, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h, _ := time.ParseDuration("15m")
|
|
|
|
time.Sleep(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ShowNotification() {
|
|
|
|
displayStr, err := MakeNotificationString()
|
|
|
|
if err == nil {
|
|
|
|
app.SendNotification(
|
|
|
|
fyne.NewNotification(
|
|
|
|
"Backups are overdue...",
|
|
|
|
fmt.Sprintf("Name\t(device)\t[overdue]\n%s", displayStr),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MakeNotificationString() (string, error) {
|
|
|
|
db.Load()
|
|
|
|
var displayStr string = ""
|
|
|
|
var runs backive.Runs
|
|
|
|
runs.Load(db)
|
2022-04-04 22:19:15 +02:00
|
|
|
fmt.Printf("Notification run\n")
|
|
|
|
for _, v := range config.Backups {
|
|
|
|
fmt.Printf("Notification run %s\n", v.Name)
|
|
|
|
if v.ShouldRun() && v.Frequency > 0 {
|
|
|
|
fmt.Printf("Notification for %s\n", v.Name)
|
2022-04-18 22:13:06 +02:00
|
|
|
lastBackup, err := runs.LastRun(v)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
freq, _ := time.ParseDuration(fmt.Sprintf("%dd", v.Frequency))
|
|
|
|
days := time.Now().Sub(lastBackup.Add(freq))
|
|
|
|
displayStr += fmt.Sprintf("%s\t(%s)\t[%f days]\n", v.Name, v.TargetDevice, days.Hours()/24)
|
2022-04-04 22:19:15 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-18 22:13:06 +02:00
|
|
|
return displayStr, nil
|
2022-04-04 22:19:15 +02:00
|
|
|
}
|
|
|
|
|
2022-03-17 20:56:24 +01:00
|
|
|
func makeTray(app fyne.App) {
|
|
|
|
if desk, ok := app.(desktop.App); ok {
|
|
|
|
menu := fyne.NewMenu(
|
|
|
|
"backive",
|
2022-04-18 22:13:06 +02:00
|
|
|
fyne.NewMenuItem("Show notifications again", func() {
|
|
|
|
ShowNotification()
|
2022-03-21 22:41:24 +01:00
|
|
|
}),
|
2022-04-18 22:13:06 +02:00
|
|
|
fyne.NewMenuItem("Hide notifications for today", func() {
|
|
|
|
doNotShowUntil = time.Now().AddDate(0, 0, 1)
|
2022-03-21 22:41:24 +01:00
|
|
|
}),
|
2022-04-18 22:13:06 +02:00
|
|
|
fyne.NewMenuItem("Hide notifications for a hour", func() {
|
|
|
|
doNotShowUntil = time.Now().Add(time.Hour)
|
2022-03-21 22:41:24 +01:00
|
|
|
}),
|
|
|
|
)
|
2022-03-17 20:56:24 +01:00
|
|
|
desk.SetSystemTrayMenu(menu)
|
|
|
|
}
|
|
|
|
}
|