Copy.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 "MoreButton"
  15. #pragma link "Rights"
  16. #pragma link "CopyParams"
  17. #pragma link "HistoryComboBox"
  18. #pragma resource "*.dfm"
  19. //---------------------------------------------------------------------------
  20. bool __fastcall DoCopyDialog(bool ToRemote,
  21. bool Move, TStrings * FileList, AnsiString & TargetDirectory,
  22. TGUICopyParamType * Params, int Options, int CopyParamAttrs, int * OutputOptions)
  23. {
  24. bool Result;
  25. TCopyDialog *CopyDialog = new TCopyDialog(Application);
  26. try
  27. {
  28. if (FLAGSET(CopyParamAttrs, cpaNoTransferMode))
  29. {
  30. // If local and remote EOL types are the same, there is no need
  31. // for ASCII (or Automatic) mode
  32. Params->TransferMode = tmBinary;
  33. }
  34. CopyDialog->ToRemote = ToRemote;
  35. CopyDialog->Options = Options;
  36. CopyDialog->CopyParamAttrs = CopyParamAttrs;
  37. if (OutputOptions != NULL)
  38. {
  39. CopyDialog->OutputOptions = *OutputOptions;
  40. }
  41. CopyDialog->Directory = TargetDirectory;
  42. CopyDialog->FileList = FileList;
  43. CopyDialog->Params = *Params;
  44. CopyDialog->Move = Move;
  45. Result = CopyDialog->Execute();
  46. if (Result)
  47. {
  48. TargetDirectory = CopyDialog->Directory;
  49. *Params = CopyDialog->Params;
  50. if (OutputOptions != NULL)
  51. {
  52. *OutputOptions = CopyDialog->OutputOptions;
  53. }
  54. }
  55. }
  56. __finally
  57. {
  58. delete CopyDialog;
  59. }
  60. return Result;
  61. }
  62. //---------------------------------------------------------------------------
  63. __fastcall TCopyDialog::TCopyDialog(TComponent* Owner)
  64. : TForm(Owner)
  65. {
  66. // on start set different value than we want to allow property-setter to proceed
  67. FToRemote = false;
  68. FMove = true;
  69. ToRemote = true;
  70. Move = false;
  71. FOptions = 0;
  72. FOutputOptions = 0;
  73. FPresetsMenu = new TPopupMenu(this);
  74. UseSystemSettings(this);
  75. }
  76. //---------------------------------------------------------------------------
  77. __fastcall TCopyDialog::~TCopyDialog()
  78. {
  79. delete FPresetsMenu;
  80. }
  81. //---------------------------------------------------------------------------
  82. void __fastcall TCopyDialog::AdjustTransferControls()
  83. {
  84. if (FileList && FileList->Count)
  85. {
  86. if (!ToRemote && !Move && FLAGSET(FOutputOptions, cooRemoteTransfer))
  87. {
  88. AnsiString Label;
  89. if (FileList->Count == 1)
  90. {
  91. AnsiString FileName;
  92. if (!ToRemote) FileName = UnixExtractFileName(FFileList->Strings[0]);
  93. else FileName = ExtractFileName(FFileList->Strings[0]);
  94. Label = FMTLOAD(REMOTE_COPY_FILE, (FileName));
  95. }
  96. else
  97. {
  98. Label = FMTLOAD(REMOTE_COPY_FILES, (FFileList->Count));
  99. }
  100. // hack to remove trailing colon used for "duplicate" prompt
  101. if (!Label.IsEmpty() &&
  102. (Label.ByteType(Label.Length()) == mbSingleByte) &&
  103. (Label[Label.Length()] == ':'))
  104. {
  105. Label.SetLength(Label.Length() - 1);
  106. }
  107. DirectoryLabel->Caption = Label;
  108. }
  109. else
  110. {
  111. AnsiString TransferStr = LoadStr(!Move ? COPY_COPY : COPY_MOVE);
  112. // currently the copy dialog is shown when downloading to temp folder
  113. // only for drag&drop downloads, for we dare to display d&d specific prompt
  114. AnsiString DirectionStr =
  115. LoadStr(((Options & coTemp) != 0) ? COPY_TODROP :
  116. (RemotePaths() ? COPY_TOREMOTE : COPY_TOLOCAL));
  117. if (FileList->Count == 1)
  118. {
  119. AnsiString FileName;
  120. if (!ToRemote) FileName = UnixExtractFileName(FFileList->Strings[0]);
  121. else FileName = ExtractFileName(FFileList->Strings[0]);
  122. DirectoryLabel->Caption = FMTLOAD(COPY_FILE,
  123. (TransferStr, FileName, DirectionStr));
  124. }
  125. else
  126. {
  127. DirectoryLabel->Caption = FMTLOAD(COPY_FILES,
  128. (TransferStr, FFileList->Count, DirectionStr));
  129. }
  130. }
  131. }
  132. if (!Move)
  133. {
  134. if (!ToRemote && FLAGSET(FOutputOptions, cooRemoteTransfer))
  135. {
  136. Caption = LoadStr(REMOTE_COPY_TITLE);
  137. }
  138. else
  139. {
  140. Caption = LoadStr(COPY_COPY_CAPTION);
  141. }
  142. CopyButton->Caption = LoadStr(COPY_COPY_BUTTON);
  143. }
  144. else
  145. {
  146. Caption = LoadStr(COPY_MOVE_CAPTION);
  147. CopyButton->Caption = LoadStr(COPY_MOVE_BUTTON);
  148. }
  149. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  150. assert(FLAGSET(Options, coAllowRemoteTransfer) || !RemoteTransfer);
  151. EnableControl(CopyParamsFrame, !RemoteTransfer);
  152. EnableControl(NewerOnlyCheck, FLAGCLEAR(Options, coDisableNewerOnly) && !RemoteTransfer);
  153. EnableControl(PresetsButton, !RemoteTransfer);
  154. }
  155. //---------------------------------------------------------------------------
  156. void __fastcall TCopyDialog::AdjustControls()
  157. {
  158. NeverShowAgainCheck->Visible = FLAGSET(Options, coDoNotShowAgain);
  159. RemoteDirectoryEdit->Visible = false;
  160. LocalDirectoryEdit->Visible = false;
  161. DirectoryEdit->Visible = FLAGCLEAR(Options, coTemp);
  162. EnableControl(DirectoryEdit, FLAGCLEAR(Options, coDisableDirectory));
  163. EnableControl(DirectoryLabel, DirectoryEdit->Enabled);
  164. EnableControl(LocalDirectoryBrowseButton, DirectoryEdit->Enabled);
  165. DirectoryLabel->FocusControl = DirectoryEdit;
  166. CopyParamsFrame->Direction = !ToRemote ? pdToLocal : pdToRemote;
  167. PresetsButton->Visible = FLAGCLEAR(Options, coDoNotUsePresets);
  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. SaveSettingsCheck->Checked = 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(SaveSettingsCheck->Checked, cooSaveSettings) |
  208. FLAGMASK(NeverShowAgainCheck->Checked, cooDoNotShowAgain);
  209. }
  210. //---------------------------------------------------------------------------
  211. void __fastcall TCopyDialog::SetCopyParamAttrs(int value)
  212. {
  213. CopyParamsFrame->CopyParamAttrs = value;
  214. }
  215. //---------------------------------------------------------------------------
  216. int __fastcall TCopyDialog::GetCopyParamAttrs()
  217. {
  218. return CopyParamsFrame->CopyParamAttrs;
  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. CopyParamsFrame->Params = value;
  240. DirectoryEdit->Text = Directory + FParams.FileMask;
  241. QueueCheck->Checked = FParams.Queue;
  242. QueueNoConfirmationCheck->Checked = FParams.QueueNoConfirmation;
  243. NewerOnlyCheck->Checked = FLAGCLEAR(Options, coDisableNewerOnly) && FParams.NewerOnly;
  244. }
  245. //---------------------------------------------------------------------------
  246. TGUICopyParamType __fastcall TCopyDialog::GetParams()
  247. {
  248. // overwrites TCopyParamType files only
  249. FParams = CopyParamsFrame->Params;
  250. FParams.FileMask = GetFileMask();
  251. FParams.Queue = QueueCheck->Checked;
  252. FParams.QueueNoConfirmation = QueueNoConfirmationCheck->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. bool RemoteTransfer = FLAGSET(FOutputOptions, cooRemoteTransfer);
  314. EnableControl(QueueCheck,
  315. ((Options & (coDisableQueue | coTemp)) == 0) && !RemoteTransfer);
  316. EnableControl(QueueNoConfirmationCheck,
  317. (((Options & coTemp) == 0) && QueueCheck->Checked) && !RemoteTransfer);
  318. QueueNoConfirmationCheck->Visible = MoreButton->Expanded;
  319. EnableControl(SaveSettingsCheck, FLAGCLEAR(Options, coDisableSaveSettings) &&
  320. !RemoteTransfer);
  321. }
  322. //---------------------------------------------------------------------------
  323. void __fastcall TCopyDialog::SetMove(bool value)
  324. {
  325. if (Move != value)
  326. {
  327. FMove = value;
  328. AdjustControls();
  329. }
  330. }
  331. //---------------------------------------------------------------------------
  332. void __fastcall TCopyDialog::FormShow(TObject * /*Sender*/)
  333. {
  334. assert(FileList && (FileList->Count > 0));
  335. if (DirectoryEdit->Enabled && DirectoryEdit->Visible)
  336. {
  337. DirectoryEdit->SetFocus();
  338. }
  339. else
  340. if (MoreButton->Expanded)
  341. {
  342. MorePanel->SetFocus();
  343. }
  344. else
  345. {
  346. CopyButton->SetFocus();
  347. }
  348. UpdateControls();
  349. InstallPathWordBreakProc(RemoteDirectoryEdit);
  350. InstallPathWordBreakProc(LocalDirectoryEdit);
  351. }
  352. //---------------------------------------------------------------------------
  353. bool __fastcall TCopyDialog::Execute()
  354. {
  355. // at start assume that copy param is current preset
  356. FPreset = GUIConfiguration->CopyParamCurrent;
  357. DirectoryEdit->Items = CustomWinConfiguration->History[
  358. ToRemote ? "RemoteTarget" : "LocalTarget"];
  359. MoreButton->Expanded = GUIConfiguration->CopyParamDialogExpanded;
  360. CopyParamsFrame->BeforeExecute();
  361. bool Result = (ShowModal() == mrOk);
  362. if (Result)
  363. {
  364. CopyParamsFrame->AfterExecute();
  365. Configuration->BeginUpdate();
  366. try
  367. {
  368. GUIConfiguration->CopyParamDialogExpanded = MoreButton->Expanded;
  369. if (FLAGSET(OutputOptions, cooSaveSettings) &&
  370. FLAGCLEAR(Options, coDisableSaveSettings))
  371. {
  372. GUIConfiguration->DefaultCopyParam = Params;
  373. }
  374. DirectoryEdit->SaveToHistory();
  375. CustomWinConfiguration->History[ToRemote ?
  376. "RemoteTarget" : "LocalTarget"] = DirectoryEdit->Items;
  377. }
  378. __finally
  379. {
  380. Configuration->EndUpdate();
  381. }
  382. }
  383. return Result;
  384. }
  385. //---------------------------------------------------------------------------
  386. void __fastcall TCopyDialog::FormCloseQuery(TObject * /*Sender*/,
  387. bool &CanClose)
  388. {
  389. if (ModalResult != mrCancel)
  390. {
  391. if (!RemotePaths() && ((Options & coTemp) == 0))
  392. {
  393. AnsiString Dir = Directory;
  394. AnsiString Drive = ExtractFileDrive(Dir);
  395. if (!DirectoryExists(Dir))
  396. {
  397. if (MessageDialog(FMTLOAD(CREATE_LOCAL_DIRECTORY, (Dir)),
  398. qtConfirmation, qaOK | qaCancel, HELP_NONE) != qaCancel)
  399. {
  400. if (!ForceDirectories(Dir))
  401. {
  402. SimpleErrorDialog(FMTLOAD(CREATE_LOCAL_DIR_ERROR, (Dir)));
  403. CanClose = false;
  404. }
  405. }
  406. else
  407. {
  408. CanClose = False;
  409. }
  410. }
  411. if (!CanClose)
  412. {
  413. DirectoryEdit->SelectAll();
  414. DirectoryEdit->SetFocus();
  415. }
  416. };
  417. if (CanClose)
  418. {
  419. ExitActiveControl(this);
  420. }
  421. }
  422. }
  423. //---------------------------------------------------------------------------
  424. void __fastcall TCopyDialog::LocalDirectoryBrowseButtonClick(
  425. TObject * /*Sender*/)
  426. {
  427. assert(!ToRemote);
  428. AnsiString ADirectory;
  429. // if we are duplicating, we have remote path there
  430. if (!RemotePaths())
  431. {
  432. ADirectory = Directory;
  433. }
  434. if (SelectDirectory(ADirectory, LoadStr(SELECT_LOCAL_DIRECTORY), true))
  435. {
  436. Directory = ADirectory;
  437. UpdateControls();
  438. }
  439. }
  440. //---------------------------------------------------------------------------
  441. void __fastcall TCopyDialog::ControlChange(TObject * /*Sender*/)
  442. {
  443. UpdateControls();
  444. ResetSystemSettings(this);
  445. }
  446. //---------------------------------------------------------------------------
  447. void __fastcall TCopyDialog::PresetsButtonClick(TObject * /*Sender*/)
  448. {
  449. TCopyParamType Param = Params;
  450. CopyParamListPopup(
  451. PresetsButton->ClientToScreen(TPoint(0, PresetsButton->Height)),
  452. FPresetsMenu, Params, FPreset, CopyParamClick, cplNone);
  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. //---------------------------------------------------------------------------