Progress.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. //---------------------------------------------------------------------
  2. #include <FormsPCH.h>
  3. #pragma hdrstop
  4. #include <windowsx.h>
  5. #include "Progress.h"
  6. //---------------------------------------------------------------------
  7. #pragma link "PathLabel"
  8. #pragma link "PngImageList"
  9. #pragma link "TB2Dock"
  10. #pragma link "TB2Item"
  11. #pragma link "TB2Toolbar"
  12. #pragma link "TBX"
  13. #pragma link "TB2ExtItems"
  14. #pragma link "TBXExtItems"
  15. #pragma resource "*.dfm"
  16. //---------------------------------------------------------------------
  17. static bool IsIndeterminate(const TSynchronizeProgress * SynchronizeProgress, const TFileOperationProgressType * ProgressData)
  18. {
  19. bool Result;
  20. // TSynchronizeProgress has its own way how to take atomic operations into account
  21. if (SynchronizeProgress != NULL)
  22. {
  23. Result = TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation);
  24. }
  25. else
  26. {
  27. Result = ProgressData->IsIndeterminate();
  28. }
  29. return Result;
  30. }
  31. //---------------------------------------------------------------------
  32. UnicodeString __fastcall TProgressForm::ProgressStr(
  33. const TSynchronizeProgress * SynchronizeProgress, const TFileOperationProgressType * ProgressData)
  34. {
  35. static const int Captions[] = { 0, 0, PROGRESS_DELETE,
  36. PROGRESS_SETPROPERTIES, 0, PROGRESS_CUSTOM_COMAND, PROGRESS_CALCULATE_SIZE,
  37. PROGRESS_REMOTE_MOVE, PROGRESS_REMOTE_COPY, PROGRESS_GETPROPERTIES,
  38. PROGRESS_CALCULATE_CHECKSUM, PROGRESS_LOCK, PROGRESS_UNLOCK };
  39. DebugAssert((unsigned int)ProgressData->Operation >= 1 && ((unsigned int)ProgressData->Operation - 1) < LENOF(Captions));
  40. int Id;
  41. if (ProgressData->IsTransfer())
  42. {
  43. Id = (ProgressData->Side == osLocal) ? PROGRESS_UPLOAD : PROGRESS_DOWNLOAD;
  44. }
  45. else
  46. {
  47. Id = Captions[(int)ProgressData->Operation - 1];
  48. DebugAssert(Id != 0);
  49. }
  50. UnicodeString Result = LoadStr(Id);
  51. if (SynchronizeProgress != NULL)
  52. {
  53. Result = LoadStr(SYNCHRONIZE_PROGRESS_SYNCHRONIZE2) + TitleSeparator + Result;
  54. }
  55. if (!IsIndeterminate(SynchronizeProgress, ProgressData))
  56. {
  57. int OverallProgress;
  58. if (SynchronizeProgress != NULL)
  59. {
  60. OverallProgress = SynchronizeProgress->Progress(ProgressData);
  61. }
  62. else
  63. {
  64. OverallProgress = ProgressData->OverallProgress();
  65. }
  66. Result = FORMAT(L"%d%% %s", (OverallProgress, Result));
  67. }
  68. return Result;
  69. }
  70. //---------------------------------------------------------------------
  71. __fastcall TProgressForm::TProgressForm(
  72. TComponent * AOwner, bool AllowMoveToQueue, bool AllowSkip, TSynchronizeProgress * SynchronizeProgress)
  73. : TForm(AOwner), FData()
  74. {
  75. FLastOperation = foNone;
  76. FLastSide = (TOperationSide)-1;
  77. FLastTotalSizeSet = false;
  78. FDataGot = false;
  79. FDataReceived = false;
  80. FCancel = csContinue;
  81. FMoveToQueue = false;
  82. FMinimizedByMe = false;
  83. FUpdateCounter = 0;
  84. FDeleteLocalToRecycleBin = false;
  85. FDeleteRemoteToRecycleBin = false;
  86. FReadOnly = false;
  87. FShowAsModalStorage = NULL;
  88. FStarted = Now();
  89. FModalBeginHooked = false;
  90. FModalLevel = -1;
  91. FPendingSkip = false;
  92. FSynchronizeProgress = SynchronizeProgress;
  93. FAllowSkip = AllowSkip;
  94. FWheelDelta = 0;
  95. UseSystemSettings(this);
  96. FOnceDoneItems.Add(odoIdle, IdleOnceDoneItem);
  97. FOnceDoneItems.Add(odoDisconnect, DisconnectOnceDoneItem);
  98. FOnceDoneItems.Add(odoSuspend, SuspendOnceDoneItem);
  99. FOnceDoneItems.Add(odoShutDown, ShutDownOnceDoneItem);
  100. ResetOnceDoneOperation();
  101. HideComponentsPanel(this);
  102. SelectScaledImageList(ImageList);
  103. SetGlobalMinimizeHandler(this, GlobalMinimize);
  104. MoveToQueueItem->Visible = AllowMoveToQueue;
  105. }
  106. //---------------------------------------------------------------------------
  107. __fastcall TProgressForm::~TProgressForm()
  108. {
  109. // to prevent raising assertion (e.g. IsProgress == True)
  110. FData.Clear();
  111. ClearGlobalMinimizeHandler(GlobalMinimize);
  112. if (IsApplicationMinimized() && FMinimizedByMe)
  113. {
  114. ShowNotification(
  115. NULL, MainInstructions(LoadStr(BALLOON_OPERATION_COMPLETE)),
  116. qtInformation);
  117. }
  118. ReleaseAsModal(this, FShowAsModalStorage);
  119. }
  120. //---------------------------------------------------------------------
  121. UnicodeString __fastcall TProgressForm::ProgressStr()
  122. {
  123. return FProgressStr;
  124. }
  125. //---------------------------------------------------------------------
  126. void __fastcall TProgressForm::UpdateControls()
  127. {
  128. DebugAssert((FData.Operation >= foCopy) && (FData.Operation <= foUnlock) &&
  129. (FData.Operation != foRename));
  130. bool TransferOperation = FData.IsTransfer();
  131. bool Indeterminate = IsIndeterminate(SynchronizeProgress, &FData);
  132. CancelItem->Enabled = !FReadOnly && (FCancel < csCancel);
  133. SkipItem->Visible = TransferOperation && FAllowSkip;
  134. SkipItem->Enabled = !FReadOnly && (FCancel < csCancelFile) && !FPendingSkip;
  135. MoveToQueueItem->Enabled = !FMoveToQueue && (FCancel == csContinue) && !FPendingSkip;
  136. bool HideOnceDone =
  137. FReadOnly ||
  138. (FData.Operation == foCalculateSize) ||
  139. (FData.Operation == foGetProperties) ||
  140. (FData.Operation == foCalculateChecksum) ||
  141. ((FData.Operation == foDelete) && (FData.Side == osLocal));
  142. CycleOnceDoneItem->Visible = !HideOnceDone;
  143. CycleOnceDoneItem->ImageIndex = CurrentOnceDoneItem()->ImageIndex;
  144. SpeedComboBoxItem->Visible = TransferOperation;
  145. if ((FData.Operation != FLastOperation) ||
  146. (FData.Side != FLastSide))
  147. {
  148. UnicodeString Animation;
  149. UnicodeString CancelCaption = Vcl_Consts_SMsgDlgCancel;
  150. int MoveToQueueImageIndex = -1;
  151. FPendingSkip = false; // just in case
  152. int MoveTransferToQueueImageIndex;
  153. if (FData.Side == osRemote)
  154. {
  155. MoveTransferToQueueImageIndex = 7;
  156. }
  157. else
  158. {
  159. MoveTransferToQueueImageIndex = 8;
  160. }
  161. switch (FData.Operation)
  162. {
  163. case foCopy:
  164. if (FData.Side == osRemote)
  165. {
  166. Animation = L"CopyRemote";
  167. }
  168. else
  169. {
  170. Animation = L"CopyLocal";
  171. }
  172. MoveToQueueImageIndex = MoveTransferToQueueImageIndex;
  173. break;
  174. case foMove:
  175. if (FData.Side == osRemote)
  176. {
  177. Animation = L"MoveRemote";
  178. }
  179. else
  180. {
  181. Animation = L"MoveLocal";
  182. }
  183. MoveToQueueImageIndex = MoveTransferToQueueImageIndex;
  184. break;
  185. case foDelete:
  186. Animation = ((FData.Side == osRemote) ? DeleteRemoteToRecycleBin : DeleteLocalToRecycleBin) ? L"Recycle" : L"Delete";
  187. MoveToQueueImageIndex = 10;
  188. break;
  189. case foCalculateSize:
  190. Animation = L"CalculateSize";
  191. CancelCaption = LoadStr(SKIP_BUTTON);
  192. MoveToQueueImageIndex = MoveTransferToQueueImageIndex;
  193. break;
  194. case foSetProperties:
  195. Animation = "SetProperties";
  196. break;
  197. case foRemoteCopy:
  198. Animation = "DuplicateLtoR";
  199. break;
  200. case foRemoteMove:
  201. Animation = "MoveLtoR";
  202. break;
  203. default:
  204. DebugAssert(
  205. (FData.Operation == foCustomCommand) ||
  206. (FData.Operation == foGetProperties) ||
  207. (FData.Operation == foCalculateChecksum) ||
  208. (FData.Operation == foLock) ||
  209. (FData.Operation == foUnlock));
  210. break;
  211. }
  212. CancelItem->Caption = CancelCaption;
  213. OperationProgress->Style = Indeterminate ? pbstMarquee : pbstNormal;
  214. if (SynchronizeProgress != NULL)
  215. {
  216. Animation = L"SynchronizeDirectories";
  217. }
  218. FFrameAnimation.Init(AnimationPaintBox, Animation);
  219. FFrameAnimation.Start();
  220. int Delta = 0;
  221. if (TransferOperation && !TransferPanel->Visible) Delta += TransferPanel->Height;
  222. else
  223. if (!TransferOperation && TransferPanel->Visible) Delta += -TransferPanel->Height;
  224. TransferPanel->Visible = TransferOperation;
  225. ClientHeight = ClientHeight + Delta;
  226. TargetLabel->Visible = TransferOperation;
  227. TargetPathLabel->Visible = TransferOperation;
  228. TargetPathLabel->UnixPath = (FData.Side == osLocal);
  229. FileLabel->UnixPath = (FData.Side == osRemote);
  230. PathLabel->Caption =
  231. LoadStr((FData.Operation == foCalculateSize) ? PROGRESS_PATH_LABEL : PROGRESS_FILE_LABEL);
  232. DebugAssert(!MoveToQueueItem->Visible || (MoveToQueueImageIndex >= 0));
  233. MoveToQueueItem->ImageIndex = MoveToQueueImageIndex;
  234. FLastOperation = FData.Operation;
  235. FLastSide = FData.Side;
  236. FLastTotalSizeSet = !FData.TotalSizeSet;
  237. }
  238. if (FLastTotalSizeSet != FData.TotalSizeSet)
  239. {
  240. StartTimeLabelLabel->Visible = !FData.TotalSizeSet;
  241. StartTimeLabel->Visible = !FData.TotalSizeSet;
  242. TimeLeftLabelLabel->Visible = FData.TotalSizeSet;
  243. TimeLeftLabel->Visible = FData.TotalSizeSet;
  244. FLastTotalSizeSet = FData.TotalSizeSet;
  245. }
  246. UnicodeString FileCaption;
  247. if ((FData.Operation == foCalculateSize) && DebugAlwaysTrue(!FData.Temp))
  248. {
  249. if (FData.Side == osRemote)
  250. {
  251. FileCaption = UnixExtractFileDir(FData.FullFileName);
  252. }
  253. else
  254. {
  255. FileCaption = ExtractFileDir(FData.FullFileName);
  256. }
  257. }
  258. else if ((FData.Side == osRemote) || !FData.Temp)
  259. {
  260. FileCaption = FData.FileName;
  261. }
  262. else
  263. {
  264. FileCaption = ExtractFileName(FData.FileName);
  265. }
  266. if (FileLabel->Caption != FileCaption)
  267. {
  268. FileLabel->Caption = FileCaption;
  269. FPendingSkip = false;
  270. }
  271. int OverallProgress;
  272. // as a side effect this prevents calling TSynchronizeProgress::Progress when we do not know total size yet
  273. // (what would cache wrong values forever)
  274. if (Indeterminate)
  275. {
  276. OverallProgress = -1;
  277. }
  278. else
  279. {
  280. if (SynchronizeProgress != NULL)
  281. {
  282. OverallProgress = SynchronizeProgress->Progress(&FData);
  283. }
  284. else
  285. {
  286. OverallProgress = FData.OverallProgress();
  287. }
  288. }
  289. OperationProgress->Position = std::max(0, OverallProgress);
  290. OperationProgress->Hint = (OverallProgress < 0) ? UnicodeString() : FORMAT(L"%d%%", (OverallProgress));
  291. FProgressStr = ProgressStr(SynchronizeProgress, &FData);
  292. Caption = FormatFormCaption(this, FProgressStr);
  293. if (TransferOperation)
  294. {
  295. if ((FData.Side == osLocal) || !FData.Temp)
  296. {
  297. TargetPathLabel->Caption = FData.Directory;
  298. }
  299. else
  300. {
  301. TargetPathLabel->Caption = LoadStr(PROGRESS_TEMP_DIR);
  302. }
  303. StartTimeLabel->Caption = FData.StartTime.TimeString();
  304. if (FData.TotalSizeSet)
  305. {
  306. UnicodeString TimeLeftCaption;
  307. bool CanShow = CanShowTimeEstimate(FData.StartTime);
  308. if (CanShow)
  309. {
  310. TDateTime TimeLeft;
  311. if (SynchronizeProgress != NULL)
  312. {
  313. TimeLeft = SynchronizeProgress->TimeLeft(&FData);
  314. }
  315. else
  316. {
  317. TimeLeft = FData.TotalTimeLeft();
  318. }
  319. TimeLeftCaption = FormatDateTimeSpan(TimeLeft);
  320. }
  321. else
  322. {
  323. TimeLeftCaption = LoadStr(PROGRESS_TIME_LEFT_CALCULATING);
  324. }
  325. TimeLeftLabel->Caption = TimeLeftCaption;
  326. }
  327. TDateTime Elapsed = FData.TimeElapsed();
  328. TimeElapsedLabel->Caption = FormatDateTimeSpan(Elapsed);
  329. BytesTransferredLabel->Caption = FormatBytes(FData.TotalTransferred);
  330. int CPS = FData.CPS();
  331. CPSLabel->Caption = FORMAT(L"%s/s", (FormatBytes(CPS)));
  332. FileProgress->Position = FData.TransferProgress();
  333. FileProgress->Hint = FORMAT(L"%d%%", (FileProgress->Position));
  334. }
  335. SizeToolbar(this, Toolbar, Dock, ToolbarPanel);
  336. }
  337. //---------------------------------------------------------------------
  338. void TProgressForm::SizeToolbar(TForm * Form, TTBXToolbar * Toolbar, TTBXDock * Dock, TPanel * ToolbarPanel)
  339. {
  340. // particularly to adjust to LargerToolbar
  341. Dock->ClientHeight = Toolbar->Height;
  342. int Delta = Dock->Height - ToolbarPanel->ClientHeight;
  343. if (Delta != 0)
  344. {
  345. ToolbarPanel->ClientHeight += Delta;
  346. DebugAssert(ToolbarPanel->Anchors.Contains(akBottom));
  347. ToolbarPanel->Top -= Delta; // to counter akBottom anchoring
  348. Form->ClientHeight += Delta;
  349. }
  350. }
  351. //---------------------------------------------------------------------
  352. static __int64 DelayStartInterval = MSecsPerSec / 2;
  353. static __int64 UpdateInterval = 1 * MSecsPerSec;
  354. //---------------------------------------------------------------------
  355. bool __fastcall TProgressForm::ReceiveData(bool Force, int ModalLevelOffset)
  356. {
  357. bool Result = false;
  358. if (FDataGot && !FDataReceived)
  359. {
  360. // CPS limit is set set only once from TFileOperationProgressType::Start.
  361. // Needs to be set even when data are not accepted yet, otherwise we would
  362. // write default value to FData in TProgressForm::SetProgressData
  363. FCPSLimit = FData.CPSLimit;
  364. // Never popup over dialog that appeared later than we started
  365. // (this can happen from UpdateTimerTimer when application is
  366. // restored while overwrite confirmation dialog [or any other]
  367. // is already shown).
  368. // TODO We should probably take as-modal windows into account too
  369. // (for extreme cases like restoring while reconnecting [as-modal TAuthenticateForm]).
  370. if ((FModalLevel < 0) || (Application->ModalLevel + ModalLevelOffset <= FModalLevel))
  371. {
  372. // Delay showing the progress until the application is restored,
  373. // otherwise the form popups up unminimized.
  374. // See solution in TMessageForm::CMShowingChanged.
  375. if (!IsApplicationMinimized() &&
  376. (Force || (MilliSecondsBetween(Now(), FStarted) > DelayStartInterval)))
  377. {
  378. FDataReceived = true;
  379. SpeedComboBoxItem->Text = SetSpeedLimit(FCPSLimit);
  380. ShowAsModal(this, FShowAsModalStorage);
  381. // particularly needed for the case, when we are showing the form delayed
  382. // because application was minimized when operation started
  383. Result = true;
  384. }
  385. else if (!FModalBeginHooked && DebugAlwaysTrue(FModalLevel < 0))
  386. {
  387. // record state as of time, the window should be shown,
  388. // had not we implemented delayed show
  389. ApplicationEvents->OnModalBegin = ApplicationModalBegin;
  390. FModalBeginHooked = true;
  391. FModalLevel = Application->ModalLevel;
  392. }
  393. }
  394. }
  395. return Result;
  396. }
  397. //---------------------------------------------------------------------------
  398. void __fastcall TProgressForm::ApplicationModalBegin(TObject * /*Sender*/)
  399. {
  400. // Popup before any modal dialog shows (typically overwrite confirmation,
  401. // as that popups nearly instantly, i.e. less than DelayStartInterval).
  402. // The Application->ModalLevel is already incremented, but we should treat it as
  403. // if it were not as the dialog is not created yet (so we can popup if we are not yet).
  404. ReceiveData(true, -1);
  405. }
  406. //---------------------------------------------------------------------
  407. void __fastcall TProgressForm::SetProgressData(TFileOperationProgressType & AData)
  408. {
  409. bool InstantUpdate = false;
  410. // workaround: to force displaing first file data immediately,
  411. // otherwise form dialog uses to be blank for first second
  412. // (until UpdateTimerTimer)
  413. if (FileLabel->Caption.IsEmpty() && !AData.FileName.IsEmpty())
  414. {
  415. InstantUpdate = true;
  416. }
  417. FData.AssignButKeepSuspendState(AData);
  418. FDataGot = true;
  419. if (!UpdateTimer->Enabled)
  420. {
  421. UpdateTimer->Interval = static_cast<int>(DelayStartInterval);
  422. UpdateTimer->Enabled = true;
  423. FSinceLastUpdate = 0;
  424. }
  425. if (ReceiveData(false, 0))
  426. {
  427. InstantUpdate = true;
  428. }
  429. if (InstantUpdate)
  430. {
  431. UpdateControls();
  432. Application->ProcessMessages();
  433. }
  434. if (ProcessGUI(FUpdateCounter % 5 == 0))
  435. {
  436. FUpdateCounter = 0;
  437. }
  438. FUpdateCounter++;
  439. AData.SetCPSLimit(FCPSLimit);
  440. }
  441. //---------------------------------------------------------------------------
  442. void __fastcall TProgressForm::UpdateTimerTimer(TObject * /*Sender*/)
  443. {
  444. // popup the progress window at least here, if SetProgressData is
  445. // not being called (typically this happens when using custom command
  446. // that launches long-lasting external process, such as visual diff)
  447. ReceiveData(false, 0);
  448. if (UpdateTimer->Interval == DelayStartInterval)
  449. {
  450. UpdateTimer->Interval = static_cast<int>(GUIUpdateInterval);
  451. }
  452. if (FDataReceived)
  453. {
  454. FSinceLastUpdate += UpdateTimer->Interval;
  455. if (FSinceLastUpdate >= UpdateInterval)
  456. {
  457. UpdateControls();
  458. FSinceLastUpdate = 0;
  459. }
  460. }
  461. }
  462. //---------------------------------------------------------------------------
  463. void __fastcall TProgressForm::FormShow(TObject * /*Sender*/)
  464. {
  465. CopySpeedLimits(CustomWinConfiguration->History[L"SpeedLimit"], SpeedComboBoxItem->Strings);
  466. ReceiveData(false, 0);
  467. if (FDataReceived)
  468. {
  469. UpdateControls();
  470. }
  471. // HACK: In command-line run (/upload), FormShow gets called twice,
  472. // leading to duplicate hook and memory leak. Make sure we unhook, just in case.
  473. // Calling unhook without hooking first is noop.
  474. UnhookFormActivation(this);
  475. HookFormActivation(this);
  476. }
  477. //---------------------------------------------------------------------------
  478. void __fastcall TProgressForm::FormHide(TObject * /*Sender*/)
  479. {
  480. UnhookFormActivation(this);
  481. // This is to counter the "infinite" timestamp in
  482. // TTerminalManager::ApplicationShowHint.
  483. // Because if form disappears on its own, hint is not hidden.
  484. Application->CancelHint();
  485. CustomWinConfiguration->History[L"SpeedLimit"] = SpeedComboBoxItem->Strings;
  486. UpdateTimer->Enabled = false;
  487. }
  488. //---------------------------------------------------------------------------
  489. void __fastcall TProgressForm::CancelItemClick(TObject * /*Sender*/)
  490. {
  491. CancelOperation();
  492. }
  493. //---------------------------------------------------------------------------
  494. void __fastcall TProgressForm::Minimize(TObject * Sender)
  495. {
  496. CallGlobalMinimizeHandler(Sender);
  497. }
  498. //---------------------------------------------------------------------------
  499. void __fastcall TProgressForm::MinimizeItemClick(TObject * Sender)
  500. {
  501. Minimize(Sender);
  502. }
  503. //---------------------------------------------------------------------------
  504. void __fastcall TProgressForm::CancelOperation()
  505. {
  506. DebugAssert(FDataReceived);
  507. if (!FData.Suspended)
  508. {
  509. // mostly useless, as suspend is called over copy of actual progress data
  510. FData.Suspend();
  511. UpdateControls();
  512. try
  513. {
  514. TCancelStatus ACancel;
  515. if (FData.TransferringFile &&
  516. (FData.TimeExpected() > GUIConfiguration->IgnoreCancelBeforeFinish))
  517. {
  518. int Result = MessageDialog(LoadStr(CANCEL_OPERATION_FATAL2), qtWarning,
  519. qaYes | qaNo | qaCancel, HELP_PROGRESS_CANCEL);
  520. switch (Result)
  521. {
  522. case qaYes:
  523. ACancel = csCancelTransfer; break;
  524. case qaNo:
  525. ACancel = csCancel; break;
  526. default:
  527. ACancel = csContinue; break;
  528. }
  529. }
  530. else
  531. {
  532. ACancel = csCancel;
  533. }
  534. SetCancelLower(ACancel);
  535. }
  536. __finally
  537. {
  538. FData.Resume();
  539. }
  540. }
  541. }
  542. //---------------------------------------------------------------------------
  543. void __fastcall TProgressForm::GlobalMinimize(TObject * /*Sender*/)
  544. {
  545. ApplicationMinimize();
  546. FMinimizedByMe = true;
  547. }
  548. //---------------------------------------------------------------------------
  549. TTBCustomItem * __fastcall TProgressForm::CurrentOnceDoneItem()
  550. {
  551. TOnceDoneItems::const_iterator Iterator = FOnceDoneItems.begin();
  552. while (Iterator != FOnceDoneItems.end())
  553. {
  554. if (Iterator->second->Checked)
  555. {
  556. return Iterator->second;
  557. }
  558. Iterator++;
  559. }
  560. DebugFail();
  561. return NULL;
  562. }
  563. //---------------------------------------------------------------------------
  564. TOnceDoneOperation __fastcall TProgressForm::GetOnceDoneOperation()
  565. {
  566. return FOnceDoneItems.LookupFirst(CurrentOnceDoneItem());
  567. }
  568. //---------------------------------------------------------------------------
  569. void __fastcall TProgressForm::SetOnceDoneItem(TTBCustomItem * Item)
  570. {
  571. TTBCustomItem * Current = CurrentOnceDoneItem();
  572. if (Current != Item)
  573. {
  574. Current->Checked = false;
  575. Item->Checked = true;
  576. // Not until we have any data to update.
  577. // Happens when set to odoDisconnect in command-line upload/download
  578. // mode from TCustomScpExplorerForm::FileOperationProgress.
  579. if (FDataGot)
  580. {
  581. UpdateControls();
  582. }
  583. }
  584. }
  585. //---------------------------------------------------------------------------
  586. void __fastcall TProgressForm::SetOnceDoneOperation(TOnceDoneOperation value)
  587. {
  588. SetOnceDoneItem(FOnceDoneItems.LookupSecond(value));
  589. }
  590. //---------------------------------------------------------------------------
  591. bool __fastcall TProgressForm::GetAllowMinimize()
  592. {
  593. return MinimizeItem->Visible;
  594. }
  595. //---------------------------------------------------------------------------
  596. void __fastcall TProgressForm::SetAllowMinimize(bool value)
  597. {
  598. MinimizeItem->Visible = value;
  599. }
  600. //---------------------------------------------------------------------------
  601. void __fastcall TProgressForm::SetReadOnly(bool value)
  602. {
  603. if (FReadOnly != value)
  604. {
  605. FReadOnly = value;
  606. if (!value)
  607. {
  608. ResetOnceDoneOperation();
  609. }
  610. UpdateControls();
  611. }
  612. }
  613. //---------------------------------------------------------------------------
  614. void __fastcall TProgressForm::ResetOnceDoneOperation()
  615. {
  616. SetOnceDoneOperation(odoIdle);
  617. }
  618. //---------------------------------------------------------------------------
  619. void __fastcall TProgressForm::CMDialogKey(TCMDialogKey & Message)
  620. {
  621. if (Message.CharCode == VK_TAB)
  622. {
  623. Toolbar->KeyboardOpen(L'\0', false);
  624. Message.Result = 1;
  625. }
  626. else
  627. {
  628. TForm::Dispatch(&Message);
  629. }
  630. }
  631. //---------------------------------------------------------------------------
  632. void __fastcall TProgressForm::Dispatch(void * AMessage)
  633. {
  634. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  635. if (Message.Msg == WM_CLOSE)
  636. {
  637. CancelOperation();
  638. }
  639. else if (Message.Msg == CM_DIALOGKEY)
  640. {
  641. CMDialogKey(reinterpret_cast<TCMDialogKey &>(Message));
  642. }
  643. else if (Message.Msg == WM_MANAGES_CAPTION)
  644. {
  645. Message.Result = 1;
  646. }
  647. else
  648. {
  649. TForm::Dispatch(AMessage);
  650. }
  651. }
  652. //---------------------------------------------------------------------------
  653. void __fastcall TProgressForm::MoveToQueueItemClick(TObject * /*Sender*/)
  654. {
  655. FMoveToQueue = true;
  656. UpdateControls();
  657. }
  658. //---------------------------------------------------------------------------
  659. void __fastcall TProgressForm::OnceDoneItemClick(TObject * Sender)
  660. {
  661. SetOnceDoneItem(dynamic_cast<TTBCustomItem *>(Sender));
  662. }
  663. //---------------------------------------------------------------------------
  664. void __fastcall TProgressForm::CycleOnceDoneItemClick(TObject * /*Sender*/)
  665. {
  666. TTBCustomItem * Item = CurrentOnceDoneItem();
  667. int Index = Item->Parent->IndexOf(Item);
  668. DebugAssert(Index >= 0);
  669. if (Index < Item->Parent->Count - 1)
  670. {
  671. Index++;
  672. }
  673. else
  674. {
  675. Index = 0;
  676. }
  677. SetOnceDoneItem(Item->Parent->Items[Index]);
  678. }
  679. //---------------------------------------------------------------------------
  680. UnicodeString __fastcall TProgressForm::ItemSpeed(const UnicodeString & Text,
  681. TTBXComboBoxItem * Item)
  682. {
  683. // Keep in sync with TNonVisualDataModule::QueueItemSpeed
  684. FCPSLimit = GetSpeedLimit(Text);
  685. UnicodeString Result = SetSpeedLimit(FCPSLimit);
  686. SaveToHistory(Item->Strings, Result);
  687. CustomWinConfiguration->History[L"SpeedLimit"] = Item->Strings;
  688. FWheelDelta = 0;
  689. return Result;
  690. }
  691. //---------------------------------------------------------------------------
  692. void __fastcall TProgressForm::SpeedComboBoxItemAcceptText(TObject * Sender,
  693. UnicodeString & NewText, bool & /*Accept*/)
  694. {
  695. TTBXComboBoxItem * Item = dynamic_cast<TTBXComboBoxItem *>(Sender);
  696. NewText = ItemSpeed(NewText, Item);
  697. }
  698. //---------------------------------------------------------------------------
  699. void __fastcall TProgressForm::SpeedComboBoxItemItemClick(TObject * Sender)
  700. {
  701. TTBXComboBoxItem * Item = dynamic_cast<TTBXComboBoxItem *>(Sender);
  702. Item->Text = ItemSpeed(Item->Text, Item);
  703. }
  704. //---------------------------------------------------------------------------
  705. void __fastcall TProgressForm::SpeedComboBoxItemAdjustImageIndex(
  706. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int /*AIndex*/, int & ImageIndex)
  707. {
  708. // Use fixed image (do not change image by item index)
  709. ImageIndex = Sender->ImageIndex;
  710. }
  711. //---------------------------------------------------------------------------
  712. void __fastcall TProgressForm::SpeedComboBoxItemClick(TObject * Sender)
  713. {
  714. ClickToolbarItem(DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender)), false);
  715. }
  716. //---------------------------------------------------------------------------
  717. void __fastcall TProgressForm::ClearCancel()
  718. {
  719. if (DebugAlwaysTrue(FCancel == csCancelFile))
  720. {
  721. FPendingSkip = true;
  722. }
  723. FCancel = csContinue;
  724. UpdateControls();
  725. }
  726. //---------------------------------------------------------------------------
  727. void __fastcall TProgressForm::SetCancelLower(TCancelStatus ACancel)
  728. {
  729. if (FCancel < ACancel)
  730. {
  731. FCancel = ACancel;
  732. UpdateControls();
  733. }
  734. }
  735. //---------------------------------------------------------------------------
  736. void __fastcall TProgressForm::SkipItemClick(TObject * /*Sender*/)
  737. {
  738. SetCancelLower(csCancelFile);
  739. }
  740. //---------------------------------------------------------------------------
  741. void __fastcall TProgressForm::MouseWheelHandler(TMessage & Message)
  742. {
  743. int X = GET_X_LPARAM(Message.LParam);
  744. int Y = GET_Y_LPARAM(Message.LParam);
  745. TPoint P = Toolbar->ScreenToClient(TPoint(X, Y));
  746. if (Toolbar->ClientRect.Contains(P))
  747. {
  748. TTBItemViewer * Viewer = Toolbar->View->Find(SpeedComboBoxItem);
  749. if (Viewer->BoundsRect.Contains(P))
  750. {
  751. int Delta = GET_WHEEL_DELTA_WPARAM(Message.WParam);
  752. FWheelDelta += Delta;
  753. unsigned long CurrentSpeed = GetSpeedLimit(SpeedComboBoxItem->Text);
  754. unsigned long Speed = CurrentSpeed;
  755. unsigned int CPS = FData.CPS();
  756. int Step = 4 * WHEEL_DELTA;
  757. if (FWheelDelta > 0)
  758. {
  759. while (FWheelDelta > Step)
  760. {
  761. if (Speed > 0)
  762. {
  763. Speed *= 2;
  764. if (Speed > std::max(MaxSpeed, static_cast<unsigned long>(CPS) * 2))
  765. {
  766. Speed = 0;
  767. }
  768. }
  769. FWheelDelta -= Step;
  770. }
  771. }
  772. else if (FWheelDelta < 0)
  773. {
  774. while (FWheelDelta < -Step)
  775. {
  776. if (Speed == 0)
  777. {
  778. Speed = 8;
  779. while (Speed * 2 < CPS)
  780. {
  781. Speed *= 2;
  782. }
  783. }
  784. else
  785. {
  786. if (Speed > MinSpeed)
  787. {
  788. Speed /= 2;
  789. }
  790. }
  791. FWheelDelta += Step;
  792. }
  793. }
  794. if (Speed != CurrentSpeed)
  795. {
  796. TTBEditItemViewer * EditViewer = dynamic_cast<TTBEditItemViewer *>(Viewer);
  797. if (EditViewer->EditControl != NULL)
  798. {
  799. EditViewer->View->CancelMode();
  800. }
  801. FCPSLimit = Speed;
  802. SpeedComboBoxItem->Text = SetSpeedLimit(Speed);
  803. }
  804. Message.Result = 1;
  805. }
  806. }
  807. TForm::MouseWheelHandler(Message);
  808. }
  809. //---------------------------------------------------------------------------
  810. void __fastcall TProgressForm::CreateWnd()
  811. {
  812. TForm::CreateWnd();
  813. ApplyColorMode(this);
  814. Toolbar->Color = Color;
  815. }
  816. //---------------------------------------------------------------------------