Copy.cpp 16 KB

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