SynchronizeProgress.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #ifndef NO_RESOURCES
  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. #endif
  23. //---------------------------------------------------------------------------
  24. // Used for comparing only
  25. __fastcall TSynchronizeProgressForm::TSynchronizeProgressForm(TComponent * Owner, bool AllowMinimize, int Files)
  26. : TForm(Owner)
  27. {
  28. FStarted = false;
  29. FCanceled = false;
  30. FShowAsModalStorage = NULL;
  31. FMinimizedByMe = false;
  32. bool KnowsTotalFiles = (Files >= 0);
  33. OperationProgress->Style = (KnowsTotalFiles ? pbstNormal : pbstMarquee);
  34. OperationProgress->Max = (KnowsTotalFiles ? Files : 1);
  35. TimeLeftLabelLabel->Visible = KnowsTotalFiles;
  36. TimeLeftLabel->Visible = KnowsTotalFiles;
  37. StartTimeLabelLabel->Visible = !KnowsTotalFiles;
  38. StartTimeLabel->Visible = !KnowsTotalFiles;
  39. UseSystemSettings(this);
  40. HideComponentsPanel(this);
  41. SelectScaledImageList(ImageList);
  42. if (!AllowMinimize)
  43. {
  44. MinimizeItem->Visible = false;
  45. }
  46. else
  47. {
  48. SetGlobalMinimizeHandler(this, GlobalMinimize);
  49. }
  50. FFrameAnimation.Init(AnimationPaintBox, L"SynchronizeDirectories");
  51. }
  52. //---------------------------------------------------------------------------
  53. __fastcall TSynchronizeProgressForm::~TSynchronizeProgressForm()
  54. {
  55. ClearGlobalMinimizeHandler(GlobalMinimize);
  56. ReleaseAsModal(this, FShowAsModalStorage);
  57. UnhookFormActivation(this);
  58. if (IsApplicationMinimized() && FMinimizedByMe)
  59. {
  60. ShowNotification(
  61. NULL, MainInstructions(LoadStr(BALLOON_OPERATION_COMPLETE)),
  62. qtInformation);
  63. }
  64. }
  65. //---------------------------------------------------------------------------
  66. void __fastcall TSynchronizeProgressForm::Start()
  67. {
  68. FStarted = true;
  69. FStartTime = Now();
  70. UpdateTimer->Enabled = true;
  71. StartTimeLabel->Caption = FStartTime.TimeString();
  72. OperationProgress->Position = OperationProgress->Min;
  73. UpdateControls();
  74. if (!IsApplicationMinimized())
  75. {
  76. // Do not show the progress when the application is minimized,
  77. // otherwise the form popups up unminimized.
  78. // Quick and dirty hack: with this form, we do not support showing it
  79. // once the application restores,
  80. // otherwise we would have to synchronize it somehow with the TProgressForm,
  81. // not to show it over the TProgressForm
  82. // See solution in TMessageForm::CMShowingChanged.
  83. ShowAsModal(this, FShowAsModalStorage);
  84. HookFormActivation(this);
  85. }
  86. FFrameAnimation.Start();
  87. }
  88. //---------------------------------------------------------------------------
  89. int __fastcall TSynchronizeProgressForm::SetData(
  90. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory, int Progress, bool & Continue)
  91. {
  92. DebugAssert(FStarted);
  93. LocalDirectoryLabel->Caption = LocalDirectory;
  94. RemoteDirectoryLabel->Caption = RemoteDirectory;
  95. OperationProgress->Position = Progress;
  96. Continue = !FCanceled;
  97. UpdateControls();
  98. Application->ProcessMessages();
  99. return CalculateProgress();
  100. }
  101. //---------------------------------------------------------------------------
  102. int __fastcall TSynchronizeProgressForm::CalculateProgress()
  103. {
  104. return (((OperationProgress->Style == pbstMarquee) || (OperationProgress->Max == 0)) ? -1 : ((OperationProgress->Position * 100) / OperationProgress->Max));
  105. }
  106. //---------------------------------------------------------------------------
  107. void __fastcall TSynchronizeProgressForm::UpdateControls()
  108. {
  109. TDateTime Elapsed;
  110. UnicodeString ACaption = FormatFormCaption(this, LoadStr(SYNCHRONIZE_PROGRESS_COMPARE));
  111. if (FStarted)
  112. {
  113. Elapsed = Now() - FStartTime;
  114. int Progress = CalculateProgress();
  115. if (Progress >= 0)
  116. {
  117. ACaption = FORMAT(L"%d%% %s", (Progress, ACaption));
  118. }
  119. }
  120. else
  121. {
  122. Elapsed = EncodeTimeVerbose(0, 0, 0, 0);
  123. }
  124. Caption = ACaption;
  125. TimeElapsedLabel->Caption = FormatDateTimeSpan(Configuration->TimeFormat, Elapsed);
  126. UnicodeString TimeLeftCaption;
  127. int Position = OperationProgress->Position;
  128. if (FStarted && CanShowTimeEstimate(FStartTime) && (Position > 0))
  129. {
  130. TDateTime TimeLeft = TDateTime(double(double(Elapsed) * (OperationProgress->Max - Position) / Position));
  131. TimeLeftCaption = FormatDateTimeSpan(Configuration->TimeFormat, TimeLeft);
  132. }
  133. else
  134. {
  135. TimeLeftCaption = LoadStr(PROGRESS_TIME_LEFT_CALCULATING);
  136. }
  137. TimeLeftLabel->Caption = TimeLeftCaption;
  138. CancelItem->Enabled = !FCanceled;
  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. //---------------------------------------------------------------------------