forked from creamlike1024/EasyLPAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.go
244 lines (226 loc) · 5.58 KB
/
control.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"encoding/json"
"fmt"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"math"
"os/exec"
"runtime"
"sort"
"strings"
)
const StatusProcess = 1
const StatusReady = 0
const Unselected = -1
var SelectedProfile = Unselected
var SelectedNotification = Unselected
var RefreshNeeded = true
var ProfileMaskNeeded bool
var NotificationMaskNeeded bool
var ProfileStateAllowDisable bool
var StatusChan = make(chan int)
var LockButtonChan = make(chan bool)
func RefreshProfile() error {
var err error
Profiles, err = LpacProfileList()
if err != nil {
return err
}
// 刷新 List
ProfileList.Refresh()
ProfileList.UnselectAll()
SwitchStateButton.SetText("Enable")
SwitchStateButton.SetIcon(theme.ConfirmIcon())
return nil
}
func RefreshNotification() error {
var err error
Notifications, err = LpacNotificationList()
if err != nil {
return err
}
sort.Slice(Notifications, func(i, j int) bool {
return Notifications[i].SeqNumber < Notifications[j].SeqNumber
})
// 刷新 List
NotificationList.Refresh()
NotificationList.UnselectAll()
return nil
}
func RefreshChipInfo() error {
var err error
ChipInfo, err = LpacChipInfo()
if err != nil {
return err
}
if ChipInfo == nil {
return nil
}
convertToString := func(value interface{}) string {
if value == nil {
return "<not set>"
}
if str, ok := value.(string); ok {
return str
}
return "<not set>"
}
EidLabel.SetText(fmt.Sprintf("EID: %s", ChipInfo.EidValue))
DefaultDpAddressLabel.SetText(fmt.Sprintf("Default SM-DP+ Address: %s", convertToString(ChipInfo.EuiccConfiguredAddresses.DefaultDpAddress)))
RootDsAddressLabel.SetText(fmt.Sprintf("Root SM-DS Address: %s", convertToString(ChipInfo.EuiccConfiguredAddresses.RootDsAddress)))
// eUICC Manufacturer Label
if eum := GetEUM(ChipInfo.EidValue); eum != nil {
manufacturer := fmt.Sprint(eum.Manufacturer, " ", CountryCodeToEmoji(eum.Country))
if productName := eum.ProductName(ChipInfo.EidValue); productName != "" {
manufacturer = fmt.Sprint(productName, " (", manufacturer, ")")
}
EUICCManufacturerLabel.SetText("Manufacturer: " + manufacturer)
} else {
EUICCManufacturerLabel.SetText("Manufacturer: Unknown")
}
// EUICCInfo2 entry
bytes, err := json.MarshalIndent(ChipInfo.EUICCInfo2, "", " ")
if err != nil {
ShowLpacErrDialog(fmt.Errorf("chip Info: failed to decode EUICCInfo2\n%s", err))
}
EuiccInfo2Entry.SetText(string(bytes))
// 计算剩余空间
freeSpace := float64(ChipInfo.EUICCInfo2.ExtCardResource.FreeNonVolatileMemory) / 1024
FreeSpaceLabel.SetText(fmt.Sprintf("Free space: %.2f KB", math.Round(freeSpace*100)/100))
CopyEidButton.Show()
SetDefaultSmdpButton.Show()
EuiccInfo2Entry.Show()
ViewCertInfoButton.Show()
EUICCManufacturerLabel.Show()
CopyEuiccInfo2Button.Show()
return nil
}
func RefreshApduDriver() {
var err error
ApduDrivers, err = LpacDriverApduList()
if err != nil {
ShowLpacErrDialog(err)
}
var options []string
for _, d := range ApduDrivers {
// exclude YubiKey and CanoKey
if strings.Contains(d.Name, "canokeys.org") || strings.Contains(d.Name, "YubiKey") {
continue
}
// Workaround: lpac shows an empty driver when no card reader inserted under macOS
if d.Name == "" {
continue
}
options = append(options, d.Name)
}
ApduDriverSelect.SetOptions(options)
ApduDriverSelect.ClearSelected()
ConfigInstance.DriverIFID = ""
ApduDriverSelect.Refresh()
}
func OpenLog() {
if err := OpenProgram(ConfigInstance.LogDir); err != nil {
d := dialog.NewError(err, WMain)
d.Show()
}
}
func OpenProgram(name string) error {
var launcher string
switch runtime.GOOS {
case "windows":
launcher = "explorer"
case "darwin":
launcher = "open"
case "linux":
launcher = "xdg-open"
}
if launcher == "" {
return fmt.Errorf("unsupported platform, failed to open")
}
return exec.Command(launcher, name).Start()
}
func Refresh() {
if ConfigInstance.DriverIFID == "" {
ShowSelectCardReaderDialog()
return
}
err := RefreshProfile()
if err != nil {
ShowLpacErrDialog(err)
return
}
err = RefreshNotification()
if err != nil {
ShowLpacErrDialog(err)
return
}
err = RefreshChipInfo()
if err != nil {
ShowLpacErrDialog(err)
return
}
RefreshNeeded = false
}
func UpdateStatusBarListener() {
for {
status := <-StatusChan
switch status {
case StatusProcess:
StatusLabel.SetText("Processing...")
StatusProcessBar.Start()
StatusProcessBar.Show()
continue
case StatusReady:
StatusLabel.SetText("Ready.")
StatusProcessBar.Stop()
StatusProcessBar.Hide()
continue
}
}
}
func LockButtonListener() {
buttons := []*widget.Button{
RefreshButton, DownloadButton, SetNicknameButton, SwitchStateButton, DeleteProfileButton,
ProcessNotificationButton, ProcessAllNotificationButton, RemoveNotificationButton, BatchRemoveNotificationButton,
SetDefaultSmdpButton, ApduDriverRefreshButton,
}
checks := []*widget.Check{
ProfileMaskCheck, NotificationMaskCheck,
}
for {
lock := <-LockButtonChan
if lock {
for _, button := range buttons {
button.Disable()
}
for _, check := range checks {
check.Disable()
}
ApduDriverSelect.Disable()
} else {
for _, button := range buttons {
button.Enable()
}
for _, check := range checks {
check.Enable()
}
ApduDriverSelect.Enable()
}
}
}
func SetDriverIFID(name string) {
for _, d := range ApduDrivers {
if name == d.Name {
// 未选择过读卡器
if ConfigInstance.DriverIFID == "" {
ConfigInstance.DriverIFID = d.Env
} else {
// 选择过读卡器,要求刷新
ConfigInstance.DriverIFID = d.Env
RefreshNeeded = true
}
}
}
}