CopyParamPreset.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 = (FCurrentRuleData != NULL);
  53. }
  54. //---------------------------------------------------------------------------
  55. void __fastcall TCopyParamPresetDialog::ControlChange(TObject * /*Sender*/)
  56. {
  57. UpdateControls();
  58. }
  59. //---------------------------------------------------------------------------
  60. bool __fastcall TCopyParamPresetDialog::Execute(TCopyParamList * CopyParamList,
  61. int & Index, const TCopyParamType & DefaultCopyParams)
  62. {
  63. FCopyParamList = CopyParamList;
  64. if ((FMode == cpmEdit) || (FMode == cpmDuplicate))
  65. {
  66. const TCopyParamRule * Rule;
  67. if (Index >= 0)
  68. {
  69. CopyParamsFrame->Params = *CopyParamList->CopyParams[Index];
  70. Rule = CopyParamList->Rules[Index];
  71. }
  72. else
  73. {
  74. CopyParamsFrame->Params = DefaultCopyParams;
  75. Rule = NULL;
  76. }
  77. if (FMode == cpmEdit)
  78. {
  79. DescriptionEdit->Text = CopyParamList->Names[Index];
  80. FIndex = Index;
  81. }
  82. else
  83. {
  84. DescriptionEdit->Text = L"";
  85. FIndex = -1; // never used
  86. Index = FCopyParamList->Count;
  87. }
  88. HasRuleCheck->Checked = (Rule != NULL);
  89. if (Rule != NULL)
  90. {
  91. SetRuleData(Rule->Data);
  92. }
  93. }
  94. else
  95. {
  96. DescriptionEdit->Text = L"";
  97. TCopyParamType Default;
  98. CopyParamsFrame->Params = Default;
  99. HasRuleCheck->Checked = false;
  100. FIndex = -1; // never used
  101. if (Index < 0)
  102. {
  103. Index = FCopyParamList->Count;
  104. }
  105. }
  106. CopyParamsFrame->BeforeExecute();
  107. bool Result = (ShowModal() == DefaultResult(this));
  108. if (Result)
  109. {
  110. CopyParamsFrame->AfterExecute();
  111. UnicodeString Name;
  112. TCopyParamType * CopyParam = NULL;
  113. TCopyParamRule * Rule = NULL;
  114. try
  115. {
  116. Name = DescriptionEdit->Text;
  117. CopyParam = new TCopyParamType(CopyParamsFrame->Params);
  118. Rule = GetRule();
  119. }
  120. catch(...)
  121. {
  122. delete CopyParam;
  123. delete Rule;
  124. throw;
  125. }
  126. if (FMode == cpmEdit)
  127. {
  128. FCopyParamList->Change(Index, Name, CopyParam, Rule);
  129. }
  130. else
  131. {
  132. FCopyParamList->Insert(Index, Name, CopyParam, Rule);
  133. }
  134. }
  135. return Result;
  136. }
  137. //---------------------------------------------------------------------------
  138. void __fastcall TCopyParamPresetDialog::SetRuleData(const TCopyParamRuleData & Data)
  139. {
  140. HostNameEdit->Text = Data.HostName;
  141. UserNameEdit->Text = Data.UserName;
  142. RemoteDirectoryEdit->Text = Data.RemoteDirectory;
  143. LocalDirectoryEdit->Text = Data.LocalDirectory;
  144. }
  145. //---------------------------------------------------------------------------
  146. TCopyParamRule * __fastcall TCopyParamPresetDialog::GetRule()
  147. {
  148. TCopyParamRule * Rule = NULL;
  149. if (HasRuleCheck->Checked)
  150. {
  151. TCopyParamRuleData Data;
  152. Data.HostName = HostNameEdit->Text;
  153. Data.UserName = UserNameEdit->Text;
  154. Data.RemoteDirectory = RemoteDirectoryEdit->Text;
  155. Data.LocalDirectory = LocalDirectoryEdit->Text;
  156. Rule = new TCopyParamRule(Data);
  157. }
  158. return Rule;
  159. }
  160. //---------------------------------------------------------------------------
  161. void __fastcall TCopyParamPresetDialog::FormShow(TObject * /*Sender*/)
  162. {
  163. InstallPathWordBreakProc(HostNameEdit);
  164. InstallPathWordBreakProc(UserNameEdit);
  165. InstallPathWordBreakProc(RemoteDirectoryEdit);
  166. InstallPathWordBreakProc(LocalDirectoryEdit);
  167. UpdateControls();
  168. }
  169. //---------------------------------------------------------------------------
  170. void __fastcall TCopyParamPresetDialog::FormCloseQuery(TObject * /*Sender*/,
  171. bool & /*CanClose*/)
  172. {
  173. if (ModalResult == DefaultResult(this))
  174. {
  175. UnicodeString Description = DescriptionEdit->Text;
  176. TCopyParamList::ValidateName(Description);
  177. TCopyParamRule * Rule = GetRule();
  178. if (Rule != NULL)
  179. {
  180. try
  181. {
  182. if (Rule->IsEmpty)
  183. {
  184. throw Exception(LoadStr(COPY_PARAM_NO_RULE));
  185. }
  186. }
  187. __finally
  188. {
  189. delete Rule;
  190. }
  191. }
  192. int Index = FCopyParamList->IndexOfName(Description);
  193. if (((FMode == cpmEdit) && (Index >= 0) && (Index != FIndex)) ||
  194. (((FMode == cpmAdd) || (FMode == cpmDuplicate)) && (Index >= 0)))
  195. {
  196. DescriptionEdit->SetFocus();
  197. throw Exception(FMTLOAD(COPY_PARAM_DUPLICATE, (Description)));
  198. }
  199. ExitActiveControl(this);
  200. }
  201. }
  202. //---------------------------------------------------------------------------
  203. void __fastcall TCopyParamPresetDialog::CurrentRuleButtonClick(
  204. TObject * /*Sender*/)
  205. {
  206. assert(FCurrentRuleData != NULL);
  207. SetRuleData(*FCurrentRuleData);
  208. }
  209. //---------------------------------------------------------------------------
  210. void __fastcall TCopyParamPresetDialog::HelpButtonClick(TObject * /*Sender*/)
  211. {
  212. FormHelp(this);
  213. }
  214. //---------------------------------------------------------------------------
  215. void __fastcall TCopyParamPresetDialog::MaskEditExit(TObject * Sender)
  216. {
  217. ValidateMaskEdit(dynamic_cast<TEdit*>(Sender));
  218. }
  219. //---------------------------------------------------------------------------