Skip to content

Commit bb51a8c

Browse files
committed
add support of stop button for progress bar, run all test cases again on Mac
1 parent 6225359 commit bb51a8c

14 files changed

Lines changed: 359 additions & 85 deletions

Documentation/Classes/FileTransfer_Dropbox.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,21 @@ End if
333333
```
334334

335335

336+
To support the stop button in the progress bar, Storage needs to be used to share progress bar and worker IDs. See example in test_curl - download.
337+
```4D
338+
// enable stop button in progress bar
339+
Use (Storage.FileTransfer_Progress)
340+
Storage.FileTransfer_Progress[$progressid]:=New shared object()
341+
End use
342+
$ftp.useCallback(Formula(ProgressCallback); $progressid)
343+
```
344+
345+
After execution of download/upload/etc, check:
346+
```4D
347+
If (Bool(Storage.FileTransfer_Progress[$progressid].Stop)) // check stop button if it was set, remove from storage
348+
// user canceled!!
349+
```
350+
351+
Don't forget to remove the object from storage when done.
352+
See test_dropbox - download for a full example
336353

Documentation/Classes/FileTransfer_GDrive.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,4 +453,21 @@ End if
453453
```
454454

455455

456+
To support the stop button in the progress bar, Storage needs to be used to share progress bar and worker IDs. See example in test_curl - download.
457+
```4D
458+
// enable stop button in progress bar
459+
Use (Storage.FileTransfer_Progress)
460+
Storage.FileTransfer_Progress[$progressid]:=New shared object()
461+
End use
462+
$ftp.useCallback(Formula(ProgressCallback); $progressid)
463+
```
464+
465+
After execution of download/upload/etc, check:
466+
```4D
467+
If (Bool(Storage.FileTransfer_Progress[$progressid].Stop)) // check stop button if it was set, remove from storage
468+
// user canceled!!
469+
```
470+
471+
Don't forget to remove the object from storage when done.
472+
See test_gdrive - download for a full example
456473

Documentation/Classes/FileTransfer_curl.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ If ($result.success)
1616
...
1717
End if
1818
```
19+
20+
For more examples see the method "test_curl".
21+
1922
## Summary
2023
| |
2124
|-|
@@ -457,7 +460,7 @@ The command returns after given wait time or before if execution is finished.
457460
|Parameter|Type||Description|
458461
|---------|--- |:---:|------|
459462
|callback|4D.Function|->|4d function to call during progress|
460-
|ID|Text|->|text to pass to callback method to identify job|
463+
|ID|Text|->|unique text to pass to callback method to identify job|
461464

462465
#### Description
463466
Allows to show a progress bar during long running operations or to get informed when command execution is complete.
@@ -500,5 +503,20 @@ Else
500503
End if
501504
```
502505

506+
To support the stop button in the progress bar, Storage needs to be used to share progress bar and worker IDs. See example in test_curl - download.
507+
```4D
508+
// enable stop button in progress bar
509+
Use (Storage.FileTransfer_Progress)
510+
Storage.FileTransfer_Progress[$progressid]:=New shared object()
511+
End use
512+
$ftp.useCallback(Formula(ProgressCallback); $progressid)
513+
```
503514

515+
After execution of download/upload/etc, check:
516+
```4D
517+
If (Bool(Storage.FileTransfer_Progress[$progressid].Stop)) // check stop button if it was set, remove from storage
518+
// user canceled!!
519+
```
504520

521+
Don't forget to remove the object from storage when done.
522+
See test_curl - download for a full example

Project/Sources/Classes/FileTransfer_curl.4dm

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ Function _runWorker($para : Text)->$result : Object
300300
$path:="curl"
301301
End if
302302

