Progress.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 "Progress.h"
  13. //---------------------------------------------------------------------
  14. #pragma link "PathLabel"
  15. #pragma link "HistoryComboBox"
  16. #ifndef NO_RESOURCES
  17. #pragma resource "*.dfm"
  18. #endif
  19. //---------------------------------------------------------------------
  20. AnsiString __fastcall TProgressForm::OperationName(TFileOperation Operation)
  21. {
  22. static const int Captions[] = { PROGRESS_COPY, PROGRESS_MOVE, PROGRESS_DELETE,
  23. PROGRESS_SETPROPERTIES, 0, PROGRESS_CUSTOM_COMAND, PROGRESS_CALCULATE_SIZE,
  24. PROGRESS_REMOTE_MOVE, PROGRESS_REMOTE_COPY, PROGRESS_GETPROPERTIES,
  25. PROGRESS_CALCULATE_CHECKSUM };
  26. assert((int)Operation >= 1 && ((int)Operation - 1) < LENOF(Captions));
  27. return LoadStr(Captions[(int)Operation - 1]);
  28. }
  29. //---------------------------------------------------------------------
  30. __fastcall TProgressForm::TProgressForm(TComponent* AOwner)
  31. : FData(), TForm(AOwner)
  32. {
  33. FLastOperation = foNone;
  34. FLastTotalSizeSet = false;
  35. FDataReceived = false;
  36. FAsciiTransferChanged = false;
  37. FResumeStatusChanged = false;
  38. FCancel = csContinue;
  39. FMinimizedByMe = false;
  40. FUpdateCounter = 0;
  41. FLastUpdate = 0;
  42. FDeleteToRecycleBin = false;
  43. FReadOnly = false;
  44. FShowAsModalStorage = NULL;
  45. UseSystemSettings(this);
  46. if (!IsGlobalMinimizeHandler())
  47. {
  48. SetGlobalMinimizeHandler(GlobalMinimize);
  49. };
  50. }
  51. //---------------------------------------------------------------------------
  52. __fastcall TProgressForm::~TProgressForm()
  53. {
  54. // to prevent raising assertion (e.g. IsProgress == True)
  55. FData.Clear();
  56. if (GetGlobalMinimizeHandler() == GlobalMinimize)
  57. {
  58. SetGlobalMinimizeHandler(NULL);
  59. }
  60. if (IsIconic(Application->Handle) && FMinimizedByMe)
  61. {
  62. ShowNotification(NULL, LoadStr(BALLOON_OPERATION_COMPLETE), qtInformation);
  63. }
  64. ReleaseAsModal(this, FShowAsModalStorage);
  65. }
  66. //---------------------------------------------------------------------
  67. void __fastcall TProgressForm::UpdateControls()
  68. {
  69. assert((FData.Operation >= foCopy) && (FData.Operation <= foCalculateChecksum) &&
  70. FData.Operation != foRename );
  71. CancelButton->Enabled = !FReadOnly;
  72. DisconnectWhenCompleteCheck->Enabled =
  73. !FReadOnly && (FData.Operation != foCalculateSize) &&
  74. (FData.Operation != foGetProperties) &&
  75. (FData.Operation != foCalculateChecksum);
  76. bool TransferOperation =
  77. ((FData.Operation == foCopy) || (FData.Operation == foMove));
  78. if (FData.Operation != FLastOperation)
  79. {
  80. bool AVisible;
  81. THandle ShellModule;
  82. try
  83. {
  84. AVisible = true;
  85. switch (FData.Operation) {
  86. case foCopy:
  87. case foMove:
  88. case foRemoteMove:
  89. case foRemoteCopy:
  90. if (FData.Count == 1) Animate->CommonAVI = aviCopyFile;
  91. else Animate->CommonAVI = aviCopyFiles;
  92. break;
  93. case foDelete:
  94. Animate->CommonAVI = (DeleteToRecycleBin ? aviRecycleFile : aviDeleteFile);
  95. break;
  96. case foSetProperties:
  97. case foGetProperties:
  98. ShellModule = SafeLoadLibrary("shell32.dll");
  99. if (!ShellModule)
  100. {
  101. Abort();
  102. }
  103. // workaround, VCL is not able to set both ResId and ResHandle otherwise
  104. Animate->Active = false;
  105. Animate->ResHandle = 0;
  106. Animate->ComponentState << csLoading;
  107. Animate->ResId = 165;
  108. Animate->ResHandle = ShellModule;
  109. Animate->ComponentState >> csLoading;
  110. Animate->Active = true;
  111. break;
  112. default:
  113. assert(FData.Operation == foCustomCommand ||
  114. FData.Operation == foCalculateSize ||
  115. FData.Operation == foCalculateChecksum);
  116. Animate->CommonAVI = aviNone;
  117. AVisible = false;
  118. }
  119. }
  120. catch (...)
  121. {
  122. AVisible = false;
  123. };
  124. int Delta = 0;
  125. if (AVisible && !Animate->Visible) Delta = Animate->Height;
  126. else
  127. if (!AVisible && Animate->Visible) Delta = -Animate->Height;
  128. MainPanel->Top = MainPanel->Top + Delta;
  129. TransferPanel->Top = TransferPanel->Top + Delta;
  130. SpeedPanel->Top = SpeedPanel->Top + Delta;
  131. Animate->Visible = AVisible;
  132. Animate->Active = AVisible;
  133. if (TransferOperation && !TransferPanel->Visible) Delta += TransferPanel->Height;
  134. else
  135. if (!TransferOperation && TransferPanel->Visible) Delta += -TransferPanel->Height;
  136. TransferPanel->Visible = TransferOperation;
  137. SpeedPanel->Visible = TransferOperation;
  138. ClientHeight = ClientHeight + Delta;
  139. DisconnectWhenCompleteCheck->Top = DisconnectWhenCompleteCheck->Top + Delta;
  140. Caption = OperationName(FData.Operation);
  141. TargetLabel->Visible = TransferOperation;
  142. TargetPathLabel->Visible = TransferOperation;
  143. TargetPathLabel->UnixPath = (FData.Side == osLocal);
  144. FileLabel->UnixPath = (FData.Side == osRemote);
  145. FLastOperation = FData.Operation;
  146. FLastTotalSizeSet = !FData.TotalSizeSet;
  147. };
  148. if (FLastTotalSizeSet != FData.TotalSizeSet)
  149. {
  150. StartTimeLabelLabel->Visible = !FData.TotalSizeSet;
  151. StartTimeLabel->Visible = !FData.TotalSizeSet;
  152. TimeLeftLabelLabel->Visible = FData.TotalSizeSet;
  153. TimeLeftLabel->Visible = FData.TotalSizeSet;
  154. FLastTotalSizeSet = FData.TotalSizeSet;
  155. }
  156. if ((FData.Side == osRemote) || !FData.Temp)
  157. {
  158. FileLabel->Caption = FData.FileName;
  159. }
  160. else
  161. {
  162. FileLabel->Caption = ExtractFileName(FData.FileName);
  163. }
  164. int OverallProgress = FData.OverallProgress();
  165. OperationProgress->Position = OverallProgress;
  166. OperationProgress->Hint = FORMAT("%d%%", (OverallProgress));
  167. Caption = FORMAT("%d%% %s", (OverallProgress, OperationName(FData.Operation)));
  168. if (TransferOperation)
  169. {
  170. if ((FData.Side == osLocal) || !FData.Temp)
  171. {
  172. TargetPathLabel->Caption = FData.Directory;
  173. }
  174. else
  175. {
  176. TargetPathLabel->Caption = LoadStr(PROGRESS_TEMP_DIR);
  177. }
  178. StartTimeLabel->Caption = FData.StartTime.TimeString();
  179. if (FData.TotalSizeSet)
  180. {
  181. TimeLeftLabel->Caption = FormatDateTimeSpan(Configuration->TimeFormat,
  182. FData.TotalTimeLeft());
  183. }
  184. TimeElapsedLabel->Caption = FormatDateTimeSpan(Configuration->TimeFormat, FData.TimeElapsed());
  185. BytesTransferedLabel->Caption = FormatBytes(FData.TotalTransfered);
  186. CPSLabel->Caption = FORMAT("%s/s", (FormatBytes(FData.CPS())));
  187. FileProgress->Position = FData.TransferProgress();
  188. FileProgress->Hint = FORMAT("%d%%", (FileProgress->Position));
  189. }
  190. }
  191. //---------------------------------------------------------------------
  192. void __fastcall TProgressForm::SetProgressData(TFileOperationProgressType & AData)
  193. {
  194. bool InstantUpdate = false;
  195. // workaround: to force displaing first file data immediatelly,
  196. // otherwise form dialog uses to be blank for first second
  197. // (until UpdateTimerTimer)
  198. if (FileLabel->Caption.IsEmpty() && !AData.FileName.IsEmpty())
  199. {
  200. InstantUpdate = true;
  201. FCPSLimit = AData.CPSLimit;
  202. }
  203. if (!FAsciiTransferChanged && FData.AsciiTransfer != AData.AsciiTransfer)
  204. {
  205. FAsciiTransferChanged = true;
  206. InstantUpdate = true;
  207. }
  208. if (!FResumeStatusChanged && FData.ResumeStatus != AData.ResumeStatus)
  209. {
  210. FResumeStatusChanged = true;
  211. InstantUpdate = true;
  212. }
  213. FData = AData;
  214. if (!FDataReceived)
  215. {
  216. FDataReceived = true;
  217. SpeedCombo->Text = SetSpeedLimit(FCPSLimit);
  218. ShowAsModal(this, FShowAsModalStorage);
  219. }
  220. if (InstantUpdate)
  221. {
  222. UpdateControls();
  223. Application->ProcessMessages();
  224. }
  225. TDateTime N = Now();
  226. static double UpdateInterval = double(1)/(24*60*60*5); // 1/5 sec
  227. if ((FUpdateCounter % 5 == 0) ||
  228. (double(N) - double(FLastUpdate) > UpdateInterval))
  229. {
  230. FLastUpdate = N;
  231. FUpdateCounter = 0;
  232. Application->ProcessMessages();
  233. }
  234. FUpdateCounter++;
  235. AData.CPSLimit = FCPSLimit;
  236. }
  237. //---------------------------------------------------------------------------
  238. void __fastcall TProgressForm::UpdateTimerTimer(TObject * /*Sender*/)
  239. {
  240. if (FDataReceived) UpdateControls();
  241. }
  242. //---------------------------------------------------------------------------
  243. void __fastcall TProgressForm::FormShow(TObject * /*Sender*/)
  244. {
  245. UpdateTimer->Enabled = true;
  246. SpeedCombo->Items = CustomWinConfiguration->History["SpeedLimit"];
  247. if (FDataReceived) UpdateControls();
  248. FLastUpdate = 0;
  249. }
  250. //---------------------------------------------------------------------------
  251. void __fastcall TProgressForm::FormHide(TObject * /*Sender*/)
  252. {
  253. CustomWinConfiguration->History["SpeedLimit"] = SpeedCombo->Items;
  254. UpdateTimer->Enabled = false;
  255. }
  256. //---------------------------------------------------------------------------
  257. void __fastcall TProgressForm::CancelButtonClick(TObject * /*Sender*/)
  258. {
  259. CancelOperation();
  260. }
  261. //---------------------------------------------------------------------------
  262. void __fastcall TProgressForm::MinimizeButtonClick(TObject * Sender)
  263. {
  264. GetGlobalMinimizeHandler()(Sender);
  265. }
  266. //---------------------------------------------------------------------------
  267. void __fastcall TProgressForm::CancelOperation()
  268. {
  269. // partially duplicated in TWinSCPFileSystem::CancelConfiguration (far\WinSCPFileSystem)
  270. assert(FDataReceived);
  271. if (!FData.Suspended)
  272. {
  273. // mostly useless, as suspend is called over copy of actual progress data
  274. FData.Suspend();
  275. UpdateControls();
  276. try
  277. {
  278. TCancelStatus ACancel;
  279. int Result;
  280. if (FData.TransferingFile &&
  281. (FData.TimeExpected() > GUIConfiguration->IgnoreCancelBeforeFinish))
  282. {
  283. Result = MessageDialog(LoadStr(CANCEL_OPERATION_FATAL), qtWarning,
  284. qaYes | qaNo | qaCancel, HELP_PROGRESS_CANCEL);
  285. }
  286. else
  287. {
  288. Result = MessageDialog(LoadStr(CANCEL_OPERATION), qtConfirmation,
  289. qaOK | qaCancel, HELP_PROGRESS_CANCEL);
  290. }
  291. switch (Result) {
  292. case qaYes:
  293. ACancel = csCancelTransfer; break;
  294. case qaOK:
  295. case qaNo:
  296. ACancel = csCancel; break;
  297. default:
  298. ACancel = csContinue; break;
  299. }
  300. if (FCancel < ACancel)
  301. {
  302. FCancel = ACancel;
  303. }
  304. }
  305. __finally
  306. {
  307. FData.Resume();
  308. }
  309. }
  310. }
  311. //---------------------------------------------------------------------------
  312. void __fastcall TProgressForm::MinimizeApp()
  313. {
  314. Application->Minimize();
  315. FMinimizedByMe = true;
  316. }
  317. //---------------------------------------------------------------------------
  318. void __fastcall TProgressForm::GlobalMinimize(TObject * /*Sender*/)
  319. {
  320. MinimizeApp();
  321. }
  322. //---------------------------------------------------------------------------
  323. void __fastcall TProgressForm::SetDisconnectWhenComplete(bool value)
  324. {
  325. DisconnectWhenCompleteCheck->Checked = value;
  326. }
  327. //---------------------------------------------------------------------------
  328. bool __fastcall TProgressForm::GetDisconnectWhenComplete()
  329. {
  330. return DisconnectWhenCompleteCheck->Checked;
  331. }
  332. //---------------------------------------------------------------------------
  333. bool __fastcall TProgressForm::GetAllowMinimize()
  334. {
  335. return MinimizeButton->Visible;
  336. }
  337. //---------------------------------------------------------------------------
  338. void __fastcall TProgressForm::SetAllowMinimize(bool value)
  339. {
  340. MinimizeButton->Visible = value;
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TProgressForm::SetReadOnly(bool value)
  344. {
  345. if (FReadOnly != value)
  346. {
  347. FReadOnly = value;
  348. if (!value)
  349. {
  350. DisconnectWhenCompleteCheck->Checked = false;
  351. }
  352. UpdateControls();
  353. }
  354. }
  355. //---------------------------------------------------------------------------
  356. void __fastcall TProgressForm::ApplyCPSLimit()
  357. {
  358. try
  359. {
  360. FCPSLimit = GetSpeedLimit(SpeedCombo->Text);
  361. }
  362. catch(...)
  363. {
  364. SpeedCombo->SetFocus();
  365. throw;
  366. }
  367. SpeedCombo->Text = SetSpeedLimit(FCPSLimit);
  368. // visualize application
  369. SpeedCombo->SelectAll();
  370. CancelButton->SetFocus();
  371. }
  372. //---------------------------------------------------------------------------
  373. void __fastcall TProgressForm::SpeedComboExit(TObject * /*Sender*/)
  374. {
  375. SpeedCombo->Text = SetSpeedLimit(FCPSLimit);
  376. }
  377. //---------------------------------------------------------------------------
  378. void __fastcall TProgressForm::SpeedComboSelect(TObject * /*Sender*/)
  379. {
  380. ApplyCPSLimit();
  381. }
  382. //---------------------------------------------------------------------------
  383. void __fastcall TProgressForm::SpeedComboKeyPress(TObject * /*Sender*/,
  384. char & Key)
  385. {
  386. // using OnKeyPress instead of OnKeyDown to catch "enter" prevents
  387. // system beep for unhandled key
  388. if (Key == '\r')
  389. {
  390. Key = '\0';
  391. ApplyCPSLimit();
  392. }
  393. }
  394. //---------------------------------------------------------------------------