Progress.cpp 26 KB

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