CopyParamPreset.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. TCopyParamType Default;
  101. CopyParamsFrame->Params = Default;
  102. if (FMode == cpmAddCurrent)
  103. {
  104. SetRuleData(*FCurrentRuleData);
  105. HasRuleCheck->Checked = true;
  106. }
  107. else
  108. {
  109. HasRuleCheck->Checked = false;
  110. }
  111. FIndex = -1; // never used
  112. if (Index < 0)
  113. {
  114. Index = FCopyParamList->Count;
  115. }
  116. }
  117. CopyParamsFrame->BeforeExecute();
  118. bool Result = (ShowModal() == DefaultResult(this));
  119. if (Result)
  120. {
  121. CopyParamsFrame->AfterExecute();
  122. UnicodeString Name;
  123. TCopyParamType * CopyParam = NULL;
  124. TCopyParamRule * Rule = NULL;
  125. try
  126. {
  127. Name = DescriptionEdit->Text;
  128. CopyParam = new TCopyParamType(CopyParamsFrame->Params);
  129. Rule = GetRule();
  130. }
  131. catch(...)
  132. {
  133. delete CopyParam;
  134. delete Rule;
  135. throw;
  136. }
  137. if (FMode == cpmEdit)
  138. {
  139. FCopyParamList->Change(Index, Name, CopyParam, Rule);
  140. }
  141. else
  142. {
  143. FCopyParamList->Insert(Index, Name, CopyParam, Rule);
  144. }
  145. }
  146. return Result;
  147. }
  148. //---------------------------------------------------------------------------
  149. void __fastcall TCopyParamPresetDialog::SetRuleData(const TCopyParamRuleData & Data)
  150. {
  151. HostNameEdit->Text = Data.HostName;
  152. UserNameEdit->Text = Data.UserName;
  153. RemoteDirectoryEdit->Text = Data.RemoteDirectory;
  154. LocalDirectoryEdit->Text = Data.LocalDirectory;
  155. }
  156. //---------------------------------------------------------------------------
  157. TCopyParamRule * __fastcall TCopyParamPresetDialog::GetRule()
  158. {
  159. TCopyParamRule * Rule = NULL;
  160. if (HasRuleCheck->Checked)
  161. {
  162. TCopyParamRuleData Data;
  163. Data.HostName = HostNameEdit->Text;
  164. Data.UserName = UserNameEdit->Text;
  165. Data.RemoteDirectory = RemoteDirectoryEdit->Text;
  166. Data.LocalDirectory = LocalDirectoryEdit->Text;
  167. Rule = new TCopyParamRule(Data);
  168. }
  169. return Rule;
  170. }
  171. //---------------------------------------------------------------------------
  172. void __fastcall TCopyParamPresetDialog::FormShow(TObject * /*Sender*/)
  173. {
  174. InstallPathWordBreakProc(HostNameEdit);
  175. InstallPathWordBreakProc(UserNameEdit);
  176. InstallPathWordBreakProc(RemoteDirectoryEdit);
  177. InstallPathWordBreakProc(LocalDirectoryEdit);
  178. UpdateControls();
  179. }
  180. //---------------------------------------------------------------------------
  181. void __fastcall TCopyParamPresetDialog::FormCloseQuery(TObject * /*Sender*/,
  182. bool & /*CanClose*/)
  183. {
  184. if (ModalResult == DefaultResult(this))
  185. {
  186. UnicodeString Description = DescriptionEdit->Text;
  187. TCopyParamList::ValidateName(Description);
  188. TCopyParamRule * Rule = GetRule();
  189. if (Rule != NULL)
  190. {
  191. try
  192. {
  193. if (Rule->IsEmpty)
  194. {
  195. throw Exception(LoadStr(COPY_PARAM_NO_RULE));
  196. }
  197. }
  198. __finally
  199. {
  200. delete Rule;
  201. }
  202. }
  203. int Index = FCopyParamList->IndexOfName(Description);
  204. if (((FMode == cpmEdit) && (Index >= 0) && (Index != FIndex)) ||
  205. (((FMode == cpmAdd) || (FMode == cpmAddCurrent) || (FMode == cpmDuplicate)) && (Index >= 0)))
  206. {
  207. DescriptionEdit->SetFocus();
  208. throw Exception(FMTLOAD(COPY_PARAM_DUPLICATE, (Description)));
  209. }
  210. ExitActiveControl(this);
  211. }
  212. }
  213. //---------------------------------------------------------------------------
  214. void __fastcall TCopyParamPresetDialog::CurrentRuleButtonClick(
  215. TObject * /*Sender*/)
  216. {
  217. DebugAssert(FCurrentRuleData != NULL);
  218. SetRuleData(*FCurrentRuleData);
  219. }
  220. //---------------------------------------------------------------------------
  221. void __fastcall TCopyParamPresetDialog::HelpButtonClick(TObject * /*Sender*/)
  222. {
  223. FormHelp(this);
  224. }
  225. //---------------------------------------------------------------------------
  226. void __fastcall TCopyParamPresetDialog::MaskEditExit(TObject * Sender)
  227. {
  228. ValidateMaskEdit(dynamic_cast<TEdit*>(Sender));
  229. }
  230. //---------------------------------------------------------------------------