FileOperationProgress.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //---------------------------------------------------------------------------
  2. #ifndef FileOperationProgressH
  3. #define FileOperationProgressH
  4. //---------------------------------------------------------------------------
  5. #include "Configuration.h"
  6. #include "CopyParam.h"
  7. #include "Exceptions.h"
  8. #include <vector>
  9. //---------------------------------------------------------------------------
  10. class TFileOperationProgressType;
  11. enum TFileOperation { foNone, foCopy, foMove, foDelete, foSetProperties,
  12. foRename, foCustomCommand, foCalculateSize, foRemoteMove, foRemoteCopy,
  13. foGetProperties, foCalculateChecksum, foLock, foUnlock };
  14. // csCancelTransfer and csRemoteAbort are used with SCP only
  15. enum TCancelStatus { csContinue = 0, csCancelFile, csCancel, csCancelTransfer, csRemoteAbort };
  16. enum TBatchOverwrite { boNo, boAll, boNone, boOlder, boAlternateResume, boAppend, boResume };
  17. typedef void __fastcall (__closure *TFileOperationProgressEvent)
  18. (TFileOperationProgressType & ProgressData);
  19. typedef void __fastcall (__closure *TFileOperationFinished)
  20. (TFileOperation Operation, TOperationSide Side, bool Temp,
  21. const UnicodeString & FileName, bool Success, TOnceDoneOperation & OnceDoneOperation);
  22. //---------------------------------------------------------------------------
  23. class TFileOperationProgressType
  24. {
  25. public:
  26. class TPersistence
  27. {
  28. friend class TFileOperationProgressType;
  29. public:
  30. TPersistence();
  31. private:
  32. void Clear(bool Batch, bool Speed);
  33. TDateTime StartTime;
  34. TBatchOverwrite BatchOverwrite;
  35. bool SkipToAll;
  36. unsigned long CPSLimit;
  37. bool CounterSet;
  38. std::vector<unsigned long> Ticks;
  39. std::vector<__int64> TotalTransferredThen;
  40. TOperationSide Side;
  41. __int64 TotalTransferred;
  42. };
  43. private:
  44. TFileOperation FOperation;
  45. UnicodeString FFileName;
  46. UnicodeString FFullFileName;
  47. UnicodeString FDirectory;
  48. bool FAsciiTransfer;
  49. bool FTransferringFile;
  50. bool FTemp;
  51. __int64 FLocalSize;
  52. __int64 FLocallyUsed;
  53. __int64 FTransferSize;
  54. __int64 FTransferredSize;
  55. __int64 FSkippedSize;
  56. bool FInProgress;
  57. bool FDone;
  58. bool FFileInProgress;
  59. TCancelStatus FCancel;
  60. int FCount;
  61. __int64 FTotalTransferBase;
  62. __int64 FTotalSkipped;
  63. __int64 FTotalSize;
  64. bool FTotalSizeSet;
  65. bool FSuspended;
  66. bool FRestored;
  67. TFileOperationProgressType * FParent;
  68. // when it was last time suspended (to calculate suspend time in Resume())
  69. unsigned int FSuspendTime;
  70. // when current file was started being transferred
  71. TDateTime FFileStartTime;
  72. int FFilesFinished;
  73. int FFilesFinishedSuccessfully;
  74. TFileOperationProgressEvent FOnProgress;
  75. TFileOperationFinished FOnFinished;
  76. bool FReset;
  77. unsigned int FLastSecond;
  78. unsigned long FRemainingCPS;
  79. TPersistence FPersistence;
  80. TCriticalSection * FSection;
  81. TCriticalSection * FUserSelectionsSection;
  82. __int64 __fastcall GetTotalTransferred();
  83. __int64 __fastcall GetTotalSize();
  84. unsigned long __fastcall GetCPSLimit();
  85. TBatchOverwrite __fastcall GetBatchOverwrite();
  86. bool __fastcall GetSkipToAll();
  87. TDateTime __fastcall GetStartTime() const { return FPersistence.StartTime; };
  88. TOperationSide __fastcall GetSide() const { return FPersistence.Side; };
  89. protected:
  90. void __fastcall ClearTransfer();
  91. inline void __fastcall DoProgress();
  92. int __fastcall OperationProgress() const;
  93. void __fastcall AddTransferredToTotals(__int64 ASize);
  94. void __fastcall AddSkipped(__int64 ASize);
  95. void __fastcall AddTotalSize(__int64 ASize);
  96. void __fastcall RollbackTransferFromTotals(__int64 ATransferredSize, __int64 ASkippedSize);
  97. unsigned int __fastcall GetCPS();
  98. void __fastcall Init();
  99. static bool __fastcall PassCancelToParent(TCancelStatus ACancel);
  100. void __fastcall DoClear(bool Batch, bool Speed);
  101. public:
  102. // common data
  103. __property TFileOperation Operation = { read = FOperation };
  104. // on what side if operation being processed (local/remote), source of copy
  105. __property TOperationSide Side = { read = GetSide };
  106. __property int Count = { read = FCount };
  107. __property UnicodeString FileName = { read = FFileName };
  108. __property UnicodeString FullFileName = { read = FFullFileName };
  109. __property UnicodeString Directory = { read = FDirectory };
  110. __property bool AsciiTransfer = { read = FAsciiTransfer };
  111. // Can be true with SCP protocol only
  112. __property bool TransferringFile = { read = FTransferringFile };
  113. __property bool Temp = { read = FTemp };
  114. // file size to read/write
  115. __property __int64 LocalSize = { read = FLocalSize };
  116. __property __int64 LocallyUsed = { read = FLocallyUsed };
  117. __property __int64 TransferSize = { read = FTransferSize };
  118. __property __int64 TransferredSize = { read = FTransferredSize };
  119. __property __int64 SkippedSize = { read = FSkippedSize };
  120. __property bool InProgress = { read = FInProgress };
  121. __property bool Done = { read = FDone };
  122. __property bool FileInProgress = { read = FFileInProgress };
  123. __property TCancelStatus Cancel = { read = GetCancel };
  124. // when operation started
  125. __property TDateTime StartTime = { read = GetStartTime };
  126. // bytes transferred
  127. __property __int64 TotalTransferred = { read = GetTotalTransferred };
  128. __property __int64 TotalSize = { read = GetTotalSize };
  129. __property int FilesFinishedSuccessfully = { read = FFilesFinishedSuccessfully };
  130. __property TBatchOverwrite BatchOverwrite = { read = GetBatchOverwrite };
  131. __property bool SkipToAll = { read = GetSkipToAll };
  132. __property unsigned long CPSLimit = { read = GetCPSLimit };
  133. __property bool TotalSizeSet = { read = FTotalSizeSet };
  134. __property bool Suspended = { read = FSuspended };
  135. __fastcall TFileOperationProgressType();
  136. __fastcall TFileOperationProgressType(
  137. TFileOperationProgressEvent AOnProgress, TFileOperationFinished AOnFinished,
  138. TFileOperationProgressType * Parent = NULL);
  139. __fastcall ~TFileOperationProgressType();
  140. void __fastcall Assign(const TFileOperationProgressType & Other);
  141. void __fastcall AssignButKeepSuspendState(const TFileOperationProgressType & Other);
  142. void __fastcall AddLocallyUsed(__int64 ASize);
  143. void __fastcall AddTransferred(__int64 ASize, bool AddToTotals = true);
  144. void __fastcall AddResumed(__int64 ASize);
  145. void __fastcall AddSkippedFileSize(__int64 ASize);
  146. void __fastcall Clear();
  147. unsigned int __fastcall CPS();
  148. void __fastcall Finish(UnicodeString FileName, bool Success,
  149. TOnceDoneOperation & OnceDoneOperation);
  150. void __fastcall Progress();
  151. unsigned long __fastcall LocalBlockSize();
  152. bool __fastcall IsLocallyDone();
  153. bool __fastcall IsTransferDone();
  154. void __fastcall SetFile(UnicodeString AFileName, bool AFileInProgress = true);
  155. void __fastcall SetFileInProgress();
  156. unsigned long __fastcall TransferBlockSize();
  157. unsigned long __fastcall AdjustToCPSLimit(unsigned long Size);
  158. void __fastcall ThrottleToCPSLimit(unsigned long Size);
  159. static unsigned long __fastcall StaticBlockSize();
  160. void __fastcall Reset();
  161. void __fastcall Resume();
  162. void __fastcall SetLocalSize(__int64 ASize);
  163. void __fastcall SetAsciiTransfer(bool AAsciiTransfer);
  164. void __fastcall SetTransferSize(__int64 ASize);
  165. void __fastcall ChangeTransferSize(__int64 ASize);
  166. void __fastcall RollbackTransfer();
  167. void __fastcall SetTotalSize(__int64 ASize);
  168. void __fastcall Start(TFileOperation AOperation, TOperationSide ASide, int ACount);
  169. void __fastcall Start(TFileOperation AOperation,
  170. TOperationSide ASide, int ACount, bool ATemp, const UnicodeString ADirectory,
  171. unsigned long ACPSLimit);
  172. void __fastcall Stop();
  173. void __fastcall SetDone();
  174. void __fastcall Suspend();
  175. void __fastcall LockUserSelections();
  176. void __fastcall UnlockUserSelections();
  177. // whole operation
  178. TDateTime __fastcall TimeElapsed();
  179. // only current file
  180. TDateTime __fastcall TimeExpected();
  181. TDateTime __fastcall TotalTimeLeft();
  182. int __fastcall TransferProgress();
  183. int __fastcall OverallProgress() const;
  184. int __fastcall TotalTransferProgress() const;
  185. void __fastcall SetSpeedCounters();
  186. void __fastcall SetTransferringFile(bool ATransferringFile);
  187. TCancelStatus __fastcall GetCancel();
  188. void __fastcall SetCancel(TCancelStatus ACancel);
  189. void __fastcall SetCancelAtLeast(TCancelStatus ACancel);
  190. bool __fastcall ClearCancelFile();
  191. void __fastcall SetCPSLimit(unsigned long ACPSLimit);
  192. void __fastcall SetBatchOverwrite(TBatchOverwrite ABatchOverwrite);
  193. void __fastcall SetSkipToAll();
  194. UnicodeString __fastcall GetLogStr(bool Done);
  195. void __fastcall Store(TPersistence & Persistence);
  196. void __fastcall Restore(TPersistence & Persistence);
  197. static bool IsIndeterminateOperation(TFileOperation Operation);
  198. };
  199. //---------------------------------------------------------------------------
  200. class TSuspendFileOperationProgress
  201. {
  202. public:
  203. __fastcall TSuspendFileOperationProgress(TFileOperationProgressType * OperationProgress)
  204. {
  205. FOperationProgress = OperationProgress;
  206. if (FOperationProgress != NULL)
  207. {
  208. FOperationProgress->Suspend();
  209. }
  210. }
  211. __fastcall ~TSuspendFileOperationProgress()
  212. {
  213. if (FOperationProgress != NULL)
  214. {
  215. FOperationProgress->Resume();
  216. }
  217. }
  218. private:
  219. TFileOperationProgressType * FOperationProgress;
  220. };
  221. //---------------------------------------------------------------------------
  222. #endif