Copy.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. #ifndef NO_RESOURCES
  19. #pragma resource "*.dfm"
  20. #endif
  21. //---------------------------------------------------------------------------
  22. bool __fastcall DoCopyDialog(bool ToRemote,
  23. bool Move, TStrings * FileList, UnicodeString & TargetDirectory,
  24. TGUICopyParamType * Params, int Options, int CopyParamAttrs, TSessionData * SessionData,
  25. int * OutputOptions)
  26. {
  27. bool Result;
  28. TCopyDialog *CopyDialog = new TCopyDialog(Application, ToRemote, Move, FileList, Options, CopyParamAttrs, SessionData);
  29. try
  30. {
  31. if (FLAGSET(CopyParamAttrs, cpaNoTransferMode))
  32. {
  33. // If local and remote EOL types are the same, there is no need
  34. // for ASCII (or Automatic) mode
  35. Params->TransferMode = tmBinary;
  36. }
  37. if (OutputOptions != NULL)
  38. {
  39. CopyDialog->OutputOptions = *OutputOptions;
  40. }
  41. CopyDialog->Directory = TargetDirectory;
  42. CopyDialog->Params = *Params;
  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(
  62. TComponent* Owner, bool ToRemote, bool Move, TStrings * FileList, int Options,
  63. int CopyParamAttrs, TSessionData * SessionData) : TForm(Owner)
  64. {
  65. FToRemote = ToRemote;
  66. FMove = Move;
  67. FOptions = Options;
  68. FCopyParamAttrs = CopyParamAttrs;
  69. FFileList = FileList;
  70. FSessionData = SessionData;
  71. FOutputOptions = 0;
  72. AdjustControls();
  73. FPresetsMenu = new TPopupMenu(this);
  74. HotTrackLabel(CopyParamLabel);
  75. CopyParamListButton(TransferSettingsButton);
  76. HotTrackLabel(ShortCutHintLabel);
  77. UseSystemSettings(this);
  78. }
  79. //---------------------------------------------------------------------------
  80. __fastcall TCopyDialog::~TCopyDialog()
  81. {
  82. delete FPresetsMenu;
  83. }
  84. //---------------------------------------------------------------------------
  85. void __fastcall TCopyDialog::AdjustTransferControls()
  86. {
  87. if (FFileList && FFileList->Count)
  88. {
  89. if (!FToRemote && !FMove && FLAGSET(FOutputOptions, cooRemoteTransfer))
  90. {
  91. UnicodeString Label;
  92. if (FFileList->Count == 1)
  93. {
  94. UnicodeString FileName;
  95. if (!FToRemote) FileName = UnixExtractFileName(FFileList->Strings[0]);
  96. else FileName = ExtractFileName(FFileList->Strings[0]);
  97. Label = FMTLOAD(REMOTE_COPY_FILE, (FileName));
  98. }
  99. else
  100. {
  101. Label = FMTLOAD(REMOTE_COPY_FILES, (FFileList->Count));
  102. }
  103. DirectoryLabel->Caption = Label;
  104. }
  105. else
  106. {
  107. UnicodeString TransferStr =
  108. LoadStr(RemotePaths() ? COPY_COPY_TOREMOTE : COPY_COPY_TOLOCAL);
  109. // currently the copy dialog is shown when downloading to temp folder
  110. // only for drag&drop downloads, for we dare to display d&d specific prompt
  111. UnicodeString DirectionStr =
  112. LoadStr(((FOptions & coTemp) != 0) ? COPY_TODROP :
  113. (RemotePaths() ? COPY_TOREMOTE : COPY_TOLOCAL));
  114. if (FFileList->Count == 1)
  115. {
  116. UnicodeString FileName;
  117. if (!FToRemote) FileName = UnixExtractFileName(FFileList->Strings[0]);
  118. else FileName = ExtractFileName(FFileList->Strings[0]);
  119. DirectoryLabel->Caption = FMTLOAD((FMove ? MOVE_FILE : COPY_FILE),
  120. (TransferStr, FileName, DirectionStr));
  121. }
  122. else
  123. {
  124. DirectoryLabel->Caption = FMTLOAD((FMove ? MOVE_FILES : COPY_FILES),
  125. (TransferStr, FFileList->Count, DirectionStr));
  126. }
  127. }
  128. }
  129. UnicodeString ImageName;
  130. UnicodeString ACaption;
  131. if (!FMove)
  132. {
  133. if (!FToRemote && FLAGSET(FOutputOptions, cooRemoteTransfer))
  134. {
  135. ACaption = LoadStr(REMOTE_COPY_TITLE);
  136. ImageName = L"Duplicate";
  137. }
  138. else
  139. {
  140. if (RemotePaths())
  141. {
  142. ACaption = LoadStr(COPY_COPY_TOREMOTE_CAPTION);
  143. ImageName = L"Upload File";
  144. }
  145. else
  146. {
  147. ACaption = LoadStr(COPY_COPY_TOLOCAL_CAPTION);
  148. ImageName = L"Download File";
  149. }
  150. }
  151. }
  152. else
  153. {
  154. if (!FToRemote && FLAGSET(FOutputOptions, cooRemoteTransfer))
  155. {
  156. ACaption = LoadStr(COPY_MOVE_CAPTION);
  157. ImageName = L"Move To";
  158. }
  159. else
  160. {
  161. if (RemotePaths())
  162. {
  163. ACaption = LoadStr(COPY_MOVE_TOREMOTE_CAPTION);
  164. ImageName = L"Upload File Remove Original";
  165. }
  166. else
  167. {
  168. ACaption = LoadStr(COPY_MOVE_TOLOCAL_CAPTION);
  169. ImageName = L"Download File Remove Original";
  170. }
  171. }
  172. }
  173. Caption = FormatFormCaption(this, ACaption);
  174. LoadDialogImage(Image, ImageName);
  175. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  176. DebugAssert(FLAGSET(FOptions, coAllowRemoteTransfer) || !RemoteTransfer);
  177. EnableControl(TransferSettingsButton, !RemoteTransfer);
  178. EnableControl(CopyParamGroup, !RemoteTransfer);
  179. }
  180. //---------------------------------------------------------------------------
  181. void __fastcall TCopyDialog::AdjustControls()
  182. {
  183. RemoteDirectoryEdit->Visible = false;
  184. LocalDirectoryEdit->Visible = false;
  185. DirectoryEdit->Visible = FLAGCLEAR(FOptions, coTemp);
  186. EnableControl(DirectoryEdit, FLAGCLEAR(FOptions, coDisableDirectory));
  187. EnableControl(DirectoryLabel, DirectoryEdit->Enabled);
  188. EnableControl(LocalDirectoryBrowseButton, DirectoryEdit->Enabled);
  189. DirectoryLabel->FocusControl = DirectoryEdit;
  190. UnicodeString QueueLabel = LoadStr(COPY_BACKGROUND);
  191. if (FLAGCLEAR(FOptions, coNoQueue))
  192. {
  193. QueueLabel = FMTLOAD(COPY_QUEUE, (QueueLabel));
  194. }
  195. QueueCheck2->Caption = QueueLabel;
  196. AdjustTransferControls();
  197. LocalDirectoryBrowseButton->Visible = !FToRemote &&
  198. FLAGCLEAR(FOptions, coTemp);
  199. if (FLAGCLEAR(FOptions, coDoNotShowAgain))
  200. {
  201. NeverShowAgainCheck->Visible = false;
  202. ClientHeight = ClientHeight -
  203. (ShortCutHintPanel->Top - NeverShowAgainCheck->Top);
  204. }
  205. if (FLAGCLEAR(FOptions, coShortCutHint) || CustomWinConfiguration->CopyShortCutHintShown)
  206. {
  207. ShortCutHintPanel->Visible = false;
  208. ClientHeight = ClientHeight - ShortCutHintPanel->Height;
  209. }
  210. UpdateControls();
  211. }
  212. //---------------------------------------------------------------------------
  213. void __fastcall TCopyDialog::SetOutputOptions(int value)
  214. {
  215. if (OutputOptions != value)
  216. {
  217. FSaveSettings = FLAGSET(value, cooSaveSettings);
  218. NeverShowAgainCheck->Checked = FLAGSET(value, cooDoNotShowAgain);
  219. FOutputOptions = (value & ~(cooDoNotShowAgain | cooSaveSettings));
  220. }
  221. }
  222. //---------------------------------------------------------------------------
  223. int __fastcall TCopyDialog::GetOutputOptions()
  224. {
  225. return FOutputOptions |
  226. FLAGMASK(FSaveSettings, cooSaveSettings) |
  227. FLAGMASK(NeverShowAgainCheck->Checked, cooDoNotShowAgain);
  228. }
  229. //---------------------------------------------------------------------------
  230. THistoryComboBox * __fastcall TCopyDialog::GetDirectoryEdit()
  231. {
  232. return FToRemote ? RemoteDirectoryEdit : LocalDirectoryEdit;
  233. }
  234. //---------------------------------------------------------------------------
  235. bool __fastcall TCopyDialog::RemotePaths()
  236. {
  237. return (FToRemote || FLAGSET(FOutputOptions, cooRemoteTransfer));
  238. }
  239. //---------------------------------------------------------------------------
  240. UnicodeString __fastcall TCopyDialog::GetFileMask()
  241. {
  242. return ExtractFileName(DirectoryEdit->Text, RemotePaths());
  243. }
  244. //---------------------------------------------------------------------------
  245. void __fastcall TCopyDialog::SetParams(const TGUICopyParamType & value)
  246. {
  247. FParams = value;
  248. FCopyParams = value;
  249. DirectoryEdit->Text = Directory + FParams.FileMask;
  250. QueueCheck2->Checked = FParams.Queue;
  251. UpdateControls();
  252. }
  253. //---------------------------------------------------------------------------
  254. TGUICopyParamType __fastcall TCopyDialog::GetParams()
  255. {
  256. // overwrites TCopyParamType fields only
  257. FParams = FCopyParams;
  258. FParams.FileMask = GetFileMask();
  259. FParams.Queue = QueueCheck2->Checked;
  260. return FParams;
  261. }
  262. //---------------------------------------------------------------------------
  263. void __fastcall TCopyDialog::SetDirectory(UnicodeString value)
  264. {
  265. if (!value.IsEmpty())
  266. {
  267. value = RemotePaths() ?
  268. UnicodeString(UnixIncludeTrailingBackslash(value)) : IncludeTrailingBackslash(value);
  269. }
  270. DirectoryEdit->Text = value + GetFileMask();
  271. }
  272. //---------------------------------------------------------------------------
  273. UnicodeString __fastcall TCopyDialog::GetDirectory()
  274. {
  275. DebugAssert(DirectoryEdit);
  276. UnicodeString Result = DirectoryEdit->Text;
  277. if (RemotePaths())
  278. {
  279. Result = UnixExtractFilePath(Result);
  280. if (!Result.IsEmpty())
  281. {
  282. Result = UnixIncludeTrailingBackslash(Result);
  283. }
  284. }
  285. else
  286. {
  287. Result = ExtractFilePath(Result);
  288. if (!Result.IsEmpty())
  289. {
  290. Result = IncludeTrailingBackslash(Result);
  291. }
  292. }
  293. return Result;
  294. }
  295. //---------------------------------------------------------------------------
  296. int __fastcall TCopyDialog::ActualCopyParamAttrs()
  297. {
  298. return FCopyParamAttrs | FLAGMASK(QueueCheck2->Checked && WinConfiguration->DefaultCopyParam.QueueParallel, cpaNoCalculateSize);
  299. }
  300. //---------------------------------------------------------------------------
  301. void __fastcall TCopyDialog::UpdateControls()
  302. {
  303. if (!FToRemote && FLAGSET(FOptions, coAllowRemoteTransfer))
  304. {
  305. UnicodeString Directory = DirectoryEdit->Text;
  306. bool RemoteTransfer = (Directory.Pos(L"\\") == 0) && (Directory.Pos(L"/") > 0);
  307. if (RemoteTransfer != FLAGSET(FOutputOptions, cooRemoteTransfer))
  308. {
  309. FOutputOptions =
  310. (FOutputOptions & ~cooRemoteTransfer) |
  311. FLAGMASK(RemoteTransfer, cooRemoteTransfer);
  312. AdjustTransferControls();
  313. }
  314. }
  315. UnicodeString InfoStr = FCopyParams.GetInfoStr(L"; ", ActualCopyParamAttrs());
  316. SetLabelHintPopup(CopyParamLabel, InfoStr);
  317. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  318. EnableControl(QueueCheck2,
  319. ((FOptions & (coDisableQueue | coTemp)) == 0) && !RemoteTransfer);
  320. }
  321. //---------------------------------------------------------------------------
  322. void __fastcall TCopyDialog::FormShow(TObject * /*Sender*/)
  323. {
  324. DebugAssert(FFileList && (FFileList->Count > 0));
  325. if (DirectoryEdit->Enabled && DirectoryEdit->Visible)
  326. {
  327. ActiveControl = DirectoryEdit;
  328. }
  329. else
  330. {
  331. ActiveControl = OkButton;
  332. }
  333. UpdateControls();
  334. InstallPathWordBreakProc(RemoteDirectoryEdit);
  335. InstallPathWordBreakProc(LocalDirectoryEdit);
  336. // Does not work when set from a contructor
  337. ShortCutHintPanel->Color = Application->HintColor;
  338. }
  339. //---------------------------------------------------------------------------
  340. bool __fastcall TCopyDialog::Execute()
  341. {
  342. // at start assume that copy param is current preset
  343. FPreset = GUIConfiguration->CopyParamCurrent;
  344. DirectoryEdit->Items = CustomWinConfiguration->History[
  345. FToRemote ? L"RemoteTarget" : L"LocalTarget"];
  346. bool Result = (ShowModal() == DefaultResult(this));
  347. if (Result)
  348. {
  349. Configuration->BeginUpdate();
  350. try
  351. {
  352. if (FLAGSET(OutputOptions, cooSaveSettings) &&
  353. FLAGCLEAR(FOptions, coDisableSaveSettings))
  354. {
  355. GUIConfiguration->DefaultCopyParam = Params;
  356. }
  357. DirectoryEdit->SaveToHistory();
  358. CustomWinConfiguration->History[FToRemote ?
  359. L"RemoteTarget" : L"LocalTarget"] = DirectoryEdit->Items;
  360. if (FLAGSET(FOptions, coShortCutHint))
  361. {
  362. CustomWinConfiguration->CopyShortCutHintShown = true;
  363. }
  364. }
  365. __finally
  366. {
  367. Configuration->EndUpdate();
  368. }
  369. }
  370. return Result;
  371. }
  372. //---------------------------------------------------------------------------
  373. void __fastcall TCopyDialog::FormCloseQuery(TObject * /*Sender*/,
  374. bool &CanClose)
  375. {
  376. if (ModalResult == DefaultResult(this))
  377. {
  378. if (!RemotePaths() && ((FOptions & coTemp) == 0))
  379. {
  380. UnicodeString Dir = Directory;
  381. UnicodeString Drive = ExtractFileDrive(Dir);
  382. if (!DirectoryExists(ApiPath(Dir)))
  383. {
  384. if (MessageDialog(MainInstructions(FMTLOAD(CREATE_LOCAL_DIRECTORY, (Dir))),
  385. qtConfirmation, qaOK | qaCancel, HELP_NONE) != qaCancel)
  386. {
  387. if (!ForceDirectories(ApiPath(Dir)))
  388. {
  389. SimpleErrorDialog(FMTLOAD(CREATE_LOCAL_DIR_ERROR, (Dir)));
  390. CanClose = false;
  391. }
  392. }
  393. else
  394. {
  395. CanClose = False;
  396. }
  397. }
  398. if (!CanClose)
  399. {
  400. DirectoryEdit->SelectAll();
  401. DirectoryEdit->SetFocus();
  402. }
  403. }
  404. if (CanClose && !IsFileNameMask(GetFileMask()) && (FFileList->Count > 1))
  405. {
  406. UnicodeString Message =
  407. FormatMultiFilesToOneConfirmation(DirectoryEdit->Text, RemotePaths());
  408. CanClose =
  409. (MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_NONE) != qaCancel);
  410. }
  411. if (CanClose)
  412. {
  413. ExitActiveControl(this);
  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. //---------------------------------------------------------------------------