1
0

Progress.cpp 28 KB

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