Progress.cpp 14 KB

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