SynchronizeProgress.cpp 6.7 KB

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