Progress.cpp 26 KB

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