Progress.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. if (CanShowTimeEstimate(FData.StartTime))
  320. {
  321. TDateTime TimeLeft;
  322. if (SynchronizeProgress != NULL)
  323. {
  324. TimeLeft = SynchronizeProgress->TimeLeft(&FData);
  325. }
  326. else
  327. {
  328. TimeLeft = FData.TotalTimeLeft();
  329. }
  330. TimeLeftCaption = FormatDateTimeSpan(Configuration->TimeFormat, TimeLeft);
  331. }
  332. else
  333. {
  334. TimeLeftCaption = LoadStr(PROGRESS_TIME_LEFT_CALCULATING);
  335. }
  336. TimeLeftLabel->Caption = TimeLeftCaption;
  337. }
  338. TimeElapsedLabel->Caption = FormatDateTimeSpan(Configuration->TimeFormat, FData.TimeElapsed());
  339. BytesTransferredLabel->Caption = FormatBytes(FData.TotalTransferred);
  340. CPSLabel->Caption = FORMAT(L"%s/s", (FormatBytes(FData.CPS())));
  341. FileProgress->Position = FData.TransferProgress();
  342. FileProgress->Hint = FORMAT(L"%d%%", (FileProgress->Position));
  343. }
  344. }
  345. //---------------------------------------------------------------------
  346. static __int64 DelayStartInterval = MSecsPerSec / 2;
  347. static __int64 UpdateInterval = 1 * MSecsPerSec;
  348. //---------------------------------------------------------------------
  349. bool __fastcall TProgressForm::ReceiveData(bool Force, int ModalLevelOffset)
  350. {
  351. bool Result = false;
  352. if (FDataGot && !FDataReceived)
  353. {
  354. // CPS limit is set set only once from TFileOperationProgressType::Start.
  355. // Needs to be set even when data are not accepted yet, otherwise we would
  356. // write default value to FData in TProgressForm::SetProgressData
  357. FCPSLimit = FData.CPSLimit;
  358. // Never popup over dialog that appeared later than we started
  359. // (this can happen from UpdateTimerTimer when application is
  360. // restored while overwrite confirmation dialog [or any other]
  361. // is already shown).
  362. // TODO We should probably take as-modal windows into account too
  363. // (for extreme cases like restoring while reconnecting [as-modal TAuthenticateForm]).
  364. if ((FModalLevel < 0) || (Application->ModalLevel + ModalLevelOffset <= FModalLevel))
  365. {
  366. // Delay showing the progress until the application is restored,
  367. // otherwise the form popups up unminimized.
  368. // See solution in TMessageForm::CMShowingChanged.
  369. if (!IsApplicationMinimized() &&
  370. (Force || (MilliSecondsBetween(Now(), FStarted) > DelayStartInterval)))
  371. {
  372. FDataReceived = true;
  373. SpeedComboBoxItem->Text = SetSpeedLimit(FCPSLimit);
  374. ShowAsModal(this, FShowAsModalStorage);
  375. // particularly needed for the case, when we are showing the form delayed
  376. // because application was minimized when operation started
  377. Result = true;
  378. }
  379. else if (!FModalBeginHooked && DebugAlwaysTrue(FModalLevel < 0))
  380. {
  381. // record state as of time, the window should be shown,
  382. // had not we implemented delayed show
  383. ApplicationEvents->OnModalBegin = ApplicationModalBegin;
  384. FModalBeginHooked = true;
  385. FModalLevel = Application->ModalLevel;
  386. }
  387. }
  388. }
  389. return Result;
  390. }
  391. //---------------------------------------------------------------------------
  392. void __fastcall TProgressForm::ApplicationModalBegin(TObject * /*Sender*/)
  393. {
  394. // Popup before any modal dialog shows (typically overwrite confirmation,
  395. // as that popups nearly instantly, i.e. less than DelayStartInterval).
  396. // The Application->ModalLevel is already incremented, but we should treat it as
  397. // if it were not as the dialog is not created yet (so we can popup if we are not yet).
  398. ReceiveData(true, -1);
  399. }
  400. //---------------------------------------------------------------------
  401. void __fastcall TProgressForm::SetProgressData(TFileOperationProgressType & AData)
  402. {
  403. bool InstantUpdate = false;
  404. // workaround: to force displaing first file data immediately,
  405. // otherwise form dialog uses to be blank for first second
  406. // (until UpdateTimerTimer)
  407. if (FileLabel->Caption.IsEmpty() && !AData.FileName.IsEmpty())
  408. {
  409. InstantUpdate = true;
  410. }
  411. FData.AssignButKeepSuspendState(AData);
  412. FDataGot = true;
  413. if (!UpdateTimer->Enabled)
  414. {
  415. UpdateTimer->Interval = static_cast<int>(DelayStartInterval);
  416. UpdateTimer->Enabled = true;
  417. FSinceLastUpdate = 0;
  418. }
  419. if (ReceiveData(false, 0))
  420. {
  421. InstantUpdate = true;
  422. }
  423. if (InstantUpdate)
  424. {
  425. UpdateControls();
  426. Application->ProcessMessages();
  427. }
  428. if (ProcessGUI(FUpdateCounter % 5 == 0))
  429. {
  430. FUpdateCounter = 0;
  431. }
  432. FUpdateCounter++;
  433. AData.SetCPSLimit(FCPSLimit);
  434. }
  435. //---------------------------------------------------------------------------
  436. void __fastcall TProgressForm::UpdateTimerTimer(TObject * /*Sender*/)
  437. {
  438. // popup the progress window at least here, if SetProgressData is
  439. // not being called (typically this happens when using custom command
  440. // that launches long-lasting external process, such as visual diff)
  441. ReceiveData(false, 0);
  442. if (UpdateTimer->Interval == DelayStartInterval)
  443. {
  444. UpdateTimer->Interval = static_cast<int>(GUIUpdateInterval);
  445. }
  446. if (FDataReceived)
  447. {
  448. FSinceLastUpdate += UpdateTimer->Interval;
  449. if (FSinceLastUpdate >= UpdateInterval)
  450. {
  451. UpdateControls();
  452. FSinceLastUpdate = 0;
  453. }
  454. }
  455. }
  456. //---------------------------------------------------------------------------
  457. void __fastcall TProgressForm::FormShow(TObject * /*Sender*/)
  458. {
  459. CopySpeedLimits(CustomWinConfiguration->History[L"SpeedLimit"], SpeedComboBoxItem->Strings);
  460. ReceiveData(false, 0);
  461. if (FDataReceived)
  462. {
  463. UpdateControls();
  464. }
  465. // HACK: In command-line run (/upload), FormShow gets called twice,
  466. // leading to duplicate hook and memory leak. Make sure we unhook, just in case.
  467. // Calling unhook without hooking first is noop.
  468. UnhookFormActivation(this);
  469. HookFormActivation(this);
  470. }
  471. //---------------------------------------------------------------------------
  472. void __fastcall TProgressForm::FormHide(TObject * /*Sender*/)
  473. {
  474. UnhookFormActivation(this);
  475. // This is to counter the "infinite" timestamp in
  476. // TTerminalManager::ApplicationShowHint.
  477. // Because if form disappears on its own, hint is not hidden.
  478. Application->CancelHint();
  479. CustomWinConfiguration->History[L"SpeedLimit"] = SpeedComboBoxItem->Strings;
  480. UpdateTimer->Enabled = false;
  481. }
  482. //---------------------------------------------------------------------------
  483. void __fastcall TProgressForm::CancelItemClick(TObject * /*Sender*/)
  484. {
  485. CancelOperation();
  486. }
  487. //---------------------------------------------------------------------------
  488. void __fastcall TProgressForm::Minimize(TObject * Sender)
  489. {
  490. CallGlobalMinimizeHandler(Sender);
  491. }
  492. //---------------------------------------------------------------------------
  493. void __fastcall TProgressForm::MinimizeItemClick(TObject * Sender)
  494. {
  495. Minimize(Sender);
  496. }
  497. //---------------------------------------------------------------------------
  498. void __fastcall TProgressForm::CancelOperation()
  499. {
  500. DebugAssert(FDataReceived);
  501. if (!FData.Suspended)
  502. {
  503. // mostly useless, as suspend is called over copy of actual progress data
  504. FData.Suspend();
  505. UpdateControls();
  506. try
  507. {
  508. TCancelStatus ACancel;
  509. if (FData.TransferringFile &&
  510. (FData.TimeExpected() > GUIConfiguration->IgnoreCancelBeforeFinish))
  511. {
  512. int Result = MessageDialog(LoadStr(CANCEL_OPERATION_FATAL2), qtWarning,
  513. qaYes | qaNo | qaCancel, HELP_PROGRESS_CANCEL);
  514. switch (Result)
  515. {
  516. case qaYes:
  517. ACancel = csCancelTransfer; break;
  518. case qaNo:
  519. ACancel = csCancel; break;
  520. default:
  521. ACancel = csContinue; break;
  522. }
  523. }
  524. else
  525. {
  526. ACancel = csCancel;
  527. }
  528. SetCancelLower(ACancel);
  529. }
  530. __finally
  531. {
  532. FData.Resume();
  533. }
  534. }
  535. }
  536. //---------------------------------------------------------------------------
  537. void __fastcall TProgressForm::GlobalMinimize(TObject * /*Sender*/)
  538. {
  539. ApplicationMinimize();
  540. FMinimizedByMe = true;
  541. }
  542. //---------------------------------------------------------------------------
  543. TTBCustomItem * __fastcall TProgressForm::CurrentOnceDoneItem()
  544. {
  545. TOnceDoneItems::const_iterator Iterator = FOnceDoneItems.begin();
  546. while (Iterator != FOnceDoneItems.end())
  547. {
  548. if (Iterator->second->Checked)
  549. {
  550. return Iterator->second;
  551. }
  552. Iterator++;
  553. }
  554. DebugFail();
  555. return NULL;
  556. }
  557. //---------------------------------------------------------------------------
  558. TOnceDoneOperation __fastcall TProgressForm::GetOnceDoneOperation()
  559. {
  560. return FOnceDoneItems.LookupFirst(CurrentOnceDoneItem());
  561. }
  562. //---------------------------------------------------------------------------
  563. void __fastcall TProgressForm::SetOnceDoneItem(TTBCustomItem * Item)
  564. {
  565. TTBCustomItem * Current = CurrentOnceDoneItem();
  566. if (Current != Item)
  567. {
  568. Current->Checked = false;
  569. Item->Checked = true;
  570. // Not until we have any data to update.
  571. // Happens when set to odoDisconnect in command-line upload/download
  572. // mode from TCustomScpExplorerForm::FileOperationProgress.
  573. if (FDataGot)
  574. {
  575. UpdateControls();
  576. }
  577. }
  578. }
  579. //---------------------------------------------------------------------------
  580. void __fastcall TProgressForm::SetOnceDoneOperation(TOnceDoneOperation value)
  581. {
  582. SetOnceDoneItem(FOnceDoneItems.LookupSecond(value));
  583. }
  584. //---------------------------------------------------------------------------
  585. bool __fastcall TProgressForm::GetAllowMinimize()
  586. {
  587. return MinimizeItem->Visible;
  588. }
  589. //---------------------------------------------------------------------------
  590. void __fastcall TProgressForm::SetAllowMinimize(bool value)
  591. {
  592. MinimizeItem->Visible = value;
  593. }
  594. //---------------------------------------------------------------------------
  595. void __fastcall TProgressForm::SetReadOnly(bool value)
  596. {
  597. if (FReadOnly != value)
  598. {
  599. FReadOnly = value;
  600. if (!value)
  601. {
  602. ResetOnceDoneOperation();
  603. }
  604. UpdateControls();
  605. }
  606. }
  607. //---------------------------------------------------------------------------
  608. void __fastcall TProgressForm::ResetOnceDoneOperation()
  609. {
  610. SetOnceDoneOperation(odoIdle);
  611. }
  612. //---------------------------------------------------------------------------
  613. void __fastcall TProgressForm::CMDialogKey(TCMDialogKey & Message)
  614. {
  615. if (Message.CharCode == VK_TAB)
  616. {
  617. Toolbar->KeyboardOpen(L'\0', false);
  618. Message.Result = 1;
  619. }
  620. else
  621. {
  622. TForm::Dispatch(&Message);
  623. }
  624. }
  625. //---------------------------------------------------------------------------
  626. void __fastcall TProgressForm::Dispatch(void * AMessage)
  627. {
  628. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  629. if (Message.Msg == WM_CLOSE)
  630. {
  631. CancelOperation();
  632. }
  633. else if (Message.Msg == CM_DIALOGKEY)
  634. {
  635. CMDialogKey(reinterpret_cast<TCMDialogKey &>(Message));
  636. }
  637. else if (Message.Msg == WM_MANAGES_CAPTION)
  638. {
  639. Message.Result = 1;
  640. }
  641. else
  642. {
  643. TForm::Dispatch(AMessage);
  644. }
  645. }
  646. //---------------------------------------------------------------------------
  647. void __fastcall TProgressForm::MoveToQueueItemClick(TObject * /*Sender*/)
  648. {
  649. FMoveToQueue = true;
  650. UpdateControls();
  651. }
  652. //---------------------------------------------------------------------------
  653. void __fastcall TProgressForm::OnceDoneItemClick(TObject * Sender)
  654. {
  655. SetOnceDoneItem(dynamic_cast<TTBCustomItem *>(Sender));
  656. }
  657. //---------------------------------------------------------------------------
  658. void __fastcall TProgressForm::CycleOnceDoneItemClick(TObject * /*Sender*/)
  659. {
  660. TTBCustomItem * Item = CurrentOnceDoneItem();
  661. int Index = Item->Parent->IndexOf(Item);
  662. DebugAssert(Index >= 0);
  663. if (Index < Item->Parent->Count - 1)
  664. {
  665. Index++;
  666. }
  667. else
  668. {
  669. Index = 0;
  670. }
  671. SetOnceDoneItem(Item->Parent->Items[Index]);
  672. }
  673. //---------------------------------------------------------------------------
  674. UnicodeString __fastcall TProgressForm::ItemSpeed(const UnicodeString & Text,
  675. TTBXComboBoxItem * Item)
  676. {
  677. // Keep in sync with TNonVisualDataModule::QueueItemSpeed
  678. FCPSLimit = GetSpeedLimit(Text);
  679. UnicodeString Result = SetSpeedLimit(FCPSLimit);
  680. SaveToHistory(Item->Strings, Result);
  681. CustomWinConfiguration->History[L"SpeedLimit"] = Item->Strings;
  682. FWheelDelta = 0;
  683. return Result;
  684. }
  685. //---------------------------------------------------------------------------
  686. void __fastcall TProgressForm::SpeedComboBoxItemAcceptText(TObject * Sender,
  687. UnicodeString & NewText, bool & /*Accept*/)
  688. {
  689. TTBXComboBoxItem * Item = dynamic_cast<TTBXComboBoxItem *>(Sender);
  690. NewText = ItemSpeed(NewText, Item);
  691. }
  692. //---------------------------------------------------------------------------
  693. void __fastcall TProgressForm::SpeedComboBoxItemItemClick(TObject * Sender)
  694. {
  695. TTBXComboBoxItem * Item = dynamic_cast<TTBXComboBoxItem *>(Sender);
  696. Item->Text = ItemSpeed(Item->Text, Item);
  697. }
  698. //---------------------------------------------------------------------------
  699. void __fastcall TProgressForm::SpeedComboBoxItemAdjustImageIndex(
  700. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int /*AIndex*/, int & ImageIndex)
  701. {
  702. // Use fixed image (do not change image by item index)
  703. ImageIndex = Sender->ImageIndex;
  704. }
  705. //---------------------------------------------------------------------------
  706. void __fastcall TProgressForm::SpeedComboBoxItemClick(TObject * Sender)
  707. {
  708. ClickToolbarItem(DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender)), false);
  709. }
  710. //---------------------------------------------------------------------------
  711. void __fastcall TProgressForm::ClearCancel()
  712. {
  713. if (DebugAlwaysTrue(FCancel == csCancelFile))
  714. {
  715. FPendingSkip = true;
  716. }
  717. FCancel = csContinue;
  718. UpdateControls();
  719. }
  720. //---------------------------------------------------------------------------
  721. void __fastcall TProgressForm::SetCancelLower(TCancelStatus ACancel)
  722. {
  723. if (FCancel < ACancel)
  724. {
  725. FCancel = ACancel;
  726. UpdateControls();
  727. }
  728. }
  729. //---------------------------------------------------------------------------
  730. void __fastcall TProgressForm::SkipItemClick(TObject * /*Sender*/)
  731. {
  732. SetCancelLower(csCancelFile);
  733. }
  734. //---------------------------------------------------------------------------
  735. void __fastcall TProgressForm::MouseWheelHandler(TMessage & Message)
  736. {
  737. int X = GET_X_LPARAM(Message.LParam);
  738. int Y = GET_Y_LPARAM(Message.LParam);
  739. TPoint P = Toolbar->ScreenToClient(TPoint(X, Y));
  740. if (Toolbar->ClientRect.Contains(P))
  741. {
  742. TTBItemViewer * Viewer = Toolbar->View->Find(SpeedComboBoxItem);
  743. if (Viewer->BoundsRect.Contains(P))
  744. {
  745. int Delta = GET_WHEEL_DELTA_WPARAM(Message.WParam);
  746. FWheelDelta += Delta;
  747. unsigned long CurrentSpeed = GetSpeedLimit(SpeedComboBoxItem->Text);
  748. unsigned long Speed = CurrentSpeed;
  749. unsigned int CPS = FData.CPS();
  750. int Step = 4 * WHEEL_DELTA;
  751. if (FWheelDelta > 0)
  752. {
  753. while (FWheelDelta > Step)
  754. {
  755. if (Speed > 0)
  756. {
  757. Speed *= 2;
  758. if (Speed > std::max(MaxSpeed, static_cast<unsigned long>(CPS) * 2))
  759. {
  760. Speed = 0;
  761. }
  762. }
  763. FWheelDelta -= Step;
  764. }
  765. }
  766. else if (FWheelDelta < 0)
  767. {
  768. while (FWheelDelta < -Step)
  769. {
  770. if (Speed == 0)
  771. {
  772. Speed = 8;
  773. while (Speed * 2 < CPS)
  774. {
  775. Speed *= 2;
  776. }
  777. }
  778. else
  779. {
  780. if (Speed > MinSpeed)
  781. {
  782. Speed /= 2;
  783. }
  784. }
  785. FWheelDelta += Step;
  786. }
  787. }
  788. if (Speed != CurrentSpeed)
  789. {
  790. TTBEditItemViewer * EditViewer = dynamic_cast<TTBEditItemViewer *>(Viewer);
  791. if (EditViewer->EditControl != NULL)
  792. {
  793. EditViewer->View->CancelMode();
  794. }
  795. FCPSLimit = Speed;
  796. SpeedComboBoxItem->Text = SetSpeedLimit(Speed);
  797. }
  798. Message.Result = 1;
  799. }
  800. }
  801. TForm::MouseWheelHandler(Message);
  802. }
  803. //---------------------------------------------------------------------------