1
0

Copy.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. SetLabelHintPopup(CopyParamLabel, InfoStr);
  314. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  315. EnableControl(QueueCheck2,
  316. ((FOptions & (coDisableQueue | coTemp)) == 0) && !RemoteTransfer);
  317. QueueIndividuallyCheck->Visible =
  318. FLAGCLEAR(FOptions, coNoQueueIndividually) &&
  319. QueueCheck2->Enabled && QueueCheck2->Checked &&
  320. (FFileList != NULL) && (FFileList->Count > 1);
  321. TransferSettingsButton->Style =
  322. FLAGCLEAR(FOptions, coDoNotUsePresets) ?
  323. TCustomButton::bsSplitButton : TCustomButton::bsPushButton;
  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 (FLAGCLEAR(FOptions, coDoNotUsePresets) && !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, FCopyParamAttrs, 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, FCopyParamAttrs, &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, FCopyParamAttrs))
  504. {
  505. UpdateControls();
  506. }
  507. }
  508. }
  509. //---------------------------------------------------------------------------
  510. void __fastcall TCopyDialog::CopyParamGroupContextPopup(TObject * /*Sender*/,
  511. TPoint & MousePos, bool & Handled)
  512. {
  513. if (FLAGCLEAR(FOptions, coDoNotUsePresets))
  514. {
  515. CopyParamListPopup(CalculatePopupRect(CopyParamGroup, MousePos), cplCustomizeDefault);
  516. Handled = true;
  517. }
  518. }
  519. //---------------------------------------------------------------------------
  520. void __fastcall TCopyDialog::CopyParamListPopup(TRect R, int AdditionalOptions)
  521. {
  522. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  523. ::CopyParamListPopup(R, FPresetsMenu,
  524. FCopyParams, FPreset, CopyParamClick,
  525. cplCustomize | AdditionalOptions |
  526. FLAGMASK(
  527. FLAGCLEAR(FOptions, coDisableSaveSettings) && !RemoteTransfer,
  528. cplSaveSettings) |
  529. FLAGMASK(FLAGCLEAR(FOutputOptions, cooRemoteTransfer) && FLAGCLEAR(FOptions, coTemp), cplGenerateCode),
  530. FCopyParamAttrs,
  531. FSaveSettings);
  532. }
  533. //---------------------------------------------------------------------------
  534. void __fastcall TCopyDialog::TransferSettingsButtonDropDownClick(TObject * /*Sender*/)
  535. {
  536. CopyParamListPopup(CalculatePopupRect(TransferSettingsButton), cplCustomizeDefault);
  537. }
  538. //---------------------------------------------------------------------------
  539. void __fastcall TCopyDialog::NeverShowAgainCheckClick(TObject * /*Sender*/)
  540. {
  541. FSaveSettings = NeverShowAgainCheck->Checked;
  542. UpdateControls();
  543. }
  544. //---------------------------------------------------------------------------
  545. void __fastcall TCopyDialog::ShortCutHintLabelClick(TObject * /*Sender*/)
  546. {
  547. DoPreferencesDialog(pmCommander);
  548. }
  549. //---------------------------------------------------------------------------