1
0

Copy.cpp 16 KB

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