forked from ying32/govcl
/
MainFormImpl.go
136 lines (116 loc) · 3.26 KB
/
MainFormImpl.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
package main
import (
"fmt"
"strings"
"time"
"unsafe"
"github.com/ying32/govcl/vcl"
"github.com/ying32/govcl/vcl/rtl"
"github.com/ying32/xldl"
)
//::private::
type TMainFormFields struct {
dloader *xldl.XLDownloader
}
func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
f.dloader = xldl.NewXLDownloader(rtl.ExtractFilePath(vcl.Application.ExeName()))
f.EditdlURL.SetText("https://qd.myapp.com/myapp/qqteam/pcqq/PCQQ2020.exe")
}
func (f *TMainForm) OnFormDestroy(sender vcl.IObject) {
}
func (f *TMainForm) OnBtnAddClick(sender vcl.IObject) {
if !f.indexOfItem(MainForm.EditdlURL.Text()) {
dlURL := MainForm.EditdlURL.Text()
item := MainForm.LVDlList.Items().Add()
item.SetCaption(fmt.Sprintf("%d", MainForm.LVDlList.Items().Count()))
item.SubItems().Add(dlURL)
item.SubItems().Add("0M")
item.SubItems().Add("0%")
item.SubItems().Add("0KB/s")
item.SubItems().Add("下载中")
f.startTask(dlURL, item)
}
}
func (f *TMainForm) OnBtnStartAllClick(sender vcl.IObject) {
for _, v := range f.dloader.Tasks {
v.Start()
}
}
func (f *TMainForm) OnBtnStopAllClick(sender vcl.IObject) {
for _, v := range f.dloader.Tasks {
v.Stop()
}
}
func (f *TMainForm) OnActStartExecute(sender vcl.IObject) {
item := f.LVDlList.Selected()
if item != nil && item.IsValid() && item.Data() != nil {
(*xldl.XLTask)(unsafe.Pointer(item.Data())).Start()
}
}
func (f *TMainForm) OnActStopExecute(sender vcl.IObject) {
item := f.LVDlList.Selected()
if item != nil && item.IsValid() && item.Data() != nil {
(*xldl.XLTask)(unsafe.Pointer(item.Data())).Stop()
}
}
func (f *TMainForm) OnApplicationException(sender vcl.IObject, e *vcl.Exception) {
}
func (f *TMainForm) indexOfItem(s string) bool {
var i int32
for i = 0; i < f.LVDlList.Items().Count(); i++ {
if f.LVDlList.Items().Item(i).SubItems().Strings(0) == s {
return true
}
}
return false
}
func (f *TMainForm) getURLFileName(s string) string {
i := strings.LastIndex(s, "/")
if i > 0 {
return s[i+1:]
}
return ""
}
func (f *TMainForm) startTask(dlURL string, item *vcl.TListItem) {
task := f.dloader.AddTask(dlURL, f.getURLFileName(dlURL))
if task.Start() {
item.SetData(unsafe.Pointer(task))
go func() {
for {
info, ret := task.Info()
if ret {
vcl.ThreadSync(func() {
item.SubItems().SetStrings(1, fmt.Sprintf("%.2fM", float64(info.TotalSize)/1024.0/1024.0))
item.SubItems().SetStrings(2, fmt.Sprintf("%.2f%%", info.Percent*100))
item.SubItems().SetStrings(3, fmt.Sprintf("%dKB/s", info.Speed/1024))
switch info.Stat {
case xldl.TSC_ERROR:
item.SubItems().SetStrings(4, "错误")
break
case xldl.TSC_PAUSE:
item.SubItems().SetStrings(4, "暂停")
case xldl.TSC_DOWNLOAD:
item.SubItems().SetStrings(4, "下载中")
case xldl.TSC_COMPLETE:
item.SubItems().SetStrings(4, "下载完成")
break
case xldl.TSC_STARTPENDING:
case xldl.TSC_STOPPENDING:
}
})
}
time.Sleep(time.Millisecond * 500)
}
}()
}
}
func (f *TMainForm) OnActDeleteExecute(sender vcl.IObject) {
item := f.LVDlList.Selected()
if item != nil && item.IsValid() && item.Data() != nil {
task := (*xldl.XLTask)(unsafe.Pointer(item.Data()))
task.Stop()
task.Delete()
task.DeleteTempFile()
item.Delete()
}
}