Progress.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. CustomWinConfiguration->History["SpeedLimit"] = SpeedCombo->Items;
  266. UpdateTimer->Enabled = false;
  267. }
  268. //---------------------------------------------------------------------------
  269. void __fastcall TProgressForm::CancelButtonClick(TObject * /*Sender*/)
  270. {
  271. CancelOperation();
  272. }
  273. //---------------------------------------------------------------------------
  274. void __fastcall TProgressForm::MinimizeButtonClick(TObject * Sender)
  275. {
  276. GetGlobalMinimizeHandler()(Sender);
  277. }
  278. //---------------------------------------------------------------------------
  279. void __fastcall TProgressForm::CancelOperation()
  280. {
  281. // partially duplicated in TWinSCPFileSystem::CancelConfiguration (far\WinSCPFileSystem)
  282. assert(FDataReceived);
  283. if (!FData.Suspended)
  284. {
  285. // mostly useless, as suspend is called over copy of actual progress data
  286. FData.Suspend();
  287. UpdateControls();
  288. try
  289. {
  290. TCancelStatus ACancel;
  291. int Result;
  292. if (FData.TransferingFile &&
  293. (FData.TimeExpected() > GUIConfiguration->IgnoreCancelBeforeFinish))
  294. {
  295. Result = MessageDialog(LoadStr(CANCEL_OPERATION_FATAL), qtWarning,
  296. qaYes | qaNo | qaCancel, HELP_PROGRESS_CANCEL);
  297. }
  298. else
  299. {
  300. Result = MessageDialog(LoadStr(CANCEL_OPERATION), qtConfirmation,
  301. qaOK | qaCancel, HELP_PROGRESS_CANCEL);
  302. }
  303. switch (Result) {
  304. case qaYes:
  305. ACancel = csCancelTransfer; break;
  306. case qaOK:
  307. case qaNo:
  308. ACancel = csCancel; break;
  309. default:
  310. ACancel = csContinue; break;
  311. }
  312. if (FCancel < ACancel)
  313. {
  314. FCancel = ACancel;
  315. }
  316. }
  317. __finally
  318. {
  319. FData.Resume();
  320. }
  321. }
  322. }
  323. //---------------------------------------------------------------------------
  324. void __fastcall TProgressForm::MinimizeApp()
  325. {
  326. Application->Minimize();
  327. FMinimizedByMe = true;
  328. }
  329. //---------------------------------------------------------------------------
  330. void __fastcall TProgressForm::GlobalMinimize(TObject * /*Sender*/)
  331. {
  332. MinimizeApp();
  333. }
  334. //---------------------------------------------------------------------------
  335. void __fastcall TProgressForm::SetOnceDoneOperation(TOnceDoneOperation value)
  336. {
  337. int Index = 0;
  338. switch (value)
  339. {
  340. case odoIdle:
  341. Index = 0;
  342. break;
  343. case odoDisconnect:
  344. Index = 1;
  345. break;
  346. case odoShutDown:
  347. Index = 2;
  348. break;
  349. default:
  350. assert(false);
  351. }
  352. OnceDoneOperationCombo->ItemIndex = Index;
  353. OnceDoneOperationComboSelect(NULL);
  354. }
  355. //---------------------------------------------------------------------------
  356. bool __fastcall TProgressForm::GetAllowMinimize()
  357. {
  358. return MinimizeButton->Visible;
  359. }
  360. //---------------------------------------------------------------------------
  361. void __fastcall TProgressForm::SetAllowMinimize(bool value)
  362. {
  363. MinimizeButton->Visible = value;
  364. }
  365. //---------------------------------------------------------------------------
  366. void __fastcall TProgressForm::SetReadOnly(bool value)
  367. {
  368. if (FReadOnly != value)
  369. {
  370. FReadOnly = value;
  371. if (!value)
  372. {
  373. ResetOnceDoneOperation();
  374. }
  375. UpdateControls();
  376. }
  377. }
  378. //---------------------------------------------------------------------------
  379. void __fastcall TProgressForm::ApplyCPSLimit()
  380. {
  381. try
  382. {
  383. FCPSLimit = GetSpeedLimit(SpeedCombo->Text);
  384. }
  385. catch(...)
  386. {
  387. SpeedCombo->SetFocus();
  388. throw;
  389. }
  390. SpeedCombo->Text = SetSpeedLimit(FCPSLimit);
  391. // visualize application
  392. SpeedCombo->SelectAll();
  393. CancelButton->SetFocus();
  394. }
  395. //---------------------------------------------------------------------------
  396. void __fastcall TProgressForm::SpeedComboExit(TObject * /*Sender*/)
  397. {
  398. SpeedCombo->Text = SetSpeedLimit(FCPSLimit);
  399. }
  400. //---------------------------------------------------------------------------
  401. void __fastcall TProgressForm::SpeedComboSelect(TObject * /*Sender*/)
  402. {
  403. ApplyCPSLimit();
  404. }
  405. //---------------------------------------------------------------------------
  406. void __fastcall TProgressForm::SpeedComboKeyPress(TObject * /*Sender*/,
  407. char & Key)
  408. {
  409. // using OnKeyPress instead of OnKeyDown to catch "enter" prevents
  410. // system beep for unhandled key
  411. if (Key == '\r')
  412. {
  413. Key = '\0';
  414. ApplyCPSLimit();
  415. }
  416. }
  417. //---------------------------------------------------------------------------
  418. void __fastcall TProgressForm::ResetOnceDoneOperation()
  419. {
  420. OnceDoneOperationCombo->ItemIndex = 0;
  421. OnceDoneOperationComboSelect(NULL);
  422. }
  423. //---------------------------------------------------------------------------
  424. void __fastcall TProgressForm::OnceDoneOperationComboSelect(TObject * /*Sender*/)
  425. {
  426. switch (OnceDoneOperationCombo->ItemIndex)
  427. {
  428. case 0:
  429. FOnceDoneOperation = odoIdle;
  430. break;
  431. case 1:
  432. FOnceDoneOperation = odoDisconnect;
  433. break;
  434. case 2:
  435. FOnceDoneOperation = odoShutDown;
  436. break;
  437. default:
  438. assert(false);
  439. }
  440. }
  441. //---------------------------------------------------------------------------
  442. void __fastcall TProgressForm::OnceDoneOperationComboCloseUp(TObject * /*Sender*/)
  443. {
  444. CancelButton->SetFocus();
  445. }
  446. //---------------------------------------------------------------------------