Progress.cpp 27 KB

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