FileOperationProgress.h 10 KB

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