Copy.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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, int * OutputOptions)
  25. {
  26. bool Result;
  27. TCopyDialog *CopyDialog = new TCopyDialog(Application, ToRemote, Move, FileList, Options, CopyParamAttrs);
  28. try
  29. {
  30. if (FLAGSET(CopyParamAttrs, cpaNoTransferMode))
  31. {
  32. // If local and remote EOL types are the same, there is no need
  33. // for ASCII (or Automatic) mode
  34. Params->TransferMode = tmBinary;
  35. }
  36. if (OutputOptions != NULL)
  37. {
  38. CopyDialog->OutputOptions = *OutputOptions;
  39. }
  40. CopyDialog->Directory = TargetDirectory;
  41. CopyDialog->Params = *Params;
  42. Result = CopyDialog->Execute();
  43. if (Result)
  44. {
  45. TargetDirectory = CopyDialog->Directory;
  46. *Params = CopyDialog->Params;
  47. if (OutputOptions != NULL)
  48. {
  49. *OutputOptions = CopyDialog->OutputOptions;
  50. }
  51. }
  52. }
  53. __finally
  54. {
  55. delete CopyDialog;
  56. }
  57. return Result;
  58. }
  59. //---------------------------------------------------------------------------
  60. __fastcall TCopyDialog::TCopyDialog(
  61. TComponent* Owner, bool ToRemote, bool Move, TStrings * FileList, int Options, int CopyParamAttrs)
  62. : TForm(Owner)
  63. {
  64. FToRemote = ToRemote;
  65. FMove = Move;
  66. FOptions = Options;
  67. FCopyParamAttrs = CopyParamAttrs;
  68. FFileList = FileList;
  69. FOutputOptions = 0;
  70. AdjustControls();
  71. FPresetsMenu = new TPopupMenu(this);
  72. HotTrackLabel(CopyParamLabel);
  73. CopyParamListButton(TransferSettingsButton);
  74. HotTrackLabel(ShortCutHintLabel);
  75. UseSystemSettings(this);
  76. }
  77. //---------------------------------------------------------------------------
  78. __fastcall TCopyDialog::~TCopyDialog()
  79. {
  80. delete FPresetsMenu;
  81. }
  82. //---------------------------------------------------------------------------
  83. void __fastcall TCopyDialog::AdjustTransferControls()
  84. {
  85. if (FFileList && FFileList->Count)
  86. {
  87. if (!FToRemote && !FMove && FLAGSET(FOutputOptions, cooRemoteTransfer))
  88. {
  89. UnicodeString Label;
  90. if (FFileList->Count == 1)
  91. {
  92. UnicodeString FileName;
  93. if (!FToRemote) FileName = UnixExtractFileName(FFileList->Strings[0]);
  94. else FileName = ExtractFileName(FFileList->Strings[0]);
  95. Label = FMTLOAD(REMOTE_COPY_FILE, (FileName));
  96. }
  97. else
  98. {
  99. Label = FMTLOAD(REMOTE_COPY_FILES, (FFileList->Count));
  100. }
  101. DirectoryLabel->Caption = Label;
  102. }
  103. else
  104. {
  105. UnicodeString TransferStr =
  106. LoadStr(RemotePaths() ? COPY_COPY_TOREMOTE : COPY_COPY_TOLOCAL);
  107. // currently the copy dialog is shown when downloading to temp folder
  108. // only for drag&drop downloads, for we dare to display d&d specific prompt
  109. UnicodeString DirectionStr =
  110. LoadStr(((FOptions & coTemp) != 0) ? COPY_TODROP :
  111. (RemotePaths() ? COPY_TOREMOTE : COPY_TOLOCAL));
  112. if (FFileList->Count == 1)
  113. {
  114. UnicodeString FileName;
  115. if (!FToRemote) FileName = UnixExtractFileName(FFileList->Strings[0]);
  116. else FileName = ExtractFileName(FFileList->Strings[0]);
  117. DirectoryLabel->Caption = FMTLOAD((FMove ? MOVE_FILE : COPY_FILE),
  118. (TransferStr, FileName, DirectionStr));
  119. }
  120. else
  121. {
  122. DirectoryLabel->Caption = FMTLOAD((FMove ? MOVE_FILES : COPY_FILES),
  123. (TransferStr, FFileList->Count, DirectionStr));
  124. }
  125. }
  126. }
  127. UnicodeString ImageName;
  128. UnicodeString ACaption;
  129. if (!FMove)
  130. {
  131. if (!FToRemote && FLAGSET(FOutputOptions, cooRemoteTransfer))
  132. {
  133. ACaption = LoadStr(REMOTE_COPY_TITLE);
  134. ImageName = L"Duplicate";
  135. }
  136. else
  137. {
  138. if (RemotePaths())
  139. {
  140. ACaption = LoadStr(COPY_COPY_TOREMOTE_CAPTION);
  141. ImageName = L"Upload File";
  142. }
  143. else
  144. {
  145. ACaption = LoadStr(COPY_COPY_TOLOCAL_CAPTION);
  146. ImageName = L"Download File";
  147. }
  148. }
  149. }
  150. else
  151. {
  152. if (!FToRemote && FLAGSET(FOutputOptions, cooRemoteTransfer))
  153. {
  154. ACaption = LoadStr(COPY_MOVE_CAPTION);
  155. ImageName = L"Move To";
  156. }
  157. else
  158. {
  159. if (RemotePaths())
  160. {
  161. ACaption = LoadStr(COPY_MOVE_TOREMOTE_CAPTION);
  162. ImageName = L"Upload File Remove Original";
  163. }
  164. else
  165. {
  166. ACaption = LoadStr(COPY_MOVE_TOLOCAL_CAPTION);
  167. ImageName = L"Download File Remove Original";
  168. }
  169. }
  170. }
  171. Caption = FormatFormCaption(this, ACaption);
  172. LoadDialogImage(Image, ImageName);
  173. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  174. DebugAssert(FLAGSET(FOptions, coAllowRemoteTransfer) || !RemoteTransfer);
  175. EnableControl(TransferSettingsButton, !RemoteTransfer);
  176. EnableControl(CopyParamGroup, !RemoteTransfer);
  177. }
  178. //---------------------------------------------------------------------------
  179. void __fastcall TCopyDialog::AdjustControls()
  180. {
  181. RemoteDirectoryEdit->Visible = false;
  182. LocalDirectoryEdit->Visible = false;
  183. DirectoryEdit->Visible = FLAGCLEAR(FOptions, coTemp);
  184. EnableControl(DirectoryEdit, FLAGCLEAR(FOptions, coDisableDirectory));
  185. EnableControl(DirectoryLabel, DirectoryEdit->Enabled);
  186. EnableControl(LocalDirectoryBrowseButton, DirectoryEdit->Enabled);
  187. DirectoryLabel->FocusControl = DirectoryEdit;
  188. UnicodeString QueueLabel = LoadStr(COPY_BACKGROUND);
  189. if (FLAGCLEAR(FOptions, coNoQueue))
  190. {
  191. QueueLabel = FMTLOAD(COPY_QUEUE, (QueueLabel));
  192. }
  193. QueueCheck2->Caption = QueueLabel;
  194. AdjustTransferControls();
  195. LocalDirectoryBrowseButton->Visible = !FToRemote &&
  196. FLAGCLEAR(FOptions, coTemp);
  197. if (FLAGCLEAR(FOptions, coDoNotShowAgain))
  198. {
  199. NeverShowAgainCheck->Visible = false;
  200. ClientHeight = ClientHeight -
  201. (ShortCutHintPanel->Top - NeverShowAgainCheck->Top);
  202. }
  203. if (FLAGCLEAR(FOptions, coShortCutHint) || CustomWinConfiguration->CopyShortCutHintShown)
  204. {
  205. ShortCutHintPanel->Visible = false;
  206. ClientHeight = ClientHeight - ShortCutHintPanel->Height;
  207. }
  208. UpdateControls();
  209. }
  210. //---------------------------------------------------------------------------
  211. void __fastcall TCopyDialog::SetOutputOptions(int value)
  212. {
  213. if (OutputOptions != value)
  214. {
  215. FSaveSettings = FLAGSET(value, cooSaveSettings);
  216. NeverShowAgainCheck->Checked = FLAGSET(value, cooDoNotShowAgain);
  217. FOutputOptions = (value & ~(cooDoNotShowAgain | cooSaveSettings));
  218. }
  219. }
  220. //---------------------------------------------------------------------------
  221. int __fastcall TCopyDialog::GetOutputOptions()
  222. {
  223. return FOutputOptions |
  224. FLAGMASK(FSaveSettings, cooSaveSettings) |
  225. FLAGMASK(NeverShowAgainCheck->Checked, cooDoNotShowAgain);
  226. }
  227. //---------------------------------------------------------------------------
  228. THistoryComboBox * __fastcall TCopyDialog::GetDirectoryEdit()
  229. {
  230. return FToRemote ? RemoteDirectoryEdit : LocalDirectoryEdit;
  231. }
  232. //---------------------------------------------------------------------------
  233. bool __fastcall TCopyDialog::RemotePaths()
  234. {
  235. return (FToRemote || FLAGSET(FOutputOptions, cooRemoteTransfer));
  236. }
  237. //---------------------------------------------------------------------------
  238. UnicodeString __fastcall TCopyDialog::GetFileMask()
  239. {
  240. return ExtractFileName(DirectoryEdit->Text, RemotePaths());
  241. }
  242. //---------------------------------------------------------------------------
  243. void __fastcall TCopyDialog::SetParams(const TGUICopyParamType & value)
  244. {
  245. FParams = value;
  246. FCopyParams = value;
  247. DirectoryEdit->Text = Directory + FParams.FileMask;
  248. QueueCheck2->Checked = FParams.Queue;
  249. QueueIndividuallyCheck->Checked = FParams.QueueIndividually;
  250. UpdateControls();
  251. }
  252. //---------------------------------------------------------------------------
  253. TGUICopyParamType __fastcall TCopyDialog::GetParams()
  254. {
  255. // overwrites TCopyParamType fields only
  256. FParams = FCopyParams;
  257. FParams.FileMask = GetFileMask();
  258. FParams.Queue = QueueCheck2->Checked;
  259. FParams.QueueIndividually = QueueIndividuallyCheck->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. CopyParamLabel->Caption = InfoStr;
  312. CopyParamLabel->Hint = InfoStr;
  313. CopyParamLabel->ShowHint =
  314. (CopyParamLabel->Canvas->TextWidth(InfoStr) > (CopyParamLabel->Width * 3 / 2));
  315. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  316. EnableControl(QueueCheck2,
  317. ((FOptions & (coDisableQueue | coTemp)) == 0) && !RemoteTransfer);
  318. QueueIndividuallyCheck->Visible =
  319. FLAGCLEAR(FOptions, coNoQueueIndividually) &&
  320. QueueCheck2->Enabled && QueueCheck2->Checked &&
  321. (FFileList != NULL) && (FFileList->Count > 1);
  322. TransferSettingsButton->Style =
  323. FLAGCLEAR(FOptions, coDoNotUsePresets) ?
  324. TCustomButton::bsSplitButton : TCustomButton::bsPushButton;
  325. }
  326. //---------------------------------------------------------------------------
  327. void __fastcall TCopyDialog::FormShow(TObject * /*Sender*/)
  328. {
  329. DebugAssert(FFileList && (FFileList->Count > 0));
  330. if (DirectoryEdit->Enabled && DirectoryEdit->Visible)
  331. {
  332. ActiveControl = DirectoryEdit;
  333. }
  334. else
  335. {
  336. ActiveControl = OkButton;
  337. }
  338. UpdateControls();
  339. InstallPathWordBreakProc(RemoteDirectoryEdit);
  340. InstallPathWordBreakProc(LocalDirectoryEdit);
  341. // Does not work when set from a contructor
  342. ShortCutHintPanel->Color = Application->HintColor;
  343. }
  344. //---------------------------------------------------------------------------
  345. bool __fastcall TCopyDialog::Execute()
  346. {
  347. // at start assume that copy param is current preset
  348. FPreset = GUIConfiguration->CopyParamCurrent;
  349. DirectoryEdit->Items = CustomWinConfiguration->History[
  350. FToRemote ? L"RemoteTarget" : L"LocalTarget"];
  351. bool Result = (ShowModal() == DefaultResult(this));
  352. if (Result)
  353. {
  354. Configuration->BeginUpdate();
  355. try
  356. {
  357. if (FLAGSET(OutputOptions, cooSaveSettings) &&
  358. FLAGCLEAR(FOptions, coDisableSaveSettings))
  359. {
  360. GUIConfiguration->DefaultCopyParam = Params;
  361. }
  362. DirectoryEdit->SaveToHistory();
  363. CustomWinConfiguration->History[FToRemote ?
  364. L"RemoteTarget" : L"LocalTarget"] = DirectoryEdit->Items;
  365. if (FLAGSET(FOptions, coShortCutHint))
  366. {
  367. CustomWinConfiguration->CopyShortCutHintShown = true;
  368. }
  369. }
  370. __finally
  371. {
  372. Configuration->EndUpdate();
  373. }
  374. }
  375. return Result;
  376. }
  377. //---------------------------------------------------------------------------
  378. void __fastcall TCopyDialog::FormCloseQuery(TObject * /*Sender*/,
  379. bool &CanClose)
  380. {
  381. if (ModalResult == DefaultResult(this))
  382. {
  383. if (!RemotePaths() && ((FOptions & coTemp) == 0))
  384. {
  385. UnicodeString Dir = Directory;
  386. UnicodeString Drive = ExtractFileDrive(Dir);
  387. if (!DirectoryExists(ApiPath(Dir)))
  388. {
  389. if (MessageDialog(MainInstructions(FMTLOAD(CREATE_LOCAL_DIRECTORY, (Dir))),
  390. qtConfirmation, qaOK | qaCancel, HELP_NONE) != qaCancel)
  391. {
  392. if (!ForceDirectories(ApiPath(Dir)))
  393. {
  394. SimpleErrorDialog(FMTLOAD(CREATE_LOCAL_DIR_ERROR, (Dir)));
  395. CanClose = false;
  396. }
  397. }
  398. else
  399. {
  400. CanClose = False;
  401. }
  402. }
  403. if (!CanClose)
  404. {
  405. DirectoryEdit->SelectAll();
  406. DirectoryEdit->SetFocus();
  407. }
  408. }
  409. if (CanClose && !IsFileNameMask(GetFileMask()) && (FFileList->Count > 1))
  410. {
  411. UnicodeString Message =
  412. FormatMultiFilesToOneConfirmation(DirectoryEdit->Text, RemotePaths());
  413. CanClose =
  414. (MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_NONE) != qaCancel);
  415. }
  416. if (CanClose)
  417. {
  418. ExitActiveControl(this);
  419. }
  420. }
  421. }
  422. //---------------------------------------------------------------------------
  423. void __fastcall TCopyDialog::LocalDirectoryBrowseButtonClick(
  424. TObject * /*Sender*/)
  425. {
  426. DebugAssert(!FToRemote);
  427. UnicodeString ADirectory;
  428. // if we are duplicating, we have remote path there
  429. if (!RemotePaths())
  430. {
  431. ADirectory = Directory;
  432. }
  433. if (SelectDirectory(ADirectory, LoadStr(SELECT_LOCAL_DIRECTORY), true))
  434. {
  435. Directory = ADirectory;
  436. UpdateControls();
  437. }
  438. }
  439. //---------------------------------------------------------------------------
  440. void __fastcall TCopyDialog::ControlChange(TObject * /*Sender*/)
  441. {
  442. UpdateControls();
  443. ResetSystemSettings(this);
  444. }
  445. //---------------------------------------------------------------------------
  446. void __fastcall TCopyDialog::TransferSettingsButtonClick(TObject * /*Sender*/)
  447. {
  448. if (FLAGCLEAR(FOptions, coDoNotUsePresets) && !SupportsSplitButton())
  449. {
  450. CopyParamListPopup(CalculatePopupRect(TransferSettingsButton), 0);
  451. }
  452. else
  453. {
  454. CopyParamGroupClick(NULL);
  455. }
  456. }
  457. //---------------------------------------------------------------------------
  458. void __fastcall TCopyDialog::CopyParamClick(TObject * Sender)
  459. {
  460. // Save including the preset-unspecific queue properties,
  461. // so that they are preserved when assigning back later
  462. TGUICopyParamType Param = Params;
  463. bool PrevSaveSettings = FSaveSettings;
  464. if (CopyParamListPopupClick(Sender, Param, FPreset, FCopyParamAttrs, &FSaveSettings))
  465. {
  466. Params = Param;
  467. }
  468. else
  469. {
  470. UpdateControls();
  471. }
  472. if (PrevSaveSettings && !FSaveSettings)
  473. {
  474. NeverShowAgainCheck->Checked = false;
  475. }
  476. }
  477. //---------------------------------------------------------------------------
  478. void __fastcall TCopyDialog::HelpButtonClick(TObject * /*Sender*/)
  479. {
  480. FormHelp(this);
  481. }
  482. //---------------------------------------------------------------------------
  483. void __fastcall TCopyDialog::CopyParamGroupClick(TObject * /*Sender*/)
  484. {
  485. if (CopyParamGroup->Enabled)
  486. {
  487. if (DoCopyParamCustomDialog(FCopyParams, FCopyParamAttrs))
  488. {
  489. UpdateControls();
  490. }
  491. }
  492. }
  493. //---------------------------------------------------------------------------
  494. void __fastcall TCopyDialog::CopyParamGroupContextPopup(TObject * /*Sender*/,
  495. TPoint & MousePos, bool & Handled)
  496. {
  497. if (FLAGCLEAR(FOptions, coDoNotUsePresets))
  498. {
  499. CopyParamListPopup(CalculatePopupRect(CopyParamGroup, MousePos), cplCustomizeDefault);
  500. Handled = true;
  501. }
  502. }
  503. //---------------------------------------------------------------------------
  504. void __fastcall TCopyDialog::CopyParamListPopup(TRect R, int AdditionalOptions)
  505. {
  506. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  507. ::CopyParamListPopup(R, FPresetsMenu,
  508. FCopyParams, FPreset, CopyParamClick,
  509. cplCustomize | AdditionalOptions |
  510. FLAGMASK(
  511. FLAGCLEAR(FOptions, coDisableSaveSettings) && !RemoteTransfer,
  512. cplSaveSettings),
  513. FCopyParamAttrs,
  514. FSaveSettings);
  515. }
  516. //---------------------------------------------------------------------------
  517. void __fastcall TCopyDialog::TransferSettingsButtonDropDownClick(TObject * /*Sender*/)
  518. {
  519. CopyParamListPopup(CalculatePopupRect(TransferSettingsButton), cplCustomizeDefault);
  520. }
  521. //---------------------------------------------------------------------------
  522. void __fastcall TCopyDialog::NeverShowAgainCheckClick(TObject * /*Sender*/)
  523. {
  524. FSaveSettings = NeverShowAgainCheck->Checked;
  525. UpdateControls();
  526. }
  527. //---------------------------------------------------------------------------
  528. void __fastcall TCopyDialog::ShortCutHintLabelClick(TObject * /*Sender*/)
  529. {
  530. DoPreferencesDialog(pmCommander);
  531. }
  532. //---------------------------------------------------------------------------