Copy.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. void __fastcall TCopyDialog::UpdateControls()
  297. {
  298. if (!FToRemote && FLAGSET(FOptions, coAllowRemoteTransfer))
  299. {
  300. UnicodeString Directory = DirectoryEdit->Text;
  301. bool RemoteTransfer = (Directory.Pos(L"\\") == 0) && (Directory.Pos(L"/") > 0);
  302. if (RemoteTransfer != FLAGSET(FOutputOptions, cooRemoteTransfer))
  303. {
  304. FOutputOptions =
  305. (FOutputOptions & ~cooRemoteTransfer) |
  306. FLAGMASK(RemoteTransfer, cooRemoteTransfer);
  307. AdjustTransferControls();
  308. }
  309. }
  310. UnicodeString InfoStr = FCopyParams.GetInfoStr(L"; ", FCopyParamAttrs);
  311. SetLabelHintPopup(CopyParamLabel, InfoStr);
  312. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  313. EnableControl(QueueCheck2,
  314. ((FOptions & (coDisableQueue | coTemp)) == 0) && !RemoteTransfer);
  315. TransferSettingsButton->Style =
  316. FLAGCLEAR(FOptions, coDoNotUsePresets) ?
  317. TCustomButton::bsSplitButton : TCustomButton::bsPushButton;
  318. }
  319. //---------------------------------------------------------------------------
  320. void __fastcall TCopyDialog::FormShow(TObject * /*Sender*/)
  321. {
  322. DebugAssert(FFileList && (FFileList->Count > 0));
  323. if (DirectoryEdit->Enabled && DirectoryEdit->Visible)
  324. {
  325. ActiveControl = DirectoryEdit;
  326. }
  327. else
  328. {
  329. ActiveControl = OkButton;
  330. }
  331. UpdateControls();
  332. InstallPathWordBreakProc(RemoteDirectoryEdit);
  333. InstallPathWordBreakProc(LocalDirectoryEdit);
  334. // Does not work when set from a contructor
  335. ShortCutHintPanel->Color = Application->HintColor;
  336. }
  337. //---------------------------------------------------------------------------
  338. bool __fastcall TCopyDialog::Execute()
  339. {
  340. // at start assume that copy param is current preset
  341. FPreset = GUIConfiguration->CopyParamCurrent;
  342. DirectoryEdit->Items = CustomWinConfiguration->History[
  343. FToRemote ? L"RemoteTarget" : L"LocalTarget"];
  344. bool Result = (ShowModal() == DefaultResult(this));
  345. if (Result)
  346. {
  347. Configuration->BeginUpdate();
  348. try
  349. {
  350. if (FLAGSET(OutputOptions, cooSaveSettings) &&
  351. FLAGCLEAR(FOptions, coDisableSaveSettings))
  352. {
  353. GUIConfiguration->DefaultCopyParam = Params;
  354. }
  355. DirectoryEdit->SaveToHistory();
  356. CustomWinConfiguration->History[FToRemote ?
  357. L"RemoteTarget" : L"LocalTarget"] = DirectoryEdit->Items;
  358. if (FLAGSET(FOptions, coShortCutHint))
  359. {
  360. CustomWinConfiguration->CopyShortCutHintShown = true;
  361. }
  362. }
  363. __finally
  364. {
  365. Configuration->EndUpdate();
  366. }
  367. }
  368. return Result;
  369. }
  370. //---------------------------------------------------------------------------
  371. void __fastcall TCopyDialog::FormCloseQuery(TObject * /*Sender*/,
  372. bool &CanClose)
  373. {
  374. if (ModalResult == DefaultResult(this))
  375. {
  376. if (!RemotePaths() && ((FOptions & coTemp) == 0))
  377. {
  378. UnicodeString Dir = Directory;
  379. UnicodeString Drive = ExtractFileDrive(Dir);
  380. if (!DirectoryExists(ApiPath(Dir)))
  381. {
  382. if (MessageDialog(MainInstructions(FMTLOAD(CREATE_LOCAL_DIRECTORY, (Dir))),
  383. qtConfirmation, qaOK | qaCancel, HELP_NONE) != qaCancel)
  384. {
  385. if (!ForceDirectories(ApiPath(Dir)))
  386. {
  387. SimpleErrorDialog(FMTLOAD(CREATE_LOCAL_DIR_ERROR, (Dir)));
  388. CanClose = false;
  389. }
  390. }
  391. else
  392. {
  393. CanClose = False;
  394. }
  395. }
  396. if (!CanClose)
  397. {
  398. DirectoryEdit->SelectAll();
  399. DirectoryEdit->SetFocus();
  400. }
  401. }
  402. if (CanClose && !IsFileNameMask(GetFileMask()) && (FFileList->Count > 1))
  403. {
  404. UnicodeString Message =
  405. FormatMultiFilesToOneConfirmation(DirectoryEdit->Text, RemotePaths());
  406. CanClose =
  407. (MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_NONE) != qaCancel);
  408. }
  409. if (CanClose)
  410. {
  411. ExitActiveControl(this);
  412. }
  413. }
  414. }
  415. //---------------------------------------------------------------------------
  416. void __fastcall TCopyDialog::LocalDirectoryBrowseButtonClick(
  417. TObject * /*Sender*/)
  418. {
  419. DebugAssert(!FToRemote);
  420. UnicodeString ADirectory;
  421. // if we are duplicating, we have remote path there
  422. if (!RemotePaths())
  423. {
  424. ADirectory = Directory;
  425. }
  426. if (SelectDirectory(ADirectory, LoadStr(SELECT_LOCAL_DIRECTORY), true))
  427. {
  428. Directory = ADirectory;
  429. UpdateControls();
  430. }
  431. }
  432. //---------------------------------------------------------------------------
  433. void __fastcall TCopyDialog::ControlChange(TObject * /*Sender*/)
  434. {
  435. UpdateControls();
  436. ResetSystemSettings(this);
  437. }
  438. //---------------------------------------------------------------------------
  439. void __fastcall TCopyDialog::TransferSettingsButtonClick(TObject * /*Sender*/)
  440. {
  441. if (FLAGCLEAR(FOptions, coDoNotUsePresets) && !SupportsSplitButton())
  442. {
  443. CopyParamListPopup(CalculatePopupRect(TransferSettingsButton), 0);
  444. }
  445. else
  446. {
  447. CopyParamGroupClick(NULL);
  448. }
  449. }
  450. //---------------------------------------------------------------------------
  451. void __fastcall TCopyDialog::GenerateCode()
  452. {
  453. TFilesSelected FilesSelected = FLAGSET(FOptions, coAllFiles) ? fsAll : fsList;
  454. DoGenerateTransferCodeDialog(FToRemote, FMove, FCopyParamAttrs, FSessionData, FilesSelected, FFileList, Directory, Params);
  455. }
  456. //---------------------------------------------------------------------------
  457. void __fastcall TCopyDialog::CopyParamClick(TObject * Sender)
  458. {
  459. // Save including the preset-unspecific queue properties,
  460. // so that they are preserved when assigning back later
  461. TGUICopyParamType Param = Params;
  462. bool PrevSaveSettings = FSaveSettings;
  463. int Result = CopyParamListPopupClick(Sender, Param, FPreset, FCopyParamAttrs, &FSaveSettings);
  464. if (Result < 0)
  465. {
  466. if (DebugAlwaysTrue(Result == -cplGenerateCode))
  467. {
  468. GenerateCode();
  469. }
  470. }
  471. else
  472. {
  473. if (Result > 0)
  474. {
  475. Params = Param;
  476. }
  477. else
  478. {
  479. UpdateControls();
  480. }
  481. if (PrevSaveSettings && !FSaveSettings)
  482. {
  483. NeverShowAgainCheck->Checked = false;
  484. }
  485. }
  486. }
  487. //---------------------------------------------------------------------------
  488. void __fastcall TCopyDialog::HelpButtonClick(TObject * /*Sender*/)
  489. {
  490. FormHelp(this);
  491. }
  492. //---------------------------------------------------------------------------
  493. void __fastcall TCopyDialog::CopyParamGroupClick(TObject * /*Sender*/)
  494. {
  495. if (CopyParamGroup->Enabled)
  496. {
  497. if (DoCopyParamCustomDialog(FCopyParams, FCopyParamAttrs))
  498. {
  499. UpdateControls();
  500. }
  501. }
  502. }
  503. //---------------------------------------------------------------------------
  504. void __fastcall TCopyDialog::CopyParamGroupContextPopup(TObject * /*Sender*/,
  505. TPoint & MousePos, bool & Handled)
  506. {
  507. if (FLAGCLEAR(FOptions, coDoNotUsePresets))
  508. {
  509. CopyParamListPopup(CalculatePopupRect(CopyParamGroup, MousePos), cplCustomizeDefault);
  510. Handled = true;
  511. }
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TCopyDialog::CopyParamListPopup(TRect R, int AdditionalOptions)
  515. {
  516. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  517. ::CopyParamListPopup(R, FPresetsMenu,
  518. FCopyParams, FPreset, CopyParamClick,
  519. cplCustomize | AdditionalOptions |
  520. FLAGMASK(
  521. FLAGCLEAR(FOptions, coDisableSaveSettings) && !RemoteTransfer,
  522. cplSaveSettings) |
  523. FLAGMASK(FLAGCLEAR(FOutputOptions, cooRemoteTransfer) && FLAGCLEAR(FOptions, coTemp), cplGenerateCode),
  524. FCopyParamAttrs,
  525. FSaveSettings);
  526. }
  527. //---------------------------------------------------------------------------
  528. void __fastcall TCopyDialog::TransferSettingsButtonDropDownClick(TObject * /*Sender*/)
  529. {
  530. CopyParamListPopup(CalculatePopupRect(TransferSettingsButton), cplCustomizeDefault);
  531. }
  532. //---------------------------------------------------------------------------
  533. void __fastcall TCopyDialog::NeverShowAgainCheckClick(TObject * /*Sender*/)
  534. {
  535. FSaveSettings = NeverShowAgainCheck->Checked;
  536. UpdateControls();
  537. }
  538. //---------------------------------------------------------------------------
  539. void __fastcall TCopyDialog::ShortCutHintLabelClick(TObject * /*Sender*/)
  540. {
  541. DoPreferencesDialog(pmCommander);
  542. }
  543. //---------------------------------------------------------------------------