Skip to content

Commit 8ce947b

Browse files
ADD: improove Busy flag
1 parent 33250e2 commit 8ce947b

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

src/rest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# REST-API
1+
# CopyCommander2, REST-API
22

33
This is the CopyCommander2 Rest API Documentation.
44

src/ucopycommander.pas

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292

9393
TWorkThread = Class(TThread)
9494
private
95+
fBusy: integer; // 0 = nichts mehr zu tun, > 0 Wir arbeiten noch irgendwas..
9596
fAJob: TJob; // Der Job der Im Moment Abgearbeitet wird.
9697
fJobProgress: Byte; // Fortschritt in Prozent füf den Aktuellen Job
9798
fStatistic: TTransfereStatistic;
@@ -146,7 +147,7 @@
146147
Procedure AddJob(Const Job: TJob);
147148
Function ExistJob(Const Job: TJob): Boolean;
148149

149-
Function JobsPending(): Boolean; // True, wenn der Worker noch irgendwas zu tun hat...
150+
Function Busy: Boolean; // True, wenn der Worker noch irgendwas zu tun hat...
150151

151152
Procedure CancelAllJobs();
152153

@@ -533,25 +534,29 @@
533534
BufferSize: Integer;
534535
FreeDiskSpace: Int64;
535536
Begin
537+
inc(fBusy);
536538
fCopyFileDetailError := 'Unknown error';
537539
Result := False;
538540
If FCancelallJobs Or fCancelActualJob Or (source = dest) Then Begin
539541
fCopyFileDetailError := '';
540542
result := true;
543+
dec(fBusy);
541544
exit;
542545
End;
543546
// Sicherstellen das dest nicht existiert, Die Fälle wo es existieren darf und
544547
// Nicht "überschrieben" werden soll klärt der Aufrufer!.
545548
If FileExistsUTF8(dest) Then Begin
546549
If Not DeleteFileUTF8(dest) Then Begin
547550
fCopyFileDetailError := 'Could not delete dest file';
551+
dec(fBusy);
548552
exit;
549553
End;
550554
End;
551555
Try
552556
SourceFile := FileOpen(utf8tosys(source), fmOpenRead);
553557
If SourceFile = 0 Then Begin
554558
fCopyFileDetailError := 'Unable to read source file';
559+
dec(fBusy);
555560
exit;
556561
End;
557562
(*
@@ -564,12 +569,14 @@
564569
FreeDiskSpace := GetFreeDiskSpaceOf(ExtractFilePath(dest));
565570
If FreeDiskSpace <= RemainingFileSize Then Begin
566571
fCopyFileDetailError := 'Not enough diskspace at destination available';
572+
dec(fBusy);
567573
exit;
568574
End;
569575
DestFile := FileCreate(utf8tosys(dest));
570576
If DestFile = 0 Then Begin
571577
fCopyFileDetailError := 'Unable to create destination file';
572578
FileClose(SourceFile);
579+
dec(fBusy);
573580
exit;
574581
End;
575582
FileSeek(SourceFile, 0, fsFromBeginning);
@@ -580,6 +587,7 @@
580587
FileClose(SourceFile);
581588
fCopyFileDetailError := '';
582589
result := true; // Das Stimmt zwar nicht, erzeugt aber im Abgang die wenigsten Fehler, Störungen
590+
dec(fBusy);
583591
exit;
584592
End;
585593
If JobPause Then Begin
@@ -593,13 +601,15 @@
593601
FileClose(SourceFile);
594602
FileClose(DestFile);
595603
fCopyFileDetailError := 'Sourcefile read error';
604+
dec(fBusy);
596605
exit;
597606
End;
598607
If FileWrite(DestFile, buffer, BufferSize) = -1 Then Begin
599608
// Irgendwas hat nen Fehler ausgelöst
600609
FileClose(SourceFile);
601610
FileClose(DestFile);
602611
fCopyFileDetailError := 'Destfile write error';
612+
dec(fBusy);
603613
exit;
604614
End;
605615
// Und noch die Statistik Nach ziehen
@@ -625,10 +635,12 @@
625635
// KA was ich da noch Vergessen habe
626636
On AV: Exception Do Begin
627637
fCopyFileDetailError := av.Message;
638+
dec(fBusy);
628639
exit;
629640
End;
630641
End;
631642
fCopyFileDetailError := '';
643+
dec(fBusy);
632644
Result := True;
633645
End;
634646

@@ -666,6 +678,7 @@
666678

667679
Procedure TWorkThread.Init;
668680
Begin
681+
fBusy := 0;
669682
fLCLSubJobArray := Nil;
670683
fAllResult := jaNotChoosen;
671684
FCancelallJobs := false;
@@ -831,12 +844,14 @@
831844
Begin
832845
result := false;
833846
If Not assigned(fAJob) Then exit;
847+
inc(fBusy);
834848
If FCancelallJobs Or fCancelActualJob Then Begin
835849
// Beim Abcanceln der Jobs rufen wir den FinishJob dennoch auf, damit die LCL die Chance hat diesen aus ihren Listen zu streichen..
836850
If Assigned(OnFinishJob) Then Begin
837851
Synchronize(@LCLOnFinishJob);
838852
End;
839853
result := true;
854+
dec(fbusy);
840855
exit;
841856
End;
842857
If Assigned(OnStartJob) Then Begin
@@ -848,11 +863,13 @@
848863
jtMoveFile, jtCopyFile: Begin
849864
If Not FileExistsUTF8(fAJob.Source) Then Begin
850865
AddToErrorLog('Source file does not exist anymore.');
866+
dec(fBusy);
851867
exit;
852868
End;
853869
// Ziel Verzeichnis erstellen
854870
If Not ForceDirectoriesUTF8(ExtractFileDir(fAJob.Dest)) Then Begin
855871
AddToErrorLog('Unable to create destination folder.');
872+
dec(fBusy);
856873
exit;
857874
End;
858875
If FileExistsUTF8(fAJob.Dest) Then Begin
@@ -864,6 +881,7 @@
864881
Synchronize(@LCLOnFinishJob);
865882
End;
866883
result := true;
884+
dec(fBusy);
867885
exit;
868886
End
869887
Else Begin
@@ -878,6 +896,7 @@
878896
Synchronize(@LCLOnFinishJob);
879897
End;
880898
FQuestionJobFifo.Push(fAJob);
899+
dec(fBusy);
881900
exit;
882901
End;
883902
jaSkip: Begin
@@ -886,11 +905,13 @@
886905
Synchronize(@LCLOnFinishJob);
887906
End;
888907
result := true;
908+
dec(fBusy);
889909
exit;
890910
End;
891911
jaReplace: Begin
892912
If Not DeleteFileUTF8(fAJob.Dest) Then Begin
893913
AddToErrorLog('Unable to delete destination file.');
914+
dec(fBusy);
894915
exit;
895916
End;
896917
End;
@@ -899,24 +920,28 @@
899920
End;
900921
If Not FileCopy(fAJob.Source, fAJob.Dest) Then Begin
901922
AddToErrorLog('Unable to copy file to destination folder: ' + fCopyFileDetailError);
923+
dec(fBusy);
902924
exit;
903925
End;
904926
If fAJob.JobType = jtMoveFile Then Begin
905927
If Not DeleteFileUTF8(fAJob.Source) Then Begin
906928
AddToErrorLog('Unable to delete file.');
929+
dec(fBusy);
907930
exit;
908931
End;
909932
End;
910933
End;
911934
jtMoveDir, jtCopyDir: Begin
912935
If Not DirectoryExistsUTF8(fAJob.Source) Then Begin
913936
AddToErrorLog('Source directory does not exist anymore.');
937+
dec(fBusy);
914938
exit;
915939
End;
916940
// Ziel Ordner Erstellen
917941
s := extractfilename(ExcludeTrailingPathDelimiter(fAJob.Source));
918942
If Not ForceDirectoriesUTF8(IncludeTrailingPathDelimiter(fAJob.Dest) + s) Then Begin
919943
AddToErrorLog('Unable to create dest dir.');
944+
dec(fBusy);
920945
exit;
921946
End;
922947
// Rekursiv Verschieben / Kopieren
@@ -949,6 +974,7 @@
949974
jtDelFile: Begin
950975
If Not DeleteFileUTF8(fAJob.Source) Then Begin
951976
AddToErrorLog('Unable to delete file.');
977+
dec(fBusy);
952978
exit;
953979
End;
954980
End;
@@ -967,6 +993,7 @@
967993
jtDelDir: Begin
968994
If Not DelFolder(fAJob.Source) Then Begin
969995
AddToErrorLog('Unable to delete folder.');
996+
dec(fBusy);
970997
exit;
971998
End;
972999
End;
@@ -975,6 +1002,7 @@
9751002
Synchronize(@LCLOnFinishJob);
9761003
End;
9771004
result := true;
1005+
dec(fbusy);
9781006
End;
9791007

9801008
Procedure TWorkThread.CheckForOnFileTransfereStatistic;
@@ -1149,9 +1177,9 @@
11491177
result := FInJobFifo.ContainsElement(job, @CompareJobs);
11501178
End;
11511179

1152-
Function TWorkThread.JobsPending: Boolean;
1180+
Function TWorkThread.Busy: Boolean;
11531181
Begin
1154-
result := getPendingJobs() <> 0;
1182+
result := (getPendingJobs() <> 0) Or (fBusy > 0);
11551183
End;
11561184

11571185
Procedure TWorkThread.CancelAllJobs;

src/unit1.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -646,10 +646,10 @@
646646
FreeRestAPI;
647647
Workthread_reference := fWorkThread;
648648
fWorkThread := Nil; // Das sorgt dafür, dass der On Idle Handler nichts mehr macht ;)
649-
If Workthread_reference.JobsPending Then Begin
649+
If Workthread_reference.Busy Then Begin
650650
Workthread_reference.OnFinishJob := Nil; // Der User Braucht auch nicht mehr sehen dass wir die Löschen
651651
Workthread_reference.CancelAllJobs();
652-
While Workthread_reference.JobsPending Do Begin
652+
While Workthread_reference.Busy Do Begin
653653
sleep(1);
654654
End;
655655
End;
@@ -679,7 +679,7 @@
679679

680680
Procedure TForm1.FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
681681
Begin
682-
If fWorkThread.JobsPending Then Begin
682+
If fWorkThread.Busy Then Begin
683683
If ID_NO = Application.MessageBox(pchar('File / Dir copying not yet finished.' + LineEnding + 'Are you shure you want to close, this will cancel all your jobs.'), 'Warning', MB_YESNO Or MB_ICONWARNING) Then Begin
684684
CanClose := false;
685685
End;

src/urestapi.pas

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,12 @@
136136
Var
137137
jn: TJSONNode;
138138
iqc: integer;
139+
busy: Boolean;
139140
Begin
140141
iqc := form1.fWorkThread.PendingJobs;
142+
busy := form1.fWorkThread.Busy Or form1.fWorkThread.HasQuestions;
141143
jn := TJSONNode.Create;
142-
jn.AddObj(TJSONValue.Create('State', specialize ifthen < String > (iqc = 0, '0', '1'), false));
144+
jn.AddObj(TJSONValue.Create('State', specialize ifthen < String > (Busy, '0', '1'), false));
143145
jn.AddObj(TJSONValue.Create('JobQueueCount', inttostr(iqc), false));
144146
jn.AddObj(TJSONValue.Create('LeftDir', form1.fLeftView.aDirectory, true));
145147
jn.AddObj(TJSONValue.Create('RightDir', form1.fRightView.aDirectory, true));

0 commit comments

Comments
 (0)