Progress.cpp 12 KB

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