SynchronizeProgress.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <WinInterface.h>
  5. #include "SynchronizeProgress.h"
  6. #include <Common.h>
  7. #include <Configuration.h>
  8. #include <CoreMain.h>
  9. #include <TextsWin.h>
  10. #include <VCLCommon.h>
  11. #include <GUITools.h>
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. #pragma link "PathLabel"
  15. #pragma link "PngImageList"
  16. #pragma link "TB2Dock"
  17. #pragma link "TB2Item"
  18. #pragma link "TB2Toolbar"
  19. #pragma link "TBX"
  20. #pragma resource "*.dfm"
  21. //---------------------------------------------------------------------------
  22. // Used for comparing only
  23. __fastcall TSynchronizeProgressForm::TSynchronizeProgressForm(TComponent * Owner, bool AllowMinimize, int Files)
  24. : TForm(Owner)
  25. {
  26. FStarted = false;
  27. FCanceled = false;
  28. FShowAsModalStorage = NULL;
  29. FMinimizedByMe = false;
  30. bool KnowsTotalFiles = (Files >= 0);
  31. OperationProgress->Style = (KnowsTotalFiles ? pbstNormal : pbstMarquee);
  32. OperationProgress->Max = (KnowsTotalFiles ? Files : 1);
  33. TimeLeftLabelLabel->Visible = KnowsTotalFiles;
  34. TimeLeftLabel->Visible = KnowsTotalFiles;
  35. StartTimeLabelLabel->Visible = !KnowsTotalFiles;
  36. StartTimeLabel->Visible = !KnowsTotalFiles;
  37. UseSystemSettings(this);
  38. HideComponentsPanel(this);
  39. SelectScaledImageList(ImageList);
  40. if (!AllowMinimize)
  41. {
  42. MinimizeItem->Visible = false;
  43. }
  44. else
  45. {
  46. SetGlobalMinimizeHandler(this, GlobalMinimize);
  47. }
  48. FFrameAnimation.Init(AnimationPaintBox, L"SynchronizeDirectories");
  49. }
  50. //---------------------------------------------------------------------------
  51. __fastcall TSynchronizeProgressForm::~TSynchronizeProgressForm()
  52. {
  53. ClearGlobalMinimizeHandler(GlobalMinimize);
  54. ReleaseAsModal(this, FShowAsModalStorage);
  55. UnhookFormActivation(this);
  56. if (IsApplicationMinimized() && FMinimizedByMe)
  57. {
  58. ShowNotification(
  59. NULL, MainInstructions(LoadStr(BALLOON_OPERATION_COMPLETE)),
  60. qtInformation);
  61. }
  62. }
  63. //---------------------------------------------------------------------------
  64. void __fastcall TSynchronizeProgressForm::Start()
  65. {
  66. FStarted = true;
  67. FStartTime = Now();
  68. UpdateTimer->Enabled = true;
  69. StartTimeLabel->Caption = FStartTime.TimeString();
  70. OperationProgress->Position = OperationProgress->Min;
  71. UpdateControls();
  72. if (!IsApplicationMinimized())
  73. {
  74. // Do not show the progress when the application is minimized,
  75. // otherwise the form popups up unminimized.
  76. // Quick and dirty hack: with this form, we do not support showing it
  77. // once the application restores,
  78. // otherwise we would have to synchronize it somehow with the TProgressForm,
  79. // not to show it over the TProgressForm
  80. // See solution in TMessageForm::CMShowingChanged.
  81. ShowAsModal(this, FShowAsModalStorage);
  82. HookFormActivation(this);
  83. }
  84. FFrameAnimation.Start();
  85. }
  86. //---------------------------------------------------------------------------
  87. int __fastcall TSynchronizeProgressForm::SetData(
  88. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory, int Progress, bool & Continue)
  89. {
  90. DebugAssert(FStarted);
  91. LocalDirectoryLabel->Caption = LocalDirectory;
  92. RemoteDirectoryLabel->Caption = RemoteDirectory;
  93. OperationProgress->Position = Progress;
  94. Continue = !FCanceled;
  95. UpdateControls();
  96. Application->ProcessMessages();
  97. return CalculateProgress();
  98. }
  99. //---------------------------------------------------------------------------
  100. int __fastcall TSynchronizeProgressForm::CalculateProgress()
  101. {
  102. return (((OperationProgress->Style == pbstMarquee) || (OperationProgress->Max == 0)) ? -1 : ((OperationProgress->Position * 100) / OperationProgress->Max));
  103. }
  104. //---------------------------------------------------------------------------
  105. void __fastcall TSynchronizeProgressForm::UpdateControls()
  106. {
  107. TDateTime Elapsed;
  108. UnicodeString ACaption = FormatFormCaption(this, LoadStr(SYNCHRONIZE_PROGRESS_COMPARE));
  109. if (FStarted)
  110. {
  111. Elapsed = Now() - FStartTime;
  112. int Progress = CalculateProgress();
  113. if (Progress >= 0)
  114. {
  115. ACaption = FORMAT(L"%d%% %s", (Progress, ACaption));
  116. }
  117. }
  118. else
  119. {
  120. Elapsed = EncodeTimeVerbose(0, 0, 0, 0);
  121. }
  122. Caption = ACaption;
  123. TimeElapsedLabel->Caption = FormatDateTimeSpan(Configuration->TimeFormat, Elapsed);
  124. UnicodeString TimeLeftCaption;
  125. int Position = OperationProgress->Position;
  126. if (FStarted && CanShowTimeEstimate(FStartTime) && (Position > 0))
  127. {
  128. TDateTime TimeLeft = TDateTime(double(double(Elapsed) * (OperationProgress->Max - Position) / Position));
  129. TimeLeftCaption = FormatDateTimeSpan(Configuration->TimeFormat, TimeLeft);
  130. }
  131. else
  132. {
  133. TimeLeftCaption = LoadStr(PROGRESS_TIME_LEFT_CALCULATING);
  134. }
  135. TimeLeftLabel->Caption = TimeLeftCaption;
  136. CancelItem->Enabled = !FCanceled;
  137. }
  138. //---------------------------------------------------------------------------
  139. void __fastcall TSynchronizeProgressForm::CancelOperation()
  140. {
  141. FCanceled = true;
  142. UpdateControls();
  143. }
  144. //---------------------------------------------------------------------------
  145. void __fastcall TSynchronizeProgressForm::UpdateTimerTimer(TObject * /*Sender*/)
  146. {
  147. UpdateControls();
  148. }
  149. //---------------------------------------------------------------------------
  150. void __fastcall TSynchronizeProgressForm::GlobalMinimize(TObject * /*Sender*/)
  151. {
  152. ApplicationMinimize();
  153. FMinimizedByMe = true;
  154. }
  155. //---------------------------------------------------------------------------
  156. void __fastcall TSynchronizeProgressForm::CMDialogKey(TCMDialogKey & Message)
  157. {
  158. if (Message.CharCode == VK_TAB)
  159. {
  160. Toolbar->KeyboardOpen(L'\0', false);
  161. Message.Result = 1;
  162. }
  163. else
  164. {
  165. TForm::Dispatch(&Message);
  166. }
  167. }
  168. //---------------------------------------------------------------------------
  169. void __fastcall TSynchronizeProgressForm::Dispatch(void * AMessage)
  170. {
  171. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  172. if (Message.Msg == WM_CLOSE)
  173. {
  174. CancelOperation();
  175. }
  176. else if (Message.Msg == CM_DIALOGKEY)
  177. {
  178. CMDialogKey(reinterpret_cast<TCMDialogKey &>(Message));
  179. }
  180. else
  181. {
  182. TForm::Dispatch(AMessage);
  183. }
  184. }
  185. //---------------------------------------------------------------------------
  186. void __fastcall TSynchronizeProgressForm::CancelItemClick(TObject * /*Sender*/)
  187. {
  188. CancelOperation();
  189. }
  190. //---------------------------------------------------------------------------
  191. void __fastcall TSynchronizeProgressForm::MinimizeItemClick(TObject * Sender)
  192. {
  193. CallGlobalMinimizeHandler(Sender);
  194. }
  195. //---------------------------------------------------------------------------