Copy.cpp 16 KB

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