Progress.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. FDataReceived = false;
  48. FAsciiTransferChanged = false;
  49. FResumeStatusChanged = false;
  50. FCancel = csContinue;
  51. FMinimizedByMe = false;
  52. FUpdateCounter = 0;
  53. FLastUpdate = 0;
  54. FDeleteToRecycleBin = false;
  55. FReadOnly = false;
  56. FShowAsModalStorage = NULL;
  57. FStarted = Now();
  58. UseSystemSettings(this);
  59. ResetOnceDoneOperation();
  60. if (CustomWinConfiguration->OperationProgressOnTop)
  61. {
  62. FOperationProgress = TopProgress;
  63. FFileProgress = BottomProgress;
  64. }
  65. else
  66. {
  67. FOperationProgress = BottomProgress;
  68. FFileProgress = TopProgress;
  69. }
  70. SetGlobalMinimizeHandler(this, GlobalMinimize);
  71. }
  72. //---------------------------------------------------------------------------
  73. __fastcall TProgressForm::~TProgressForm()
  74. {
  75. // to prevent raising assertion (e.g. IsProgress == True)
  76. FData.Clear();
  77. ClearGlobalMinimizeHandler(GlobalMinimize);
  78. if (IsApplicationMinimized() && FMinimizedByMe)
  79. {
  80. ShowNotification(NULL, LoadStr(BALLOON_OPERATION_COMPLETE), qtInformation);
  81. }
  82. ReleaseAsModal(this, FShowAsModalStorage);
  83. }
  84. //---------------------------------------------------------------------
  85. void __fastcall TProgressForm::UpdateControls()
  86. {
  87. assert((FData.Operation >= foCopy) && (FData.Operation <= foCalculateChecksum) &&
  88. FData.Operation != foRename );
  89. CancelButton->Enabled = !FReadOnly;
  90. OnceDoneOperationCombo->Enabled =
  91. !FReadOnly && (FData.Operation != foCalculateSize) &&
  92. (FData.Operation != foGetProperties) &&
  93. (FData.Operation != foCalculateChecksum);
  94. OnceDoneOperationLabel->Enabled = OnceDoneOperationCombo->Enabled;
  95. bool TransferOperation =
  96. ((FData.Operation == foCopy) || (FData.Operation == foMove));
  97. if (FData.Operation != FLastOperation)
  98. {
  99. bool AVisible;
  100. THandle ShellModule;
  101. try
  102. {
  103. AVisible = true;
  104. switch (FData.Operation) {
  105. case foCopy:
  106. case foMove:
  107. case foRemoteMove:
  108. case foRemoteCopy:
  109. if (FData.Count == 1) Animate->CommonAVI = aviCopyFile;
  110. else Animate->CommonAVI = aviCopyFiles;
  111. break;
  112. case foDelete:
  113. Animate->CommonAVI = (DeleteToRecycleBin ? aviRecycleFile : aviDeleteFile);
  114. break;
  115. case foSetProperties:
  116. case foGetProperties:
  117. ShellModule = SafeLoadLibrary(L"shell32.dll");
  118. if (!ShellModule)
  119. {
  120. Abort();
  121. }
  122. // workaround, VCL is not able to set both ResId and ResHandle otherwise
  123. Animate->Active = false;
  124. Animate->ResHandle = 0;
  125. Animate->ComponentState << csLoading;
  126. Animate->ResId = 165;
  127. Animate->ResHandle = ShellModule;
  128. Animate->ComponentState >> csLoading;
  129. Animate->Active = true;
  130. break;
  131. default:
  132. assert(FData.Operation == foCustomCommand ||
  133. FData.Operation == foCalculateSize ||
  134. FData.Operation == foCalculateChecksum);
  135. Animate->CommonAVI = aviNone;
  136. AVisible = false;
  137. }
  138. }
  139. catch (...)
  140. {
  141. AVisible = false;
  142. };
  143. int Delta = 0;
  144. if (AVisible && !Animate->Visible) Delta = Animate->Height;
  145. else
  146. if (!AVisible && Animate->Visible) Delta = -Animate->Height;
  147. MainPanel->Top = MainPanel->Top + Delta;
  148. TransferPanel->Top = TransferPanel->Top + Delta;
  149. SpeedPanel->Top = SpeedPanel->Top + Delta;
  150. Animate->Visible = AVisible;
  151. Animate->Active = AVisible;
  152. if (TransferOperation && !TransferPanel->Visible) Delta += TransferPanel->Height;
  153. else
  154. if (!TransferOperation && TransferPanel->Visible) Delta += -TransferPanel->Height;
  155. TransferPanel->Visible = TransferOperation;
  156. SpeedPanel->Visible = TransferOperation;
  157. ClientHeight = ClientHeight + Delta;
  158. Caption = OperationName(FData.Operation, FData.Side);
  159. TargetLabel->Visible = TransferOperation;
  160. TargetPathLabel->Visible = TransferOperation;
  161. TargetPathLabel->UnixPath = (FData.Side == osLocal);
  162. FileLabel->UnixPath = (FData.Side == osRemote);
  163. FLastOperation = FData.Operation;
  164. FLastTotalSizeSet = !FData.TotalSizeSet;
  165. };
  166. if (FLastTotalSizeSet != FData.TotalSizeSet)
  167. {
  168. StartTimeLabelLabel->Visible = !FData.TotalSizeSet;
  169. StartTimeLabel->Visible = !FData.TotalSizeSet;
  170. TimeLeftLabelLabel->Visible = FData.TotalSizeSet;
  171. TimeLeftLabel->Visible = FData.TotalSizeSet;
  172. FLastTotalSizeSet = FData.TotalSizeSet;
  173. }
  174. if ((FData.Side == osRemote) || !FData.Temp)
  175. {
  176. FileLabel->Caption = FData.FileName;
  177. }
  178. else
  179. {
  180. FileLabel->Caption = ExtractFileName(FData.FileName);
  181. }
  182. int OverallProgress = FData.OverallProgress();
  183. FOperationProgress->Position = OverallProgress;
  184. FOperationProgress->Hint = FORMAT(L"%d%%", (OverallProgress));
  185. Caption = FORMAT(L"%d%% %s", (OverallProgress, OperationName(FData.Operation, FData.Side)));
  186. if (TransferOperation)
  187. {
  188. if ((FData.Side == osLocal) || !FData.Temp)
  189. {
  190. TargetPathLabel->Caption = FData.Directory;
  191. }
  192. else
  193. {
  194. TargetPathLabel->Caption = LoadStr(PROGRESS_TEMP_DIR);
  195. }
  196. StartTimeLabel->Caption = FData.StartTime.TimeString();
  197. if (FData.TotalSizeSet)
  198. {
  199. TimeLeftLabel->Caption = FormatDateTimeSpan(Configuration->TimeFormat,
  200. FData.TotalTimeLeft());
  201. }
  202. TimeElapsedLabel->Caption = FormatDateTimeSpan(Configuration->TimeFormat, FData.TimeElapsed());
  203. BytesTransferedLabel->Caption = FormatBytes(FData.TotalTransfered);
  204. CPSLabel->Caption = FORMAT(L"%s/s", (FormatBytes(FData.CPS())));
  205. FFileProgress->Position = FData.TransferProgress();
  206. FFileProgress->Hint = FORMAT(L"%d%%", (FFileProgress->Position));
  207. }
  208. }
  209. //---------------------------------------------------------------------
  210. static TDateTime DelayStartInterval(static_cast<double>(OneSecond/5));
  211. //---------------------------------------------------------------------
  212. void __fastcall TProgressForm::SetProgressData(TFileOperationProgressType & AData)
  213. {
  214. TDateTime N = Now();
  215. bool InstantUpdate = false;
  216. // workaround: to force displaing first file data immediatelly,
  217. // otherwise form dialog uses to be blank for first second
  218. // (until UpdateTimerTimer)
  219. if (FileLabel->Caption.IsEmpty() && !AData.FileName.IsEmpty())
  220. {
  221. InstantUpdate = true;
  222. }
  223. if (!FAsciiTransferChanged && FData.AsciiTransfer != AData.AsciiTransfer)
  224. {
  225. FAsciiTransferChanged = true;
  226. InstantUpdate = true;
  227. }
  228. if (!FResumeStatusChanged && FData.ResumeStatus != AData.ResumeStatus)
  229. {
  230. FResumeStatusChanged = true;
  231. InstantUpdate = true;
  232. }
  233. FData = AData;
  234. // delay showing the progress until the application is restored,
  235. // otherwise the form popups up unminimized.
  236. if (!FDataReceived && !IsApplicationMinimized() &&
  237. ((N - FStarted) > DelayStartInterval))
  238. {
  239. FDataReceived = true;
  240. // CPS limit is set set only once from TFileOperationProgressType::Start
  241. FCPSLimit = AData.CPSLimit;
  242. SpeedCombo->Text = SetSpeedLimit(FCPSLimit);
  243. ShowAsModal(this, FShowAsModalStorage);
  244. // particularly needed for the case, when we are showing the form delayed
  245. // because application was minimized when operation started
  246. InstantUpdate = true;
  247. }
  248. if (InstantUpdate)
  249. {
  250. UpdateControls();
  251. Application->ProcessMessages();
  252. }
  253. static double UpdateInterval = static_cast<double>(OneSecond/5); // 1/5 sec
  254. if ((FUpdateCounter % 5 == 0) ||
  255. (double(N) - double(FLastUpdate) > UpdateInterval))
  256. {
  257. FLastUpdate = N;
  258. FUpdateCounter = 0;
  259. Application->ProcessMessages();
  260. }
  261. FUpdateCounter++;
  262. AData.CPSLimit = FCPSLimit;
  263. }
  264. //---------------------------------------------------------------------------
  265. void __fastcall TProgressForm::UpdateTimerTimer(TObject * /*Sender*/)
  266. {
  267. if (FDataReceived) UpdateControls();
  268. }
  269. //---------------------------------------------------------------------------
  270. void __fastcall TProgressForm::FormShow(TObject * /*Sender*/)
  271. {
  272. UpdateTimer->Enabled = true;
  273. SpeedCombo->Items = CustomWinConfiguration->History[L"SpeedLimit"];
  274. if (FDataReceived) UpdateControls();
  275. FLastUpdate = 0;
  276. }
  277. //---------------------------------------------------------------------------
  278. void __fastcall TProgressForm::FormHide(TObject * /*Sender*/)
  279. {
  280. // This is to counter the "infinite" timestamp in
  281. // TTerminalManager::ApplicationShowHint.
  282. // Because if form disappears on its own, hint is not hidden.
  283. Application->CancelHint();
  284. CustomWinConfiguration->History[L"SpeedLimit"] = SpeedCombo->Items;
  285. UpdateTimer->Enabled = false;
  286. }
  287. //---------------------------------------------------------------------------
  288. void __fastcall TProgressForm::CancelButtonClick(TObject * /*Sender*/)
  289. {
  290. CancelOperation();
  291. }
  292. //---------------------------------------------------------------------------
  293. void __fastcall TProgressForm::MinimizeButtonClick(TObject * Sender)
  294. {
  295. CallGlobalMinimizeHandler(Sender);
  296. }
  297. //---------------------------------------------------------------------------
  298. void __fastcall TProgressForm::CancelOperation()
  299. {
  300. assert(FDataReceived);
  301. if (!FData.Suspended)
  302. {
  303. // mostly useless, as suspend is called over copy of actual progress data
  304. FData.Suspend();
  305. UpdateControls();
  306. try
  307. {
  308. TCancelStatus ACancel;
  309. int Result;
  310. if (FData.TransferingFile &&
  311. (FData.TimeExpected() > GUIConfiguration->IgnoreCancelBeforeFinish))
  312. {
  313. Result = MessageDialog(LoadStr(CANCEL_OPERATION_FATAL2), qtWarning,
  314. qaYes | qaNo | qaCancel, HELP_PROGRESS_CANCEL);
  315. }
  316. else
  317. {
  318. Result = MessageDialog(LoadStr(CANCEL_OPERATION2), qtConfirmation,
  319. qaOK | qaCancel, HELP_PROGRESS_CANCEL);
  320. }
  321. switch (Result) {
  322. case qaYes:
  323. ACancel = csCancelTransfer; break;
  324. case qaOK:
  325. case qaNo:
  326. ACancel = csCancel; break;
  327. default:
  328. ACancel = csContinue; break;
  329. }
  330. if (FCancel < ACancel)
  331. {
  332. FCancel = ACancel;
  333. }
  334. }
  335. __finally
  336. {
  337. FData.Resume();
  338. }
  339. }
  340. }
  341. //---------------------------------------------------------------------------
  342. void __fastcall TProgressForm::GlobalMinimize(TObject * /*Sender*/)
  343. {
  344. Application->Minimize();
  345. FMinimizedByMe = true;
  346. }
  347. //---------------------------------------------------------------------------
  348. void __fastcall TProgressForm::SetOnceDoneOperation(TOnceDoneOperation value)
  349. {
  350. int Index = 0;
  351. switch (value)
  352. {
  353. case odoIdle:
  354. Index = 0;
  355. break;
  356. case odoDisconnect:
  357. Index = 1;
  358. break;
  359. case odoShutDown:
  360. Index = 2;
  361. break;
  362. default:
  363. assert(false);
  364. }
  365. OnceDoneOperationCombo->ItemIndex = Index;
  366. OnceDoneOperationComboSelect(NULL);
  367. }
  368. //---------------------------------------------------------------------------
  369. bool __fastcall TProgressForm::GetAllowMinimize()
  370. {
  371. return MinimizeButton->Visible;
  372. }
  373. //---------------------------------------------------------------------------
  374. void __fastcall TProgressForm::SetAllowMinimize(bool value)
  375. {
  376. MinimizeButton->Visible = value;
  377. }
  378. //---------------------------------------------------------------------------
  379. void __fastcall TProgressForm::SetReadOnly(bool value)
  380. {
  381. if (FReadOnly != value)
  382. {
  383. FReadOnly = value;
  384. if (!value)
  385. {
  386. ResetOnceDoneOperation();
  387. }
  388. UpdateControls();
  389. }
  390. }
  391. //---------------------------------------------------------------------------
  392. void __fastcall TProgressForm::ApplyCPSLimit()
  393. {
  394. try
  395. {
  396. FCPSLimit = GetSpeedLimit(SpeedCombo->Text);
  397. }
  398. catch(...)
  399. {
  400. SpeedCombo->SetFocus();
  401. throw;
  402. }
  403. SpeedCombo->Text = SetSpeedLimit(FCPSLimit);
  404. // visualize application
  405. SpeedCombo->SelectAll();
  406. CancelButton->SetFocus();
  407. }
  408. //---------------------------------------------------------------------------
  409. void __fastcall TProgressForm::SpeedComboExit(TObject * /*Sender*/)
  410. {
  411. SpeedCombo->Text = SetSpeedLimit(FCPSLimit);
  412. }
  413. //---------------------------------------------------------------------------
  414. void __fastcall TProgressForm::SpeedComboSelect(TObject * /*Sender*/)
  415. {
  416. ApplyCPSLimit();
  417. }
  418. //---------------------------------------------------------------------------
  419. void __fastcall TProgressForm::SpeedComboKeyPress(TObject * /*Sender*/,
  420. wchar_t & Key)
  421. {
  422. // using OnKeyPress instead of OnKeyDown to catch "enter" prevents
  423. // system beep for unhandled key
  424. if (Key == L'\r')
  425. {
  426. Key = L'\0';
  427. ApplyCPSLimit();
  428. }
  429. }
  430. //---------------------------------------------------------------------------
  431. void __fastcall TProgressForm::ResetOnceDoneOperation()
  432. {
  433. OnceDoneOperationCombo->ItemIndex = 0;
  434. OnceDoneOperationComboSelect(NULL);
  435. }
  436. //---------------------------------------------------------------------------
  437. void __fastcall TProgressForm::OnceDoneOperationComboSelect(TObject * /*Sender*/)
  438. {
  439. switch (OnceDoneOperationCombo->ItemIndex)
  440. {
  441. case 0:
  442. FOnceDoneOperation = odoIdle;
  443. break;
  444. case 1:
  445. FOnceDoneOperation = odoDisconnect;
  446. break;
  447. case 2:
  448. FOnceDoneOperation = odoShutDown;
  449. break;
  450. default:
  451. assert(false);
  452. }
  453. }
  454. //---------------------------------------------------------------------------
  455. void __fastcall TProgressForm::OnceDoneOperationComboCloseUp(TObject * /*Sender*/)
  456. {
  457. CancelButton->SetFocus();
  458. }
  459. //---------------------------------------------------------------------------
  460. #pragma warn -8080