Copy.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <WinInterface.h>
  6. #include <ScpMain.h>
  7. #include <TextsWin.h>
  8. #include <VCLCommon.h>
  9. #include <CustomWinConfiguration.h>
  10. #include "Copy.h"
  11. //---------------------------------------------------------------------------
  12. #pragma package(smart_init)
  13. #pragma link "MoreButton"
  14. #pragma link "Rights"
  15. #pragma link "CopyParams"
  16. #pragma link "HistoryComboBox"
  17. #pragma resource "*.dfm"
  18. //---------------------------------------------------------------------------
  19. bool __fastcall DoCopyDialog(bool ToRemote,
  20. bool Move, TStrings * FileList, AnsiString & TargetDirectory,
  21. TGUICopyParamType * Params, int Options)
  22. {
  23. bool Result;
  24. TCopyDialog *CopyDialog = new TCopyDialog(Application);
  25. try
  26. {
  27. if ((Options & coDisableTransferMode) != 0)
  28. {
  29. // If local and remote EOL types are the same, there is no need
  30. // for ASCII (or Automatic) mode
  31. Params->TransferMode = tmBinary;
  32. }
  33. CopyDialog->ToRemote = ToRemote;
  34. CopyDialog->Options = Options;
  35. CopyDialog->Directory = TargetDirectory;
  36. CopyDialog->FileList = FileList;
  37. CopyDialog->Params = *Params;
  38. CopyDialog->Move = Move;
  39. Result = CopyDialog->Execute();
  40. if (Result)
  41. {
  42. TargetDirectory = CopyDialog->Directory;
  43. *Params = CopyDialog->Params;
  44. }
  45. }
  46. __finally
  47. {
  48. delete CopyDialog;
  49. }
  50. return Result;
  51. }
  52. //---------------------------------------------------------------------------
  53. __fastcall TCopyDialog::TCopyDialog(TComponent* Owner)
  54. : TForm(Owner)
  55. {
  56. // on start set different value than we want to allow property-setter to proceed
  57. FToRemote = false;
  58. FMove = true;
  59. ToRemote = true;
  60. Move = false;
  61. FOptions = 0;
  62. UseSystemSettings(this);
  63. }
  64. //---------------------------------------------------------------------------
  65. void __fastcall TCopyDialog::AdjustControls()
  66. {
  67. RemoteDirectoryEdit->Visible = false;
  68. LocalDirectoryEdit->Visible = false;
  69. DirectoryEdit->Visible = ((Options & coDragDropTemp) == 0);
  70. DirectoryEdit->Enabled = ((Options & coDisableDirectory) == 0);
  71. DirectoryLabel->FocusControl = DirectoryEdit;
  72. CopyParamsFrame->Direction = !ToRemote ? pdToLocal : pdToRemote;
  73. CopyParamsFrame->AllowTransferMode = ((Options & coDisableTransferMode) == 0);
  74. UpdateControls();
  75. }
  76. //---------------------------------------------------------------------------
  77. void __fastcall TCopyDialog::SetToRemote(bool value)
  78. {
  79. if (FToRemote != value)
  80. {
  81. AnsiString ADirectory = DirectoryEdit->Text;
  82. FToRemote = value;
  83. DirectoryEdit->Text = ADirectory;
  84. AdjustControls();
  85. }
  86. }
  87. //---------------------------------------------------------------------------
  88. void __fastcall TCopyDialog::SetOptions(int value)
  89. {
  90. if (Options != value)
  91. {
  92. FOptions = value;
  93. AdjustControls();
  94. }
  95. }
  96. //---------------------------------------------------------------------------
  97. THistoryComboBox * __fastcall TCopyDialog::GetDirectoryEdit()
  98. {
  99. return ToRemote ? RemoteDirectoryEdit : LocalDirectoryEdit;
  100. }
  101. //---------------------------------------------------------------------------
  102. AnsiString __fastcall TCopyDialog::GetFileMask()
  103. {
  104. return ToRemote ? UnixExtractFileName(DirectoryEdit->Text) :
  105. ExtractFileName(DirectoryEdit->Text);
  106. }
  107. //---------------------------------------------------------------------------
  108. void __fastcall TCopyDialog::SetParams(const TGUICopyParamType & value)
  109. {
  110. FParams = value;
  111. CopyParamsFrame->Params = value;
  112. DirectoryEdit->Text = Directory + FParams.FileMask;
  113. QueueCheck->Checked = FParams.Queue;
  114. QueueNoConfirmationCheck->Checked = FParams.QueueNoConfirmation;
  115. }
  116. //---------------------------------------------------------------------------
  117. TGUICopyParamType __fastcall TCopyDialog::GetParams()
  118. {
  119. // overwrites TCopyParamType files only
  120. FParams = CopyParamsFrame->Params;
  121. FParams.FileMask = GetFileMask();
  122. FParams.Queue = QueueCheck->Checked;
  123. FParams.QueueNoConfirmation = QueueNoConfirmationCheck->Checked;
  124. return FParams;
  125. }
  126. //---------------------------------------------------------------------------
  127. void __fastcall TCopyDialog::SetDirectory(AnsiString value)
  128. {
  129. if (!value.IsEmpty())
  130. {
  131. value = ToRemote ? UnixIncludeTrailingBackslash(value) :
  132. IncludeTrailingBackslash(value);
  133. }
  134. DirectoryEdit->Text = value + GetFileMask();
  135. }
  136. //---------------------------------------------------------------------------
  137. AnsiString __fastcall TCopyDialog::GetDirectory()
  138. {
  139. assert(DirectoryEdit);
  140. AnsiString Result = DirectoryEdit->Text;
  141. if (ToRemote)
  142. {
  143. Result = UnixExtractFilePath(Result);
  144. if (!Result.IsEmpty())
  145. {
  146. Result = UnixIncludeTrailingBackslash(Result);
  147. }
  148. }
  149. else
  150. {
  151. Result = ExtractFilePath(Result);
  152. if (!Result.IsEmpty())
  153. {
  154. Result = IncludeTrailingBackslash(Result);
  155. }
  156. }
  157. return Result;
  158. }
  159. //---------------------------------------------------------------------------
  160. void __fastcall TCopyDialog::SetFileList(TStrings * value)
  161. {
  162. if (FFileList != value)
  163. {
  164. FFileList = value;
  165. UpdateControls();
  166. }
  167. }
  168. //---------------------------------------------------------------------------
  169. void __fastcall TCopyDialog::UpdateControls()
  170. {
  171. if (FileList && FileList->Count)
  172. {
  173. AnsiString TransferStr = LoadStr(!Move ? COPY_COPY : COPY_MOVE);
  174. AnsiString DirectionStr =
  175. LoadStr(((Options & coDragDropTemp) != 0) ? COPY_TODROP :
  176. (ToRemote ? COPY_TOREMOTE : COPY_TOLOCAL));
  177. if (FileList->Count == 1)
  178. {
  179. AnsiString FileName;
  180. if (!ToRemote) FileName = UnixExtractFileName(FFileList->Strings[0]);
  181. else FileName = ExtractFileName(FFileList->Strings[0]);
  182. DirectoryLabel->Caption = FMTLOAD(COPY_FILE,
  183. (TransferStr, FileName, DirectionStr));
  184. }
  185. else
  186. {
  187. DirectoryLabel->Caption = FMTLOAD(COPY_FILES,
  188. (TransferStr, FFileList->Count, DirectionStr));
  189. }
  190. }
  191. if (!Move)
  192. {
  193. Caption = LoadStr(COPY_COPY_CAPTION);
  194. CopyButton->Caption = LoadStr(COPY_COPY_BUTTON);
  195. }
  196. else
  197. {
  198. Caption = LoadStr(COPY_MOVE_CAPTION);
  199. CopyButton->Caption = LoadStr(COPY_MOVE_BUTTON);
  200. }
  201. LocalDirectoryBrowseButton->Visible = !ToRemote &&
  202. ((Options & coDragDropTemp) == 0);
  203. EnableControl(QueueCheck,
  204. (Options & (coDisableQueue | coDragDropTemp)) == 0);
  205. EnableControl(QueueNoConfirmationCheck,
  206. ((Options & coDragDropTemp) == 0) && QueueCheck->Checked);
  207. QueueNoConfirmationCheck->Visible = MoreButton->Expanded;
  208. }
  209. //---------------------------------------------------------------------------
  210. void __fastcall TCopyDialog::SetMove(bool value)
  211. {
  212. if (Move != value)
  213. {
  214. FMove = value;
  215. UpdateControls();
  216. }
  217. }
  218. //---------------------------------------------------------------------------
  219. void __fastcall TCopyDialog::FormShow(TObject * /*Sender*/)
  220. {
  221. assert(FileList && (FileList->Count > 0));
  222. if (DirectoryEdit->Enabled && DirectoryEdit->Visible)
  223. {
  224. DirectoryEdit->SetFocus();
  225. }
  226. else
  227. if (MoreButton->Expanded)
  228. {
  229. MorePanel->SetFocus();
  230. }
  231. else
  232. {
  233. CopyButton->SetFocus();
  234. }
  235. UpdateControls();
  236. }
  237. //---------------------------------------------------------------------------
  238. bool __fastcall TCopyDialog::Execute()
  239. {
  240. DirectoryEdit->Items = CustomWinConfiguration->History[
  241. ToRemote ? "RemoteTarget" : "LocalTarget"];
  242. SaveSettingsCheck->Checked = false;
  243. MoreButton->Expanded = GUIConfiguration->CopyParamDialogExpanded;
  244. CopyParamsFrame->BeforeExecute();
  245. bool Result = (ShowModal() == mrOk);
  246. if (Result)
  247. {
  248. CopyParamsFrame->AfterExecute();
  249. Configuration->BeginUpdate();
  250. try
  251. {
  252. GUIConfiguration->CopyParamDialogExpanded = MoreButton->Expanded;
  253. if (SaveSettingsCheck->Checked)
  254. {
  255. GUIConfiguration->CopyParam = Params;
  256. }
  257. DirectoryEdit->SaveToHistory();
  258. CustomWinConfiguration->History[ToRemote ?
  259. "RemoteTarget" : "LocalTarget"] = DirectoryEdit->Items;
  260. }
  261. __finally
  262. {
  263. Configuration->EndUpdate();
  264. }
  265. }
  266. return Result;
  267. }
  268. //---------------------------------------------------------------------------
  269. void __fastcall TCopyDialog::FormCloseQuery(TObject * /*Sender*/,
  270. bool &CanClose)
  271. {
  272. if (ModalResult != mrCancel)
  273. {
  274. if (!ToRemote && ((Options & coDragDropTemp) == 0))
  275. {
  276. AnsiString Dir = Directory;
  277. AnsiString Drive = ExtractFileDrive(Dir);
  278. if (Drive.IsEmpty() || (Drive.Length() != 2) || (Drive[2] != ':'))
  279. {
  280. SimpleErrorDialog(LoadStr(ABSOLUTE_PATH_REQUIRED));
  281. CanClose = false;
  282. }
  283. else if (!DirectoryExists(Dir))
  284. {
  285. if (MessageDialog(FMTLOAD(CREATE_LOCAL_DIRECTORY, (Dir)),
  286. qtConfirmation, qaOK | qaCancel, 0) != qaCancel)
  287. {
  288. if (!ForceDirectories(Dir))
  289. {
  290. SimpleErrorDialog(FMTLOAD(CREATE_LOCAL_DIR_ERROR, (Dir)));
  291. CanClose = false;
  292. }
  293. }
  294. else
  295. {
  296. CanClose = False;
  297. }
  298. }
  299. if (!CanClose)
  300. {
  301. DirectoryEdit->SelectAll();
  302. DirectoryEdit->SetFocus();
  303. }
  304. };
  305. if (CanClose && (Params.TransferMode == tmAutomatic))
  306. {
  307. TFileMasks Masks = CopyParamsFrame->AsciiFileMask;
  308. int Start, Length;
  309. if (!Masks.IsValid(Start, Length))
  310. {
  311. MoreButton->Expanded = true;
  312. CanClose = false;
  313. SimpleErrorDialog(FMTLOAD(MASK_ERROR, (Masks.Masks.SubString(Start+1, Length))));
  314. // After closing dialog whole text is selected, we want to select only invalid mask
  315. CopyParamsFrame->SelectMask(Start, Length);
  316. }
  317. }
  318. }
  319. }
  320. //---------------------------------------------------------------------------
  321. void __fastcall TCopyDialog::LocalDirectoryBrowseButtonClick(
  322. TObject * /*Sender*/)
  323. {
  324. assert(!ToRemote);
  325. AnsiString Directory = LocalDirectoryEdit->Text;
  326. if (SelectDirectory(Directory, LoadStr(SELECT_LOCAL_DIRECTORY), true))
  327. {
  328. LocalDirectoryEdit->Text = Directory;
  329. }
  330. }
  331. //---------------------------------------------------------------------------
  332. void __fastcall TCopyDialog::DirectoryEditKeyDown(TObject * Sender,
  333. WORD & Key, TShiftState Shift)
  334. {
  335. PathComboBoxKeyDown(dynamic_cast<TCustomComboBox*>(Sender), Key, Shift,
  336. (Sender == RemoteDirectoryEdit));
  337. }
  338. //---------------------------------------------------------------------------
  339. void __fastcall TCopyDialog::ControlChange(TObject * /*Sender*/)
  340. {
  341. UpdateControls();
  342. }
  343. //---------------------------------------------------------------------------