Progress.cpp 27 KB

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