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