forked from alphaonex86/Supercopier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCCopyThread.pas
1348 lines (1130 loc) · 42.4 KB
/
SCCopyThread.pas
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
This file is part of SuperCopier.
SuperCopier is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
SuperCopier is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
}
unit SCCopyThread;
{$MODE Delphi}
interface
uses
Windows,Commctrl,Classes,Messages,SCObjectThreadList,SCWorkThread,SCWorkThreadList,
SCCopier,SCWideUnbufferedCopier,SCCommon,SCConfig,SCBaseList,
SCDirList,{SCSystray,}SCCopyForm,SCDiskSpaceForm,SCCollisionForm,SCCopyErrorForm,
SCBaseListQueue,Forms,ShlObj,ShellApi, ExtCtrls, Dialogs;
type
TCopyThread=class(TWorkThread)
private
Copier:TCopier;
BaseListQueue:TBaseListQueue;
FDefaultDir,SrcDir:WideString;
FIsMove:Boolean;
FIsThreadAlive:Boolean;
// variables pour la copie
LastCopyWindowUpdate:Integer;
LastCopiedSize:Int64;
NumSamples,CurrentSample:Integer;
SpeedSamples:array of integer;
SpeedSamplesFirstPass:Boolean;
CopySpeed:Integer;
ThrottleLastTime:Integer;
ThrottleLastCopiedSize:Int64;
Sync:record
Copy:record
Form:TCopyForm;
Paused,SkipPending,CancelPending:Boolean;
State:TCopyWindowState;
ConfigData:TCopyWindowConfigData;
ConfigDataModifiedByThread:Boolean; // mettre a true si la config doit кtre copiйe de la thread vers la fenкtre
lvErrorListEmpty:Boolean;
FormCaption,
llFromCaption,
llToCaption,
llFileCaption,
llAllCaption,
llSpeedCaption:WideString;
ggFileProgress,ggFileMax,
ggAllProgress,ggAllMax:Int64;
ggAllRemaining,ggFileRemaining:WideString;
Error:record
Time:TDateTime;
Action,Target,ErrorText:WideString;
end;
end;
DiskSpace:record
Form:TDiskSpaceForm;
Volumes:TDiskSpaceWarningVolumeArray;
Action:TDiskSpaceAction;
end;
Collision:record
Form:TCollisionForm;
Action:TCollisionAction;
SameForNext:Boolean;
FileName:WideString;
CustomRename:Boolean;
end;
CopyError:record
Form:TCopyErrorForm;
ErrorText:WideString;
Action:TCopyErrorAction;
SameForNext:Boolean;
end;
Notification:record
TargetForm:TForm;
UseMainIcon:Boolean;
IconType:TBalloonFlags;
Title,Text:WideString;
end;
end;
function CheckWaitingBaseList:Boolean;
procedure HandlePause;
function CopierFileCollision(var NewName:WideString):TCollisionAction;
function CopierDiskSpaceWarning(Volumes:TDiskSpaceWarningVolumeArray):Boolean;
function CopierCopyError(ErrorText:WideString):TCopyErrorAction;
procedure CopierGenericError(Action,Target,ErrorText:WideString);
function CopierCopyProgress:Boolean;
function CopierRecurseProgress(CurrentItem:TDirItem):Boolean;
//Copy
procedure SyncInitCopy;
procedure SyncEndCopy;
procedure SyncUpdateCopy;
procedure SyncSetFileListviewCount;
procedure SyncUpdateFileListview;
procedure SyncAddToErrorLog;
procedure SyncSaveErrorLog;
procedure SyncShowNotificationBalloon;
//DiskSpace
procedure SyncInitDiskSpace;
procedure SyncEndDiskSpace;
procedure SyncCheckDiskSpace;
//Collision
procedure SyncInitCollision;
procedure SyncEndCollision;
procedure SyncCheckCollision;
//CopyError
procedure SyncInitCopyError;
procedure SyncEndCopyError;
procedure SyncCheckCopyError;
protected
function GetDisplayName:WideString;override;
procedure Execute;override;
public
property IsMove:boolean read FIsMove;
property DefaultDir:WideString read FDefaultDir;
constructor Create(PIsMove:Boolean);
destructor Destroy;override;
function CanHandle(pSrcDir,pDestDir:WideString):boolean;
procedure AddBaseList(BaseList:TBaseList;AddMode:TBaselistAddMode=amDefaultDir;Dir:WideString='');
function LockCopier:TCopier;
procedure UnlockCopier;
procedure UpdateCopyWindow;
procedure Cancel;override;
end;
implementation
uses SysUtils,SCMainForm,FileCtrl,SCLocStrings, ComCtrls,
SCFileList, StrUtils,SCWin32,Math;
//******************************************************************************
//******************************************************************************
//******************************************************************************
// TCopyThread: thread de copie de fichiers, gиre la fenиtre de copie,
// les synchros, dйlиgue la copie au Copier et gиre ses йvиnements
//******************************************************************************
//******************************************************************************
//******************************************************************************
//******************************************************************************
// Create
//******************************************************************************
constructor TCopyThread.Create(PIsMove:Boolean);
begin
inherited Create;
FreeOnTerminate:=True;
FThreadType:=wttCopy;
FIsThreadAlive:=True;
// on choisis le copier en fonction de la config et de l'OS
// Copier:=TAnsiBufferedCopier.Create;//destroy the source
Copier:=TWideUnbufferedCopier.Create;
Copier.BufferSize:=Config.Values.CopyBufferSize;
BaseListQueue:=TBaseListQueue.Create;
FDefaultDir:='?';
SrcDir:='?';
FIsMove:=PIsMove;
Copier.CopyAttributes:=(Config.Values.SaveAttributesOnCopy and not IsMove) or
(Config.Values.SaveAttributesOnMove and IsMove);
Copier.CopySecurity:=(Config.Values.SaveSecurityOnCopy and not IsMove) or
(Config.Values.SaveSecurityOnMove and IsMove);
// init calcul vitesse
NumSamples:=0;
if Config.Values.CopyWindowUpdateInterval<>0 then
NumSamples:=Config.Values.CopySpeedAveragingInterval div Config.Values.CopyWindowUpdateInterval;
if NumSamples=0 then NumSamples:=1;
SetLength(SpeedSamples,NumSamples);
// йvиnements du copier
Copier.OnFileCollision:=CopierFileCollision;
Copier.OnDiskSpaceWarning:=CopierDiskSpaceWarning;
Copier.OnCopyError:=CopierCopyError;
Copier.OnGenericError:=CopierGenericError;
Copier.OnCopyProgress:=CopierCopyProgress;
Copier.OnRecurseProgress:=CopierRecurseProgress;
// crйation de la fenкtre
Synchronize(SyncInitCopy);
// tout est initialisй, on peut lancer la thread
Resume;
end;
//******************************************************************************
// Destroy
//******************************************************************************
destructor TCopyThread.Destroy;
begin
if FIsThreadAlive then
begin
// annuler la copie
FreeOnTerminate:=False; // pour йviter que le Cancel fasse que le Destroy soit rappelй
Cancel;
WaitFor;
end;
// sauvegarde automatique du log des erreurs
if Config.Values.ErrorLogAutoSave then Synchronize(SyncSaveErrorLog);
// destruction de la fenкtre
Synchronize(SyncEndCopy);
BaseListQueue.Free;
Copier.Free;
SetLength(SpeedSamples,0);
inherited Destroy;
end;
//******************************************************************************
// GetDisplayName: implйmentation de TWorkThread.GetDisplayName
//******************************************************************************
function TCopyThread.GetDisplayName:WideString;
begin
if FIsMove then
Result:=WideFormat(lsMoveDisplayName,[SrcDir,FDefaultDir])
else
Result:=WideFormat(lsCopyDisplayName,[SrcDir,FDefaultDir]);
end;
//******************************************************************************
// CanHandle: retourne true si le mode de playlist permets a la thread de
// prendre en charge la copie
//******************************************************************************
function TCopyThread.CanHandle(pSrcDir,pDestDir:WideString):boolean;
var SameSource,SameDest:Boolean;
begin
Result:=False;
SameSource:=SamePhysicalDrive(pSrcDir,SrcDir);
SameDest:=SamePhysicalDrive(pDestDir,DefaultDir);
case Config.Values.CopyListHandlingMode of
chmAlways:
Result:=True;
chmSameSource:
Result:=SameSource;
chmSameDestination:
Result:=SameDest;
chmSameSourceAndDestination:
Result:=SameSource and SameDest;
chmSameSourceorDestination:
Result:=SameSource or SameDest;
end;
if Result and Config.Values.CopyListHandlingConfirm then
begin
Result:=MessageBoxW(Sync.Copy.Form.Handle,
PWideChar(lsConfirmCopylistAdd),
PWideChar(DisplayName),
MB_YESNO or MB_ICONQUESTION or MB_APPLMODAL or MB_SETFOREGROUND)=IDYES;
end;
end;
//******************************************************************************
// AddBaseList: ajoute une baselist de fichiers а copier
//******************************************************************************
procedure TCopyThread.AddBaseList(BaseList:TBaseList;AddMode:TBaselistAddMode=amDefaultDir;Dir:WideString='');
var DestDir:WideString;
AddOk:Boolean;
BLQueueItem:TBaseListQueueItem;
begin
AddOk:=True;
case AddMode of
amDefaultDir:
begin
Assert(FDefaultDir<>'?','No DefaultDir');
DestDir:=FDefaultDir;
end;
amSpecifyDest:
begin
Assert(Dir<>'','No DestDir given');
DestDir:=Dir;
end;
amPromptForDest,
amPromptForDestAndSetDefault:
begin
DestDir:=FDefaultDir;
AddOk:=BrowseForFolder(lsChooseDestDir,DestDir,Sync.Copy.Form.Handle);
if (AddMode=amPromptForDestAndSetDefault) and AddOk then FDefaultDir:=DestDir;
end;
end;
if AddOk then
begin
dbgln('AddBaseList DestDir='+DestDir);
// le premier appel a AddBaseList dйterminera le rйpertoire par dйfaut
if FDefaultDir='?' then
begin
FDefaultDir:=IncludeTrailingBackslash(DestDir);
SrcDir:=ExtractFilePath(BaseList[0].SrcName);
end;
// on ajoute la baselist а la queue
BaseListQueue.Lock;
try
BLQueueItem:=TBaseListQueueItem.Create;
BLQueueItem.BaseList:=BaseList;
BLQueueItem.DestDir:=DestDir;
BaseListQueue.Push(BLQueueItem);
finally
BaseListQueue.Unlock;
end;
end
else
begin
// libйrer la baselist
BaseList.Free;
end;
end;
//******************************************************************************
// CheckWaitingBaseList: teste des BaseList sont en attente, si oui elles sont
// traitйes et la fonction renvoie true;
//******************************************************************************
function TCopyThread.CheckWaitingBaseList:Boolean;
var BLQueueItem:TBaseListQueueItem;
begin
Result:=False;
BaseListQueue.Lock;
try
// on boucle tant que la queue n'est pas vidйe
while BaseListQueue.Count>0 do
begin
Result:=True;
BLQueueItem:=BaseListQueue.Pop;
// on ajoute les fichiers au copier
Copier.AddBaseList(BLQueueItem.BaseList,BLQueueItem.DestDir);
// on vйrifie si il y a assez de place
Copier.VerifyFreeSpace;
// on a ajoutй des fichiers, mаj de lvFileList
Synchronize(SyncSetFileListviewCount);
BLQueueItem.Free;
end;
finally
BaseListQueue.Unlock;
end;
end;
//******************************************************************************
// HandlePause:
//******************************************************************************
procedure TCopyThread.HandlePause;
var OldState:TCopyWindowState;
begin
with Sync.Copy do
begin
if Paused then
begin
OldState:=State;
State:=cwsPaused;
while Paused and not CancelPending do
begin
CheckWaitingBaseList;
UpdateCopyWindow;
Sleep(DEFAULT_WAIT);
end;
State:=OldState;
UpdateCopyWindow;
end;
end;
end;
//******************************************************************************
// LockCopier: bloque les donnйes du copier et renvoie sa rйfйrence
//******************************************************************************
function TCopyThread.LockCopier:TCopier;
begin
Copier.FileList.Lock;
Copier.DirList.Lock;
Result:=Copier;
end;
//******************************************************************************
// UnlockCopier: dйbloque les donnйes du copier
//******************************************************************************
procedure TCopyThread.UnlockCopier;
begin
Copier.FileList.Unlock;
Copier.DirList.Unlock;
end;
//******************************************************************************
// Execute: corps de la thread, sers de chef d'orchestre pour le copier
//******************************************************************************
procedure TCopyThread.Execute;
var CopyError,UnfinishedCopy:Boolean;
begin
try
try
// йtat de dйpart
Sync.Copy.State:=cwsWaiting;
repeat
UpdateCopyWindow;
// attendre les donnйes
while (not CheckWaitingBaseList) and (not Sync.Copy.CancelPending) and (Copier.FileList.Count=0) do
begin
UpdateCopyWindow;
Sleep(DEFAULT_WAIT);
end;
// init des veriables servant a calculer la vitesse
LastCopyWindowUpdate:=GetTickCount;
LastCopiedSize:=Copier.CopiedSize;
CurrentSample:=-1;
CopySpeed:=0;
SpeedSamplesFirstPass:=True;
ThrottleLastTime:=GetTickCount;
ThrottleLastCopiedSize:=Copier.CopiedSize;
if Copier.FirstCopy then
begin
dbgln('Copy Start');
// boucle principale
repeat
// gйrer la pause
HandlePause;
// vйrifier si il y a une baselist en attente
CheckWaitingBaseList;
// mаj de lvFileItems
Synchronize(SyncUpdateFileListview);
// mаj de la fenкtre
Sync.Copy.State:=cwsCopying;
UpdateCopyWindow;
if Copier.ManageFileAction(Config.Values.CopyResumeNoAgeVerification) then
begin
// dbgln('Copying: '+Copier.CurrentCopy.FileItem.SrcFullName);
// dbgln(' -> '+Copier.CurrentCopy.FileItem.DestFullName);
Copier.VerifyOrCreateDir;
CopyError:=not Copier.DoCopy;
UnfinishedCopy:=(Copier.CurrentCopy.CopiedSize+Copier.CurrentCopy.SkippedSize)<Copier.CurrentCopy.FileItem.SrcSize;
if not CopyError then
begin
Copier.CopyAttributesAndSecurity;
// dйplacement et le fichier а йtй copiй en entier -> on peut supprimer le source
if FIsMove and not UnfinishedCopy then
Copier.DeleteSrcFile;
end;
// gestion suppression copies non terminйes
if UnfinishedCopy and Config.Values.DeleteUnfinishedCopies and
not (CopyError and Config.Values.DontDeleteOnCopyError) then
begin
Copier.DeleteDestFile;
end;
end;
if Sync.Copy.CancelPending then Copier.CurrentCopy.NextAction:=cpaCancel;
until not Copier.NextCopy;
dbgln('Copy End');
end;
Sync.Copy.State:=cwsCopyEnd;
// tout afficher a 100% si la fenкtre reste ouverte alors que la copie est finie
Sync.Copy.ggFileProgress:=Sync.Copy.ggFileMax;
Sync.Copy.ggAllProgress:=Sync.Copy.ggAllMax;
Synchronize(SyncUpdateFileListview); // lvFileList n'est pas а jour aprиs la copie du dernier item
if (Copier.CurrentCopy.NextAction<>cpaCancel) and (not Sync.Copy.CancelPending) then
begin
// notifier de la fin de la copie
with Sync.Notification do
begin
TargetForm:=nil;
UseMainIcon:=True;
IconType:=TBalloonFlags.bfInfo;
Title:=lsCopyEndNotifyTitle;
Text:=WideFormat(lsCopyEndNotifyText,[DisplayName,Sync.Copy.llSpeedCaption]);
end;
Synchronize(SyncShowNotificationBalloon);
// crйer les reps vide et supprimer les reps source
Copier.CreateEmptyDirs;
if FIsMove then Copier.DeleteSrcDirs;
end;
// on boucle tant que la fenкtre de copie doit rester ouverte
until Sync.Copy.CancelPending or (Copier.CurrentCopy.NextAction=cpaCancel) or
(Sync.Copy.ConfigData.CopyEndAction=cweClose) or
((Sync.Copy.ConfigData.CopyEndAction=cweDontCloseIfErrors) and Sync.Copy.lvErrorListEmpty);
except
// afficher les exception qui n'ont pas йtйes trappйes (et qui sont de toute faзon des bugs)
on E:Exception do SCWin32.MessageBox(Sync.Copy.Form.Handle,E.Message,'SuperCopier - Critical error!',MB_ICONERROR);
end;
finally
// dй-rescencer la thread
WorkThreadList.Remove(Self);
FIsThreadAlive:=False;
end;
end;
//******************************************************************************
//******************************************************************************
// Evenиments du copier
//******************************************************************************
//******************************************************************************
//******************************************************************************
// CopierFileCollision: evenement du copier
//******************************************************************************
function TCopyThread.CopierFileCollision(var NewName:WideString):TCollisionAction;
begin
dbgln('CopierFileCollision');
with Sync.Collision do
begin
if Sync.Copy.ConfigData.CollisionAction=claNone then // aucune action automatique choisie?
begin
FileName:=Copier.CurrentCopy.FileItem.DestName;
Synchronize(SyncInitCollision);
// notification pour le systray
with Sync.Notification do
begin
TargetForm:=Form;
UseMainIcon:=False;
Title:=lsCollisionNotifyTitle;
Text:=WideFormat(lsCollisionNotifyText,[DisplayName,Copier.CurrentCopy.FileItem.DestFullName]);
IconType:=TBalloonFlags.bfWarning;
end;
Synchronize(SyncShowNotificationBalloon);
while (Action=claNone) and (not Sync.Copy.CancelPending) do
begin
Synchronize(SyncCheckCollision);
Sleep(DEFAULT_WAIT);
end;
if Sync.Copy.CancelPending then Action:=claCancel;
Synchronize(SyncEndCollision);
Result:=Action;
if SameForNext then
begin
Sync.Copy.ConfigDataModifiedByThread:=True;
Sync.Copy.ConfigData.CollisionAction:=Action;
end;
end
else
begin
Result:=Sync.Copy.ConfigData.CollisionAction;
end;
// rйcupйrer le nouveau nom pour le fichier si renommage
if Result in [claRenameNew,claRenameOld] then
begin
if CustomRename then
begin
NewName:=FileName;
end
else
begin
// on renomme en fonction du pattern choisi dans la config
if Result=claRenameNew then
NewName:=PatternRename(Copier.CurrentCopy.FileItem.DestName,Copier.CurrentCopy.DirItem.Destpath,Config.Values.RenameNewPattern)
else
NewName:=PatternRename(Copier.CurrentCopy.FileItem.DestName,Copier.CurrentCopy.DirItem.Destpath,Config.Values.RenameOldPattern);
end;
end;
end;
end;
//******************************************************************************
// CopierDiskSpaceWarning: evenement du copier
//******************************************************************************
function TCopyThread.CopierDiskSpaceWarning(Volumes:TDiskSpaceWarningVolumeArray):Boolean;
begin
dbgln('CopierDiskSpaceWarning');
Sync.DiskSpace.Volumes:=Volumes;
Synchronize(SyncInitDiskSpace);
// notification pour le systray
with Sync.Notification do
begin
TargetForm:=Sync.DiskSpace.Form;
UseMainIcon:=False;
Title:=lsDiskSpaceNotifyTitle;
Text:=DisplayName;
IconType:=TBalloonFlags.bfWarning;
end;
Synchronize(SyncShowNotificationBalloon);
while (Sync.DiskSpace.Action=dsaNone) and (not Sync.Copy.CancelPending) do
begin
Synchronize(SyncCheckDiskSpace);
Sleep(DEFAULT_WAIT);
end;
if Sync.Copy.CancelPending then Sync.DiskSpace.Action:=dsaCancel;
Synchronize(SyncEndDiskSpace);
Result:=Sync.DiskSpace.Action=dsaForce;
end;
//******************************************************************************
// CopierCopyError: evenement du copier
//******************************************************************************
function TCopyThread.CopierCopyError(ErrorText:WideString):TCopyErrorAction;
begin
dbgln('CopierCopyError: '+ErrorText);
// ajout de l'erreur а la liste des erreurs
with Sync.Copy do
begin
Error.Time:=Now;
Error.Action:=lsCopyAction;
Error.Target:=Copier.CurrentCopy.FileItem.SrcFullName;
Error.ErrorText:=ErrorText;
end;
Synchronize(SyncAddToErrorLog);
//gestion de l'erreur
Sync.CopyError.ErrorText:=ErrorText;
with Sync.CopyError do
begin
if Sync.Copy.ConfigData.CopyErrorAction=ceaNone then // aucune action automatique choisie?
begin
Synchronize(SyncInitCopyError);
// notification pour le systray
with Sync.Notification do
begin
TargetForm:=Form;
UseMainIcon:=False;
Title:=lsCopyErrorNotifyTitle;
Text:=WideFormat(lsCopyErrorNotifyText,[DisplayName,Copier.CurrentCopy.FileItem.DestFullName,ErrorText]);
IconType:=TBalloonFlags.bfError;
end;
Synchronize(SyncShowNotificationBalloon);
while (Action=ceaNone) and (not Sync.Copy.CancelPending) do
begin
Synchronize(SyncCheckCopyError);
Sleep(DEFAULT_WAIT);
end;
if Sync.Copy.CancelPending then Action:=ceaCancel;
Synchronize(SyncEndCopyError);
Result:=Action;
if SameForNext then
begin
Sync.Copy.ConfigDataModifiedByThread:=True;
Sync.Copy.ConfigData.CopyErrorAction:=Action;
end;
end
else
begin
// attendre un certain temps entre 2 erreurs de copie sur un mкme fichier
if Copier.CurrentCopy.FileItem.CopyTryCount>1 then
begin
Sleep(Config.Values.CopyErrorRetryInterval);
end;
Result:=Sync.Copy.ConfigData.CopyErrorAction;
end;
end;
end;
//******************************************************************************
// CopierGenericError: evenement du copier
//******************************************************************************
procedure TCopyThread.CopierGenericError(Action,Target,ErrorText:WideString);
begin
dbgln('CopierGenericError: '+Action+' '+ErrorText+' '+Target);
// notification pour le systray
with Sync.Notification do
begin
TargetForm:=nil;
UseMainIcon:=False;
Title:=lsGenericErrorNotifyTitle;
Text:=WideFormat(lsGenericErrorNotifyText,[DisplayName,Action,Target,ErrorText]);
IconType:=TBalloonFlags.bfError;
end;
Synchronize(SyncShowNotificationBalloon);
Sync.Copy.Error.Time:=Now;
Sync.Copy.Error.Action:=Action;
Sync.Copy.Error.Target:=Target;
Sync.Copy.Error.ErrorText:=ErrorText;
Synchronize(SyncAddToErrorLog);
end;
//******************************************************************************
// CopierCopyProgress: evenement du copier
// renvoyer false pour annuler la copie en cours
//******************************************************************************
function TCopyThread.CopierCopyProgress:Boolean;
var CurTime:Integer;
ThrottleTime:Integer;
DataSizeForThrottleTime:Int64;
//ComputeCopySpeed: calcul de la vitesse de copie
procedure ComputeCopySpeed;
var TempCopySpeed,TempCopyTime:integer;
Total:Int64;
i,UsedSamples:Integer;
begin
// calcul de la vitesse instantanйe
TempCopyTime:=CurTime-LastCopyWindowUpdate;
if TempCopyTime<>0 then
TempCopySpeed:=Round((Copier.CopiedSize-LastCopiedSize) * MSecsPerSec / TempCopyTime)
else
TempCopySpeed:=0;
LastCopiedSize:=Copier.CopiedSize;
// ajout а la liste des prйcйdentes vitesses
CurrentSample:=(CurrentSample+1) mod NumSamples;
SpeedSamples[CurrentSample]:=TempCopySpeed;
if CurrentSample=NumSamples-1 then SpeedSamplesFirstPass:=False;
// on fait la moyenne pour avoir la vitesse а afficher
Total:=0;
if SpeedSamplesFirstPass then
UsedSamples:=CurrentSample+1
else
UsedSamples:=NumSamples;
for i:=0 to UsedSamples-1 do
Total:=Total+SpeedSamples[i];
CopySpeed:=Total div UsedSamples;
end;
//ComputeThrottleCopySpeed: calcul de la vitesse de copie lorsque la limitation de vitesse est activйe
// (vitesse instantanйe sur l'intervale de throttle)
procedure ComputeThrottleCopySpeed;
var TempCopyTime:Integer;
begin
TempCopyTime:=CurTime-ThrottleLastTime;
if TempCopyTime<>0 then
CopySpeed:=Round((Copier.CopiedSize-ThrottleLastCopiedSize) * MSecsPerSec / TempCopyTime)
else
CopySpeed:=0;
end;
begin
with Sync.Copy do
begin
// vйrifier si il y a une baselist en attente
CheckWaitingBaseList;
CurTime:=GetTickCount;
State:=cwsCopying;
if not ConfigData.ThrottleEnabled then
begin
// mаj de la fenкtre si nйcesaire
if CurTime>=(LastCopyWindowUpdate+Config.Values.CopyWindowUpdateInterval) then
begin
ComputeCopySpeed;
UpdateCopyWindow;
LastCopyWindowUpdate:=CurTime;
end;
end
else
begin
// gestion limitation de vitesse
DataSizeForThrottleTime:=Int64(ConfigData.ThrottleSpeedLimit)*1024*Config.Values.CopyThrottleInterval div MSecsPerSec;
if Copier.CopiedSize>=(ThrottleLastCopiedSize+DataSizeForThrottleTime) then
begin
ThrottleTime:=Config.Values.CopyThrottleInterval-(CurTime-ThrottleLastTime);
if ThrottleTime>0 then Sleep(ThrottleTime);
CurTime:=GetTickCount;
ComputeThrottleCopySpeed;
UpdateCopyWindow;
ThrottleLastTime:=CurTime;
ThrottleLastCopiedSize:=Copier.CopiedSize;
end
else
begin
Synchronize(SyncUpdateCopy);
end;
end;
// gestion de la pause
HandlePause;
// gestion Skip/Cancel
Result:=not (CancelPending or SkipPending);
end;
end;
//******************************************************************************
// CopierRecurseProgress: evenement du copier
// renvoyer false pour annuler la rйcursion
//******************************************************************************
function TCopyThread.CopierRecurseProgress(CurrentItem:TDirItem):Boolean;
begin
Sync.Copy.llAllCaption:=lsCreatingCopyList;
Sync.Copy.llFileCaption:=CurrentItem.SrcPath;
Sync.Copy.State:=cwsRecursing;
Synchronize(SyncUpdateCopy);
// gйrer la pause
HandlePause;
Result:=not Sync.Copy.CancelPending;
end;
//******************************************************************************
// UpdateCopyWindow: mаj des infos de la fenкtre de copie
//******************************************************************************
procedure TCopyThread.UpdateCopyWindow;
var TmpStr:String;
AllRemaining,FileRemaining:TDateTime;
Percent:Integer;
StateUpdated:Boolean;
//ComputeRemainingTime: calcul du temps restant
procedure ComputeRemainingTime;
begin
AllRemaining:=0;
FileRemaining:=0;
if CopySpeed<>0 then
with Copier do
begin
AllRemaining:=(FileList.TotalSize-CopiedSize-SkippedSize)/CopySpeed/SecsPerDay;
FileRemaining:=(CurrentCopy.FileItem.SrcSize-CurrentCopy.CopiedSize-CurrentCopy.SkippedSize)/CopySpeed/SecsPerDay;
end;
end;
begin
StateUpdated:=False;
repeat
with Sync.Copy,Copier do
begin
// calcul du caption
Case State of
cwsWaiting,
cwsRecursing:
FormCaption:=WideFormat(lsCopyWindowWaitingCaption,[GetDisplayName]);
cwsPaused:
FormCaption:=WideFormat(lsCopyWindowPausedCaption,[GetDisplayName]);
cwsCancelling:
FormCaption:=WideFormat(lsCopyWindowCancellingCaption,[GetDisplayName]);
cwsCopyEnd:
if lvErrorListEmpty then
FormCaption:=WideFormat(lsCopyWindowCopyEndCaption,[GetDisplayName])
else
FormCaption:=WideFormat(lsCopyWindowCopyEndErrorsCaption,[GetDisplayName]);
else
begin
if ggAllMax>0 then Percent:=Round(ggAllProgress*100/ggAllMax) else Percent:=0;
FormCaption:=WideFormat('%d%% - %s',[Percent,GetDisplayName]);
end;
end;
// infos sur la copie
if Assigned(Copier.CurrentCopy.FileItem) and (Copier.FileList.Count>0) then
begin
ComputeRemainingTime;
llFromCaption:=CurrentCopy.DirItem.SrcPath;
llToCaption:=CurrentCopy.DirItem.Destpath;
llAllCaption:=WideFormat(lsAll,[CopiedCount+1,FileList.TotalCount,SizeToString(FileList.TotalSize,Config.Values.SizeUnit)]);
llFileCaption:=WideFormat(lsFile,[CurrentCopy.FileItem.SrcName,SizeToString(CurrentCopy.FileItem.SrcSize,Config.Values.SizeUnit)]);
ggFileProgress:=CurrentCopy.CopiedSize+CurrentCopy.SkippedSize;
ggFileMax:=CurrentCopy.FileItem.SrcSize;
ggAllProgress:=CopiedSize+SkippedSize;
ggAllMax:=FileList.TotalSize;
llSpeedCaption:=WideFormat(lsSpeed,[CopySpeed / 1024]);
DateTimeToString(TmpStr,'hh:nn:ss',AllRemaining);
ggAllRemaining:=WideFormat(lsRemaining,[TmpStr]);
DateTimeToString(TmpStr,'hh:nn:ss',FileRemaining);
ggFileRemaining:=WideFormat(lsRemaining,[TmpStr]);
end;
Synchronize(SyncUpdateCopy);
// gestion de l'йtat annulation
if CancelPending then
begin
Sync.Copy.State:=cwsCancelling;
StateUpdated:=not StateUpdated; // йtat changй -> on refait la mаj
end;
end;
until not StateUpdated;
end;
//******************************************************************************
// Cancel: annule la copie en cours
//******************************************************************************
procedure TCopyThread.Cancel;
begin
try
LockCopier;
Sync.Copy.CancelPending:=True;
Copier.CurrentCopy.NextAction:=cpaCancel;
finally
UnlockCopier;
end;
end;
//******************************************************************************
//******************************************************************************
// Mйthodes de synchro
//******************************************************************************
//******************************************************************************
//******************************************************************************
// SyncInitCopy: crйation et initialisation de la fenкtre de copie
//******************************************************************************
procedure TCopyThread.SyncInitCopy;
begin
with Sync.Copy do
begin
Form:=TCopyForm.Create(nil);
Form.CopyThread:=Self;
ggFileProgress:=0;
ggFileMax:=0;
ggAllProgress:=0;
ggAllMax:=0;
State:=cwsWaiting;