Copy.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. if (OutputOptions != NULL)
  36. {
  37. CopyDialog->OutputOptions = *OutputOptions;
  38. }
  39. CopyDialog->Directory = TargetDirectory;
  40. CopyDialog->FileList = FileList;
  41. CopyDialog->Params = *Params;
  42. CopyDialog->Move = Move;
  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::AdjustTransferControls()
  81. {
  82. if (FileList && FileList->Count)
  83. {
  84. if (!ToRemote && !Move && FLAGSET(FOutputOptions, cooRemoteTransfer))
  85. {
  86. AnsiString Label;
  87. if (FileList->Count == 1)
  88. {
  89. AnsiString FileName;
  90. if (!ToRemote) FileName = UnixExtractFileName(FFileList->Strings[0]);
  91. else FileName = ExtractFileName(FFileList->Strings[0]);
  92. Label = FMTLOAD(REMOTE_COPY_FILE, (FileName));
  93. }
  94. else
  95. {
  96. Label = FMTLOAD(REMOTE_COPY_FILES, (FFileList->Count));
  97. }
  98. // hack to remove trailing colon used for "duplicate" prompt
  99. if (!Label.IsEmpty() &&
  100. (Label.ByteType(Label.Length()) == mbSingleByte) &&
  101. (Label[Label.Length()] == ':'))
  102. {
  103. Label.SetLength(Label.Length() - 1);
  104. }
  105. DirectoryLabel->Caption = Label;
  106. }
  107. else
  108. {
  109. AnsiString TransferStr = LoadStr(!Move ? COPY_COPY : COPY_MOVE);
  110. // currently the copy dialog is shown when downloading to temp folder
  111. // only for drag&drop downloads, for we dare to display d&d specific prompt
  112. AnsiString DirectionStr =
  113. LoadStr(((Options & coTemp) != 0) ? COPY_TODROP :
  114. (RemotePaths() ? COPY_TOREMOTE : COPY_TOLOCAL));
  115. if (FileList->Count == 1)
  116. {
  117. AnsiString FileName;
  118. if (!ToRemote) FileName = UnixExtractFileName(FFileList->Strings[0]);
  119. else FileName = ExtractFileName(FFileList->Strings[0]);
  120. DirectoryLabel->Caption = FMTLOAD(COPY_FILE,
  121. (TransferStr, FileName, DirectionStr));
  122. }
  123. else
  124. {
  125. DirectoryLabel->Caption = FMTLOAD(COPY_FILES,
  126. (TransferStr, FFileList->Count, DirectionStr));
  127. }
  128. }
  129. }
  130. if (!Move)
  131. {
  132. if (!ToRemote && FLAGSET(FOutputOptions, cooRemoteTransfer))
  133. {
  134. Caption = LoadStr(REMOTE_COPY_TITLE);
  135. }
  136. else
  137. {
  138. Caption = LoadStr(COPY_COPY_CAPTION);
  139. }
  140. CopyButton->Caption = LoadStr(COPY_COPY_BUTTON);
  141. }
  142. else
  143. {
  144. Caption = LoadStr(COPY_MOVE_CAPTION);
  145. CopyButton->Caption = LoadStr(COPY_MOVE_BUTTON);
  146. }
  147. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  148. assert(FLAGSET(Options, coAllowRemoteTransfer) || !RemoteTransfer);
  149. EnableControl(CopyParamsFrame, !RemoteTransfer);
  150. EnableControl(NewerOnlyCheck, FLAGCLEAR(Options, coDisableNewerOnly) && !RemoteTransfer);
  151. EnableControl(PresetsButton, !RemoteTransfer);
  152. }
  153. //---------------------------------------------------------------------------
  154. void __fastcall TCopyDialog::AdjustControls()
  155. {
  156. if (FLAGSET(Options, coDoNotShowAgain))
  157. {
  158. SaveSettingsCheck->Caption = LoadStr(NEVER_SHOW_DIALOG_AGAIN);
  159. }
  160. RemoteDirectoryEdit->Visible = false;
  161. LocalDirectoryEdit->Visible = false;
  162. DirectoryEdit->Visible = FLAGCLEAR(Options, coTemp);
  163. EnableControl(DirectoryEdit, FLAGCLEAR(Options, coDisableDirectory));
  164. EnableControl(DirectoryLabel, DirectoryEdit->Enabled);
  165. EnableControl(LocalDirectoryBrowseButton, DirectoryEdit->Enabled);
  166. DirectoryLabel->FocusControl = DirectoryEdit;
  167. CopyParamsFrame->Direction = !ToRemote ? pdToLocal : pdToRemote;
  168. CopyParamsFrame->Options =
  169. FLAGMASK(FLAGCLEAR(Options, coDisableTransferMode), cfAllowTransferMode) |
  170. FLAGMASK(!Move, cfAllowExcludeMask) |
  171. FLAGMASK(!Move && ToRemote, cfAllowClearArchive);
  172. PresetsButton->Visible = FLAGCLEAR(Options, coDoNotUsePresets);
  173. AdjustTransferControls();
  174. LocalDirectoryBrowseButton->Visible = !ToRemote &&
  175. ((Options & coTemp) == 0);
  176. UpdateControls();
  177. }
  178. //---------------------------------------------------------------------------
  179. void __fastcall TCopyDialog::SetToRemote(bool value)
  180. {
  181. if (FToRemote != value)
  182. {
  183. AnsiString ADirectory = DirectoryEdit->Text;
  184. FToRemote = value;
  185. DirectoryEdit->Text = ADirectory;
  186. AdjustControls();
  187. }
  188. }
  189. //---------------------------------------------------------------------------
  190. void __fastcall TCopyDialog::SetOptions(int value)
  191. {
  192. if (Options != value)
  193. {
  194. FOptions = value;
  195. AdjustControls();
  196. }
  197. }
  198. //---------------------------------------------------------------------------
  199. void __fastcall TCopyDialog::SetOutputOptions(int value)
  200. {
  201. if (OutputOptions != value)
  202. {
  203. SaveSettingsCheck->Checked = FLAGSET(FOutputOptions, cooDoNotShowAgain);
  204. FOutputOptions = (value & ~cooDoNotShowAgain);
  205. }
  206. }
  207. //---------------------------------------------------------------------------
  208. int __fastcall TCopyDialog::GetOutputOptions()
  209. {
  210. return FOutputOptions |
  211. FLAGMASK(SaveSettingsCheck->Checked, cooDoNotShowAgain);
  212. }
  213. //---------------------------------------------------------------------------
  214. THistoryComboBox * __fastcall TCopyDialog::GetDirectoryEdit()
  215. {
  216. return ToRemote ? RemoteDirectoryEdit : LocalDirectoryEdit;
  217. }
  218. //---------------------------------------------------------------------------
  219. bool __fastcall TCopyDialog::RemotePaths()
  220. {
  221. return (ToRemote || FLAGSET(FOutputOptions, cooRemoteTransfer));
  222. }
  223. //---------------------------------------------------------------------------
  224. AnsiString __fastcall TCopyDialog::GetFileMask()
  225. {
  226. return RemotePaths() ? UnixExtractFileName(DirectoryEdit->Text) :
  227. ExtractFileName(DirectoryEdit->Text);
  228. }
  229. //---------------------------------------------------------------------------
  230. void __fastcall TCopyDialog::SetParams(const TGUICopyParamType & value)
  231. {
  232. FParams = value;
  233. CopyParamsFrame->Params = value;
  234. DirectoryEdit->Text = Directory + FParams.FileMask;
  235. QueueCheck->Checked = FParams.Queue;
  236. QueueNoConfirmationCheck->Checked = FParams.QueueNoConfirmation;
  237. NewerOnlyCheck->Checked = FLAGCLEAR(Options, coDisableNewerOnly) && FParams.NewerOnly;
  238. }
  239. //---------------------------------------------------------------------------
  240. TGUICopyParamType __fastcall TCopyDialog::GetParams()
  241. {
  242. // overwrites TCopyParamType files only
  243. FParams = CopyParamsFrame->Params;
  244. FParams.FileMask = GetFileMask();
  245. FParams.Queue = QueueCheck->Checked;
  246. FParams.QueueNoConfirmation = QueueNoConfirmationCheck->Checked;
  247. FParams.NewerOnly = FLAGCLEAR(Options, coDisableNewerOnly) && NewerOnlyCheck->Checked;
  248. return FParams;
  249. }
  250. //---------------------------------------------------------------------------
  251. void __fastcall TCopyDialog::SetDirectory(AnsiString value)
  252. {
  253. if (!value.IsEmpty())
  254. {
  255. value = RemotePaths() ?
  256. UnixIncludeTrailingBackslash(value) : IncludeTrailingBackslash(value);
  257. }
  258. DirectoryEdit->Text = value + GetFileMask();
  259. }
  260. //---------------------------------------------------------------------------
  261. AnsiString __fastcall TCopyDialog::GetDirectory()
  262. {
  263. assert(DirectoryEdit);
  264. AnsiString Result = DirectoryEdit->Text;
  265. if (RemotePaths())
  266. {
  267. Result = UnixExtractFilePath(Result);
  268. if (!Result.IsEmpty())
  269. {
  270. Result = UnixIncludeTrailingBackslash(Result);
  271. }
  272. }
  273. else
  274. {
  275. Result = ExtractFilePath(Result);
  276. if (!Result.IsEmpty())
  277. {
  278. Result = IncludeTrailingBackslash(Result);
  279. }
  280. }
  281. return Result;
  282. }
  283. //---------------------------------------------------------------------------
  284. void __fastcall TCopyDialog::SetFileList(TStrings * value)
  285. {
  286. if (FFileList != value)
  287. {
  288. FFileList = value;
  289. AdjustControls();
  290. }
  291. }
  292. //---------------------------------------------------------------------------
  293. void __fastcall TCopyDialog::UpdateControls()
  294. {
  295. if (!ToRemote && FLAGSET(Options, coAllowRemoteTransfer))
  296. {
  297. AnsiString Directory = DirectoryEdit->Text;
  298. bool RemoteTransfer = (Directory.Pos("\\") == 0) && (Directory.Pos("/") > 0);
  299. if (RemoteTransfer != FLAGSET(FOutputOptions, cooRemoteTransfer))
  300. {
  301. FOutputOptions =
  302. (FOutputOptions & ~cooRemoteTransfer) |
  303. FLAGMASK(RemoteTransfer, cooRemoteTransfer);
  304. AdjustTransferControls();
  305. }
  306. }
  307. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  308. EnableControl(QueueCheck,
  309. ((Options & (coDisableQueue | coTemp)) == 0) && !RemoteTransfer);
  310. EnableControl(QueueNoConfirmationCheck,
  311. (((Options & coTemp) == 0) && QueueCheck->Checked) && !RemoteTransfer);
  312. QueueNoConfirmationCheck->Visible = MoreButton->Expanded;
  313. EnableControl(SaveSettingsCheck, FLAGCLEAR(Options, coDisableSaveSettings) &&
  314. !RemoteTransfer);
  315. }
  316. //---------------------------------------------------------------------------
  317. void __fastcall TCopyDialog::SetMove(bool value)
  318. {
  319. if (Move != value)
  320. {
  321. FMove = value;
  322. AdjustControls();
  323. }
  324. }
  325. //---------------------------------------------------------------------------
  326. void __fastcall TCopyDialog::FormShow(TObject * /*Sender*/)
  327. {
  328. assert(FileList && (FileList->Count > 0));
  329. if (DirectoryEdit->Enabled && DirectoryEdit->Visible)
  330. {
  331. DirectoryEdit->SetFocus();
  332. }
  333. else
  334. if (MoreButton->Expanded)
  335. {
  336. MorePanel->SetFocus();
  337. }
  338. else
  339. {
  340. CopyButton->SetFocus();
  341. }
  342. UpdateControls();
  343. InstallPathWordBreakProc(RemoteDirectoryEdit);
  344. InstallPathWordBreakProc(LocalDirectoryEdit);
  345. }
  346. //---------------------------------------------------------------------------
  347. bool __fastcall TCopyDialog::Execute()
  348. {
  349. // at start assume that copy param is current preset
  350. FPreset = GUIConfiguration->CopyParamCurrent;
  351. DirectoryEdit->Items = CustomWinConfiguration->History[
  352. ToRemote ? "RemoteTarget" : "LocalTarget"];
  353. MoreButton->Expanded = GUIConfiguration->CopyParamDialogExpanded;
  354. CopyParamsFrame->BeforeExecute();
  355. bool Result = (ShowModal() == mrOk);
  356. if (Result)
  357. {
  358. CopyParamsFrame->AfterExecute();
  359. Configuration->BeginUpdate();
  360. try
  361. {
  362. GUIConfiguration->CopyParamDialogExpanded = MoreButton->Expanded;
  363. if (FLAGSET(OutputOptions, cooSaveSettings) &&
  364. FLAGCLEAR(Options, coDisableSaveSettings))
  365. {
  366. GUIConfiguration->DefaultCopyParam = Params;
  367. }
  368. DirectoryEdit->SaveToHistory();
  369. CustomWinConfiguration->History[ToRemote ?
  370. "RemoteTarget" : "LocalTarget"] = DirectoryEdit->Items;
  371. }
  372. __finally
  373. {
  374. Configuration->EndUpdate();
  375. }
  376. }
  377. return Result;
  378. }
  379. //---------------------------------------------------------------------------
  380. void __fastcall TCopyDialog::FormCloseQuery(TObject * /*Sender*/,
  381. bool &CanClose)
  382. {
  383. if (ModalResult != mrCancel)
  384. {
  385. if (!RemotePaths() && ((Options & coTemp) == 0))
  386. {
  387. AnsiString Dir = Directory;
  388. AnsiString Drive = ExtractFileDrive(Dir);
  389. if (!DirectoryExists(Dir))
  390. {
  391. if (MessageDialog(FMTLOAD(CREATE_LOCAL_DIRECTORY, (Dir)),
  392. qtConfirmation, qaOK | qaCancel, HELP_NONE) != qaCancel)
  393. {
  394. if (!ForceDirectories(Dir))
  395. {
  396. SimpleErrorDialog(FMTLOAD(CREATE_LOCAL_DIR_ERROR, (Dir)));
  397. CanClose = false;
  398. }
  399. }
  400. else
  401. {
  402. CanClose = False;
  403. }
  404. }
  405. if (!CanClose)
  406. {
  407. DirectoryEdit->SelectAll();
  408. DirectoryEdit->SetFocus();
  409. }
  410. };
  411. if (CanClose)
  412. {
  413. CopyParamsFrame->Validate();
  414. }
  415. }
  416. }
  417. //---------------------------------------------------------------------------
  418. void __fastcall TCopyDialog::LocalDirectoryBrowseButtonClick(
  419. TObject * /*Sender*/)
  420. {
  421. assert(!ToRemote);
  422. AnsiString Directory;
  423. // if we are duplicating, we have remote path there
  424. if (RemotePaths())
  425. {
  426. Directory = LocalDirectoryEdit->Text;
  427. }
  428. if (SelectDirectory(Directory, LoadStr(SELECT_LOCAL_DIRECTORY), true))
  429. {
  430. LocalDirectoryEdit->Text = Directory;
  431. UpdateControls();
  432. }
  433. }
  434. //---------------------------------------------------------------------------
  435. void __fastcall TCopyDialog::ControlChange(TObject * /*Sender*/)
  436. {
  437. UpdateControls();
  438. ResetSystemSettings(this);
  439. }
  440. //---------------------------------------------------------------------------
  441. void __fastcall TCopyDialog::PresetsButtonClick(TObject * /*Sender*/)
  442. {
  443. TCopyParamType Param = Params;
  444. CopyParamListPopup(
  445. PresetsButton->ClientToScreen(TPoint(0, PresetsButton->Height)),
  446. FPresetsMenu, Params, FPreset, CopyParamClick, cplNone);
  447. }
  448. //---------------------------------------------------------------------------
  449. void __fastcall TCopyDialog::CopyParamClick(TObject * Sender)
  450. {
  451. TCopyParamType Param = Params;
  452. if (CopyParamListPopupClick(Sender, Param, FPreset))
  453. {
  454. Params = Param;
  455. }
  456. }
  457. //---------------------------------------------------------------------------
  458. void __fastcall TCopyDialog::HelpButtonClick(TObject * /*Sender*/)
  459. {
  460. FormHelp(this);
  461. }
  462. //---------------------------------------------------------------------------