Progress.cpp 13 KB

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