Copy.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <WinInterface.h>
  6. #include <CoreMain.h>
  7. #include <TextsWin.h>
  8. #include <VCLCommon.h>
  9. #include <CustomWinConfiguration.h>
  10. #include <Tools.h>
  11. #include <GUITools.h>
  12. #include "Copy.h"
  13. //---------------------------------------------------------------------------
  14. #pragma package(smart_init)
  15. #pragma link "Rights"
  16. #pragma link "CopyParams"
  17. #pragma link "HistoryComboBox"
  18. #pragma resource "*.dfm"
  19. //---------------------------------------------------------------------------
  20. bool __fastcall DoCopyDialog(
  21. bool ToRemote, bool Move, TStrings * FileList, UnicodeString & TargetDirectory,
  22. TGUICopyParamType * Params, int Options, int CopyParamAttrs, TSessionData * SessionData,
  23. int * OutputOptions, int AutoSubmit)
  24. {
  25. bool Result;
  26. TCopyDialog *CopyDialog = new TCopyDialog(Application, ToRemote, Move, FileList, Options, CopyParamAttrs, SessionData);
  27. try
  28. {
  29. if (FLAGSET(CopyParamAttrs, cpaNoTransferMode))
  30. {
  31. // If local and remote EOL types are the same, there is no need
  32. // for ASCII (or Automatic) mode
  33. Params->TransferMode = tmBinary;
  34. }
  35. if (OutputOptions != NULL)
  36. {
  37. CopyDialog->OutputOptions = *OutputOptions;
  38. }
  39. CopyDialog->Directory = TargetDirectory;
  40. CopyDialog->Params = *Params;
  41. if (AutoSubmit > 0)
  42. {
  43. InitiateDialogTimeout(CopyDialog, AutoSubmit * MSecsPerSec, CopyDialog->OkButton);
  44. }
  45. Result = CopyDialog->Execute();
  46. if (Result)
  47. {
  48. TargetDirectory = CopyDialog->Directory;
  49. *Params = CopyDialog->Params;
  50. if (OutputOptions != NULL)
  51. {
  52. *OutputOptions = CopyDialog->OutputOptions;
  53. }
  54. }
  55. }
  56. __finally
  57. {
  58. delete CopyDialog;
  59. }
  60. return Result;
  61. }
  62. //---------------------------------------------------------------------------
  63. __fastcall TCopyDialog::TCopyDialog(
  64. TComponent* Owner, bool ToRemote, bool Move, TStrings * FileList, int Options,
  65. int CopyParamAttrs, TSessionData * SessionData) : TForm(Owner)
  66. {
  67. FToRemote = ToRemote;
  68. FMove = Move;
  69. FOptions = Options;
  70. FCopyParamAttrs = CopyParamAttrs;
  71. FFileList = FileList;
  72. FSessionData = SessionData;
  73. FOutputOptions = 0;
  74. AdjustControls();
  75. FPresetsMenu = new TPopupMenu(this);
  76. HotTrackLabel(CopyParamLabel);
  77. CopyParamListButton(TransferSettingsButton);
  78. HotTrackLabel(ShortCutHintLabel);
  79. UseSystemSettings(this);
  80. }
  81. //---------------------------------------------------------------------------
  82. __fastcall TCopyDialog::~TCopyDialog()
  83. {
  84. delete FPresetsMenu;
  85. }
  86. //---------------------------------------------------------------------------
  87. void __fastcall TCopyDialog::AdjustTransferControls()
  88. {
  89. if (FFileList && FFileList->Count)
  90. {
  91. if (!FToRemote && !FMove && FLAGSET(FOutputOptions, cooRemoteTransfer))
  92. {
  93. UnicodeString Label;
  94. if (FFileList->Count == 1)
  95. {
  96. UnicodeString FileName;
  97. if (!FToRemote) FileName = UnixExtractFileName(FFileList->Strings[0]);
  98. else FileName = ExtractFileName(FFileList->Strings[0]);
  99. Label = FMTLOAD(REMOTE_COPY_FILE, (FileName));
  100. }
  101. else
  102. {
  103. Label = FMTLOAD(REMOTE_COPY_FILES, (FFileList->Count));
  104. }
  105. DirectoryLabel->Caption = Label;
  106. }
  107. else
  108. {
  109. UnicodeString TransferStr =
  110. LoadStr(RemotePaths() ? COPY_COPY_TOREMOTE : COPY_COPY_TOLOCAL);
  111. // currently the copy dialog is shown when downloading to temp folder
  112. // only for drag&drop downloads, so we dare to display d&d specific prompt
  113. UnicodeString DirectionStr =
  114. LoadStr(((FOptions & coTemp) != 0) ? COPY_TODROP :
  115. (RemotePaths() ? COPY_TOREMOTE : COPY_TOLOCAL));
  116. if (FFileList->Count == 1)
  117. {
  118. UnicodeString FileName;
  119. if (!FToRemote) FileName = UnixExtractFileName(FFileList->Strings[0]);
  120. else FileName = ExtractFileName(FFileList->Strings[0]);
  121. DirectoryLabel->Caption = FMTLOAD((FMove ? MOVE_FILE : COPY_FILE),
  122. (TransferStr, FileName, DirectionStr));
  123. }
  124. else
  125. {
  126. DirectoryLabel->Caption = FMTLOAD((FMove ? MOVE_FILES : COPY_FILES),
  127. (TransferStr, FFileList->Count, DirectionStr));
  128. }
  129. }
  130. }
  131. UnicodeString ImageName;
  132. UnicodeString ACaption;
  133. if (!FMove)
  134. {
  135. if (!FToRemote && FLAGSET(FOutputOptions, cooRemoteTransfer))
  136. {
  137. ACaption = LoadStr(REMOTE_COPY_TITLE);
  138. ImageName = L"Duplicate";
  139. }
  140. else
  141. {
  142. if (RemotePaths())
  143. {
  144. ACaption = LoadStr(COPY_COPY_TOREMOTE_CAPTION);
  145. ImageName = L"Upload File";
  146. }
  147. else
  148. {
  149. ACaption = LoadStr(COPY_COPY_TOLOCAL_CAPTION);
  150. ImageName = L"Download File";
  151. }
  152. }
  153. }
  154. else
  155. {
  156. if (!FToRemote && FLAGSET(FOutputOptions, cooRemoteTransfer))
  157. {
  158. ACaption = LoadStr(COPY_MOVE_CAPTION);
  159. ImageName = L"Move To";
  160. }
  161. else
  162. {
  163. if (RemotePaths())
  164. {
  165. ACaption = LoadStr(COPY_MOVE_TOREMOTE_CAPTION);
  166. ImageName = L"Upload File Remove Original";
  167. }
  168. else
  169. {
  170. ACaption = LoadStr(COPY_MOVE_TOLOCAL_CAPTION);
  171. ImageName = L"Download File Remove Original";
  172. }
  173. }
  174. }
  175. Caption = FormatFormCaption(this, ACaption);
  176. LoadDialogImage(Image, ImageName);
  177. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  178. DebugAssert(FLAGSET(FOptions, coAllowRemoteTransfer) || !RemoteTransfer);
  179. EnableControl(TransferSettingsButton, !RemoteTransfer);
  180. EnableControl(CopyParamGroup, !RemoteTransfer);
  181. }
  182. //---------------------------------------------------------------------------
  183. void __fastcall TCopyDialog::AdjustControls()
  184. {
  185. RemoteDirectoryEdit->Visible = false;
  186. LocalDirectoryEdit->Visible = false;
  187. DirectoryEdit->Visible = FLAGCLEAR(FOptions, coTemp);
  188. EnableControl(DirectoryEdit, FLAGCLEAR(FOptions, coDisableDirectory));
  189. EnableControl(DirectoryLabel, DirectoryEdit->Enabled);
  190. EnableControl(LocalDirectoryBrowseButton, DirectoryEdit->Enabled);
  191. DirectoryLabel->FocusControl = DirectoryEdit;
  192. UnicodeString QueueLabel = LoadStr(COPY_BACKGROUND);
  193. if (FLAGCLEAR(FOptions, coNoQueue))
  194. {
  195. QueueLabel = FMTLOAD(COPY_QUEUE, (QueueLabel));
  196. }
  197. QueueCheck2->Caption = QueueLabel;
  198. AdjustTransferControls();
  199. LocalDirectoryBrowseButton->Visible = !FToRemote &&
  200. FLAGCLEAR(FOptions, coTemp);
  201. if (FLAGCLEAR(FOptions, coDoNotShowAgain))
  202. {
  203. // Command-line transfers
  204. NeverShowAgainCheck->Visible = false;
  205. ClientHeight = ClientHeight -
  206. (ShortCutHintPanel->Top - NeverShowAgainCheck->Top);
  207. }
  208. if (FLAGCLEAR(FOptions, coShortCutHint) || CustomWinConfiguration->CopyShortCutHintShown)
  209. {
  210. ShortCutHintPanel->Visible = false;
  211. ClientHeight = ClientHeight - ShortCutHintPanel->Height;
  212. }
  213. UpdateControls();
  214. }
  215. //---------------------------------------------------------------------------
  216. void __fastcall TCopyDialog::SetOutputOptions(int value)
  217. {
  218. if (OutputOptions != value)
  219. {
  220. FSaveSettings = FLAGSET(value, cooSaveSettings);
  221. NeverShowAgainCheck->Checked = FLAGSET(value, cooDoNotShowAgain);
  222. FOutputOptions = (value & ~(cooDoNotShowAgain | cooSaveSettings));
  223. }
  224. }
  225. //---------------------------------------------------------------------------
  226. int __fastcall TCopyDialog::GetOutputOptions()
  227. {
  228. return FOutputOptions |
  229. FLAGMASK(FSaveSettings, cooSaveSettings) |
  230. FLAGMASK(NeverShowAgainCheck->Checked, cooDoNotShowAgain);
  231. }
  232. //---------------------------------------------------------------------------
  233. THistoryComboBox * __fastcall TCopyDialog::GetDirectoryEdit()
  234. {
  235. return FToRemote ? RemoteDirectoryEdit : LocalDirectoryEdit;
  236. }
  237. //---------------------------------------------------------------------------
  238. bool __fastcall TCopyDialog::RemotePaths()
  239. {
  240. return (FToRemote || FLAGSET(FOutputOptions, cooRemoteTransfer));
  241. }
  242. //---------------------------------------------------------------------------
  243. UnicodeString __fastcall TCopyDialog::GetFileMask()
  244. {
  245. return ExtractFileName(DirectoryEdit->Text, RemotePaths());
  246. }
  247. //---------------------------------------------------------------------------
  248. void __fastcall TCopyDialog::SetParams(const TGUICopyParamType & value)
  249. {
  250. FParams = value;
  251. FCopyParams = value;
  252. DirectoryEdit->Text = Directory + FParams.FileMask;
  253. QueueCheck2->Checked = FParams.Queue;
  254. UpdateControls();
  255. }
  256. //---------------------------------------------------------------------------
  257. TGUICopyParamType __fastcall TCopyDialog::GetParams()
  258. {
  259. // overwrites TCopyParamType fields only
  260. FParams = FCopyParams;
  261. FParams.FileMask = GetFileMask();
  262. FParams.Queue = QueueCheck2->Checked;
  263. return FParams;
  264. }
  265. //---------------------------------------------------------------------------
  266. void __fastcall TCopyDialog::SetDirectory(UnicodeString value)
  267. {
  268. if (!value.IsEmpty())
  269. {
  270. value = RemotePaths() ?
  271. UnicodeString(UnixIncludeTrailingBackslash(value)) : IncludeTrailingBackslash(value);
  272. }
  273. DirectoryEdit->Text = value + GetFileMask();
  274. }
  275. //---------------------------------------------------------------------------
  276. UnicodeString __fastcall TCopyDialog::GetDirectory()
  277. {
  278. DebugAssert(DirectoryEdit);
  279. UnicodeString Result = DirectoryEdit->Text;
  280. if (RemotePaths())
  281. {
  282. Result = UnixExtractFilePath(Result);
  283. if (!Result.IsEmpty())
  284. {
  285. Result = UnixIncludeTrailingBackslash(Result);
  286. }
  287. }
  288. else
  289. {
  290. Result = ExtractFilePath(Result);
  291. if (!Result.IsEmpty())
  292. {
  293. Result = IncludeTrailingBackslash(Result);
  294. }
  295. }
  296. return Result;
  297. }
  298. //---------------------------------------------------------------------------
  299. int __fastcall TCopyDialog::ActualCopyParamAttrs()
  300. {
  301. return FCopyParamAttrs | FLAGMASK(QueueCheck2->Checked && WinConfiguration->DefaultCopyParam.QueueParallel, cpaNoCalculateSize);
  302. }
  303. //---------------------------------------------------------------------------
  304. void __fastcall TCopyDialog::UpdateControls()
  305. {
  306. if (!FToRemote && FLAGSET(FOptions, coAllowRemoteTransfer))
  307. {
  308. UnicodeString Directory = DirectoryEdit->Text;
  309. bool RemoteTransfer = (Directory.Pos(L"\\") == 0) && (Directory.Pos(L"/") > 0);
  310. if (RemoteTransfer != FLAGSET(FOutputOptions, cooRemoteTransfer))
  311. {
  312. FOutputOptions =
  313. (FOutputOptions & ~cooRemoteTransfer) |
  314. FLAGMASK(RemoteTransfer, cooRemoteTransfer);
  315. AdjustTransferControls();
  316. }
  317. }
  318. UnicodeString InfoStr = FCopyParams.GetInfoStr(L"; ", ActualCopyParamAttrs());
  319. SetLabelHintPopup(CopyParamLabel, InfoStr);
  320. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  321. EnableControl(QueueCheck2,
  322. ((FOptions & (coDisableQueue | coTemp)) == 0) && !RemoteTransfer);
  323. }
  324. //---------------------------------------------------------------------------
  325. void __fastcall TCopyDialog::FormShow(TObject * /*Sender*/)
  326. {
  327. DebugAssert(FFileList && (FFileList->Count > 0));
  328. if (DirectoryEdit->Enabled && DirectoryEdit->Visible)
  329. {
  330. ActiveControl = DirectoryEdit;
  331. }
  332. else
  333. {
  334. ActiveControl = OkButton;
  335. }
  336. UpdateControls();
  337. InstallPathWordBreakProc(RemoteDirectoryEdit);
  338. InstallPathWordBreakProc(LocalDirectoryEdit);
  339. // Does not work when set from a contructor
  340. ShortCutHintPanel->Color = Application->HintColor;
  341. }
  342. //---------------------------------------------------------------------------
  343. bool __fastcall TCopyDialog::Execute()
  344. {
  345. // at start assume that copy param is current preset
  346. FPreset = GUIConfiguration->CopyParamCurrent;
  347. DirectoryEdit->Items = CustomWinConfiguration->History[
  348. FToRemote ? L"RemoteTarget" : L"LocalTarget"];
  349. bool Result = (ShowModal() == DefaultResult(this));
  350. if (Result)
  351. {
  352. Configuration->BeginUpdate();
  353. try
  354. {
  355. if (FLAGSET(OutputOptions, cooSaveSettings) &&
  356. FLAGCLEAR(FOptions, coDisableSaveSettings))
  357. {
  358. GUIConfiguration->DefaultCopyParam = Params;
  359. }
  360. DirectoryEdit->SaveToHistory();
  361. CustomWinConfiguration->History[FToRemote ?
  362. L"RemoteTarget" : L"LocalTarget"] = DirectoryEdit->Items;
  363. if (FLAGSET(FOptions, coShortCutHint))
  364. {
  365. CustomWinConfiguration->CopyShortCutHintShown = true;
  366. }
  367. }
  368. __finally
  369. {
  370. Configuration->EndUpdate();
  371. }
  372. }
  373. return Result;
  374. }
  375. //---------------------------------------------------------------------------
  376. void __fastcall TCopyDialog::FormCloseQuery(TObject * /*Sender*/,
  377. bool &CanClose)
  378. {
  379. if (ModalResult == DefaultResult(this))
  380. {
  381. ExitActiveControl(this);
  382. if (!RemotePaths() && ((FOptions & coTemp) == 0))
  383. {
  384. UnicodeString Dir = Directory;
  385. UnicodeString Drive = ExtractFileDrive(Dir);
  386. if (!DirectoryExists(ApiPath(Dir)))
  387. {
  388. if (MessageDialog(MainInstructions(FMTLOAD(CREATE_LOCAL_DIRECTORY, (Dir))),
  389. qtConfirmation, qaOK | qaCancel, HELP_NONE) != qaCancel)
  390. {
  391. if (!ForceDirectories(ApiPath(Dir)))
  392. {
  393. SimpleErrorDialog(FMTLOAD(CREATE_LOCAL_DIR_ERROR, (Dir)));
  394. CanClose = false;
  395. }
  396. }
  397. else
  398. {
  399. CanClose = False;
  400. }
  401. }
  402. if (!CanClose)
  403. {
  404. DirectoryEdit->SelectAll();
  405. DirectoryEdit->SetFocus();
  406. }
  407. }
  408. if (CanClose && !IsFileNameMask(GetFileMask()) && (FFileList->Count > 1))
  409. {
  410. UnicodeString Message =
  411. FormatMultiFilesToOneConfirmation(DirectoryEdit->Text, RemotePaths());
  412. CanClose =
  413. (MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_NONE) != qaCancel);
  414. }
  415. }
  416. }
  417. //---------------------------------------------------------------------------
  418. void __fastcall TCopyDialog::LocalDirectoryBrowseButtonClick(
  419. TObject * /*Sender*/)
  420. {
  421. DebugAssert(!FToRemote);
  422. UnicodeString ADirectory;
  423. // if we are duplicating, we have remote path there
  424. if (!RemotePaths())
  425. {
  426. ADirectory = Directory;
  427. }
  428. if (SelectDirectory(ADirectory, LoadStr(SELECT_LOCAL_DIRECTORY), true))
  429. {
  430. Directory = ADirectory;
  431. UpdateControls();
  432. }
  433. }
  434. //---------------------------------------------------------------------------
  435. void __fastcall TCopyDialog::ControlChange(TObject * /*Sender*/)
  436. {
  437. UpdateControls();
  438. ResetSystemSettings(this);
  439. }
  440. //---------------------------------------------------------------------------
  441. void __fastcall TCopyDialog::TransferSettingsButtonClick(TObject * /*Sender*/)
  442. {
  443. if (!SupportsSplitButton())
  444. {
  445. CopyParamListPopup(CalculatePopupRect(TransferSettingsButton), 0);
  446. }
  447. else
  448. {
  449. CopyParamGroupClick(NULL);
  450. }
  451. }
  452. //---------------------------------------------------------------------------
  453. void __fastcall TCopyDialog::GenerateCode()
  454. {
  455. TFilesSelected FilesSelected = FLAGSET(FOptions, coAllFiles) ? fsAll : fsList;
  456. DoGenerateTransferCodeDialog(FToRemote, FMove, ActualCopyParamAttrs(), FSessionData, FilesSelected, FFileList, Directory, Params);
  457. }
  458. //---------------------------------------------------------------------------
  459. void __fastcall TCopyDialog::CopyParamClick(TObject * Sender)
  460. {
  461. // Save including the preset-unspecific queue properties,
  462. // so that they are preserved when assigning back later
  463. TGUICopyParamType Param = Params;
  464. bool PrevSaveSettings = FSaveSettings;
  465. int Result = CopyParamListPopupClick(Sender, Param, FPreset, ActualCopyParamAttrs(), &FSaveSettings);
  466. if (Result < 0)
  467. {
  468. if (DebugAlwaysTrue(Result == -cplGenerateCode))
  469. {
  470. GenerateCode();
  471. }
  472. }
  473. else
  474. {
  475. if (Result > 0)
  476. {
  477. Params = Param;
  478. }
  479. else
  480. {
  481. UpdateControls();
  482. }
  483. if (PrevSaveSettings && !FSaveSettings)
  484. {
  485. NeverShowAgainCheck->Checked = false;
  486. }
  487. }
  488. }
  489. //---------------------------------------------------------------------------
  490. void __fastcall TCopyDialog::HelpButtonClick(TObject * /*Sender*/)
  491. {
  492. FormHelp(this);
  493. }
  494. //---------------------------------------------------------------------------
  495. void __fastcall TCopyDialog::CopyParamGroupClick(TObject * /*Sender*/)
  496. {
  497. if (CopyParamGroup->Enabled)
  498. {
  499. if (DoCopyParamCustomDialog(FCopyParams, ActualCopyParamAttrs()))
  500. {
  501. UpdateControls();
  502. }
  503. }
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TCopyDialog::CopyParamGroupContextPopup(TObject * /*Sender*/,
  507. TPoint & MousePos, bool & Handled)
  508. {
  509. CopyParamListPopup(CalculatePopupRect(CopyParamGroup, MousePos), cplCustomizeDefault);
  510. Handled = true;
  511. }
  512. //---------------------------------------------------------------------------
  513. void __fastcall TCopyDialog::CopyParamListPopup(TRect R, int AdditionalOptions)
  514. {
  515. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  516. ::CopyParamListPopup(R, FPresetsMenu,
  517. FCopyParams, FPreset, CopyParamClick,
  518. AdditionalOptions |
  519. FLAGMASK(
  520. FLAGCLEAR(FOptions, coDisableSaveSettings) && !RemoteTransfer,
  521. cplSaveSettings) |
  522. FLAGMASK(FLAGCLEAR(FOutputOptions, cooRemoteTransfer) && FLAGCLEAR(FOptions, coTemp), cplGenerateCode),
  523. ActualCopyParamAttrs(),
  524. FSaveSettings);
  525. }
  526. //---------------------------------------------------------------------------
  527. void __fastcall TCopyDialog::TransferSettingsButtonDropDownClick(TObject * /*Sender*/)
  528. {
  529. CopyParamListPopup(CalculatePopupRect(TransferSettingsButton), cplCustomizeDefault);
  530. }
  531. //---------------------------------------------------------------------------
  532. void __fastcall TCopyDialog::NeverShowAgainCheckClick(TObject * /*Sender*/)
  533. {
  534. FSaveSettings = NeverShowAgainCheck->Checked;
  535. UpdateControls();
  536. }
  537. //---------------------------------------------------------------------------
  538. void __fastcall TCopyDialog::ShortCutHintLabelClick(TObject * /*Sender*/)
  539. {
  540. DoPreferencesDialog(pmCommander);
  541. }
  542. //---------------------------------------------------------------------------
  543. void __fastcall TCopyDialog::LocalDirectoryEditExit(TObject *)
  544. {
  545. if (!RemotePaths())
  546. {
  547. if (DirectoryExistsFix(LocalDirectoryEdit->Text))
  548. {
  549. LocalDirectoryEdit->Text = IncludeTrailingBackslash(LocalDirectoryEdit->Text) + AnyMask;
  550. }
  551. }
  552. }
  553. //---------------------------------------------------------------------------