Progress.cpp 28 KB

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