Progress.cpp 15 KB

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