Copy.cpp 18 KB

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