-
Notifications
You must be signed in to change notification settings - Fork 80
/
file_transfer_notification.go
345 lines (295 loc) · 8.45 KB
/
file_transfer_notification.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package gui
import (
"log"
"github.com/coyim/gotk3adapter/gtki"
)
type fileNotification struct {
area gtki.Box
label gtki.Label
image gtki.Image
name string
progress float64
state string
directory bool
sending bool
receiving bool
afterCancelHook func()
afterFailHook func()
afterSucceedHook func()
}
type fileTransferNotification struct {
area gtki.Box
image gtki.Image
label gtki.Label
box gtki.Box
progressBar gtki.ProgressBar
button gtki.Button
labelButton gtki.Label
totalProgress float64
files []*fileNotification
count int
canceled bool
}
func resizeFileName(name string) string {
var fileName string
if len(name) > 20 {
fileName = name[:21] + "..."
return fileName
}
return name
}
func (file *fileNotification) afterCancel(f func()) {
file.afterCancelHook = f
}
func (file *fileNotification) afterFail(f func()) {
file.afterFailHook = f
}
func (file *fileNotification) afterSucceed(f func()) {
file.afterSucceedHook = f
}
func (file *fileNotification) destroy() {
file.cancel()
}
func (file *fileNotification) update(fileName string, prov gtki.CssProvider) {
updateWithStyle(file.label, prov)
file.label.SetLabel(fileName)
file.image.Hide()
}
func (b *builder) fileTransferNotifInit() *fileTransferNotification {
fileTransferNotif := &fileTransferNotification{}
b.getItems(
"file-transfer", &fileTransferNotif.area,
"image-file-transfer", &fileTransferNotif.image,
"label-file-transfer", &fileTransferNotif.label,
"info-file-transfer", &fileTransferNotif.box,
"bar-file-transfer", &fileTransferNotif.progressBar,
"button-file-transfer", &fileTransferNotif.button,
"button-label-file-transfer", &fileTransferNotif.labelButton,
)
return fileTransferNotif
}
func (conv *conversationPane) newFileTransfer(fileName string, dir, send, receive bool) *fileNotification {
if !conv.fileTransferNotif.area.IsVisible() {
prov := providerWithCSS("box { background-color: #fff9f3; color: #000000; border: 3px; }")
updateWithStyle(conv.fileTransferNotif.area, prov)
conv.fileTransferNotif.progressBar.SetFraction(0.0)
conv.fileTransferNotif.canceled = false
}
info := conv.createFileTransferNotification(fileName, dir, send, receive)
conv.fileTransferNotif.area.SetVisible(true)
countSending := 0
countReceiving := 0
label := "File transfer started"
if dir {
label = "Directory transfer started"
}
// TODO: this can be updated by the inside file transfer function
for _, f := range conv.fileTransferNotif.files {
if f.sending {
countSending++
}
if f.receiving {
countReceiving++
}
}
if countSending > 0 && countReceiving == 0 {
doInUIThread(func() {
conv.updateFileTransferNotification(label, "Cancel", "filetransfer_send.svg")
})
} else if countSending == 0 && countReceiving > 0 {
doInUIThread(func() {
conv.updateFileTransferNotification(label, "Cancel", "filetransfer_receive.svg")
})
} else if countSending > 0 && countReceiving > 0 {
doInUIThread(func() {
conv.updateFileTransferNotification(label, "Cancel", "filetransfer_receive_send.svg")
})
}
return info
}
func (conv *conversationPane) createFileTransferNotification(fileName string, dir, send, receive bool) *fileNotification {
b := newBuilder("FileTransferNotification")
file := &fileNotification{directory: dir, sending: send, receiving: receive, state: stateInProgress}
b.getItems(
"area-file-transfer-info", &file.area,
"name-file-transfer-info", &file.label,
"image-file-transfer-info", &file.image,
)
b.ConnectSignals(map[string]interface{}{
"on_destroy_single_file_transfer": file.destroy,
})
file.name = fileName
if send {
fileName = "Sending: " + fileName
} else {
fileName = "Receiving: " + fileName
}
file.label.SetLabel(fileName)
conv.fileTransferNotif.count++
conv.fileTransferNotif.canceled = false
conv.fileTransferNotif.totalProgress = 0
conv.fileTransferNotif.box.Add(file.area)
file.area.ShowAll()
conv.fileTransferNotif.files = append(conv.fileTransferNotif.files, file)
return file
}
func (conv *conversationPane) updateFileTransferNotification(label, buttonLabel, image string) {
if buttonLabel == "Close" {
prov := providerWithCSS("label { margin-right: 3px; margin-left: 3px; }")
updateWithStyle(conv.fileTransferNotif.labelButton, prov)
}
log.Printf(label)
conv.fileTransferNotif.label.SetLabel(label)
conv.fileTransferNotif.labelButton.SetLabel(buttonLabel)
setImageFromFile(conv.fileTransferNotif.image, image)
}
const stateInProgress = "in-progress"
const stateSuccess = "success"
const stateFailed = "failed"
const stateCanceled = "canceled"
func (conv *conversationPane) updateFileTransfer(file *fileNotification) {
conv.fileTransferNotif.totalProgress = 0
count := 0
haveSuccess := false
for _, f := range conv.fileTransferNotif.files {
switch f.state {
case stateInProgress:
count++
conv.fileTransferNotif.totalProgress += f.progress
case stateSuccess:
haveSuccess = true
}
}
var upd float64
if count == 0 {
if haveSuccess {
upd = 100
} else {
upd = conv.fileTransferNotif.totalProgress
}
} else {
upd = conv.fileTransferNotif.totalProgress / float64(count)
}
doInUIThread(func() {
conv.fileTransferNotif.progressBar.SetFraction(upd)
})
}
func fileTransferCalculateStates(countCompleted, countCanceled, countFailed, countDirs, countTotal int, canceledBefore bool) (label, image string, canceled bool) {
verb := "successful"
image = "success.svg"
canceled = canceledBefore
if countCanceled+countFailed == countTotal {
image = "failure.svg"
canceled = true
verb = "failed"
if countCanceled > countFailed {
verb = "canceled"
}
}
label = "File transfer " + verb
if countDirs > 0 {
label = "Directory transfer " + verb
}
return
}
func (conv *conversationPane) updateFileTransferNotificationCounts() {
countCompleted := 0
countCanceled := 0
countFailed := 0
countTotal := 0
countDirs := 0
for _, f := range conv.fileTransferNotif.files {
switch f.state {
case stateInProgress:
case stateSuccess:
countCompleted++
case stateCanceled:
countCompleted++
countCanceled++
case stateFailed:
countCompleted++
countFailed++
}
if f.directory {
countDirs++
}
countTotal++
}
conv.fileTransferNotif.count = countTotal - countCompleted
if countCompleted == countTotal {
label, image, c := fileTransferCalculateStates(countCompleted, countCanceled, countFailed, countDirs, countTotal, conv.fileTransferNotif.canceled)
conv.fileTransferNotif.canceled = c
doInUIThread(func() {
conv.updateFileTransferNotification(label, "Close", image)
})
}
}
func (conv *conversationPane) isFileTransferNotifCanceled() bool {
return conv.fileTransferNotif.canceled
}
func canceledProvider() gtki.CssProvider {
return providerWithCSS("label { color: #cc3636; }")
}
func successProvider() gtki.CssProvider {
return providerWithCSS("label { color: #89AF8F; }")
}
func (file *fileNotification) cancel() {
if file.state != stateInProgress {
return
}
file.state = stateCanceled
file.progress = 0
file.update("Canceled: "+file.name, canceledProvider())
hook := file.afterCancelHook
file.afterCancelHook = nil
if hook != nil {
hook()
}
}
func (file *fileNotification) fail() {
if file.state != stateInProgress {
return
}
file.state = stateFailed
file.progress = 0
file.update("Failed: "+file.name, canceledProvider())
hook := file.afterFailHook
file.afterFailHook = nil
if hook != nil {
hook()
}
}
// TODO: this can be replaced with the same bool
func (file *fileNotification) succeed(purpose string) {
if file.state != stateInProgress {
return
}
file.state = stateSuccess
if purpose == "send" {
file.update("Sent: "+file.name, successProvider())
} else {
file.update("Received: "+file.name, successProvider())
}
hook := file.afterSucceedHook
file.afterSucceedHook = nil
if hook != nil {
hook()
}
}
func (conv *conversationPane) onDestroyFileTransferNotif() {
label := conv.fileTransferNotif.labelButton.GetLabel()
if label == "Cancel" {
for _, f := range conv.fileTransferNotif.files {
f.cancel()
}
} else {
conv.fileTransferNotif.canceled = false
conv.fileTransferNotif.area.SetVisible(false)
conv.fileTransferNotif.progressBar.SetFraction(0.0)
for i := range conv.fileTransferNotif.files {
conv.fileTransferNotif.files[i].area.Destroy()
}
conv.fileTransferNotif.files = conv.fileTransferNotif.files[:0]
conv.fileTransferNotif.count = 0
}
}