forked from libsgh/chrome_updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chrome_plus.go
155 lines (148 loc) · 5.29 KB
/
chrome_plus.go
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
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
jsoniter "github.com/json-iterator/go"
"io"
"log"
"net/http"
"os"
"path"
"path/filepath"
"sync"
)
func chromePlusScreen(win fyne.Window, data *SettingsData) fyne.CanvasObject {
var githubReleaseMap map[string]GithubRelease
var versionList []string
chromePlusRadio := widget.NewRadioGroup([]string{
"Bush2021"}, func(value string) {
data.chromePlus.Set(value)
})
versionSelect := widget.NewSelect([]string{}, func(ver string) {
setPlusVer(data, ver, githubReleaseMap)
})
versionSelect.PlaceHolder = LoadString("VersionSelectPlaceHolder")
versionSelect.Disable()
chromePlusRadio.Selected = getString(data.chromePlus)
chromePlusRadio.Disable()
downBtn := widget.NewButtonWithIcon(LoadString("InstallBtnLabel"), theme.DownloadIcon(), func() {
ov, _ := data.oldPlusVer.Get()
cv, _ := data.curPlusVer.Get()
if cv == ov && fileExist(path.Join(getString(data.installPath), "version.dll")) {
alertInfo(LoadString("NoNeedUpdateMsg"), win)
} else {
chromeInUse := isProcessExist("chrome.exe")
if chromeInUse {
alertInfo(LoadString("ChromeRunningMsg"), win)
} else {
data.plusBtnStatus.Set(true)
installPlus(data, win)
}
}
})
checkBtn := widget.NewButtonWithIcon(LoadString("CheckBtnLabel"), theme.SearchIcon(), func() {
githubReleaseMap, versionList = getChromePlusInfo(getString(data.ghProxy))
if githubReleaseMap != nil && len(versionList) > 0 {
versionSelect.SetOptions(versionList)
versionSelect.SetSelected(versionList[0])
setPlusVer(data, versionList[0], githubReleaseMap)
versionSelect.Enable()
downBtn.Enable()
} else {
alertInfo(LoadString("CheckErrMsg"), win)
}
})
data.plusBtnStatus.AddListener(binding.NewDataListener(func() {
if getBool(data.plusBtnStatus) {
downBtn.Disable()
} else {
downBtn.Enable()
}
}))
curVerLabel := widget.NewLabelWithData(data.curPlusVer)
curVerLabel.TextStyle.Bold = true
form := widget.NewForm(
&widget.FormItem{Text: LoadString("NowVerLabel"), Widget: widget.NewLabelWithData(data.oldPlusVer)},
&widget.FormItem{Text: LoadString("LatestVerLabel"), Widget: versionSelect},
&widget.FormItem{Text: LoadString("BranchLabel"), Widget: chromePlusRadio},
)
rich := widget.NewRichTextFromMarkdown(LoadString("MarkdownMsg"))
rich.Wrapping = fyne.TextWrapWord
infoCard := widget.NewCard("", "", rich)
plusDownloadProgress = widget.NewProgressBar()
plusDownloadProgress.TextFormatter = func() string {
if plusDownloadProgress.Max*0.9 == plusDownloadProgress.Value {
return fmt.Sprintf(LoadString("PlusDownloadedMsg"))
} else if plusDownloadProgress.Max == plusDownloadProgress.Value {
return "安装完成"
} else if plusDownloadProgress.Value == -1 {
return "下载失败,请稍后重试"
}
return fmt.Sprintf(LoadString("PlusDownloadingMsg"))
}
data.plusProcessStatus.AddListener(binding.NewDataListener(func() {
if getBool(data.plusProcessStatus) {
plusDownloadProgress.Show()
} else {
plusDownloadProgress.Hide()
}
}))
return container.New(&buttonLayout{}, container.NewVBox(form,
infoCard,
), container.NewVBox(plusDownloadProgress, container.NewGridWithColumns(2, checkBtn, downBtn)))
}
func setPlusVer(data *SettingsData, ver string, releaseMap map[string]GithubRelease) {
plusInfo := releaseMap[ver]
data.curPlusVer.Set(plusInfo.TagName)
data.plusDownloadUrl.Set(pathJoin(getString(data.ghProxy), plusInfo.Assets[0].BrowserDownloadURL))
}
var (
plusDownloadProgress *widget.ProgressBar
)
func installPlus(data *SettingsData, win fyne.Window) {
url := getString(data.plusDownloadUrl)
plusDownloadProgress.SetValue(0)
data.plusProcessStatus.Set(true)
sysInfo := getInfo()
parentPath, _ := data.installPath.Get()
downloadProgress.SetValue(0)
fileName := getFileName(url)
fileName = filepath.Join(parentPath, fileName)
fileSize, _ := getFileSize(url)
var wg = &sync.WaitGroup{}
GoroutineDownload(url, fileName, 4, 5*1024*1024, 30, fileSize, plusDownloadProgress, wg)
UnCompress7zFilter(fileName, parentPath, sysInfo.goarch)
os.Rename(filepath.Join(parentPath, sysInfo.goarch, "App", "version.dll"), path.Join(parentPath, "version.dll"))
os.Rename(filepath.Join(parentPath, sysInfo.goarch, "App", "chrome++.ini"), path.Join(parentPath, "chrome++.ini"))
//clean tmp dir
os.Remove(fileName)
os.RemoveAll(filepath.Join(parentPath, sysInfo.goarch))
plusDownloadProgress.SetValue(1)
defer data.oldPlusVer.Set(getString(data.curPlusVer))
defer data.plusBtnStatus.Set(false)
alertInfo(LoadString("InstalledMsg"), win)
}
func getChromePlusInfo(ghProxy string) (map[string]GithubRelease, []string) {
response, err := http.Get(pathJoin(ghProxy, "https://raw.githubusercontent.com/libsgh/ghapi-json-generator/output/v2/repos/Bush2021/chrome_plus/releases%3Fper_page%3D10/data.json"))
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
data, err := io.ReadAll(response.Body)
var githubReleases []GithubRelease
jsoniter.UnmarshalFromString(string(data), &githubReleases)
if err != nil {
fmt.Println(err)
}
result := make(map[string]GithubRelease)
versionList := make([]string, 0)
for _, item := range githubReleases {
result[item.TagName] = item
versionList = append(versionList, item.TagName)
}
return result, versionList
}