Progress.cpp 18 KB

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