-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.go
More file actions
144 lines (119 loc) · 3.68 KB
/
Copy pathversion.go
File metadata and controls
144 lines (119 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
// TODO: allow disabling update check (after config file is there)
// version is passed at build time
var version string = "dev"
type githubRelease struct {
TagName string `json:"tag_name"`
HTMLURL string `json:"html_url"`
Body string `json:"body"`
}
// checkForUpdates fetches the latest release from GitHub API in the background
func checkForUpdates(w fyne.Window, a fyne.App) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.github.com/repos/Softorage/7z-GUI-Linux/releases/latest", nil)
if err != nil {
return
}
req.Header.Set("User-Agent", "7GL-App/"+version)
req.Header.Set("Accept", "application/vnd.github.v3+json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return
}
var rel githubRelease
if err := json.NewDecoder(resp.Body).Decode(&rel); err != nil {
return
}
if isNewerVersion(rel.TagName, version) {
fyne.Do(func() {
showUpdateDialog(w, a, rel)
})
}
}
// isNewerVersion compares semver strings (e.g. "v1.2.0" vs "1.1.9")
func isNewerVersion(latest, current string) bool {
latestClean := strings.TrimPrefix(strings.TrimSpace(latest), "v")
latestClean = strings.TrimPrefix(latestClean, "V")
currentClean := strings.TrimPrefix(strings.TrimSpace(current), "v")
currentClean = strings.TrimPrefix(currentClean, "V")
// Split prerelease/metadata suffixes if present
latestMain := strings.Split(latestClean, "-")[0]
currentMain := strings.Split(currentClean, "-")[0]
lParts := strings.Split(latestMain, ".")
cParts := strings.Split(currentMain, ".")
maxLen := len(lParts)
if len(cParts) > maxLen {
maxLen = len(cParts)
}
for i := 0; i < maxLen; i++ {
var lNum, cNum int
if i < len(lParts) {
lNum, _ = strconv.Atoi(lParts[i])
}
if i < len(cParts) {
cNum, _ = strconv.Atoi(cParts[i])
}
if lNum > cNum {
return true
} else if lNum < cNum {
return false
}
}
return false
}
func showUpdateDialog(w fyne.Window, a fyne.App, rel githubRelease) {
relURL, err := url.Parse(rel.HTMLURL)
if err != nil {
relURL, _ = url.Parse("https://github.com/Softorage/7z-GUI-Linux/releases/latest")
}
heading := widget.NewLabelWithStyle("New Update Available!", fyne.TextAlignCenter, fyne.TextStyle{Bold: true})
verInfo := widget.NewLabel(fmt.Sprintf("Installed: %s ➜ Latest: %s", version, rel.TagName))
verInfo.Alignment = fyne.TextAlignCenter
contentObjects := []fyne.CanvasObject{heading, verInfo}
if notes := strings.TrimSpace(rel.Body); notes != "" {
notesHeader := widget.NewLabelWithStyle("Release Notes:", fyne.TextAlignLeading, fyne.TextStyle{Bold: true})
//notesText := widget.NewRichTextFromMarkdown(notes)
notesText := widget.NewRichText(&widget.TextSegment{
Text: notes,
Style: widget.RichTextStyle{
ColorName: theme.ColorNamePlaceHolder, // e.g., ColorNamePrimary, ColorNamePlaceHolder, ColorNameWarning, ColorNameError
},
})
notesText.Wrapping = fyne.TextWrapWord
notesBox := container.NewGridWrap(fyne.NewSize(440, 160), container.NewPadded(container.NewVScroll(notesText)))
contentObjects = append(contentObjects, widget.NewSeparator(), notesHeader, notesBox)
}
dialog.ShowCustomConfirm(
"Software Update",
"Download Update ↗",
"Later",
container.NewVBox(contentObjects...),
func(confirm bool) {
if confirm {
a.OpenURL(relURL)
}
},
w,
)
}