CopyParamPreset.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. Data.HostName = HostNameEdit->Text;
  163. Data.UserName = UserNameEdit->Text;
  164. Data.RemoteDirectory = RemoteDirectoryEdit->Text;
  165. Data.LocalDirectory = LocalDirectoryEdit->Text;
  166. Rule = new TCopyParamRule(Data);
  167. }
  168. return Rule;
  169. }
  170. //---------------------------------------------------------------------------
  171. void __fastcall TCopyParamPresetDialog::FormShow(TObject * /*Sender*/)
  172. {
  173. InstallPathWordBreakProc(HostNameEdit);
  174. InstallPathWordBreakProc(UserNameEdit);
  175. InstallPathWordBreakProc(RemoteDirectoryEdit);
  176. InstallPathWordBreakProc(LocalDirectoryEdit);
  177. UpdateControls();
  178. }
  179. //---------------------------------------------------------------------------
  180. void __fastcall TCopyParamPresetDialog::FormCloseQuery(TObject * /*Sender*/,
  181. bool & /*CanClose*/)
  182. {
  183. if (ModalResult == DefaultResult(this))
  184. {
  185. UnicodeString Description = DescriptionEdit->Text;
  186. TCopyParamList::ValidateName(Description);
  187. TCopyParamRule * Rule = GetRule();
  188. if (Rule != NULL)
  189. {
  190. try
  191. {
  192. if (Rule->IsEmpty)
  193. {
  194. throw Exception(LoadStr(COPY_PARAM_NO_RULE));
  195. }
  196. }
  197. __finally
  198. {
  199. delete Rule;
  200. }
  201. }
  202. int Index = FCopyParamList->IndexOfName(Description);
  203. if (((FMode == cpmEdit) && (Index >= 0) && (Index != FIndex)) ||
  204. (((FMode == cpmAdd) || (FMode == cpmAddCurrent) || (FMode == cpmDuplicate)) && (Index >= 0)))
  205. {
  206. DescriptionEdit->SetFocus();
  207. throw Exception(FMTLOAD(COPY_PARAM_DUPLICATE, (Description)));
  208. }
  209. ExitActiveControl(this);
  210. }
  211. }
  212. //---------------------------------------------------------------------------
  213. void __fastcall TCopyParamPresetDialog::CurrentRuleButtonClick(
  214. TObject * /*Sender*/)
  215. {
  216. DebugAssert(FCurrentRuleData != NULL);
  217. SetRuleData(*FCurrentRuleData);
  218. }
  219. //---------------------------------------------------------------------------
  220. void __fastcall TCopyParamPresetDialog::HelpButtonClick(TObject * /*Sender*/)
  221. {
  222. FormHelp(this);
  223. }
  224. //---------------------------------------------------------------------------
  225. void __fastcall TCopyParamPresetDialog::MaskEditExit(TObject * Sender)
  226. {
  227. ValidateMaskEdit(dynamic_cast<TEdit*>(Sender));
  228. }
  229. //---------------------------------------------------------------------------