Progress.cpp 12 KB

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