Copy.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 "Copy.h"
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. #pragma link "Rights"
  15. #pragma link "CopyParams"
  16. #pragma link "HistoryComboBox"
  17. #ifndef NO_RESOURCES
  18. #pragma resource "*.dfm"
  19. #endif
  20. //---------------------------------------------------------------------------
  21. bool __fastcall DoCopyDialog(bool ToRemote,
  22. bool Move, TStrings * FileList, UnicodeString & TargetDirectory,
  23. TGUICopyParamType * Params, int Options, int CopyParamAttrs, int * OutputOptions)
  24. {
  25. bool Result;
  26. TCopyDialog *CopyDialog = new TCopyDialog(Application);
  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. CopyDialog->ToRemote = ToRemote;
  36. CopyDialog->Options = Options;
  37. CopyDialog->CopyParamAttrs = CopyParamAttrs;
  38. if (OutputOptions != NULL)
  39. {
  40. CopyDialog->OutputOptions = *OutputOptions;
  41. }
  42. CopyDialog->Directory = TargetDirectory;
  43. CopyDialog->FileList = FileList;
  44. CopyDialog->Params = *Params;
  45. CopyDialog->Move = Move;
  46. Result = CopyDialog->Execute();
  47. if (Result)
  48. {
  49. TargetDirectory = CopyDialog->Directory;
  50. *Params = CopyDialog->Params;
  51. if (OutputOptions != NULL)
  52. {
  53. *OutputOptions = CopyDialog->OutputOptions;
  54. }
  55. }
  56. }
  57. __finally
  58. {
  59. delete CopyDialog;
  60. }
  61. return Result;
  62. }
  63. //---------------------------------------------------------------------------
  64. __fastcall TCopyDialog::TCopyDialog(TComponent* Owner)
  65. : TForm(Owner)
  66. {
  67. // on start set different value than we want to allow property-setter to proceed
  68. FToRemote = false;
  69. FMove = true;
  70. ToRemote = true;
  71. Move = false;
  72. FOptions = 0;
  73. FOutputOptions = 0;
  74. FCopyParamAttrs = 0;
  75. FPresetsMenu = new TPopupMenu(this);
  76. HotTrackLabel(CopyParamLabel);
  77. UseSystemSettings(this);
  78. }
  79. //---------------------------------------------------------------------------
  80. __fastcall TCopyDialog::~TCopyDialog()
  81. {
  82. delete FPresetsMenu;
  83. }
  84. //---------------------------------------------------------------------------
  85. void __fastcall TCopyDialog::AdjustTransferControls()
  86. {
  87. if (FileList && FileList->Count)
  88. {
  89. if (!ToRemote && !Move && FLAGSET(FOutputOptions, cooRemoteTransfer))
  90. {
  91. UnicodeString Label;
  92. if (FileList->Count == 1)
  93. {
  94. UnicodeString FileName;
  95. if (!ToRemote) 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 = LoadStr(!Move ? COPY_COPY : COPY_MOVE);
  108. // currently the copy dialog is shown when downloading to temp folder
  109. // only for drag&drop downloads, for we dare to display d&d specific prompt
  110. UnicodeString DirectionStr =
  111. LoadStr(((Options & coTemp) != 0) ? COPY_TODROP :
  112. (RemotePaths() ? COPY_TOREMOTE : COPY_TOLOCAL));
  113. if (FileList->Count == 1)
  114. {
  115. UnicodeString FileName;
  116. if (!ToRemote) FileName = UnixExtractFileName(FFileList->Strings[0]);
  117. else FileName = ExtractFileName(FFileList->Strings[0]);
  118. DirectoryLabel->Caption = FMTLOAD(COPY_FILE,
  119. (TransferStr, FileName, DirectionStr));
  120. }
  121. else
  122. {
  123. DirectoryLabel->Caption = FMTLOAD(COPY_FILES,
  124. (TransferStr, FFileList->Count, DirectionStr));
  125. }
  126. }
  127. }
  128. if (!Move)
  129. {
  130. if (!ToRemote && FLAGSET(FOutputOptions, cooRemoteTransfer))
  131. {
  132. Caption = LoadStr(REMOTE_COPY_TITLE);
  133. }
  134. else
  135. {
  136. Caption = LoadStr(COPY_COPY_CAPTION);
  137. }
  138. CopyButton->Caption = LoadStr(COPY_COPY_BUTTON);
  139. }
  140. else
  141. {
  142. Caption = LoadStr(COPY_MOVE_CAPTION);
  143. CopyButton->Caption = LoadStr(COPY_MOVE_BUTTON);
  144. }
  145. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  146. assert(FLAGSET(Options, coAllowRemoteTransfer) || !RemoteTransfer);
  147. EnableControl(NewerOnlyCheck, FLAGCLEAR(Options, coDisableNewerOnly) && !RemoteTransfer);
  148. EnableControl(TransferSettingsButton, !RemoteTransfer);
  149. EnableControl(CopyParamGroup, !RemoteTransfer);
  150. }
  151. //---------------------------------------------------------------------------
  152. void __fastcall TCopyDialog::AdjustControls()
  153. {
  154. NeverShowAgainCheck->Visible = FLAGSET(Options, coDoNotShowAgain);
  155. RemoteDirectoryEdit->Visible = false;
  156. LocalDirectoryEdit->Visible = false;
  157. DirectoryEdit->Visible = FLAGCLEAR(Options, coTemp);
  158. EnableControl(DirectoryEdit, FLAGCLEAR(Options, coDisableDirectory));
  159. EnableControl(DirectoryLabel, DirectoryEdit->Enabled);
  160. EnableControl(LocalDirectoryBrowseButton, DirectoryEdit->Enabled);
  161. DirectoryLabel->FocusControl = DirectoryEdit;
  162. UnicodeString QueueLabel = LoadStr(COPY_BACKGROUND);
  163. if (FLAGCLEAR(Options, coNoQueue))
  164. {
  165. QueueLabel = FMTLOAD(COPY_QUEUE, (QueueLabel));
  166. }
  167. QueueCheck2->Caption = QueueLabel;
  168. QueueIndividuallyCheck->Visible = FLAGCLEAR(Options, coNoQueueIndividually);
  169. AdjustTransferControls();
  170. LocalDirectoryBrowseButton->Visible = !ToRemote &&
  171. ((Options & coTemp) == 0);
  172. UpdateControls();
  173. }
  174. //---------------------------------------------------------------------------
  175. void __fastcall TCopyDialog::SetToRemote(bool value)
  176. {
  177. if (FToRemote != value)
  178. {
  179. UnicodeString ADirectory = DirectoryEdit->Text;
  180. FToRemote = value;
  181. DirectoryEdit->Text = ADirectory;
  182. AdjustControls();
  183. }
  184. }
  185. //---------------------------------------------------------------------------
  186. void __fastcall TCopyDialog::SetOptions(int value)
  187. {
  188. if (Options != value)
  189. {
  190. FOptions = value;
  191. AdjustControls();
  192. }
  193. }
  194. //---------------------------------------------------------------------------
  195. void __fastcall TCopyDialog::SetOutputOptions(int value)
  196. {
  197. if (OutputOptions != value)
  198. {
  199. FSaveSettings = FLAGSET(FOutputOptions, cooSaveSettings);
  200. NeverShowAgainCheck->Checked = FLAGSET(FOutputOptions, cooDoNotShowAgain);
  201. FOutputOptions = (value & ~(cooDoNotShowAgain | cooSaveSettings));
  202. }
  203. }
  204. //---------------------------------------------------------------------------
  205. int __fastcall TCopyDialog::GetOutputOptions()
  206. {
  207. return FOutputOptions |
  208. FLAGMASK(FSaveSettings, cooSaveSettings) |
  209. FLAGMASK(NeverShowAgainCheck->Checked, cooDoNotShowAgain);
  210. }
  211. //---------------------------------------------------------------------------
  212. void __fastcall TCopyDialog::SetCopyParamAttrs(int value)
  213. {
  214. FCopyParamAttrs = value;
  215. }
  216. //---------------------------------------------------------------------------
  217. int __fastcall TCopyDialog::GetCopyParamAttrs()
  218. {
  219. return FCopyParamAttrs;
  220. }
  221. //---------------------------------------------------------------------------
  222. THistoryComboBox * __fastcall TCopyDialog::GetDirectoryEdit()
  223. {
  224. return ToRemote ? RemoteDirectoryEdit : LocalDirectoryEdit;
  225. }
  226. //---------------------------------------------------------------------------
  227. bool __fastcall TCopyDialog::RemotePaths()
  228. {
  229. return (ToRemote || FLAGSET(FOutputOptions, cooRemoteTransfer));
  230. }
  231. //---------------------------------------------------------------------------
  232. UnicodeString __fastcall TCopyDialog::GetFileMask()
  233. {
  234. return ExtractFileName(DirectoryEdit->Text, RemotePaths());
  235. }
  236. //---------------------------------------------------------------------------
  237. void __fastcall TCopyDialog::SetParams(const TGUICopyParamType & value)
  238. {
  239. FParams = value;
  240. FCopyParams = value;
  241. DirectoryEdit->Text = Directory + FParams.FileMask;
  242. QueueCheck2->Checked = FParams.Queue;
  243. QueueIndividuallyCheck->Checked = FParams.QueueIndividually;
  244. NewerOnlyCheck->Checked = FLAGCLEAR(Options, coDisableNewerOnly) && FParams.NewerOnly;
  245. UpdateControls();
  246. }
  247. //---------------------------------------------------------------------------
  248. TGUICopyParamType __fastcall TCopyDialog::GetParams()
  249. {
  250. // overwrites TCopyParamType fields only
  251. FParams = FCopyParams;
  252. FParams.FileMask = GetFileMask();
  253. FParams.Queue = QueueCheck2->Checked;
  254. FParams.QueueIndividually = QueueIndividuallyCheck->Checked;
  255. FParams.NewerOnly = FLAGCLEAR(Options, coDisableNewerOnly) && NewerOnlyCheck->Checked;
  256. return FParams;
  257. }
  258. //---------------------------------------------------------------------------
  259. void __fastcall TCopyDialog::SetDirectory(UnicodeString value)
  260. {
  261. if (!value.IsEmpty())
  262. {
  263. value = RemotePaths() ?
  264. UnicodeString(UnixIncludeTrailingBackslash(value)) : IncludeTrailingBackslash(value);
  265. }
  266. DirectoryEdit->Text = value + GetFileMask();
  267. }
  268. //---------------------------------------------------------------------------
  269. UnicodeString __fastcall TCopyDialog::GetDirectory()
  270. {
  271. assert(DirectoryEdit);
  272. UnicodeString Result = DirectoryEdit->Text;
  273. if (RemotePaths())
  274. {
  275. Result = UnixExtractFilePath(Result);
  276. if (!Result.IsEmpty())
  277. {
  278. Result = UnixIncludeTrailingBackslash(Result);
  279. }
  280. }
  281. else
  282. {
  283. Result = ExtractFilePath(Result);
  284. if (!Result.IsEmpty())
  285. {
  286. Result = IncludeTrailingBackslash(Result);
  287. }
  288. }
  289. return Result;
  290. }
  291. //---------------------------------------------------------------------------
  292. void __fastcall TCopyDialog::SetFileList(TStrings * value)
  293. {
  294. if (FFileList != value)
  295. {
  296. FFileList = value;
  297. AdjustControls();
  298. }
  299. }
  300. //---------------------------------------------------------------------------
  301. void __fastcall TCopyDialog::UpdateControls()
  302. {
  303. if (!ToRemote && FLAGSET(Options, coAllowRemoteTransfer))
  304. {
  305. UnicodeString Directory = DirectoryEdit->Text;
  306. bool RemoteTransfer = (Directory.Pos(L"\\") == 0) && (Directory.Pos(L"/") > 0);
  307. if (RemoteTransfer != FLAGSET(FOutputOptions, cooRemoteTransfer))
  308. {
  309. FOutputOptions =
  310. (FOutputOptions & ~cooRemoteTransfer) |
  311. FLAGMASK(RemoteTransfer, cooRemoteTransfer);
  312. AdjustTransferControls();
  313. }
  314. }
  315. UnicodeString InfoStr = FCopyParams.GetInfoStr(L"; ", CopyParamAttrs);
  316. CopyParamLabel->Caption = InfoStr;
  317. CopyParamLabel->Hint = InfoStr;
  318. CopyParamLabel->ShowHint =
  319. (CopyParamLabel->Canvas->TextWidth(InfoStr) > (CopyParamLabel->Width * 3 / 2));
  320. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  321. EnableControl(QueueCheck2,
  322. ((Options & (coDisableQueue | coTemp)) == 0) && !RemoteTransfer);
  323. EnableControl(QueueIndividuallyCheck,
  324. QueueCheck2->Enabled && QueueCheck2->Checked &&
  325. FileList && (FileList->Count > 1));
  326. TransferSettingsButton->Style =
  327. FLAGCLEAR(Options, coDoNotUsePresets) ?
  328. TCustomButton::bsSplitButton : TCustomButton::bsPushButton;
  329. }
  330. //---------------------------------------------------------------------------
  331. void __fastcall TCopyDialog::SetMove(bool value)
  332. {
  333. if (Move != value)
  334. {
  335. FMove = value;
  336. AdjustControls();
  337. }
  338. }
  339. //---------------------------------------------------------------------------
  340. void __fastcall TCopyDialog::FormShow(TObject * /*Sender*/)
  341. {
  342. assert(FileList && (FileList->Count > 0));
  343. if (DirectoryEdit->Enabled && DirectoryEdit->Visible)
  344. {
  345. ActiveControl = DirectoryEdit;
  346. }
  347. else
  348. {
  349. ActiveControl = CopyButton;
  350. }
  351. UpdateControls();
  352. InstallPathWordBreakProc(RemoteDirectoryEdit);
  353. InstallPathWordBreakProc(LocalDirectoryEdit);
  354. }
  355. //---------------------------------------------------------------------------
  356. bool __fastcall TCopyDialog::Execute()
  357. {
  358. // at start assume that copy param is current preset
  359. FPreset = GUIConfiguration->CopyParamCurrent;
  360. DirectoryEdit->Items = CustomWinConfiguration->History[
  361. ToRemote ? L"RemoteTarget" : L"LocalTarget"];
  362. bool Result = (ShowModal() == mrOk);
  363. if (Result)
  364. {
  365. Configuration->BeginUpdate();
  366. try
  367. {
  368. if (FLAGSET(OutputOptions, cooSaveSettings) &&
  369. FLAGCLEAR(Options, coDisableSaveSettings))
  370. {
  371. GUIConfiguration->DefaultCopyParam = Params;
  372. }
  373. DirectoryEdit->SaveToHistory();
  374. CustomWinConfiguration->History[ToRemote ?
  375. L"RemoteTarget" : L"LocalTarget"] = DirectoryEdit->Items;
  376. }
  377. __finally
  378. {
  379. Configuration->EndUpdate();
  380. }
  381. }
  382. return Result;
  383. }
  384. //---------------------------------------------------------------------------
  385. void __fastcall TCopyDialog::FormCloseQuery(TObject * /*Sender*/,
  386. bool &CanClose)
  387. {
  388. if (ModalResult != mrCancel)
  389. {
  390. if (!RemotePaths() && ((Options & coTemp) == 0))
  391. {
  392. UnicodeString Dir = Directory;
  393. UnicodeString Drive = ExtractFileDrive(Dir);
  394. if (!DirectoryExists(Dir))
  395. {
  396. if (MessageDialog(FMTLOAD(CREATE_LOCAL_DIRECTORY, (Dir)),
  397. qtConfirmation, qaOK | qaCancel, HELP_NONE) != qaCancel)
  398. {
  399. if (!ForceDirectories(Dir))
  400. {
  401. SimpleErrorDialog(FMTLOAD(CREATE_LOCAL_DIR_ERROR, (Dir)));
  402. CanClose = false;
  403. }
  404. }
  405. else
  406. {
  407. CanClose = False;
  408. }
  409. }
  410. if (!CanClose)
  411. {
  412. DirectoryEdit->SelectAll();
  413. DirectoryEdit->SetFocus();
  414. }
  415. };
  416. if (CanClose)
  417. {
  418. ExitActiveControl(this);
  419. }
  420. }
  421. }
  422. //---------------------------------------------------------------------------
  423. void __fastcall TCopyDialog::LocalDirectoryBrowseButtonClick(
  424. TObject * /*Sender*/)
  425. {
  426. assert(!ToRemote);
  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(Options, coDoNotUsePresets) && !SupportsSplitButton())
  449. {
  450. CopyParamListPopup(
  451. TransferSettingsButton->ClientToScreen(TPoint(0, TransferSettingsButton->Height)),
  452. 0);
  453. }
  454. else
  455. {
  456. CopyParamGroupClick(NULL);
  457. }
  458. }
  459. //---------------------------------------------------------------------------
  460. void __fastcall TCopyDialog::CopyParamClick(TObject * Sender)
  461. {
  462. TCopyParamType Param = Params;
  463. if (CopyParamListPopupClick(Sender, Param, FPreset, CopyParamAttrs))
  464. {
  465. Params = Param;
  466. }
  467. }
  468. //---------------------------------------------------------------------------
  469. void __fastcall TCopyDialog::HelpButtonClick(TObject * /*Sender*/)
  470. {
  471. FormHelp(this);
  472. }
  473. //---------------------------------------------------------------------------
  474. void __fastcall TCopyDialog::CopyParamGroupClick(TObject * /*Sender*/)
  475. {
  476. if (CopyParamGroup->Enabled)
  477. {
  478. if (DoCopyParamCustomDialog(FCopyParams, CopyParamAttrs))
  479. {
  480. UpdateControls();
  481. }
  482. }
  483. }
  484. //---------------------------------------------------------------------------
  485. void __fastcall TCopyDialog::CopyParamGroupContextPopup(TObject * /*Sender*/,
  486. TPoint & MousePos, bool & Handled)
  487. {
  488. if (FLAGCLEAR(Options, coDoNotUsePresets))
  489. {
  490. CopyParamListPopup(CopyParamGroup->ClientToScreen(MousePos), cplCustomizeDefault);
  491. Handled = true;
  492. }
  493. }
  494. //---------------------------------------------------------------------------
  495. void __fastcall TCopyDialog::CopyParamListPopup(TPoint P, int AdditionalOptions)
  496. {
  497. ::CopyParamListPopup(P, FPresetsMenu,
  498. FCopyParams, FPreset, CopyParamClick,
  499. cplCustomize | AdditionalOptions |
  500. FLAGMASK(
  501. FLAGCLEAR(Options, coDisableSaveSettings) &&
  502. FLAGCLEAR(FOutputOptions, cooRemoteTransfer),
  503. cplSaveSettings),
  504. &FSaveSettings);
  505. }
  506. //---------------------------------------------------------------------------
  507. void __fastcall TCopyDialog::TransferSettingsButtonDropDownClick(TObject * /*Sender*/)
  508. {
  509. CopyParamListPopup(
  510. TransferSettingsButton->ClientToScreen(TPoint(0, TransferSettingsButton->Height)),
  511. cplCustomizeDefault);
  512. }
  513. //---------------------------------------------------------------------------