303+
$path+=" -f" // failure report
304+
303305
If ((This:C1470._noProgress#Null:C1517) && (This:C1470._noProgress))
304306
$path+=" --no-progress-meter"
305307
End if
@@ -322,6 +324,7 @@ Function _runWorker($para : Text)->$result : Object
322324
$command:=$path+" "+$para
323325
$old:=Method called on error:C704
324326
ON ERR CALL:C155(Formula:C1597(ErrorHandler).source)
327+
$workerpara.variables:=New object:C1471("userCancel"; "false")
325328
This:C1470._worker:=4D:C1709.SystemWorker.new($command; $workerpara)
326329
$worker:=This:C1470._worker
327330

@@ -330,20 +333,25 @@ Function _runWorker($para : Text)->$result : Object
330333
$result:=New object:C1471("data"; "async"; "success"; True:C214)
331334
Else
332335
$worker.wait()
333-
If (($worker.responseError#Null:C1517) && ($worker.responseError#""))
334-
$result:=New object:C1471("responseError"; $worker.responseError; "success"; False:C215)
335-
$pos:=Position:C15("curl: "; $worker.responseError; *)
336-
If ($pos>0)
337-
$result.error:=Replace string:C233(Substring:C12($worker.responseError; $pos+6); Char:C90(10); "")
338-
Else
339-
If (($worker.response#Null:C1517) && ($worker.response#"")) // seems not to be an error, sometime curl set's process bar in error and result in response.
340-
$result:=New object:C1471("data"; $worker.response; "success"; True:C214)
336+
If (Bool:C1537($worker.userCancel))
337+
$result:=New object:C1471("responseError"; "Cancel by user"; "success"; False:C215)
338+
Else
339+
If (($worker.responseError#Null:C1517) && ($worker.responseError#""))
340+
$result:=New object:C1471("responseError"; $worker.responseError; "success"; False:C215)
341+
$pos:=Position:C15("curl: "; $worker.responseError; *)
342+
If ($pos>0)
343+
$result.error:=Replace string:C233(Substring:C12($worker.responseError; $pos+6); Char:C90(10); "")
341344
Else
342-
$result:=New object:C1471("data"; $worker.responseError; "success"; True:C214)
345+
// seems not to be an error, curl set's process bar in error and no result in response.
346+
If ($worker.response#"")
347+
$result:=New object:C1471("data"; $worker.response; "success"; True:C214)
348+
Else
349+
$result:=New object:C1471("data"; $worker.responseError; "success"; True:C214)
350+
End if
343351
End if
352+
Else
353+
$result:=New object:C1471("data"; $worker.response; "success"; True:C214)
344354
End if
345-
Else
346-
$result:=New object:C1471("data"; $worker.response; "success"; True:C214)
347355
End if
348356
End if
349357
Else

Project/Sources/Classes/SystemWorkerProperties.4dm

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Class constructor($type : Text; $data : Object; $callback : 4D:C1709.Function; $
77
If (Count parameters:C259>2)
88
This:C1470.callback:=$callback
99
This:C1470.callbackID:=$callbackID
10+
This:C1470.stopbutton:=$stopButton
1011
ASSERT:C1129(Value type:C1509($callback)=Is object:K8:27; "Callback must be of type function")
1112
ASSERT:C1129(OB Instance of:C1731($callback; 4D:C1709.Function); "Callback must be of type function")
1213
ASSERT:C1129($callbackID#""; "Callback ID Method must not be empty")
@@ -23,6 +24,14 @@ Class constructor($type : Text; $data : Object; $callback : 4D:C1709.Function; $
2324
Function onDataError($systemworker : Object; $data : Object)
2425
// called when data is received from curl or dropbox to handle progress bar
2526

27+
// check for stop button in progress bar
28+
If ((This:C1470.callbackID#"") && (This:C1470.callback#Null:C1517))
29+
If (Bool:C1537(Storage:C1525.FileTransfer_Progress[This:C1470.callbackID].Stop))
30+
$systemworker.terminate()
31+
return
32+
End if
33+
End if
34+
2635
If (String:C10($data.data)#"")
2736
This:C1470.data.text+=$data.data
2837
//This._createFile("onDataError"; This.data.text) // debug
@@ -61,5 +70,5 @@ Function onTerminate($systemworker : Object; $data : Object)
6170

6271

6372
Function _createFile($title : Text; $textBody : Text)
64-
// debug
73+
// debug only
6574
TEXT TO DOCUMENT:C1237(Get 4D folder:C485(Current resources folder:K5:16)+$title+".txt"; $textBody)

Project/Sources/Methods/ProgressCallback.4dm

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,57 @@
44

55
// $ID is set through code - $message comes from curl
66

7-
var ProgressBarID : Integer
7+
C_COLLECTION:C1488(FileTransfer_ProgressBarCol)
8+
If (FileTransfer_ProgressBarCol=Null:C1517)
9+
FileTransfer_ProgressBarCol:=New collection:C1472
10+
End if
11+
12+
$currentProgress:=FileTransfer_ProgressBarCol.query("ID=:1"; $ID)
13+
If ($currentProgress.length#0)
14+
$ProgressBarID:=$currentProgress[0].progressID
15+
Else
16+
$ProgressBarID:=0
17+
End if
818

9-
If ((ProgressBarID=0) && ($value#100))
10-
ProgressBarID:=Progress New
11-
Progress SET TITLE(ProgressBarID; $ID)
19+
If (($ProgressBarID=0) && ($value#100))
20+
$ProgressBarID:=Progress New
21+
Progress SET TITLE($ProgressBarID; $ID)
22+
FileTransfer_ProgressBarCol.push(New object:C1471("ID"; $ID; "progressID"; $ProgressBarID))
23+
24+
// check if we want stop, if yes, add it to storage
25+
If (Storage:C1525.FileTransfer_Progress[$id]#Null:C1517)
26+
Use (Storage:C1525.FileTransfer_Progress[$id])
27+
Storage:C1525.FileTransfer_Progress[$id].ProgressID:=$ProgressBarID
28+
Storage:C1525.FileTransfer_Progress[$id].Stop:=False:C215
29+
End use
30+
Progress SET BUTTON ENABLED($ProgressBarID; True:C214)
31+
End if
1232
End if
1333

14-
If (ProgressBarID#0)
34+
If ($ProgressBarID#0)
35+
If (Progress Stopped($ProgressBarID)) // only if stop button is enabled
36+
$col:=OB Keys:C1719(Storage:C1525.FileTransfer_Progress)
37+
For each ($key; $col)
38+
If ($key=$ID)
39+
Use (Storage:C1525.FileTransfer_Progress[$key])
40+
Storage:C1525.FileTransfer_Progress[$key].Stop:=True:C214
41+
End use
42+
End if
43+
End for each
44+
End if
1545
Case of
1646
: ($value=100)
17-
Progress QUIT(ProgressBarID)
18-
ProgressBarID:=0
47+
Progress QUIT($ProgressBarID)
48+
// aus Col entfernen
49+
$index:=FileTransfer_ProgressBarCol.indexOf($currentProgress[0])
50+
FileTransfer_ProgressBarCol.remove($index)
51+
$ProgressBarID:=0
1952
: ($value<0)
2053
$message2:=Replace string:C233($message; " "; "") // ignore totally empty messages, happens with gdrive
2154
If ($message2#"")
22-
Progress SET MESSAGE(ProgressBarID; $message)
55+
Progress SET MESSAGE($ProgressBarID; $message)
2356
End if
2457
Else
25-
Progress SET PROGRESS(ProgressBarID; $value/100)
58+
Progress SET PROGRESS($ProgressBarID; $value/100)
2659
End case
2760
End if

Project/Sources/Methods/test_GDrive.4dm

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ End if
100100

101101

102102
If (False:C215)
103-
$ftp.useCallback(Formula:C1597(ProgressCallback); "Download 4D.dmg")
103+
$ftp.useCallback(Formula:C1597(ProgressCallback); "upload pdf")
104104
$ftp.setAsyncMode(True:C214)
105105

106106
$source:=System folder:C487(Desktop:K41:16)+"Heap.pdf"
@@ -122,17 +122,38 @@ End if
122122

123123

124124
If (True:C214) // export
125+
$progressid:="export slide show"
126+
If (Storage:C1525.FileTransfer_Progress=Null:C1517)
127+
Use (Storage:C1525)
128+
Storage:C1525.FileTransfer_Progress:=New shared object:C1526
129+
End use
130+
End if
131+
// enable stop button in progress bar
132+
Use (Storage:C1525.FileTransfer_Progress)
133+
Storage:C1525.FileTransfer_Progress[$progressid]:=New shared object:C1526()
134+
End use
135+
136+
$ftp.useCallback(Formula:C1597(ProgressCallback); $progressid)
137+
$ftp.setAsyncMode(False:C215)
125138
$target:=System folder:C487(Desktop:K41:16)+"result.pdf"
126139
$target:=Convert path system to POSIX:C1106($target)
127140
$source:="TAM February 2022"
128141
$result:=$ftp.export($source; $target; ""; "application/vnd.openxmlformats-officedocument.presentationml.presentation")
129-
If ($result.success)
130-
$answer:=$result.data
142+
If (Bool:C1537(Storage:C1525.FileTransfer_Progress[$progressid].Stop)) // check stop button if it was set, remove from storage
143+
// user canceled!!
144+
Else
145+
If ($result.success)
146+
$answer:=$result.data
147+
End if
131148
End if
149+
Use (Storage:C1525.FileTransfer_Progress) // clear storage
150+
OB REMOVE:C1226(Storage:C1525.FileTransfer_Progress; $progressid)
151+
End use
132152
End if
133153

134154

135155

156+
136157
// import
137158
If (False:C215)
138159
$source:=System folder:C487(Desktop:K41:16)+"test studio.txt"

0 commit comments

Comments
 (0)