-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsenderbridge.go
More file actions
178 lines (154 loc) · 3.73 KB
/
Copy pathsenderbridge.go
File metadata and controls
178 lines (154 loc) · 3.73 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
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
package main
import (
"fmt"
"os"
"strconv"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/widgets"
)
type SenderBridge struct {
core.QObject
_ func() `constructor:"init"`
_ func() `signal:"clickAddFile,auto"`
_ string `property:"code"`
_ *SendFileTableModel `property:"TableModel"`
}
func (l *SenderBridge) init() {
senderBridge = l
l.SetTableModel(senderTableModel)
}
func (b *SenderBridge) clickAddFile() {
jobtotal := new(int64)
jobdone := new(int64)
feedbackstr := new(string)
mycode := new(string) // The real code
code := new(string) // Transport the code from thread to copy into mycode
var tableIndex int = 0
filenames := widgets.QFileDialog_GetOpenFileNames(nil, "some caption", "", "", "", 0)
if len(filenames) < 1 {
return
}
filename := filenames[0]
fmt.Printf("Sending: %s\n", filename)
stat, err := os.Stat(filename)
if err != nil {
showError(fmt.Sprintf("Failed to read %s: %s", filename, err))
}
*jobtotal = stat.Size()
*jobdone = 0
tableIndex = senderTableModel.addNative(
filename,
*mycode,
strconv.FormatInt(*jobtotal, 10),
"0",
"Added")
// Handling results of changes in
// jobdone, feedbackstr, code
t := core.NewQTimer(nil)
t.ConnectEvent(func(e *core.QEvent) bool {
// Handle errors and finish
if len(*feedbackstr) > 0 {
t.DisconnectEvent()
if *feedbackstr == "Done" {
senderTableModel.editIdx(
tableIndex,
filename,
*mycode,
strconv.FormatInt(*jobtotal, 10),
strconv.FormatInt(*jobdone, 10),
"Done")
} else {
senderTableModel.editIdx(
tableIndex,
filename,
*mycode,
strconv.FormatInt(*jobtotal, 10),
strconv.FormatInt(*jobdone, 10),
"Error")
showError(*feedbackstr)
}
return true
}
// Handle start and upload updates
if *jobdone > 0 && *jobdone < *jobtotal {
senderTableModel.editIdx(
tableIndex,
filename,
*mycode,
strconv.FormatInt(*jobtotal, 10),
strconv.FormatInt(*jobdone, 10),
"Uploading") //
}
// Handle successful registering/adding of file
// Once!
if len(*code) > 0 {
*mycode = *code
senderTableModel.editIdx(
tableIndex,
filename,
*mycode,
strconv.FormatInt(*jobtotal, 10),
strconv.FormatInt(*jobdone, 10),
"Added")
*code = ""
}
return true
})
t.Start(200) // Every x ms
// Thread doing the actual file sending
// It communicates with parent via variables
// jobdone, feedbackstr, code
go func() {
/*
defer func() {
if err := recover(); err != nil {
*errstr = fmt.Sprintf("%v", err)
}
}()
*/
// Start downloading it in the background
if stat.IsDir() {
code2, status, err := sendDir(filename, jobdone, feedbackstr)
if err != nil {
*feedbackstr = fmt.Sprintf("Error sending message: %s", err)
return
}
// Cant update TableModel here in this go func()
// Return it to the QTimer
// before waiting for upload result
*code = code2
// Wait till its finished
s := <-status
if s.OK {
fmt.Println("file sent")
*feedbackstr = "Done"
} else {
*feedbackstr = fmt.Sprintf("Send error: %s", s.Error)
}
} else {
code2, status, err := sendFile(filename, jobdone, feedbackstr)
if err != nil {
*feedbackstr = fmt.Sprintf("Error sending message: %s", err)
return
}
// Cant update TableModel here in this go func()
// Return it to the QTimer
// before waiting for upload result
*code = code2
// Wait till its finished
s := <-status
if s.OK {
fmt.Println("file sent")
*feedbackstr = "Done"
} else {
*feedbackstr = fmt.Sprintf("Send error: %s", s.Error)
}
}
//*feedbackstr = "ok"
}()
/*senderTableModel.edit(
filename,
strconv.FormatInt(stat.Size(), 10),
strconv.FormatInt(stat.Size(), 10),
"Done")*/
}