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