Copy.cpp 17 KB

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