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. #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. QueueIndividuallyCheck->Checked = FParams.QueueIndividually;
  252. UpdateControls();
  253. }
  254. //---------------------------------------------------------------------------
  255. TGUICopyParamType __fastcall TCopyDialog::GetParams()
  256. {
  257. // overwrites TCopyParamType fields only
  258. FParams = FCopyParams;
  259. FParams.FileMask = GetFileMask();
  260. FParams.Queue = QueueCheck2->Checked;
  261. FParams.QueueIndividually = QueueIndividuallyCheck->Checked;
  262. return FParams;
  263. }
  264. //---------------------------------------------------------------------------
  265. void __fastcall TCopyDialog::SetDirectory(UnicodeString value)
  266. {
  267. if (!value.IsEmpty())
  268. {
  269. value = RemotePaths() ?
  270. UnicodeString(UnixIncludeTrailingBackslash(value)) : IncludeTrailingBackslash(value);
  271. }
  272. DirectoryEdit->Text = value + GetFileMask();
  273. }
  274. //---------------------------------------------------------------------------
  275. UnicodeString __fastcall TCopyDialog::GetDirectory()
  276. {
  277. DebugAssert(DirectoryEdit);
  278. UnicodeString Result = DirectoryEdit->Text;
  279. if (RemotePaths())
  280. {
  281. Result = UnixExtractFilePath(Result);
  282. if (!Result.IsEmpty())
  283. {
  284. Result = UnixIncludeTrailingBackslash(Result);
  285. }
  286. }
  287. else
  288. {
  289. Result = ExtractFilePath(Result);
  290. if (!Result.IsEmpty())
  291. {
  292. Result = IncludeTrailingBackslash(Result);
  293. }
  294. }
  295. return Result;
  296. }
  297. //---------------------------------------------------------------------------
  298. void __fastcall TCopyDialog::UpdateControls()
  299. {
  300. if (!FToRemote && FLAGSET(FOptions, coAllowRemoteTransfer))
  301. {
  302. UnicodeString Directory = DirectoryEdit->Text;
  303. bool RemoteTransfer = (Directory.Pos(L"\\") == 0) && (Directory.Pos(L"/") > 0);
  304. if (RemoteTransfer != FLAGSET(FOutputOptions, cooRemoteTransfer))
  305. {
  306. FOutputOptions =
  307. (FOutputOptions & ~cooRemoteTransfer) |
  308. FLAGMASK(RemoteTransfer, cooRemoteTransfer);
  309. AdjustTransferControls();
  310. }
  311. }
  312. UnicodeString InfoStr = FCopyParams.GetInfoStr(L"; ", FCopyParamAttrs);
  313. CopyParamLabel->Caption = InfoStr;
  314. CopyParamLabel->Hint = InfoStr;
  315. CopyParamLabel->ShowHint =
  316. (CopyParamLabel->Canvas->TextWidth(InfoStr) > (CopyParamLabel->Width * 3 / 2));
  317. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  318. EnableControl(QueueCheck2,
  319. ((FOptions & (coDisableQueue | coTemp)) == 0) && !RemoteTransfer);
  320. QueueIndividuallyCheck->Visible =
  321. FLAGCLEAR(FOptions, coNoQueueIndividually) &&
  322. QueueCheck2->Enabled && QueueCheck2->Checked &&
  323. (FFileList != NULL) && (FFileList->Count > 1);
  324. TransferSettingsButton->Style =
  325. FLAGCLEAR(FOptions, coDoNotUsePresets) ?
  326. TCustomButton::bsSplitButton : TCustomButton::bsPushButton;
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TCopyDialog::FormShow(TObject * /*Sender*/)
  330. {
  331. DebugAssert(FFileList && (FFileList->Count > 0));
  332. if (DirectoryEdit->Enabled && DirectoryEdit->Visible)
  333. {
  334. ActiveControl = DirectoryEdit;
  335. }
  336. else
  337. {
  338. ActiveControl = OkButton;
  339. }
  340. UpdateControls();
  341. InstallPathWordBreakProc(RemoteDirectoryEdit);
  342. InstallPathWordBreakProc(LocalDirectoryEdit);
  343. // Does not work when set from a contructor
  344. ShortCutHintPanel->Color = Application->HintColor;
  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. FToRemote ? L"RemoteTarget" : L"LocalTarget"];
  353. bool Result = (ShowModal() == DefaultResult(this));
  354. if (Result)
  355. {
  356. Configuration->BeginUpdate();
  357. try
  358. {
  359. if (FLAGSET(OutputOptions, cooSaveSettings) &&
  360. FLAGCLEAR(FOptions, coDisableSaveSettings))
  361. {
  362. GUIConfiguration->DefaultCopyParam = Params;
  363. }
  364. DirectoryEdit->SaveToHistory();
  365. CustomWinConfiguration->History[FToRemote ?
  366. L"RemoteTarget" : L"LocalTarget"] = DirectoryEdit->Items;
  367. if (FLAGSET(FOptions, coShortCutHint))
  368. {
  369. CustomWinConfiguration->CopyShortCutHintShown = true;
  370. }
  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 == DefaultResult(this))
  384. {
  385. if (!RemotePaths() && ((FOptions & coTemp) == 0))
  386. {
  387. UnicodeString Dir = Directory;
  388. UnicodeString Drive = ExtractFileDrive(Dir);
  389. if (!DirectoryExists(ApiPath(Dir)))
  390. {
  391. if (MessageDialog(MainInstructions(FMTLOAD(CREATE_LOCAL_DIRECTORY, (Dir))),
  392. qtConfirmation, qaOK | qaCancel, HELP_NONE) != qaCancel)
  393. {
  394. if (!ForceDirectories(ApiPath(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 && !IsFileNameMask(GetFileMask()) && (FFileList->Count > 1))
  412. {
  413. UnicodeString Message =
  414. FormatMultiFilesToOneConfirmation(DirectoryEdit->Text, RemotePaths());
  415. CanClose =
  416. (MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_NONE) != qaCancel);
  417. }
  418. if (CanClose)
  419. {
  420. ExitActiveControl(this);
  421. }
  422. }
  423. }
  424. //---------------------------------------------------------------------------
  425. void __fastcall TCopyDialog::LocalDirectoryBrowseButtonClick(
  426. TObject * /*Sender*/)
  427. {
  428. DebugAssert(!FToRemote);
  429. UnicodeString ADirectory;
  430. // if we are duplicating, we have remote path there
  431. if (!RemotePaths())
  432. {
  433. ADirectory = Directory;
  434. }
  435. if (SelectDirectory(ADirectory, LoadStr(SELECT_LOCAL_DIRECTORY), true))
  436. {
  437. Directory = ADirectory;
  438. UpdateControls();
  439. }
  440. }
  441. //---------------------------------------------------------------------------
  442. void __fastcall TCopyDialog::ControlChange(TObject * /*Sender*/)
  443. {
  444. UpdateControls();
  445. ResetSystemSettings(this);
  446. }
  447. //---------------------------------------------------------------------------
  448. void __fastcall TCopyDialog::TransferSettingsButtonClick(TObject * /*Sender*/)
  449. {
  450. if (FLAGCLEAR(FOptions, coDoNotUsePresets) && !SupportsSplitButton())
  451. {
  452. CopyParamListPopup(CalculatePopupRect(TransferSettingsButton), 0);
  453. }
  454. else
  455. {
  456. CopyParamGroupClick(NULL);
  457. }
  458. }
  459. //---------------------------------------------------------------------------
  460. void __fastcall TCopyDialog::GenerateCode()
  461. {
  462. TFilesSelected FilesSelected = FLAGSET(FOptions, coAllFiles) ? fsAll : fsList;
  463. DoGenerateTransferCodeDialog(FToRemote, FMove, FCopyParamAttrs, FSessionData, FilesSelected, FFileList, Directory, Params);
  464. }
  465. //---------------------------------------------------------------------------
  466. void __fastcall TCopyDialog::CopyParamClick(TObject * Sender)
  467. {
  468. // Save including the preset-unspecific queue properties,
  469. // so that they are preserved when assigning back later
  470. TGUICopyParamType Param = Params;
  471. bool PrevSaveSettings = FSaveSettings;
  472. int Result = CopyParamListPopupClick(Sender, Param, FPreset, FCopyParamAttrs, &FSaveSettings);
  473. if (Result < 0)
  474. {
  475. if (DebugAlwaysTrue(Result == -cplGenerateCode))
  476. {
  477. GenerateCode();
  478. }
  479. }
  480. else
  481. {
  482. if (Result > 0)
  483. {
  484. Params = Param;
  485. }
  486. else
  487. {
  488. UpdateControls();
  489. }
  490. if (PrevSaveSettings && !FSaveSettings)
  491. {
  492. NeverShowAgainCheck->Checked = false;
  493. }
  494. }
  495. }
  496. //---------------------------------------------------------------------------
  497. void __fastcall TCopyDialog::HelpButtonClick(TObject * /*Sender*/)
  498. {
  499. FormHelp(this);
  500. }
  501. //---------------------------------------------------------------------------
  502. void __fastcall TCopyDialog::CopyParamGroupClick(TObject * /*Sender*/)
  503. {
  504. if (CopyParamGroup->Enabled)
  505. {
  506. if (DoCopyParamCustomDialog(FCopyParams, FCopyParamAttrs))
  507. {
  508. UpdateControls();
  509. }
  510. }
  511. }
  512. //---------------------------------------------------------------------------
  513. void __fastcall TCopyDialog::CopyParamGroupContextPopup(TObject * /*Sender*/,
  514. TPoint & MousePos, bool & Handled)
  515. {
  516. if (FLAGCLEAR(FOptions, coDoNotUsePresets))
  517. {
  518. CopyParamListPopup(CalculatePopupRect(CopyParamGroup, MousePos), cplCustomizeDefault);
  519. Handled = true;
  520. }
  521. }
  522. //---------------------------------------------------------------------------
  523. void __fastcall TCopyDialog::CopyParamListPopup(TRect R, int AdditionalOptions)
  524. {
  525. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  526. ::CopyParamListPopup(R, FPresetsMenu,
  527. FCopyParams, FPreset, CopyParamClick,
  528. cplCustomize | AdditionalOptions |
  529. FLAGMASK(
  530. FLAGCLEAR(FOptions, coDisableSaveSettings) && !RemoteTransfer,
  531. cplSaveSettings) |
  532. FLAGMASK(FLAGCLEAR(FOutputOptions, cooRemoteTransfer) && FLAGCLEAR(FOptions, coTemp), cplGenerateCode),
  533. FCopyParamAttrs,
  534. FSaveSettings);
  535. }
  536. //---------------------------------------------------------------------------
  537. void __fastcall TCopyDialog::TransferSettingsButtonDropDownClick(TObject * /*Sender*/)
  538. {
  539. CopyParamListPopup(CalculatePopupRect(TransferSettingsButton), cplCustomizeDefault);
  540. }
  541. //---------------------------------------------------------------------------
  542. void __fastcall TCopyDialog::NeverShowAgainCheckClick(TObject * /*Sender*/)
  543. {
  544. FSaveSettings = NeverShowAgainCheck->Checked;
  545. UpdateControls();
  546. }
  547. //---------------------------------------------------------------------------
  548. void __fastcall TCopyDialog::ShortCutHintLabelClick(TObject * /*Sender*/)
  549. {
  550. DoPreferencesDialog(pmCommander);
  551. }
  552. //---------------------------------------------------------------------------