Progress.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 <CustomWinConfiguration.h>
  11. #include <GUITools.h>
  12. #include <BaseUtils.hpp>
  13. #include <DateUtils.hpp>
  14. #include "Progress.h"
  15. //---------------------------------------------------------------------
  16. #pragma link "HistoryComboBox"
  17. #pragma link "PathLabel"
  18. #ifndef NO_RESOURCES
  19. #pragma resource "*.dfm"
  20. #endif
  21. //---------------------------------------------------------------------
  22. UnicodeString __fastcall TProgressForm::OperationName(TFileOperation Operation, TOperationSide Side)
  23. {
  24. static const int Captions[] = { 0, 0, PROGRESS_DELETE,
  25. PROGRESS_SETPROPERTIES, 0, PROGRESS_CUSTOM_COMAND, PROGRESS_CALCULATE_SIZE,
  26. PROGRESS_REMOTE_MOVE, PROGRESS_REMOTE_COPY, PROGRESS_GETPROPERTIES,
  27. PROGRESS_CALCULATE_CHECKSUM };
  28. assert((unsigned int)Operation >= 1 && ((unsigned int)Operation - 1) < LENOF(Captions));
  29. int Id;
  30. if ((Operation == foCopy) || (Operation == foMove))
  31. {
  32. Id = (Side == osLocal) ? PROGRESS_UPLOAD : PROGRESS_DOWNLOAD;
  33. }
  34. else
  35. {
  36. Id = Captions[(int)Operation - 1];
  37. assert(Id != 0);
  38. }
  39. return LoadStr(Id);
  40. }
  41. //---------------------------------------------------------------------
  42. __fastcall TProgressForm::TProgressForm(TComponent* AOwner)
  43. : FData(), TForm(AOwner)
  44. {
  45. FLastOperation = foNone;
  46. FLastTotalSizeSet = false;
  47. FDataGot = false;
  48. FDataReceived = false;
  49. FAsciiTransferChanged = false;
  50. FResumeStatusChanged = false;
  51. FCancel = csContinue;
  52. FMinimizedByMe = false;
  53. FUpdateCounter = 0;
  54. FLastUpdate = 0;
  55. FDeleteToRecycleBin = false;
  56. FReadOnly = false;
  57. FShowAsModalStorage = NULL;
  58. FStarted = Now();
  59. FModalBeginHooked = false;
  60. FPrevApplicationModalBegin = NULL;
  61. FModalLevel = -1;
  62. UseSystemSettings(this);
  63. ResetOnceDoneOperation();
  64. if (CustomWinConfiguration->OperationProgressOnTop)
  65. {
  66. FOperationProgress = TopProgress;
  67. FFileProgress = BottomProgress;
  68. }
  69. else
  70. {
  71. FOperationProgress = BottomProgress;
  72. FFileProgress = TopProgress;
  73. }
  74. SetGlobalMinimizeHandler(this, GlobalMinimize);
  75. }
  76. //---------------------------------------------------------------------------
  77. __fastcall TProgressForm::~TProgressForm()
  78. {
  79. // to prevent raising assertion (e.g. IsProgress == True)
  80. FData.Clear();
  81. ClearGlobalMinimizeHandler(GlobalMinimize);
  82. if (IsApplicationMinimized() && FMinimizedByMe)
  83. {
  84. ShowNotification(NULL, LoadStr(BALLOON_OPERATION_COMPLETE), qtInformation);
  85. }
  86. if (FModalBeginHooked)
  87. {
  88. assert(Application->OnModalBegin == ApplicationModalBegin);
  89. Application->OnModalBegin = FPrevApplicationModalBegin;
  90. FModalBeginHooked = false;
  91. }
  92. ReleaseAsModal(this, FShowAsModalStorage);
  93. }
  94. //---------------------------------------------------------------------
  95. void __fastcall TProgressForm::UpdateControls()
  96. {
  97. assert((FData.Operation >= foCopy) && (FData.Operation <= foCalculateChecksum) &&
  98. FData.Operation != foRename );
  99. CancelButton->Enabled = !FReadOnly;
  100. OnceDoneOperationCombo->Enabled =
  101. !FReadOnly && (FData.Operation != foCalculateSize) &&
  102. (FData.Operation != foGetProperties) &&
  103. (FData.Operation != foCalculateChecksum);
  104. OnceDoneOperationLabel->Enabled = OnceDoneOperationCombo->Enabled;
  105. bool TransferOperation =
  106. ((FData.Operation == foCopy) || (FData.Operation == foMove));
  107. if (FData.Operation != FLastOperation)
  108. {
  109. bool AVisible;
  110. THandle ShellModule;
  111. try
  112. {
  113. AVisible = true;
  114. switch (FData.Operation) {
  115. case foCopy:
  116. case foMove:
  117. case foRemoteMove:
  118. case foRemoteCopy:
  119. if (FData.Count == 1) Animate->CommonAVI = aviCopyFile;
  120. else Animate->CommonAVI = aviCopyFiles;
  121. break;
  122. case foDelete:
  123. Animate->CommonAVI = (DeleteToRecycleBin ? aviRecycleFile : aviDeleteFile);
  124. break;
  125. case foSetProperties:
  126. case foGetProperties:
  127. ShellModule = SafeLoadLibrary(L"shell32.dll");
  128. if (!ShellModule)
  129. {
  130. Abort();
  131. }
  132. // workaround, VCL is not able to set both ResId and ResHandle otherwise
  133. Animate->Active = false;
  134. Animate->ResHandle = 0;
  135. Animate->ComponentState << csLoading;
  136. Animate->ResId = 165;
  137. Animate->ResHandle = ShellModule;
  138. Animate->ComponentState >> csLoading;
  139. Animate->Active = true;
  140. break;
  141. default:
  142. assert(FData.Operation == foCustomCommand ||
  143. FData.Operation == foCalculateSize ||
  144. FData.Operation == foCalculateChecksum);
  145. Animate->CommonAVI = aviNone;
  146. AVisible = false;
  147. }
  148. }
  149. catch (...)
  150. {
  151. AVisible = false;
  152. };
  153. int Delta = 0;
  154. if (AVisible && !Animate->Visible) Delta = Animate->Height;
  155. else
  156. if (!AVisible && Animate->Visible) Delta = -Animate->Height;
  157. MainPanel->Top = MainPanel->Top + Delta;
  158. TransferPanel->Top = TransferPanel->Top + Delta;
  159. SpeedPanel->Top = SpeedPanel->Top + Delta;
  160. Animate->Visible = AVisible;
  161. Animate->Active = AVisible;
  162. if (TransferOperation && !TransferPanel->Visible) Delta += TransferPanel->Height;
  163. else
  164. if (!TransferOperation && TransferPanel->Visible) Delta += -TransferPanel->Height;
  165. TransferPanel->Visible = TransferOperation;
  166. SpeedPanel->Visible = TransferOperation;
  167. ClientHeight = ClientHeight + Delta;
  168. Caption = OperationName(FData.Operation, FData.Side);
  169. TargetLabel->Visible = TransferOperation;
  170. TargetPathLabel->Visible = TransferOperation;
  171. TargetPathLabel->UnixPath = (FData.Side == osLocal);
  172. FileLabel->UnixPath = (FData.Side == osRemote);
  173. FLastOperation = FData.Operation;
  174. FLastTotalSizeSet = !FData.TotalSizeSet;
  175. };
  176. if (FLastTotalSizeSet != FData.TotalSizeSet)
  177. {
  178. StartTimeLabelLabel->Visible = !FData.TotalSizeSet;
  179. StartTimeLabel->Visible = !FData.TotalSizeSet;
  180. TimeLeftLabelLabel->Visible = FData.TotalSizeSet;
  181. TimeLeftLabel->Visible = FData.TotalSizeSet;
  182. FLastTotalSizeSet = FData.TotalSizeSet;
  183. }
  184. if ((FData.Side == osRemote) || !FData.Temp)
  185. {
  186. FileLabel->Caption = FData.FileName;
  187. }
  188. else
  189. {
  190. FileLabel->Caption = ExtractFileName(FData.FileName);
  191. }
  192. int OverallProgress = FData.OverallProgress();
  193. FOperationProgress->Position = OverallProgress;
  194. FOperationProgress->Hint = FORMAT(L"%d%%", (OverallProgress));
  195. Caption = FORMAT(L"%d%% %s", (OverallProgress, OperationName(FData.Operation, FData.Side)));
  196. if (TransferOperation)
  197. {
  198. if ((FData.Side == osLocal) || !FData.Temp)
  199. {
  200. TargetPathLabel->Caption = FData.Directory;
  201. }
  202. else
  203. {
  204. TargetPathLabel->Caption = LoadStr(PROGRESS_TEMP_DIR);
  205. }
  206. StartTimeLabel->Caption = FData.StartTime.TimeString();
  207. if (FData.TotalSizeSet)
  208. {
  209. TimeLeftLabel->Caption = FormatDateTimeSpan(Configuration->TimeFormat,
  210. FData.TotalTimeLeft());
  211. }
  212. TimeElapsedLabel->Caption = FormatDateTimeSpan(Configuration->TimeFormat, FData.TimeElapsed());
  213. BytesTransferedLabel->Caption = FormatBytes(FData.TotalTransfered);
  214. CPSLabel->Caption = FORMAT(L"%s/s", (FormatBytes(FData.CPS())));
  215. FFileProgress->Position = FData.TransferProgress();
  216. FFileProgress->Hint = FORMAT(L"%d%%", (FFileProgress->Position));
  217. }
  218. }
  219. //---------------------------------------------------------------------
  220. static TDateTime DelayStartInterval(static_cast<double>(OneSecond/5));
  221. static TDateTime UpdateInterval(static_cast<double>(OneSecond));
  222. //---------------------------------------------------------------------
  223. bool __fastcall TProgressForm::ReceiveData(bool Force, int ModalLevelOffset)
  224. {
  225. bool Result = false;
  226. if (FDataGot && !FDataReceived)
  227. {
  228. // CPS limit is set set only once from TFileOperationProgressType::Start.
  229. // Needs to be set even when data are not accepted yet, otherwise we would
  230. // write default value to FData in TProgressForm::SetProgressData
  231. FCPSLimit = FData.CPSLimit;
  232. // Never popup over dialog that appeared later than we started
  233. // (this can happen from UpdateTimerTimer when application is
  234. // restored while overwrite confirmation dialog [or any other]
  235. // is already shown).
  236. // TODO We should probably take as-modal windows into account too
  237. // (for extreme cases like restoring while reconnecting [as-modal TAuthenticateForm]).
  238. if ((FModalLevel < 0) || (Application->ModalLevel + ModalLevelOffset <= FModalLevel))
  239. {
  240. // delay showing the progress until the application is restored,
  241. // otherwise the form popups up unminimized.
  242. if (!IsApplicationMinimized() &&
  243. (Force || ((Now() - FStarted) > DelayStartInterval)))
  244. {
  245. FDataReceived = true;
  246. SpeedCombo->Text = SetSpeedLimit(FCPSLimit);
  247. ShowAsModal(this, FShowAsModalStorage);
  248. // particularly needed for the case, when we are showing the form delayed
  249. // because application was minimized when operation started
  250. Result = true;
  251. }
  252. else if (!FModalBeginHooked && ALWAYS_TRUE(FModalLevel < 0))
  253. {
  254. // record state as of time, the window should be shown,
  255. // had not we implemented delayed show
  256. FPrevApplicationModalBegin = Application->OnModalBegin;
  257. Application->OnModalBegin = ApplicationModalBegin;
  258. FModalBeginHooked = true;
  259. FModalLevel = Application->ModalLevel;
  260. }
  261. }
  262. }
  263. return Result;
  264. }
  265. //---------------------------------------------------------------------------
  266. void __fastcall TProgressForm::ApplicationModalBegin(TObject * Sender)
  267. {
  268. // Popup before any modal dialog shows (typically overwrite confirmation,
  269. // as that popups nerly instantly, i.e. less than DelayStartInterval).
  270. // The Application->ModalLevel is already incremented, but we should treat is as
  271. // if it were not as the dialog is not created yet (so we can popup if we are not yet).
  272. ReceiveData(true, -1);
  273. if (FPrevApplicationModalBegin != NULL)
  274. {
  275. FPrevApplicationModalBegin(Sender);
  276. }
  277. }
  278. //---------------------------------------------------------------------
  279. void __fastcall TProgressForm::SetProgressData(TFileOperationProgressType & AData)
  280. {
  281. TDateTime N = Now();
  282. bool InstantUpdate = false;
  283. // workaround: to force displaing first file data immediatelly,
  284. // otherwise form dialog uses to be blank for first second
  285. // (until UpdateTimerTimer)
  286. if (FileLabel->Caption.IsEmpty() && !AData.FileName.IsEmpty())
  287. {
  288. InstantUpdate = true;
  289. }
  290. if (!FAsciiTransferChanged && FData.AsciiTransfer != AData.AsciiTransfer)
  291. {
  292. FAsciiTransferChanged = true;
  293. InstantUpdate = true;
  294. }
  295. if (!FResumeStatusChanged && FData.ResumeStatus != AData.ResumeStatus)
  296. {
  297. FResumeStatusChanged = true;
  298. InstantUpdate = true;
  299. }
  300. FData = AData;
  301. FDataGot = true;
  302. if (!UpdateTimer->Enabled)
  303. {
  304. UpdateTimer->Interval = static_cast<unsigned int>(MilliSecondsBetween(TDateTime(), DelayStartInterval));
  305. UpdateTimer->Enabled = true;
  306. FSinceLastUpdate = TDateTime();
  307. }
  308. if (ReceiveData(false, 0))
  309. {
  310. InstantUpdate = true;
  311. }
  312. if (InstantUpdate)
  313. {
  314. UpdateControls();
  315. Application->ProcessMessages();
  316. }
  317. static double UpdateInterval = static_cast<double>(OneSecond/5); // 1/5 sec
  318. if ((FUpdateCounter % 5 == 0) ||
  319. (double(N) - double(FLastUpdate) > UpdateInterval))
  320. {
  321. FLastUpdate = N;
  322. FUpdateCounter = 0;
  323. Application->ProcessMessages();
  324. }
  325. FUpdateCounter++;
  326. AData.CPSLimit = FCPSLimit;
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TProgressForm::UpdateTimerTimer(TObject * /*Sender*/)
  330. {
  331. // popup the progress window at least here, if SetProgressData is
  332. // not being called (typically this happens when using custom command
  333. // that launches long-lasting external process, such as visual diff)
  334. ReceiveData(false, 0);
  335. if (FDataReceived)
  336. {
  337. FSinceLastUpdate = IncMilliSecond(FSinceLastUpdate, UpdateTimer->Interval);
  338. if (FSinceLastUpdate >= UpdateInterval)
  339. {
  340. UpdateControls();
  341. FSinceLastUpdate = TDateTime();
  342. }
  343. }
  344. }
  345. //---------------------------------------------------------------------------
  346. void __fastcall TProgressForm::FormShow(TObject * /*Sender*/)
  347. {
  348. SpeedCombo->Items = CustomWinConfiguration->History[L"SpeedLimit"];
  349. ReceiveData(false, 0);
  350. if (FDataReceived)
  351. {
  352. UpdateControls();
  353. }
  354. FLastUpdate = 0;
  355. }
  356. //---------------------------------------------------------------------------
  357. void __fastcall TProgressForm::FormHide(TObject * /*Sender*/)
  358. {
  359. // This is to counter the "infinite" timestamp in
  360. // TTerminalManager::ApplicationShowHint.
  361. // Because if form disappears on its own, hint is not hidden.
  362. Application->CancelHint();
  363. CustomWinConfiguration->History[L"SpeedLimit"] = SpeedCombo->Items;
  364. UpdateTimer->Enabled = false;
  365. }
  366. //---------------------------------------------------------------------------
  367. void __fastcall TProgressForm::CancelButtonClick(TObject * /*Sender*/)
  368. {
  369. CancelOperation();
  370. }
  371. //---------------------------------------------------------------------------
  372. void __fastcall TProgressForm::MinimizeButtonClick(TObject * Sender)
  373. {
  374. CallGlobalMinimizeHandler(Sender);
  375. }
  376. //---------------------------------------------------------------------------
  377. void __fastcall TProgressForm::CancelOperation()
  378. {
  379. assert(FDataReceived);
  380. if (!FData.Suspended)
  381. {
  382. // mostly useless, as suspend is called over copy of actual progress data
  383. FData.Suspend();
  384. UpdateControls();
  385. try
  386. {
  387. TCancelStatus ACancel;
  388. int Result;
  389. if (FData.TransferingFile &&
  390. (FData.TimeExpected() > GUIConfiguration->IgnoreCancelBeforeFinish))
  391. {
  392. Result = MessageDialog(LoadStr(CANCEL_OPERATION_FATAL2), qtWarning,
  393. qaYes | qaNo | qaCancel, HELP_PROGRESS_CANCEL);
  394. }
  395. else
  396. {
  397. Result = MessageDialog(LoadStr(CANCEL_OPERATION2), qtConfirmation,
  398. qaOK | qaCancel, HELP_PROGRESS_CANCEL);
  399. }
  400. switch (Result) {
  401. case qaYes:
  402. ACancel = csCancelTransfer; break;
  403. case qaOK:
  404. case qaNo:
  405. ACancel = csCancel; break;
  406. default:
  407. ACancel = csContinue; break;
  408. }
  409. if (FCancel < ACancel)
  410. {
  411. FCancel = ACancel;
  412. }
  413. }
  414. __finally
  415. {
  416. FData.Resume();
  417. }
  418. }
  419. }
  420. //---------------------------------------------------------------------------
  421. void __fastcall TProgressForm::GlobalMinimize(TObject * /*Sender*/)
  422. {
  423. Application->Minimize();
  424. FMinimizedByMe = true;
  425. }
  426. //---------------------------------------------------------------------------
  427. void __fastcall TProgressForm::SetOnceDoneOperation(TOnceDoneOperation value)
  428. {
  429. int Index = 0;
  430. switch (value)
  431. {
  432. case odoIdle:
  433. Index = 0;
  434. break;
  435. case odoDisconnect:
  436. Index = 1;
  437. break;
  438. case odoShutDown:
  439. Index = 2;
  440. break;
  441. default:
  442. assert(false);
  443. }
  444. OnceDoneOperationCombo->ItemIndex = Index;
  445. OnceDoneOperationComboSelect(NULL);
  446. }
  447. //---------------------------------------------------------------------------
  448. bool __fastcall TProgressForm::GetAllowMinimize()
  449. {
  450. return MinimizeButton->Visible;
  451. }
  452. //---------------------------------------------------------------------------
  453. void __fastcall TProgressForm::SetAllowMinimize(bool value)
  454. {
  455. MinimizeButton->Visible = value;
  456. }
  457. //---------------------------------------------------------------------------
  458. void __fastcall TProgressForm::SetReadOnly(bool value)
  459. {
  460. if (FReadOnly != value)
  461. {
  462. FReadOnly = value;
  463. if (!value)
  464. {
  465. ResetOnceDoneOperation();
  466. }
  467. UpdateControls();
  468. }
  469. }
  470. //---------------------------------------------------------------------------
  471. void __fastcall TProgressForm::ApplyCPSLimit()
  472. {
  473. try
  474. {
  475. FCPSLimit = GetSpeedLimit(SpeedCombo->Text);
  476. }
  477. catch(...)
  478. {
  479. SpeedCombo->SetFocus();
  480. throw;
  481. }
  482. SpeedCombo->Text = SetSpeedLimit(FCPSLimit);
  483. // visualize application
  484. SpeedCombo->SelectAll();
  485. CancelButton->SetFocus();
  486. }
  487. //---------------------------------------------------------------------------
  488. void __fastcall TProgressForm::SpeedComboExit(TObject * /*Sender*/)
  489. {
  490. SpeedCombo->Text = SetSpeedLimit(FCPSLimit);
  491. }
  492. //---------------------------------------------------------------------------
  493. void __fastcall TProgressForm::SpeedComboSelect(TObject * /*Sender*/)
  494. {
  495. ApplyCPSLimit();
  496. }
  497. //---------------------------------------------------------------------------
  498. void __fastcall TProgressForm::SpeedComboKeyPress(TObject * /*Sender*/,
  499. wchar_t & Key)
  500. {
  501. // using OnKeyPress instead of OnKeyDown to catch "enter" prevents
  502. // system beep for unhandled key
  503. if (Key == L'\r')
  504. {
  505. Key = L'\0';
  506. ApplyCPSLimit();
  507. }
  508. }
  509. //---------------------------------------------------------------------------
  510. void __fastcall TProgressForm::ResetOnceDoneOperation()
  511. {
  512. OnceDoneOperationCombo->ItemIndex = 0;
  513. OnceDoneOperationComboSelect(NULL);
  514. }
  515. //---------------------------------------------------------------------------
  516. void __fastcall TProgressForm::OnceDoneOperationComboSelect(TObject * /*Sender*/)
  517. {
  518. switch (OnceDoneOperationCombo->ItemIndex)
  519. {
  520. case 0:
  521. FOnceDoneOperation = odoIdle;
  522. break;
  523. case 1:
  524. FOnceDoneOperation = odoDisconnect;
  525. break;
  526. case 2:
  527. FOnceDoneOperation = odoShutDown;
  528. break;
  529. default:
  530. assert(false);
  531. }
  532. }
  533. //---------------------------------------------------------------------------
  534. void __fastcall TProgressForm::OnceDoneOperationComboCloseUp(TObject * /*Sender*/)
  535. {
  536. CancelButton->SetFocus();
  537. }
  538. //---------------------------------------------------------------------------
  539. #pragma warn -8080