CopyParamPreset.cpp 7.3 KB

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