Progress.cpp 12 KB

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