Copy.cpp 10 KB

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