Copy.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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, int * OutputOptions)
  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. if (OutputOptions != NULL)
  40. {
  41. CopyDialog->OutputOptions = *OutputOptions;
  42. }
  43. Result = CopyDialog->Execute();
  44. if (Result)
  45. {
  46. TargetDirectory = CopyDialog->Directory;
  47. *Params = CopyDialog->Params;
  48. if (OutputOptions != NULL)
  49. {
  50. *OutputOptions = CopyDialog->OutputOptions;
  51. }
  52. }
  53. }
  54. __finally
  55. {
  56. delete CopyDialog;
  57. }
  58. return Result;
  59. }
  60. //---------------------------------------------------------------------------
  61. __fastcall TCopyDialog::TCopyDialog(TComponent* Owner)
  62. : TForm(Owner)
  63. {
  64. // on start set different value than we want to allow property-setter to proceed
  65. FToRemote = false;
  66. FMove = true;
  67. ToRemote = true;
  68. Move = false;
  69. FOptions = 0;
  70. FOutputOptions = 0;
  71. FPresetsMenu = new TPopupMenu(this);
  72. UseSystemSettings(this);
  73. }
  74. //---------------------------------------------------------------------------
  75. __fastcall TCopyDialog::~TCopyDialog()
  76. {
  77. delete FPresetsMenu;
  78. }
  79. //---------------------------------------------------------------------------
  80. void __fastcall TCopyDialog::AdjustControls()
  81. {
  82. if (FLAGSET(Options, coDoNotShowAgain))
  83. {
  84. SaveSettingsCheck->Caption = LoadStr(NEVER_SHOW_DIALOG_AGAIN);
  85. }
  86. RemoteDirectoryEdit->Visible = false;
  87. LocalDirectoryEdit->Visible = false;
  88. DirectoryEdit->Visible = FLAGCLEAR(Options, coTemp);
  89. EnableControl(DirectoryEdit, FLAGCLEAR(Options, coDisableDirectory));
  90. EnableControl(DirectoryLabel, DirectoryEdit->Enabled);
  91. EnableControl(LocalDirectoryBrowseButton, DirectoryEdit->Enabled);
  92. DirectoryLabel->FocusControl = DirectoryEdit;
  93. CopyParamsFrame->Direction = !ToRemote ? pdToLocal : pdToRemote;
  94. CopyParamsFrame->Options =
  95. FLAGMASK(FLAGCLEAR(Options, coDisableTransferMode), cfAllowTransferMode) |
  96. FLAGMASK(!Move, cfAllowExcludeMask) |
  97. FLAGMASK(!Move && ToRemote, cfAllowClearArchive);
  98. EnableControl(NewerOnlyCheck, FLAGCLEAR(Options, coDisableNewerOnly));
  99. if (FileList && FileList->Count)
  100. {
  101. AnsiString TransferStr = LoadStr(!Move ? COPY_COPY : COPY_MOVE);
  102. // currently the copy dialog is shown when downloading to temp folder
  103. // only for drag&drop downloads, for we dare to display d&d specific prompt
  104. AnsiString DirectionStr =
  105. LoadStr(((Options & coTemp) != 0) ? COPY_TODROP :
  106. (ToRemote ? COPY_TOREMOTE : COPY_TOLOCAL));
  107. if (FileList->Count == 1)
  108. {
  109. AnsiString FileName;
  110. if (!ToRemote) FileName = UnixExtractFileName(FFileList->Strings[0]);
  111. else FileName = ExtractFileName(FFileList->Strings[0]);
  112. DirectoryLabel->Caption = FMTLOAD(COPY_FILE,
  113. (TransferStr, FileName, DirectionStr));
  114. }
  115. else
  116. {
  117. DirectoryLabel->Caption = FMTLOAD(COPY_FILES,
  118. (TransferStr, FFileList->Count, DirectionStr));
  119. }
  120. }
  121. if (!Move)
  122. {
  123. Caption = LoadStr(COPY_COPY_CAPTION);
  124. CopyButton->Caption = LoadStr(COPY_COPY_BUTTON);
  125. }
  126. else
  127. {
  128. Caption = LoadStr(COPY_MOVE_CAPTION);
  129. CopyButton->Caption = LoadStr(COPY_MOVE_BUTTON);
  130. }
  131. LocalDirectoryBrowseButton->Visible = !ToRemote &&
  132. ((Options & coTemp) == 0);
  133. UpdateControls();
  134. }
  135. //---------------------------------------------------------------------------
  136. void __fastcall TCopyDialog::SetToRemote(bool value)
  137. {
  138. if (FToRemote != value)
  139. {
  140. AnsiString ADirectory = DirectoryEdit->Text;
  141. FToRemote = value;
  142. DirectoryEdit->Text = ADirectory;
  143. AdjustControls();
  144. }
  145. }
  146. //---------------------------------------------------------------------------
  147. void __fastcall TCopyDialog::SetOptions(int value)
  148. {
  149. if (Options != value)
  150. {
  151. FOptions = value;
  152. AdjustControls();
  153. }
  154. }
  155. //---------------------------------------------------------------------------
  156. void __fastcall TCopyDialog::SetOutputOptions(int value)
  157. {
  158. if (OutputOptions != value)
  159. {
  160. SaveSettingsCheck->Checked = FLAGSET(FOutputOptions, cooDoNotShowAgain);
  161. FOutputOptions = (value & ~cooDoNotShowAgain);
  162. }
  163. }
  164. //---------------------------------------------------------------------------
  165. int __fastcall TCopyDialog::GetOutputOptions()
  166. {
  167. return FOutputOptions |
  168. FLAGMASK(SaveSettingsCheck->Checked, cooDoNotShowAgain);
  169. }
  170. //---------------------------------------------------------------------------
  171. THistoryComboBox * __fastcall TCopyDialog::GetDirectoryEdit()
  172. {
  173. return ToRemote ? RemoteDirectoryEdit : LocalDirectoryEdit;
  174. }
  175. //---------------------------------------------------------------------------
  176. AnsiString __fastcall TCopyDialog::GetFileMask()
  177. {
  178. return ToRemote ? UnixExtractFileName(DirectoryEdit->Text) :
  179. ExtractFileName(DirectoryEdit->Text);
  180. }
  181. //---------------------------------------------------------------------------
  182. void __fastcall TCopyDialog::SetParams(const TGUICopyParamType & value)
  183. {
  184. FParams = value;
  185. CopyParamsFrame->Params = value;
  186. DirectoryEdit->Text = Directory + FParams.FileMask;
  187. QueueCheck->Checked = FParams.Queue;
  188. QueueNoConfirmationCheck->Checked = FParams.QueueNoConfirmation;
  189. NewerOnlyCheck->Checked = FLAGCLEAR(Options, coDisableNewerOnly) && FParams.NewerOnly;
  190. }
  191. //---------------------------------------------------------------------------
  192. TGUICopyParamType __fastcall TCopyDialog::GetParams()
  193. {
  194. // overwrites TCopyParamType files only
  195. FParams = CopyParamsFrame->Params;
  196. FParams.FileMask = GetFileMask();
  197. FParams.Queue = QueueCheck->Checked;
  198. FParams.QueueNoConfirmation = QueueNoConfirmationCheck->Checked;
  199. FParams.NewerOnly = FLAGCLEAR(Options, coDisableNewerOnly) && NewerOnlyCheck->Checked;
  200. return FParams;
  201. }
  202. //---------------------------------------------------------------------------
  203. void __fastcall TCopyDialog::SetDirectory(AnsiString value)
  204. {
  205. if (!value.IsEmpty())
  206. {
  207. value = ToRemote ? UnixIncludeTrailingBackslash(value) :
  208. IncludeTrailingBackslash(value);
  209. }
  210. DirectoryEdit->Text = value + GetFileMask();
  211. }
  212. //---------------------------------------------------------------------------
  213. AnsiString __fastcall TCopyDialog::GetDirectory()
  214. {
  215. assert(DirectoryEdit);
  216. AnsiString Result = DirectoryEdit->Text;
  217. if (ToRemote)
  218. {
  219. Result = UnixExtractFilePath(Result);
  220. if (!Result.IsEmpty())
  221. {
  222. Result = UnixIncludeTrailingBackslash(Result);
  223. }
  224. }
  225. else
  226. {
  227. Result = ExtractFilePath(Result);
  228. if (!Result.IsEmpty())
  229. {
  230. Result = IncludeTrailingBackslash(Result);
  231. }
  232. }
  233. return Result;
  234. }
  235. //---------------------------------------------------------------------------
  236. void __fastcall TCopyDialog::SetFileList(TStrings * value)
  237. {
  238. if (FFileList != value)
  239. {
  240. FFileList = value;
  241. AdjustControls();
  242. }
  243. }
  244. //---------------------------------------------------------------------------
  245. void __fastcall TCopyDialog::UpdateControls()
  246. {
  247. EnableControl(QueueCheck,
  248. (Options & (coDisableQueue | coTemp)) == 0);
  249. EnableControl(QueueNoConfirmationCheck,
  250. ((Options & coTemp) == 0) && QueueCheck->Checked);
  251. QueueNoConfirmationCheck->Visible = MoreButton->Expanded;
  252. EnableControl(SaveSettingsCheck, FLAGCLEAR(Options, coDisableSaveSettings));
  253. }
  254. //---------------------------------------------------------------------------
  255. void __fastcall TCopyDialog::SetMove(bool value)
  256. {
  257. if (Move != value)
  258. {
  259. FMove = value;
  260. AdjustControls();
  261. }
  262. }
  263. //---------------------------------------------------------------------------
  264. void __fastcall TCopyDialog::FormShow(TObject * /*Sender*/)
  265. {
  266. assert(FileList && (FileList->Count > 0));
  267. if (DirectoryEdit->Enabled && DirectoryEdit->Visible)
  268. {
  269. DirectoryEdit->SetFocus();
  270. }
  271. else
  272. if (MoreButton->Expanded)
  273. {
  274. MorePanel->SetFocus();
  275. }
  276. else
  277. {
  278. CopyButton->SetFocus();
  279. }
  280. UpdateControls();
  281. InstallPathWordBreakProc(RemoteDirectoryEdit);
  282. InstallPathWordBreakProc(LocalDirectoryEdit);
  283. }
  284. //---------------------------------------------------------------------------
  285. bool __fastcall TCopyDialog::Execute()
  286. {
  287. // at start assume that copy param is current preset
  288. FPreset = GUIConfiguration->CopyParamCurrent;
  289. DirectoryEdit->Items = CustomWinConfiguration->History[
  290. ToRemote ? "RemoteTarget" : "LocalTarget"];
  291. MoreButton->Expanded = GUIConfiguration->CopyParamDialogExpanded;
  292. CopyParamsFrame->BeforeExecute();
  293. bool Result = (ShowModal() == mrOk);
  294. if (Result)
  295. {
  296. CopyParamsFrame->AfterExecute();
  297. Configuration->BeginUpdate();
  298. try
  299. {
  300. GUIConfiguration->CopyParamDialogExpanded = MoreButton->Expanded;
  301. if (FLAGSET(OutputOptions, cooSaveSettings) &&
  302. FLAGCLEAR(Options, coDisableSaveSettings))
  303. {
  304. GUIConfiguration->DefaultCopyParam = Params;
  305. }
  306. DirectoryEdit->SaveToHistory();
  307. CustomWinConfiguration->History[ToRemote ?
  308. "RemoteTarget" : "LocalTarget"] = DirectoryEdit->Items;
  309. }
  310. __finally
  311. {
  312. Configuration->EndUpdate();
  313. }
  314. }
  315. return Result;
  316. }
  317. //---------------------------------------------------------------------------
  318. void __fastcall TCopyDialog::FormCloseQuery(TObject * /*Sender*/,
  319. bool &CanClose)
  320. {
  321. if (ModalResult != mrCancel)
  322. {
  323. if (!ToRemote && ((Options & coTemp) == 0))
  324. {
  325. AnsiString Dir = Directory;
  326. AnsiString Drive = ExtractFileDrive(Dir);
  327. if (!DirectoryExists(Dir))
  328. {
  329. if (MessageDialog(FMTLOAD(CREATE_LOCAL_DIRECTORY, (Dir)),
  330. qtConfirmation, qaOK | qaCancel, HELP_NONE) != qaCancel)
  331. {
  332. if (!ForceDirectories(Dir))
  333. {
  334. SimpleErrorDialog(FMTLOAD(CREATE_LOCAL_DIR_ERROR, (Dir)));
  335. CanClose = false;
  336. }
  337. }
  338. else
  339. {
  340. CanClose = False;
  341. }
  342. }
  343. if (!CanClose)
  344. {
  345. DirectoryEdit->SelectAll();
  346. DirectoryEdit->SetFocus();
  347. }
  348. };
  349. if (CanClose)
  350. {
  351. CopyParamsFrame->Validate();
  352. }
  353. }
  354. }
  355. //---------------------------------------------------------------------------
  356. void __fastcall TCopyDialog::LocalDirectoryBrowseButtonClick(
  357. TObject * /*Sender*/)
  358. {
  359. assert(!ToRemote);
  360. AnsiString Directory = LocalDirectoryEdit->Text;
  361. if (SelectDirectory(Directory, LoadStr(SELECT_LOCAL_DIRECTORY), true))
  362. {
  363. LocalDirectoryEdit->Text = Directory;
  364. }
  365. }
  366. //---------------------------------------------------------------------------
  367. void __fastcall TCopyDialog::ControlChange(TObject * /*Sender*/)
  368. {
  369. UpdateControls();
  370. ResetSystemSettings(this);
  371. }
  372. //---------------------------------------------------------------------------
  373. void __fastcall TCopyDialog::PresetsButtonClick(TObject * /*Sender*/)
  374. {
  375. TCopyParamType Param = Params;
  376. CopyParamListPopup(
  377. PresetsButton->ClientToScreen(TPoint(0, PresetsButton->Height)),
  378. FPresetsMenu, Params, FPreset, CopyParamClick, cplNone);
  379. }
  380. //---------------------------------------------------------------------------
  381. void __fastcall TCopyDialog::CopyParamClick(TObject * Sender)
  382. {
  383. TCopyParamType Param = Params;
  384. if (CopyParamListPopupClick(Sender, Param, FPreset))
  385. {
  386. Params = Param;
  387. }
  388. }
  389. //---------------------------------------------------------------------------
  390. void __fastcall TCopyDialog::HelpButtonClick(TObject * /*Sender*/)
  391. {
  392. FormHelp(this);
  393. }
  394. //---------------------------------------------------------------------------