CopyParamPreset.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <TextsWin.h>
  6. #include <GUIConfiguration.h>
  7. #include <GUITools.h>
  8. #include <Tools.h>
  9. #include "CopyParamPreset.h"
  10. #include "VCLCommon.h"
  11. //---------------------------------------------------------------------------
  12. #pragma package(smart_init)
  13. #pragma link "CopyParams"
  14. #pragma resource "*.dfm"
  15. //---------------------------------------------------------------------------
  16. bool __fastcall DoCopyParamPresetDialog(TCopyParamList * CopyParamList,
  17. int & Index, TCopyParamPresetMode Mode, TCopyParamRuleData * CurrentRuleData,
  18. const TCopyParamType & DefaultCopyParams)
  19. {
  20. bool Result;
  21. TCopyParamPresetDialog * Dialog = new TCopyParamPresetDialog(GetFormOwner(), Mode, CurrentRuleData);
  22. try
  23. {
  24. Result = Dialog->Execute(CopyParamList, Index, DefaultCopyParams);
  25. }
  26. __finally
  27. {
  28. delete Dialog;
  29. }
  30. return Result;
  31. }
  32. //---------------------------------------------------------------------------
  33. __fastcall TCopyParamPresetDialog::TCopyParamPresetDialog(TComponent * Owner,
  34. TCopyParamPresetMode Mode, TCopyParamRuleData * CurrentRuleData)
  35. : TForm(Owner)
  36. {
  37. SetCorrectFormParent(this);
  38. UseSystemSettings(this);
  39. FMode = Mode;
  40. FCurrentRuleData = CurrentRuleData;
  41. Caption = LoadStr(Mode == cpmEdit ? COPY_PARAM_EDIT : COPY_PARAM_ADD);
  42. HintLabel(RuleMaskHintText,
  43. FORMAT(L"%s\n \n%s",(LoadStr(MASK_HINT2), LoadStr(COMBINING_MASKS_HINT))));
  44. }
  45. //---------------------------------------------------------------------------
  46. void __fastcall TCopyParamPresetDialog::UpdateControls()
  47. {
  48. EnableControl(OkButton, !DescriptionEdit->Text.IsEmpty());
  49. EnableControl(RuleGroup, HasRuleCheck->Checked);
  50. CurrentRuleButton->Visible =
  51. (FCurrentRuleData != NULL) &&
  52. // current rule data are loaded implicitly
  53. (FMode != cpmAddCurrent);
  54. }
  55. //---------------------------------------------------------------------------
  56. void __fastcall TCopyParamPresetDialog::ControlChange(TObject * /*Sender*/)
  57. {
  58. UpdateControls();
  59. }
  60. //---------------------------------------------------------------------------
  61. bool __fastcall TCopyParamPresetDialog::Execute(TCopyParamList * CopyParamList,
  62. int & Index, const TCopyParamType & DefaultCopyParams)
  63. {
  64. FCopyParamList = CopyParamList;
  65. if ((FMode == cpmEdit) || (FMode == cpmDuplicate))
  66. {
  67. const TCopyParamRule * Rule;
  68. if (Index >= 0)
  69. {
  70. CopyParamsFrame->Params = *CopyParamList->CopyParams[Index];
  71. Rule = CopyParamList->Rules[Index];
  72. }
  73. else
  74. {
  75. CopyParamsFrame->Params = DefaultCopyParams;
  76. Rule = NULL;
  77. }
  78. if (FMode == cpmEdit)
  79. {
  80. DescriptionEdit->Text = CopyParamList->Names[Index];
  81. FIndex = Index;
  82. }
  83. else
  84. {
  85. DescriptionEdit->Text = L"";
  86. FIndex = -1; // never used
  87. Index = FCopyParamList->Count;
  88. }
  89. HasRuleCheck->Checked = (Rule != NULL);
  90. if (Rule != NULL)
  91. {
  92. SetRuleData(Rule->Data);
  93. }
  94. }
  95. else
  96. {
  97. DescriptionEdit->Text = L"";
  98. CopyParamsFrame->Params = DefaultCopyParams;
  99. if (FMode == cpmAddCurrent)
  100. {
  101. SetRuleData(*FCurrentRuleData);
  102. HasRuleCheck->Checked = true;
  103. }
  104. else
  105. {
  106. HasRuleCheck->Checked = false;
  107. }
  108. FIndex = -1; // never used
  109. if (Index < 0)
  110. {
  111. Index = FCopyParamList->Count;
  112. }
  113. }
  114. CopyParamsFrame->BeforeExecute();
  115. bool Result = (ShowModal() == DefaultResult(this));
  116. if (Result)
  117. {
  118. CopyParamsFrame->AfterExecute();
  119. UnicodeString Name;
  120. TCopyParamType * CopyParam = NULL;
  121. TCopyParamRule * Rule = NULL;
  122. try
  123. {
  124. Name = DescriptionEdit->Text;
  125. CopyParam = new TCopyParamType(CopyParamsFrame->Params);
  126. Rule = GetRule();
  127. }
  128. catch(...)
  129. {
  130. delete CopyParam;
  131. delete Rule;
  132. throw;
  133. }
  134. if (FMode == cpmEdit)
  135. {
  136. FCopyParamList->Change(Index, Name, CopyParam, Rule);
  137. }
  138. else
  139. {
  140. FCopyParamList->Insert(Index, Name, CopyParam, Rule);
  141. }
  142. }
  143. return Result;
  144. }
  145. //---------------------------------------------------------------------------
  146. void __fastcall TCopyParamPresetDialog::SetRuleData(const TCopyParamRuleData & Data)
  147. {
  148. HostNameEdit->Text = Data.HostName;
  149. UserNameEdit->Text = Data.UserName;
  150. RemoteDirectoryEdit->Text = Data.RemoteDirectory;
  151. LocalDirectoryEdit->Text = Data.LocalDirectory;
  152. }
  153. //---------------------------------------------------------------------------
  154. TCopyParamRule * __fastcall TCopyParamPresetDialog::GetRule()
  155. {
  156. TCopyParamRule * Rule = NULL;
  157. if (HasRuleCheck->Checked)
  158. {
  159. TCopyParamRuleData Data;
  160. // Last resort check, in case the mask escapes validation in OnExit by IsCancelButtonBeingClicked
  161. ValidateMask(HostNameEdit->Text);
  162. Data.HostName = HostNameEdit->Text;
  163. ValidateMask(UserNameEdit->Text);
  164. Data.UserName = UserNameEdit->Text;
  165. ValidateMask(RemoteDirectoryEdit->Text);
  166. Data.RemoteDirectory = RemoteDirectoryEdit->Text;
  167. ValidateMask(LocalDirectoryEdit->Text);
  168. Data.LocalDirectory = LocalDirectoryEdit->Text;
  169. Rule = new TCopyParamRule(Data);
  170. }
  171. return Rule;
  172. }
  173. //---------------------------------------------------------------------------
  174. void __fastcall TCopyParamPresetDialog::FormShow(TObject * /*Sender*/)
  175. {
  176. InstallPathWordBreakProc(HostNameEdit);
  177. InstallPathWordBreakProc(UserNameEdit);
  178. InstallPathWordBreakProc(RemoteDirectoryEdit);
  179. InstallPathWordBreakProc(LocalDirectoryEdit);
  180. UpdateControls();
  181. }
  182. //---------------------------------------------------------------------------
  183. void __fastcall TCopyParamPresetDialog::FormCloseQuery(TObject * /*Sender*/,
  184. bool & /*CanClose*/)
  185. {
  186. if (ModalResult == DefaultResult(this))
  187. {
  188. UnicodeString Description = DescriptionEdit->Text;
  189. TCopyParamList::ValidateName(Description);
  190. TCopyParamRule * Rule = GetRule();
  191. if (Rule != NULL)
  192. {
  193. try
  194. {
  195. if (Rule->IsEmpty)
  196. {
  197. throw Exception(LoadStr(COPY_PARAM_NO_RULE));
  198. }
  199. }
  200. __finally
  201. {
  202. delete Rule;
  203. }
  204. }
  205. int Index = FCopyParamList->IndexOfName(Description);
  206. if (((FMode == cpmEdit) && (Index >= 0) && (Index != FIndex)) ||
  207. (((FMode == cpmAdd) || (FMode == cpmAddCurrent) || (FMode == cpmDuplicate)) && (Index >= 0)))
  208. {
  209. DescriptionEdit->SetFocus();
  210. throw Exception(FMTLOAD(COPY_PARAM_DUPLICATE, (Description)));
  211. }
  212. ExitActiveControl(this);
  213. }
  214. }
  215. //---------------------------------------------------------------------------
  216. void __fastcall TCopyParamPresetDialog::CurrentRuleButtonClick(
  217. TObject * /*Sender*/)
  218. {
  219. DebugAssert(FCurrentRuleData != NULL);
  220. SetRuleData(*FCurrentRuleData);
  221. }
  222. //---------------------------------------------------------------------------
  223. void __fastcall TCopyParamPresetDialog::HelpButtonClick(TObject * /*Sender*/)
  224. {
  225. FormHelp(this);
  226. }
  227. //---------------------------------------------------------------------------
  228. void __fastcall TCopyParamPresetDialog::MaskEditExit(TObject * Sender)
  229. {
  230. ValidateMaskEdit(dynamic_cast<TEdit*>(Sender));
  231. }
  232. //---------------------------------------------------------------------------