Files
lambda/gtk-update.go

137 lines
3.0 KiB
Go

package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"io"
"net/http"
"strconv"
"github.com/BurntSushi/toml"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"github.com/gen2brain/beeep"
"github.com/jasonlovesdoggo/gopen"
)
func updateCheck() error {
req, err := http.Get("https://sunglocto.net/lambda_update")
if err != nil {
return err
}
defer req.Body.Close()
p, err := io.ReadAll(req.Body)
if err != nil {
return err
}
newest_version, err := strconv.Atoi(string(p))
if err != nil {
return err
}
current_version, err := strconv.Atoi(string(lambda_version))
if err != nil {
return err
}
if newest_version > current_version {
win := gtk.NewWindow()
win.SetDefaultSize(1, 1)
box := gtk.NewBox(gtk.OrientationVertical, 5)
header := gtk.NewLabel(loadedLocale["updateAvailable"])
txt := gtk.NewLabel(fmt.Sprintf(loadedLocale["versionAvailable"], newest_version))
btn := gtk.NewButtonWithLabel(loadedLocale["seeSite"])
btn.ConnectClicked(func() {
gopen.Open("https://sunglocto.net/lambda")
win.SetVisible(false)
})
header.AddCSSClass("author")
box.Append(header)
box.Append(txt)
box.Append(btn)
win.SetChild(box)
win.SetResizable(false)
win.SetVisible(true)
}
return nil
}
func showInitialConsent() {
win := gtk.NewWindow()
win.SetDefaultSize(600, 1)
win.SetTitle(loadedLocale["updateCheck"])
box := gtk.NewBox(gtk.OrientationVertical, 5)
textbox := gtk.NewBox(gtk.OrientationVertical, 5)
subtextbox := gtk.NewBox(gtk.OrientationHorizontal, 5)
header := gtk.NewLabel(loadedLocale["updateCheck"])
header.AddCSSClass("author")
text := gtk.NewLabel(loadedLocale["updateWarning"])
text.SetHAlign(gtk.AlignCenter)
textbox.Append(header)
i := gtk.NewImageFromPaintable(clientAssetsLoad("update"))
i.SetPixelSize(50)
subtextbox.Append(i)
subtextbox.Append(text)
textbox.Append(subtextbox)
buttonbox := gtk.NewBox(gtk.OrientationHorizontal, 0)
yes := gtk.NewButtonWithLabel(loadedLocale["yes"])
yes.SetHExpand(true)
yes.ConnectClicked(func() {
win.SetVisible(false)
save("yes")
})
no := gtk.NewButtonWithLabel(loadedLocale["no"])
no.SetHExpand(true)
no.ConnectClicked(func() {
win.SetVisible(false)
save("no")
})
buttonbox.Append(yes)
buttonbox.Append(no)
box.Append(textbox)
box.Append(buttonbox)
win.SetChild(box)
win.SetResizable(false)
win.SetVisible(true)
}
func save(choice string) {
loadedConfig.CheckUpdate = choice
var b bytes.Buffer
e := toml.NewEncoder(&b)
e.Encode(loadedConfig)
p, err := ensureConfig()
if err != nil {
panic(err)
}
os.WriteFile(filepath.Join(p, "lambda.toml"), b.Bytes(), 0644)
fmt.Println("a")
// Please add tenary operators to go istg this is painful
// TODO: This approach may not work for RTL languages OR languages with different structure like CJK
willwont := make(map[string]string)
willwont["yes"] = loadedLocale["updateWill"]
willwont["no"] = loadedLocale["updateWont"]
beeep.Notify(loadedLocale["attention"], fmt.Sprintf(loadedLocale["updateSave"], willwont[choice]), commentBytes)
}