Progress.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <AssociatedStatusBar.hpp>
  5. #include <Common.h>
  6. #include <ScpMain.h>
  7. #include <TextsWin.h>
  8. #include <VCLCommon.h>
  9. #include <WinInterface.h>
  10. #include "Progress.h"
  11. //---------------------------------------------------------------------
  12. #pragma link "PathLabel"
  13. #pragma resource "*.dfm"
  14. //---------------------------------------------------------------------
  15. AnsiString __fastcall TProgressForm::OperationName(TFileOperation Operation)
  16. {
  17. static const int Captions[] = { PROGRESS_COPY, PROGRESS_MOVE, PROGRESS_DELETE,
  18. PROGRESS_SETPROPERTIES, 0, PROGRESS_CUSTOM_COMAND, PROGRESS_CALCULATE_SIZE };
  19. assert((int)Operation >= 1 && ((int)Operation - 1) < LENOF(Captions));
  20. return LoadStr(Captions[(int)Operation - 1]);
  21. }
  22. //---------------------------------------------------------------------
  23. __fastcall TProgressForm::TProgressForm(TComponent* AOwner)
  24. : FData(), TForm(AOwner)
  25. {
  26. FLastOperation = foNone;
  27. FLastTotalSizeSet = false;
  28. FDataReceived = false;
  29. FAsciiTransferChanged = false;
  30. FResumeStatusChanged = false;
  31. FCancel = csContinue;
  32. FMinimizedByMe = false;
  33. FUpdateCounter = 0;
  34. FLastUpdate = 0;
  35. FDeleteToRecycleBin = false;
  36. FShowAsModalStorage = NULL;
  37. UseSystemSettings(this);
  38. }
  39. //---------------------------------------------------------------------------
  40. __fastcall TProgressForm::~TProgressForm()
  41. {
  42. // to prevent raising assertion (e.g. IsProgress == True)
  43. FData.Clear();
  44. if (IsIconic(Application->Handle) && FMinimizedByMe)
  45. Application->Restore();
  46. ReleaseAsModal(this, FShowAsModalStorage);
  47. }
  48. //---------------------------------------------------------------------
  49. void __fastcall TProgressForm::UpdateControls()
  50. {
  51. assert((FData.Operation >= foCopy) && (FData.Operation <= foCalculateSize) &&
  52. FData.Operation != foRename );
  53. bool TransferOperation =
  54. ((FData.Operation == foCopy) || (FData.Operation == foMove));
  55. if (FData.Operation != FLastOperation)
  56. {
  57. bool AVisible;
  58. THandle ShellModule;
  59. try
  60. {
  61. AVisible = true;
  62. switch (FData.Operation) {
  63. case foCopy:
  64. case foMove:
  65. if (FData.Count == 1) Animate->CommonAVI = aviCopyFile;
  66. else Animate->CommonAVI = aviCopyFiles;
  67. break;
  68. case foDelete:
  69. Animate->CommonAVI = ((FData.Side == osLocal) && DeleteToRecycleBin) ?
  70. aviRecycleFile : aviDeleteFile;
  71. break;
  72. case foSetProperties:
  73. ShellModule = SafeLoadLibrary("shell32.dll");
  74. if (!ShellModule)
  75. {
  76. Abort();
  77. }
  78. // workaround, VCL is not able to set both ResId and ResHandle otherwise
  79. Animate->Active = false;
  80. Animate->ResHandle = 0;
  81. Animate->ComponentState << csLoading;
  82. Animate->ResId = 165;
  83. Animate->ResHandle = ShellModule;
  84. Animate->ComponentState >> csLoading;
  85. Animate->Active = true;
  86. break;
  87. default:
  88. assert(FData.Operation == foCustomCommand || FData.Operation == foCalculateSize);
  89. Animate->CommonAVI = aviNone;
  90. AVisible = false;
  91. }
  92. }
  93. catch (...)
  94. {
  95. AVisible = false;
  96. };
  97. int Delta = 0;
  98. if (AVisible && !Animate->Visible) Delta = Animate->Height;
  99. else
  100. if (!AVisible && Animate->Visible) Delta = -Animate->Height;
  101. MainPanel->Top = MainPanel->Top + Delta;
  102. TransferPanel->Top = TransferPanel->Top + Delta;
  103. SpeedPanel->Top = SpeedPanel->Top + Delta;
  104. Animate->Visible = AVisible;
  105. Animate->Active = AVisible;
  106. if (TransferOperation && !TransferPanel->Visible) Delta += TransferPanel->Height;
  107. else
  108. if (!TransferOperation && TransferPanel->Visible) Delta += -TransferPanel->Height;
  109. TransferPanel->Visible = TransferOperation;
  110. SpeedPanel->Visible = TransferOperation;
  111. ClientHeight = ClientHeight + Delta;
  112. DisconnectWhenCompleteCheck->Top = DisconnectWhenCompleteCheck->Top + Delta;
  113. Caption = OperationName(FData.Operation);
  114. TargetLabel->Visible = TransferOperation;
  115. TargetPathLabel->Visible = TransferOperation;
  116. TargetPathLabel->UnixPath = (FData.Side == osLocal);
  117. FileLabel->UnixPath = (FData.Side == osRemote);
  118. FLastOperation = FData.Operation;
  119. FLastTotalSizeSet = !FData.TotalSizeSet;
  120. };
  121. if (FLastTotalSizeSet != FData.TotalSizeSet)
  122. {
  123. StartTimeLabelLabel->Visible = !FData.TotalSizeSet;
  124. StartTimeLabel->Visible = !FData.TotalSizeSet;
  125. TimeEstimatedLabelLabel->Visible = FData.TotalSizeSet;
  126. TimeEstimatedLabel->Visible = FData.TotalSizeSet;
  127. FLastTotalSizeSet = FData.TotalSizeSet;
  128. }
  129. FileLabel->Caption = FData.FileName;
  130. int OverallProgress = FData.OverallProgress();
  131. OperationProgress->Position = OverallProgress;
  132. OperationProgress->Hint = FORMAT("%d%%", (OverallProgress));
  133. Caption = FORMAT("%d%% %s", (OverallProgress, OperationName(FData.Operation)));
  134. if (TransferOperation)
  135. {
  136. if (!FData.DragDrop || (FData.Side == osLocal))
  137. {
  138. TargetPathLabel->Caption = FData.Directory;
  139. }
  140. else
  141. {
  142. TargetPathLabel->Caption = LoadStr(PROGRESS_DRAGDROP_TARGET);
  143. }
  144. StartTimeLabel->Caption = FData.StartTime.TimeString();
  145. if (FData.TotalSizeSet)
  146. {
  147. TimeEstimatedLabel->Caption = FormatDateTime(Configuration->TimeFormat,
  148. FData.TotalTimeExpected());
  149. }
  150. TimeElapsedLabel->Caption = FormatDateTime(Configuration->TimeFormat, FData.TimeElapsed());
  151. BytesTransferedLabel->Caption = FormatBytes(FData.TotalTransfered);
  152. CPSLabel->Caption = FORMAT("%s/s", (FormatBytes(FData.CPS())));
  153. FileProgress->Position = FData.TransferProgress();
  154. FileProgress->Hint = FORMAT("%d%%", (FileProgress->Position));
  155. TransferModeLabel->Caption =
  156. LoadStr(FData.AsciiTransfer ? TRANSFER_ASCII : TRANSFER_BINARY);
  157. AnsiString ResumeStatusStr;
  158. switch (FData.ResumeStatus) {
  159. case rsEnabled: ResumeStatusStr = LoadStr(RESUME_ENABLED); break;
  160. case rsDisabled: ResumeStatusStr = LoadStr(RESUME_DISABLED); break;
  161. case rsNotAvailable: ResumeStatusStr = LoadStr(RESUME_NOT_AVAILABLE); break;
  162. default: assert(false);
  163. }
  164. ResumeLabel->Caption = ResumeStatusStr;
  165. }
  166. }
  167. //---------------------------------------------------------------------
  168. void __fastcall TProgressForm::SetProgressData(const TFileOperationProgressType &AData)
  169. {
  170. bool InstantUpdate = false;
  171. // workaround: to force displaing first file data immediatelly,
  172. // otherwise form dialog uses to be blank for first second
  173. // (until UpdateTimerTimer)
  174. if (FileLabel->Caption.IsEmpty() && !AData.FileName.IsEmpty())
  175. {
  176. InstantUpdate = true;
  177. }
  178. if (!FAsciiTransferChanged && FData.AsciiTransfer != AData.AsciiTransfer)
  179. {
  180. FAsciiTransferChanged = true;
  181. InstantUpdate = true;
  182. }
  183. if (!FResumeStatusChanged && FData.ResumeStatus != AData.ResumeStatus)
  184. {
  185. FResumeStatusChanged = true;
  186. InstantUpdate = true;
  187. }
  188. FData = AData;
  189. if (!FDataReceived)
  190. {
  191. FDataReceived = true;
  192. ShowAsModal(this, FShowAsModalStorage);
  193. }
  194. if (InstantUpdate)
  195. {
  196. UpdateControls();
  197. Application->ProcessMessages();
  198. }
  199. TDateTime N = Now();
  200. if ((double)FLastUpdate && SpeedPanel->Visible && SpeedBar->Position < 100)
  201. {
  202. assert(SpeedBar->Position > 0);
  203. __int64 MilliSecondsTr = (double(N) - double(FLastUpdate)) * MSecsPerDay;
  204. int MilliSecondsWait = int(double(MilliSecondsTr > 100000 ? 100000 : MilliSecondsTr) *
  205. (double(100)/double(SpeedBar->Position) - 1));
  206. MilliSecondsWait = (MilliSecondsWait > 2000) ? 2000 : MilliSecondsWait;
  207. while (MilliSecondsWait > 0 && FCancel == csContinue)
  208. {
  209. SleepEx(MilliSecondsWait > 300 ? 300 : MilliSecondsWait, true);
  210. MilliSecondsWait -= 300;
  211. Application->ProcessMessages();
  212. }
  213. }
  214. static double UpdateInterval = double(1)/(24*60*60*5); // 1/5 sec
  215. if ((FUpdateCounter % 5 == 0) ||
  216. (double(N) - double(FLastUpdate) > UpdateInterval))
  217. {
  218. FLastUpdate = N;
  219. FUpdateCounter = 0;
  220. Application->ProcessMessages();
  221. }
  222. FUpdateCounter++;
  223. }
  224. //---------------------------------------------------------------------------
  225. void __fastcall TProgressForm::UpdateTimerTimer(TObject * /*Sender*/)
  226. {
  227. if (FDataReceived) UpdateControls();
  228. }
  229. //---------------------------------------------------------------------------
  230. void __fastcall TProgressForm::FormShow(TObject * /*Sender*/)
  231. {
  232. UpdateTimer->Enabled = true;
  233. if (FDataReceived) UpdateControls();
  234. SpeedBar->Position = 100;
  235. FLastUpdate = 0;
  236. }
  237. //---------------------------------------------------------------------------
  238. void __fastcall TProgressForm::FormHide(TObject * /*Sender*/)
  239. {
  240. UpdateTimer->Enabled = false;
  241. }
  242. //---------------------------------------------------------------------------
  243. void __fastcall TProgressForm::CancelButtonClick(TObject * /*Sender*/)
  244. {
  245. CancelOperation();
  246. }
  247. //---------------------------------------------------------------------------
  248. void __fastcall TProgressForm::MinimizeButtonClick(TObject * /*Sender*/)
  249. {
  250. MinimizeApp();
  251. }
  252. //---------------------------------------------------------------------------
  253. void __fastcall TProgressForm::CancelOperation()
  254. {
  255. // partially duplicated in TWinSCPFileSystem::CancelConfiguration (far\WinSCPFileSystem)
  256. assert(FDataReceived);
  257. if (!FData.Suspended)
  258. {
  259. FData.Suspend();
  260. UpdateControls();
  261. try
  262. {
  263. TCancelStatus ACancel;
  264. int Result;
  265. if (FData.TransferingFile &&
  266. (FData.TimeExpected() > Configuration->IgnoreCancelBeforeFinish))
  267. {
  268. Result = MessageDialog(LoadStr(CANCEL_OPERATION_FATAL), qtWarning,
  269. qaYes | qaNo | qaCancel, 0);
  270. }
  271. else
  272. {
  273. Result = MessageDialog(LoadStr(CANCEL_OPERATION), qtConfirmation,
  274. qaOK | qaCancel, 0);
  275. }
  276. switch (Result) {
  277. case qaYes:
  278. ACancel = csCancelTransfer; break;
  279. case qaOK:
  280. case qaNo:
  281. ACancel = csCancel; break;
  282. default:
  283. ACancel = csContinue; break;
  284. }
  285. if (FCancel < ACancel)
  286. {
  287. FCancel = ACancel;
  288. }
  289. }
  290. __finally
  291. {
  292. FData.Resume();
  293. }
  294. }
  295. }
  296. //---------------------------------------------------------------------------
  297. void __fastcall TProgressForm::MinimizeApp()
  298. {
  299. Application->Minimize();
  300. FMinimizedByMe = true;
  301. }
  302. //---------------------------------------------------------------------------
  303. void __fastcall TProgressForm::SetDisconnectWhenComplete(bool value)
  304. {
  305. DisconnectWhenCompleteCheck->Checked = value;
  306. }
  307. //---------------------------------------------------------------------------
  308. bool __fastcall TProgressForm::GetDisconnectWhenComplete()
  309. {
  310. return DisconnectWhenCompleteCheck->Checked;
  311. }
  312. //---------------------------------------------------------------------------
  313. bool __fastcall TProgressForm::GetAllowMinimize()
  314. {
  315. return MinimizeButton->Visible;
  316. }
  317. //---------------------------------------------------------------------------
  318. void __fastcall TProgressForm::SetAllowMinimize(bool value)
  319. {
  320. MinimizeButton->Visible = value;
  321. }