Copy.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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, AnsiString & 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. UseSystemSettings(this);
  77. }
  78. //---------------------------------------------------------------------------
  79. __fastcall TCopyDialog::~TCopyDialog()
  80. {
  81. delete FPresetsMenu;
  82. }
  83. //---------------------------------------------------------------------------
  84. void __fastcall TCopyDialog::AdjustTransferControls()
  85. {
  86. if (FileList && FileList->Count)
  87. {
  88. if (!ToRemote && !Move && FLAGSET(FOutputOptions, cooRemoteTransfer))
  89. {
  90. AnsiString Label;
  91. if (FileList->Count == 1)
  92. {
  93. AnsiString FileName;
  94. if (!ToRemote) FileName = UnixExtractFileName(FFileList->Strings[0]);
  95. else FileName = ExtractFileName(FFileList->Strings[0]);
  96. Label = FMTLOAD(REMOTE_COPY_FILE, (FileName));
  97. }
  98. else
  99. {
  100. Label = FMTLOAD(REMOTE_COPY_FILES, (FFileList->Count));
  101. }
  102. DirectoryLabel->Caption = Label;
  103. }
  104. else
  105. {
  106. AnsiString TransferStr = LoadStr(!Move ? COPY_COPY : COPY_MOVE);
  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. AnsiString DirectionStr =
  110. LoadStr(((Options & coTemp) != 0) ? COPY_TODROP :
  111. (RemotePaths() ? COPY_TOREMOTE : COPY_TOLOCAL));
  112. if (FileList->Count == 1)
  113. {
  114. AnsiString FileName;
  115. if (!ToRemote) FileName = UnixExtractFileName(FFileList->Strings[0]);
  116. else FileName = ExtractFileName(FFileList->Strings[0]);
  117. DirectoryLabel->Caption = FMTLOAD(COPY_FILE,
  118. (TransferStr, FileName, DirectionStr));
  119. }
  120. else
  121. {
  122. DirectoryLabel->Caption = FMTLOAD(COPY_FILES,
  123. (TransferStr, FFileList->Count, DirectionStr));
  124. }
  125. }
  126. }
  127. if (!Move)
  128. {
  129. if (!ToRemote && FLAGSET(FOutputOptions, cooRemoteTransfer))
  130. {
  131. Caption = LoadStr(REMOTE_COPY_TITLE);
  132. }
  133. else
  134. {
  135. Caption = LoadStr(COPY_COPY_CAPTION);
  136. }
  137. CopyButton->Caption = LoadStr(COPY_COPY_BUTTON);
  138. }
  139. else
  140. {
  141. Caption = LoadStr(COPY_MOVE_CAPTION);
  142. CopyButton->Caption = LoadStr(COPY_MOVE_BUTTON);
  143. }
  144. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  145. assert(FLAGSET(Options, coAllowRemoteTransfer) || !RemoteTransfer);
  146. EnableControl(NewerOnlyCheck, FLAGCLEAR(Options, coDisableNewerOnly) && !RemoteTransfer);
  147. EnableControl(TransferSettingsButton, !RemoteTransfer);
  148. EnableControl(CopyParamGroup, !RemoteTransfer);
  149. }
  150. //---------------------------------------------------------------------------
  151. void __fastcall TCopyDialog::AdjustControls()
  152. {
  153. NeverShowAgainCheck->Visible = FLAGSET(Options, coDoNotShowAgain);
  154. RemoteDirectoryEdit->Visible = false;
  155. LocalDirectoryEdit->Visible = false;
  156. DirectoryEdit->Visible = FLAGCLEAR(Options, coTemp);
  157. EnableControl(DirectoryEdit, FLAGCLEAR(Options, coDisableDirectory));
  158. EnableControl(DirectoryLabel, DirectoryEdit->Enabled);
  159. EnableControl(LocalDirectoryBrowseButton, DirectoryEdit->Enabled);
  160. DirectoryLabel->FocusControl = DirectoryEdit;
  161. AnsiString QueueLabel = LoadStr(COPY_BACKGROUND);
  162. if (FLAGCLEAR(Options, coNoQueue))
  163. {
  164. QueueLabel = FMTLOAD(COPY_QUEUE, (QueueLabel));
  165. }
  166. QueueCheck2->Caption = QueueLabel;
  167. QueueIndividuallyCheck->Visible = FLAGCLEAR(Options, coNoQueueIndividually);
  168. AdjustTransferControls();
  169. LocalDirectoryBrowseButton->Visible = !ToRemote &&
  170. ((Options & coTemp) == 0);
  171. UpdateControls();
  172. }
  173. //---------------------------------------------------------------------------
  174. void __fastcall TCopyDialog::SetToRemote(bool value)
  175. {
  176. if (FToRemote != value)
  177. {
  178. AnsiString ADirectory = DirectoryEdit->Text;
  179. FToRemote = value;
  180. DirectoryEdit->Text = ADirectory;
  181. AdjustControls();
  182. }
  183. }
  184. //---------------------------------------------------------------------------
  185. void __fastcall TCopyDialog::SetOptions(int value)
  186. {
  187. if (Options != value)
  188. {
  189. FOptions = value;
  190. AdjustControls();
  191. }
  192. }
  193. //---------------------------------------------------------------------------
  194. void __fastcall TCopyDialog::SetOutputOptions(int value)
  195. {
  196. if (OutputOptions != value)
  197. {
  198. FSaveSettings = FLAGSET(FOutputOptions, cooSaveSettings);
  199. NeverShowAgainCheck->Checked = FLAGSET(FOutputOptions, cooDoNotShowAgain);
  200. FOutputOptions = (value & ~(cooDoNotShowAgain | cooSaveSettings));
  201. }
  202. }
  203. //---------------------------------------------------------------------------
  204. int __fastcall TCopyDialog::GetOutputOptions()
  205. {
  206. return FOutputOptions |
  207. FLAGMASK(FSaveSettings, cooSaveSettings) |
  208. FLAGMASK(NeverShowAgainCheck->Checked, cooDoNotShowAgain);
  209. }
  210. //---------------------------------------------------------------------------
  211. void __fastcall TCopyDialog::SetCopyParamAttrs(int value)
  212. {
  213. FCopyParamAttrs = value;
  214. }
  215. //---------------------------------------------------------------------------
  216. int __fastcall TCopyDialog::GetCopyParamAttrs()
  217. {
  218. return FCopyParamAttrs;
  219. }
  220. //---------------------------------------------------------------------------
  221. THistoryComboBox * __fastcall TCopyDialog::GetDirectoryEdit()
  222. {
  223. return ToRemote ? RemoteDirectoryEdit : LocalDirectoryEdit;
  224. }
  225. //---------------------------------------------------------------------------
  226. bool __fastcall TCopyDialog::RemotePaths()
  227. {
  228. return (ToRemote || FLAGSET(FOutputOptions, cooRemoteTransfer));
  229. }
  230. //---------------------------------------------------------------------------
  231. AnsiString __fastcall TCopyDialog::GetFileMask()
  232. {
  233. return ExtractFileName(DirectoryEdit->Text, RemotePaths());
  234. }
  235. //---------------------------------------------------------------------------
  236. void __fastcall TCopyDialog::SetParams(const TGUICopyParamType & value)
  237. {
  238. FParams = value;
  239. FCopyParams = value;
  240. DirectoryEdit->Text = Directory + FParams.FileMask;
  241. QueueCheck2->Checked = FParams.Queue;
  242. QueueIndividuallyCheck->Checked = FParams.QueueIndividually;
  243. NewerOnlyCheck->Checked = FLAGCLEAR(Options, coDisableNewerOnly) && FParams.NewerOnly;
  244. UpdateControls();
  245. }
  246. //---------------------------------------------------------------------------
  247. TGUICopyParamType __fastcall TCopyDialog::GetParams()
  248. {
  249. // overwrites TCopyParamType fields only
  250. FParams = FCopyParams;
  251. FParams.FileMask = GetFileMask();
  252. FParams.Queue = QueueCheck2->Checked;
  253. FParams.QueueIndividually = QueueIndividuallyCheck->Checked;
  254. FParams.NewerOnly = FLAGCLEAR(Options, coDisableNewerOnly) && NewerOnlyCheck->Checked;
  255. return FParams;
  256. }
  257. //---------------------------------------------------------------------------
  258. void __fastcall TCopyDialog::SetDirectory(AnsiString value)
  259. {
  260. if (!value.IsEmpty())
  261. {
  262. value = RemotePaths() ?
  263. UnixIncludeTrailingBackslash(value) : IncludeTrailingBackslash(value);
  264. }
  265. DirectoryEdit->Text = value + GetFileMask();
  266. }
  267. //---------------------------------------------------------------------------
  268. AnsiString __fastcall TCopyDialog::GetDirectory()
  269. {
  270. assert(DirectoryEdit);
  271. AnsiString Result = DirectoryEdit->Text;
  272. if (RemotePaths())
  273. {
  274. Result = UnixExtractFilePath(Result);
  275. if (!Result.IsEmpty())
  276. {
  277. Result = UnixIncludeTrailingBackslash(Result);
  278. }
  279. }
  280. else
  281. {
  282. Result = ExtractFilePath(Result);
  283. if (!Result.IsEmpty())
  284. {
  285. Result = IncludeTrailingBackslash(Result);
  286. }
  287. }
  288. return Result;
  289. }
  290. //---------------------------------------------------------------------------
  291. void __fastcall TCopyDialog::SetFileList(TStrings * value)
  292. {
  293. if (FFileList != value)
  294. {
  295. FFileList = value;
  296. AdjustControls();
  297. }
  298. }
  299. //---------------------------------------------------------------------------
  300. void __fastcall TCopyDialog::UpdateControls()
  301. {
  302. if (!ToRemote && FLAGSET(Options, coAllowRemoteTransfer))
  303. {
  304. AnsiString Directory = DirectoryEdit->Text;
  305. bool RemoteTransfer = !Directory.IsEmpty() && ExtractFileDrive(Directory).IsEmpty();
  306. if (RemoteTransfer != FLAGSET(FOutputOptions, cooRemoteTransfer))
  307. {
  308. FOutputOptions =
  309. (FOutputOptions & ~cooRemoteTransfer) |
  310. FLAGMASK(RemoteTransfer, cooRemoteTransfer);
  311. AdjustTransferControls();
  312. }
  313. }
  314. AnsiString InfoStr = FCopyParams.GetInfoStr("; ", CopyParamAttrs);
  315. CopyParamLabel->Caption = InfoStr;
  316. CopyParamLabel->Hint = InfoStr;
  317. CopyParamLabel->ShowHint =
  318. (CopyParamLabel->Canvas->TextWidth(InfoStr) > (CopyParamLabel->Width * 3 / 2));
  319. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  320. EnableControl(QueueCheck2,
  321. ((Options & (coDisableQueue | coTemp)) == 0) && !RemoteTransfer);
  322. EnableControl(QueueIndividuallyCheck,
  323. QueueCheck2->Enabled && QueueCheck2->Checked &&
  324. FileList && (FileList->Count > 1));
  325. }
  326. //---------------------------------------------------------------------------
  327. void __fastcall TCopyDialog::SetMove(bool value)
  328. {
  329. if (Move != value)
  330. {
  331. FMove = value;
  332. AdjustControls();
  333. }
  334. }
  335. //---------------------------------------------------------------------------
  336. void __fastcall TCopyDialog::FormShow(TObject * /*Sender*/)
  337. {
  338. assert(FileList && (FileList->Count > 0));
  339. if (DirectoryEdit->Enabled && DirectoryEdit->Visible)
  340. {
  341. ActiveControl = DirectoryEdit;
  342. }
  343. else
  344. {
  345. ActiveControl = CopyButton;
  346. }
  347. UpdateControls();
  348. InstallPathWordBreakProc(RemoteDirectoryEdit);
  349. InstallPathWordBreakProc(LocalDirectoryEdit);
  350. }
  351. //---------------------------------------------------------------------------
  352. bool __fastcall TCopyDialog::Execute()
  353. {
  354. // at start assume that copy param is current preset
  355. FPreset = GUIConfiguration->CopyParamCurrent;
  356. DirectoryEdit->Items = CustomWinConfiguration->History[
  357. ToRemote ? "RemoteTarget" : "LocalTarget"];
  358. bool Result = (ShowModal() == mrOk);
  359. if (Result)
  360. {
  361. Configuration->BeginUpdate();
  362. try
  363. {
  364. if (FLAGSET(OutputOptions, cooSaveSettings) &&
  365. FLAGCLEAR(Options, coDisableSaveSettings))
  366. {
  367. GUIConfiguration->DefaultCopyParam = Params;
  368. }
  369. DirectoryEdit->SaveToHistory();
  370. CustomWinConfiguration->History[ToRemote ?
  371. "RemoteTarget" : "LocalTarget"] = DirectoryEdit->Items;
  372. }
  373. __finally
  374. {
  375. Configuration->EndUpdate();
  376. }
  377. }
  378. return Result;
  379. }
  380. //---------------------------------------------------------------------------
  381. void __fastcall TCopyDialog::FormCloseQuery(TObject * /*Sender*/,
  382. bool &CanClose)
  383. {
  384. if (ModalResult != mrCancel)
  385. {
  386. if (!RemotePaths() && ((Options & coTemp) == 0))
  387. {
  388. AnsiString Dir = Directory;
  389. AnsiString Drive = ExtractFileDrive(Dir);
  390. if (!DirectoryExists(Dir))
  391. {
  392. if (MessageDialog(FMTLOAD(CREATE_LOCAL_DIRECTORY, (Dir)),
  393. qtConfirmation, qaOK | qaCancel, HELP_NONE) != qaCancel)
  394. {
  395. if (!ForceDirectories(Dir))
  396. {
  397. SimpleErrorDialog(FMTLOAD(CREATE_LOCAL_DIR_ERROR, (Dir)));
  398. CanClose = false;
  399. }
  400. }
  401. else
  402. {
  403. CanClose = False;
  404. }
  405. }
  406. if (!CanClose)
  407. {
  408. DirectoryEdit->SelectAll();
  409. DirectoryEdit->SetFocus();
  410. }
  411. };
  412. if (CanClose)
  413. {
  414. ExitActiveControl(this);
  415. }
  416. }
  417. }
  418. //---------------------------------------------------------------------------
  419. void __fastcall TCopyDialog::LocalDirectoryBrowseButtonClick(
  420. TObject * /*Sender*/)
  421. {
  422. assert(!ToRemote);
  423. AnsiString ADirectory;
  424. // if we are duplicating, we have remote path there
  425. if (!RemotePaths())
  426. {
  427. ADirectory = Directory;
  428. }
  429. if (SelectDirectory(ADirectory, LoadStr(SELECT_LOCAL_DIRECTORY), true))
  430. {
  431. Directory = ADirectory;
  432. UpdateControls();
  433. }
  434. }
  435. //---------------------------------------------------------------------------
  436. void __fastcall TCopyDialog::ControlChange(TObject * /*Sender*/)
  437. {
  438. UpdateControls();
  439. ResetSystemSettings(this);
  440. }
  441. //---------------------------------------------------------------------------
  442. void __fastcall TCopyDialog::TransferSettingsButtonClick(TObject * /*Sender*/)
  443. {
  444. if (FLAGCLEAR(Options, coDoNotUsePresets))
  445. {
  446. CopyParamListPopup(
  447. TransferSettingsButton->ClientToScreen(TPoint(0, TransferSettingsButton->Height)),
  448. 0);
  449. }
  450. else
  451. {
  452. CopyParamGroupDblClick(NULL);
  453. }
  454. }
  455. //---------------------------------------------------------------------------
  456. void __fastcall TCopyDialog::CopyParamClick(TObject * Sender)
  457. {
  458. TCopyParamType Param = Params;
  459. if (CopyParamListPopupClick(Sender, Param, FPreset, CopyParamAttrs))
  460. {
  461. Params = Param;
  462. }
  463. }
  464. //---------------------------------------------------------------------------
  465. void __fastcall TCopyDialog::HelpButtonClick(TObject * /*Sender*/)
  466. {
  467. FormHelp(this);
  468. }
  469. //---------------------------------------------------------------------------
  470. void __fastcall TCopyDialog::CopyParamGroupDblClick(TObject * /*Sender*/)
  471. {
  472. if (CopyParamGroup->Enabled)
  473. {
  474. if (DoCopyParamCustomDialog(FCopyParams, CopyParamAttrs))
  475. {
  476. UpdateControls();
  477. }
  478. }
  479. }
  480. //---------------------------------------------------------------------------
  481. void __fastcall TCopyDialog::CopyParamGroupContextPopup(TObject * /*Sender*/,
  482. TPoint & MousePos, bool & Handled)
  483. {
  484. if (FLAGCLEAR(Options, coDoNotUsePresets))
  485. {
  486. CopyParamListPopup(CopyParamGroup->ClientToScreen(MousePos), cplCustomizeDefault);
  487. Handled = true;
  488. }
  489. }
  490. //---------------------------------------------------------------------------
  491. void __fastcall TCopyDialog::CopyParamListPopup(TPoint P, int AdditionalOptions)
  492. {
  493. ::CopyParamListPopup(P, FPresetsMenu,
  494. FCopyParams, FPreset, CopyParamClick,
  495. cplCustomize | AdditionalOptions |
  496. FLAGMASK(
  497. FLAGCLEAR(Options, coDisableSaveSettings) &&
  498. FLAGCLEAR(FOutputOptions, cooRemoteTransfer),
  499. cplSaveSettings),
  500. &FSaveSettings);
  501. }