Progress.cpp 11 KB

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